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