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