blob: 561199c3bf7ccb78b4a78e289ebddc90bcdd004c [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 tests for hotlistdetails page."""
7from __future__ import print_function
8from __future__ import division
9from __future__ import absolute_import
10
11import logging
Adrià Vilanova Martínez9f9ade52022-10-10 23:20:11 +020012try:
13 from mox3 import mox
14except ImportError:
15 import mox
Copybara854996b2021-09-07 19:36:02 +000016import unittest
17import mock
18
19import ezt
20
21from framework import permissions
22from features import features_constants
23from services import service_manager
24from features import hotlistdetails
25from proto import features_pb2
26from testing import fake
27from testing import testing_helpers
28
29class HotlistDetailsTest(unittest.TestCase):
30 """Unit tests for the HotlistDetails servlet class."""
31
32 def setUp(self):
33 self.user_service = fake.UserService()
34 self.user_1 = self.user_service.TestAddUser('111@test.com', 111)
35 self.user_2 = self.user_service.TestAddUser('user2@test.com', 222)
36 services = service_manager.Services(
37 features=fake.FeaturesService(), user=self.user_service)
38 self.servlet = hotlistdetails.HotlistDetails(
39 'req', 'res', services=services)
40 self.hotlist = self.servlet.services.features.TestAddHotlist(
41 'hotlist', summary='hotlist summary', description='hotlist description',
42 owner_ids=[111], editor_ids=[222])
43 self.request, self.mr = testing_helpers.GetRequestObjects(
44 hotlist=self.hotlist)
45 self.mr.auth.user_id = 111
46 self.private_hotlist = services.features.TestAddHotlist(
47 'private_hotlist', owner_ids=[111], editor_ids=[222], is_private=True)
48 self.mox = mox.Mox()
49
50 def tearDown(self):
51 self.mox.UnsetStubs()
52 self.mox.ResetAll()
53
54 def testAssertBasePermission(self):
55 # non-members cannot view private hotlists
56 mr = testing_helpers.MakeMonorailRequest(
57 hotlist=self.private_hotlist, perms=permissions.EMPTY_PERMISSIONSET)
58 mr.auth.effective_ids = {333}
59 self.assertRaises(permissions.PermissionException,
60 self.servlet.AssertBasePermission, mr)
61
62 # members can view private hotlists
63 mr = testing_helpers.MakeMonorailRequest(
64 hotlist=self.private_hotlist)
65 mr.auth.effective_ids = {222, 444}
66 self.servlet.AssertBasePermission(mr)
67
68 # non-members can view public hotlists
69 mr = testing_helpers.MakeMonorailRequest(
70 hotlist=self.hotlist)
71 mr.auth.effective_ids = {333, 444}
72 self.servlet.AssertBasePermission(mr)
73
74 # members can view public hotlists
75 mr = testing_helpers.MakeMonorailRequest(
76 hotlist=self.hotlist)
77 mr.auth.effective_ids = {111, 333}
78 self.servlet.AssertBasePermission(mr)
79
80 def testGatherPageData(self):
81 self.mr.auth.effective_ids = [222]
82 self.mr.perms = permissions.EMPTY_PERMISSIONSET
83 page_data = self.servlet.GatherPageData(self.mr)
84 self.assertEqual('hotlist summary', page_data['initial_summary'])
85 self.assertEqual('hotlist description', page_data['initial_description'])
86 self.assertEqual('hotlist', page_data['initial_name'])
87 self.assertEqual(features_constants.DEFAULT_COL_SPEC,
88 page_data['initial_default_col_spec'])
89 self.assertEqual(ezt.boolean(False), page_data['initial_is_private'])
90
91 # editor is viewing, so cant_administer_hotlist is True
92 self.assertEqual(ezt.boolean(True), page_data['cant_administer_hotlist'])
93
94 # owner is veiwing, so cant_administer_hotlist is False
95 self.mr.auth.effective_ids = [111]
96 page_data = self.servlet.GatherPageData(self.mr)
97 self.assertEqual(ezt.boolean(False), page_data['cant_administer_hotlist'])
98
99 def testProcessFormData(self):
100 mr = testing_helpers.MakeMonorailRequest(
101 hotlist=self.hotlist,
102 path='/u/111/hotlists/%s/details' % self.hotlist.hotlist_id,
103 services=service_manager.Services(user=self.user_service),
104 perms=permissions.EMPTY_PERMISSIONSET)
105 mr.auth.effective_ids = {111}
106 mr.auth.user_id = 111
107 post_data = fake.PostData(
108 name=['hotlist'],
109 summary = ['hotlist summary'],
110 description = ['hotlist description'],
111 default_col_spec = ['test default col spec'])
112 url = self.servlet.ProcessFormData(mr, post_data)
113 self.assertTrue((
114 '/u/111/hotlists/%d/details?saved=' % self.hotlist.hotlist_id) in url)
115
116 @mock.patch('features.hotlist_helpers.RemoveHotlist')
117 def testProcessFormData_DeleteHotlist(self, fake_rh):
118 mr = testing_helpers.MakeMonorailRequest(
119 hotlist=self.hotlist,
120 path='/u/111/hotlists/%s/details' % self.hotlist.hotlist_id,
121 services=service_manager.Services(user=self.user_service),
122 perms=permissions.EMPTY_PERMISSIONSET)
123 mr.auth.effective_ids = {self.user_1.user_id}
124 mr.auth.user_id = self.user_1.user_id
125 mr.auth.email = self.user_1.email
126
127 post_data = fake.PostData(deletestate=['true'])
128 url = self.servlet.ProcessFormData(mr, post_data)
129 fake_rh.assert_called_once_with(
130 mr.cnxn, mr.hotlist_id, self.servlet.services)
131 self.assertTrue(('/u/%s/hotlists?saved=' % self.user_1.email) in url)
132
133 def testProcessFormData_RejectTemplate(self):
134 mr = testing_helpers.MakeMonorailRequest(
135 hotlist=self.hotlist,
136 path='/u/111/hotlists/%s/details' % self.hotlist.hotlist_id,
137 services=service_manager.Services(user=self.user_service),
138 perms=permissions.EMPTY_PERMISSIONSET)
139 mr.auth.user_id = 111
140 mr.auth.effective_ids = {111}
141 post_data = fake.PostData(
142 summary = [''],
143 name = [''],
144 description = ['fake description'],
145 default_col_spec = ['test default col spec'])
146 self.mox.StubOutWithMock(self.servlet, 'PleaseCorrect')
147 self.servlet.PleaseCorrect(
148 mr, initial_summary='',
149 initial_description='fake description', initial_name = '',
150 initial_default_col_spec = 'test default col spec')
151 self.mox.ReplayAll()
152
153 url = self.servlet.ProcessFormData(mr, post_data)
154 self.mox.VerifyAll()
155 self.assertEqual(hotlistdetails._MSG_NAME_MISSING, mr.errors.name)
156 self.assertEqual(hotlistdetails._MSG_SUMMARY_MISSING,
157 mr.errors.summary)
158 self.assertIsNone(url)
159
160 def testProcessFormData_DuplicateName(self):
161 self.servlet.services.features.TestAddHotlist(
162 'FirstHotlist', summary='hotlist summary', description='description',
163 owner_ids=[111], editor_ids=[])
164 mr = testing_helpers.MakeMonorailRequest(
165 hotlist=self.hotlist,
166 path='/u/111/hotlists/%s/details' % (self.hotlist.hotlist_id),
167 services=service_manager.Services(user=self.user_service),
168 perms=permissions.EMPTY_PERMISSIONSET)
169 mr.auth.user_id = 111
170 mr.auth.effective_ids = {111}
171 post_data = fake.PostData(
172 summary = ['hotlist summary'],
173 name = ['FirstHotlist'],
174 description = ['description'],
175 default_col_spec = ['test default col spec'])
176 self.mox.StubOutWithMock(self.servlet, 'PleaseCorrect')
177 self.servlet.PleaseCorrect(
178 mr, initial_summary='hotlist summary',
179 initial_description='description', initial_name = 'FirstHotlist',
180 initial_default_col_spec = 'test default col spec')
181 self.mox.ReplayAll()
182
183 url = self.servlet.ProcessFormData(mr, post_data)
184 self.mox.VerifyAll()
185 self.assertEqual(hotlistdetails._MSG_HOTLIST_NAME_NOT_AVAIL,
186 mr.errors.name)
187 self.assertIsNone(url)
188
189 def testProcessFormData_Bad(self):
190 mr = testing_helpers.MakeMonorailRequest(
191 hotlist=self.hotlist,
192 path='/u/111/hotlists/%s/details' % (self.hotlist.hotlist_id),
193 services=service_manager.Services(user=self.user_service),
194 perms=permissions.EMPTY_PERMISSIONSET)
195 mr.auth.user_id = 111
196 mr.auth.effective_ids = {111}
197 post_data = fake.PostData(
198 summary = ['hotlist summary'],
199 name = ['2badName'],
200 description = ['fake description'],
201 default_col_spec = ['test default col spec'])
202 self.mox.StubOutWithMock(self.servlet, 'PleaseCorrect')
203 self.servlet.PleaseCorrect(
204 mr, initial_summary='hotlist summary',
205 initial_description='fake description', initial_name = '2badName',
206 initial_default_col_spec = 'test default col spec')
207 self.mox.ReplayAll()
208
209 url = self.servlet.ProcessFormData(mr, post_data)
210 self.mox.VerifyAll()
211 self.assertEqual(hotlistdetails._MSG_INVALID_HOTLIST_NAME,
212 mr.errors.name)
213 self.assertIsNone(url)
214
215 def testProcessFormData_NoPermissions(self):
216 mr = testing_helpers.MakeMonorailRequest(
217 hotlist=self.hotlist,
218 path='/u/111/hotlists/%s/details' % (self.hotlist.hotlist_id),
219 services=service_manager.Services(user=self.user_service),
220 perms=permissions.EMPTY_PERMISSIONSET)
221 mr.auth.user_id = self.user_2.user_id
222 mr.auth.effective_ids = {self.user_2.user_id}
223 post_data = fake.PostData(
224 summary = ['hotlist summary'],
225 name = ['hotlist'],
226 description = ['fake description'],
227 default_col_spec = ['test default col spec'])
228 with self.assertRaises(permissions.PermissionException):
229 self.servlet.ProcessFormData(mr, post_data)