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