blob: 3229382be624926ece41c2b5a0a76af1e58b2129 [file] [log] [blame]
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +01001# Copyright 2016 The Chromium Authors
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
Copybara854996b2021-09-07 19:36:02 +00004
5"""Unit test for Hotlist creation servlet."""
6from __future__ import print_function
7from __future__ import division
8from __future__ import absolute_import
9
Adrià Vilanova Martínez9f9ade52022-10-10 23:20:11 +020010try:
11 from mox3 import mox
12except ImportError:
13 import mox
Copybara854996b2021-09-07 19:36:02 +000014import unittest
15
16import settings
17from framework import permissions
18from features import hotlistcreate
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +010019from mrproto import site_pb2
Copybara854996b2021-09-07 19:36:02 +000020from services import service_manager
21from testing import fake
22from testing import testing_helpers
23
24
25class HotlistCreateTest(unittest.TestCase):
26 """Tests for the HotlistCreate servlet."""
27
28 def setUp(self):
29 self.cnxn = 'fake cnxn'
30 self.mr = testing_helpers.MakeMonorailRequest()
31 self.services = service_manager.Services(project=fake.ProjectService(),
32 user=fake.UserService(),
33 issue=fake.IssueService(),
34 features=fake.FeaturesService())
Adrià Vilanova Martínez9f9ade52022-10-10 23:20:11 +020035 self.servlet = hotlistcreate.HotlistCreate(services=self.services)
Copybara854996b2021-09-07 19:36:02 +000036 self.mox = mox.Mox()
37
38 def tearDown(self):
39 self.mox.UnsetStubs()
40 self.mox.ResetAll()
41
42 def CheckAssertBasePermissions(
43 self, restriction, expect_admin_ok, expect_nonadmin_ok):
44 old_hotlist_creation_restriction = settings.hotlist_creation_restriction
45 settings.hotlist_creation_restriction = restriction
46
47 mr = testing_helpers.MakeMonorailRequest(
48 perms=permissions.GetPermissions(None, {}, None))
49 self.assertRaises(
50 permissions.PermissionException,
51 self.servlet.AssertBasePermission, mr)
52
53 mr = testing_helpers.MakeMonorailRequest()
54 if expect_admin_ok:
55 self.servlet.AssertBasePermission(mr)
56 else:
57 self.assertRaises(
58 permissions.PermissionException,
59 self.servlet.AssertBasePermission, mr)
60
61 mr = testing_helpers.MakeMonorailRequest(
62 perms=permissions.GetPermissions(mr.auth.user_pb, {111}, None))
63 if expect_nonadmin_ok:
64 self.servlet.AssertBasePermission(mr)
65 else:
66 self.assertRaises(
67 permissions.PermissionException,
68 self.servlet.AssertBasePermission, mr)
69
70 settings.hotlist_creation_restriction = old_hotlist_creation_restriction
71
72 def testAssertBasePermission(self):
73 self.CheckAssertBasePermissions(
74 site_pb2.UserTypeRestriction.ANYONE, True, True)
75 self.CheckAssertBasePermissions(
76 site_pb2.UserTypeRestriction.ADMIN_ONLY, True, False)
77 self.CheckAssertBasePermissions(
78 site_pb2.UserTypeRestriction.NO_ONE, False, False)
79
80 def testGatherPageData(self):
81 page_data = self.servlet.GatherPageData(self.mr)
82 self.assertEqual('st6', page_data['user_tab_mode'])
83 self.assertEqual('', page_data['initial_name'])
84 self.assertEqual('', page_data['initial_summary'])
85 self.assertEqual('', page_data['initial_description'])
86 self.assertEqual('', page_data['initial_editors'])
87 self.assertEqual('no', page_data['initial_privacy'])
88
89 def testProcessFormData(self):
90 self.servlet.services.user.TestAddUser('owner', 111)
91 self.mr.auth.user_id = 111
92 post_data = fake.PostData(hotlistname=['Hotlist'], summary=['summ'],
93 description=['hey'],
94 editors=[''], is_private=['yes'])
95 url = self.servlet.ProcessFormData(self.mr, post_data)
96 self.assertTrue('/u/111/hotlists/Hotlist' in url)
97
98 def testProcessFormData_OwnerInEditors(self):
99 self.servlet.services.user.TestAddUser('owner_editor', 222)
100 self.mr.auth.user_id = 222
101 self.mr.cnxn = 'fake cnxn'
102 post_data = fake.PostData(hotlistname=['Hotlist-owner-editor'],
103 summary=['summ'],
104 description=['hi'],
105 editors=['owner_editor'], is_private=['yes'])
106 url = self.servlet.ProcessFormData(self.mr, post_data)
107 self.assertTrue('/u/222/hotlists/Hotlist-owner-editor' in url)
108 hotlists_by_id = self.servlet.services.features.LookupHotlistIDs(
109 self.mr.cnxn, ['Hotlist-owner-editor'], [222])
110 self.assertTrue(('hotlist-owner-editor', 222) in hotlists_by_id)
111 hotlist_id = hotlists_by_id[('hotlist-owner-editor', 222)]
112 hotlist = self.servlet.services.features.GetHotlist(
113 self.mr.cnxn, hotlist_id, use_cache=False)
114 self.assertEqual(hotlist.owner_ids, [222])
115 self.assertEqual(hotlist.editor_ids, [])
116
117 def testProcessFormData_RejectTemplateInvalid(self):
118 mr = testing_helpers.MakeMonorailRequest()
119 # invalid hotlist name and nonexistent editor
120 post_data = fake.PostData(hotlistname=['123BadName'], summary=['summ'],
121 description=['hey'],
122 editors=['test@email.com'], is_private=['yes'])
123 self.mox.StubOutWithMock(self.servlet, 'PleaseCorrect')
124 self.servlet.PleaseCorrect(
125 mr, initial_name = '123BadName', initial_summary='summ',
126 initial_description='hey',
127 initial_editors='test@email.com', initial_privacy='yes')
128 self.mox.ReplayAll()
129 url = self.servlet.ProcessFormData(mr, post_data)
130 self.mox.VerifyAll()
131 self.assertEqual(mr.errors.hotlistname, 'Invalid hotlist name')
132 self.assertEqual(mr.errors.editors,
133 'One or more editor emails is not valid.')
134 self.assertIsNone(url)
135
136 def testProcessFormData_RejectTemplateMissing(self):
137 mr = testing_helpers.MakeMonorailRequest()
138 # missing name and summary
139 post_data = fake.PostData()
140 self.mox.StubOutWithMock(self.servlet, 'PleaseCorrect')
141 self.servlet.PleaseCorrect(mr, initial_name = None, initial_summary=None,
142 initial_description='',
143 initial_editors='', initial_privacy=None)
144 self.mox.ReplayAll()
145 url = self.servlet.ProcessFormData(mr, post_data)
146 self.mox.VerifyAll()
147 self.assertEqual(mr.errors.hotlistname, 'Missing hotlist name')
148 self.assertEqual(mr.errors.summary,'Missing hotlist summary')
149 self.assertIsNone(url)