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