blob: 5b31fa353917d26e202a44c0889ca1b76e54c540 [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 the fielddetail 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
16import logging
17
18import webapp2
19
20import ezt
21
22from framework import permissions
23from proto import project_pb2
24from proto import tracker_pb2
25from services import service_manager
26from testing import fake
27from testing import testing_helpers
28from tracker import fielddetail
29from tracker import tracker_bizobj
30from tracker import tracker_views
31
32
33class FieldDetailTest(unittest.TestCase):
34
35 def setUp(self):
36 self.services = service_manager.Services(
37 user=fake.UserService(),
38 config=fake.ConfigService(),
39 project=fake.ProjectService())
40 self.servlet = fielddetail.FieldDetail(
41 'req', 'res', services=self.services)
42 self.project = self.services.project.TestAddProject('proj')
43 self.mr = testing_helpers.MakeMonorailRequest(
44 project=self.project, perms=permissions.OWNER_ACTIVE_PERMISSIONSET)
45 self.config = self.services.config.GetProjectConfig(
46 'fake cnxn', self.project.project_id)
47 self.services.config.StoreConfig('fake cnxn', self.config)
48 self.fd = tracker_bizobj.MakeFieldDef(
49 123, 789, 'CPU', tracker_pb2.FieldTypes.INT_TYPE, None,
50 '', False, False, False, None, None, '', False, '', '',
51 tracker_pb2.NotifyTriggers.NEVER, 'no_action', 'doc', False)
52 self.fd.admin_ids = [111]
53 self.fd.editor_ids = [222]
54 self.config.field_defs.append(self.fd)
55 self.services.user.TestAddUser('gatsby@example.com', 111)
56 self.services.user.TestAddUser('sport@example.com', 222)
57 self.mr.field_name = 'CPU'
58
59 # Approvals
60 self.approval_def = tracker_pb2.ApprovalDef(
61 approval_id=234, approver_ids=[111], survey='Question 1?')
62 self.sub_fd = tracker_pb2.FieldDef(
63 field_name='UIMocks', approval_id=234, applicable_type='')
64 self.sub_fd_deleted = tracker_pb2.FieldDef(
65 field_name='UIMocksDeleted', approval_id=234, applicable_type='',
66 is_deleted=True)
67 self.config.field_defs.extend([self.sub_fd, self.sub_fd_deleted])
68 self.config.approval_defs.append(self.approval_def)
69 self.approval_fd = tracker_bizobj.MakeFieldDef(
70 234, 789, 'UIReview', tracker_pb2.FieldTypes.APPROVAL_TYPE, None,
71 '', False, False, False, None, None, '', False, '', '',
72 tracker_pb2.NotifyTriggers.NEVER, 'no_action', 'doc', False)
73 self.config.field_defs.append(self.approval_fd)
74
75 self.mox = mox.Mox()
76
77 def tearDown(self):
78 self.mox.UnsetStubs()
79 self.mox.ResetAll()
80
81 def testGetFieldDef_NotFound(self):
82 self.mr.field_name = 'NeverHeardOfIt'
83 self.assertRaises(
84 webapp2.HTTPException,
85 self.servlet._GetFieldDef, self.mr)
86
87 def testGetFieldDef_Normal(self):
88 actual_config, actual_fd = self.servlet._GetFieldDef(self.mr)
89 self.assertEqual(self.config, actual_config)
90 self.assertEqual(self.fd, actual_fd)
91
92 def testAssertBasePermission_AnyoneCanView(self):
93 self.servlet.AssertBasePermission(self.mr)
94 self.mr.perms = permissions.COMMITTER_ACTIVE_PERMISSIONSET
95 self.servlet.AssertBasePermission(self.mr)
96 self.mr.perms = permissions.CONTRIBUTOR_ACTIVE_PERMISSIONSET
97 self.servlet.AssertBasePermission(self.mr)
98 self.mr.perms = permissions.READ_ONLY_PERMISSIONSET
99 self.servlet.AssertBasePermission(self.mr)
100
101 def testAssertBasePermission_MembersOnly(self):
102 self.project.access = project_pb2.ProjectAccess.MEMBERS_ONLY
103 # The project members can view the field definition.
104 self.servlet.AssertBasePermission(self.mr)
105 self.mr.perms = permissions.COMMITTER_ACTIVE_PERMISSIONSET
106 self.servlet.AssertBasePermission(self.mr)
107 self.mr.perms = permissions.CONTRIBUTOR_ACTIVE_PERMISSIONSET
108 self.servlet.AssertBasePermission(self.mr)
109 # Non-member is not allowed to view anything in the project.
110 self.mr.perms = permissions.EMPTY_PERMISSIONSET
111 self.assertRaises(
112 permissions.PermissionException,
113 self.servlet.AssertBasePermission, self.mr)
114
115 def testGatherPageData_ReadWrite(self):
116 page_data = self.servlet.GatherPageData(self.mr)
117 self.assertEqual(self.servlet.PROCESS_TAB_LABELS,
118 page_data['admin_tab_mode'])
119 self.assertTrue(page_data['allow_edit'])
120 self.assertEqual('gatsby@example.com', page_data['initial_admins'])
121 self.assertEqual('sport@example.com', page_data['initial_editors'])
122 field_def_view = page_data['field_def']
123 self.assertEqual('CPU', field_def_view.field_name)
124 self.assertEqual(page_data['approval_subfields'], [])
125 self.assertEqual(page_data['initial_approvers'], '')
126
127 def testGatherPageData_ReadOnly(self):
128 self.mr.perms = permissions.READ_ONLY_PERMISSIONSET
129 page_data = self.servlet.GatherPageData(self.mr)
130 self.assertEqual(self.servlet.PROCESS_TAB_LABELS,
131 page_data['admin_tab_mode'])
132 self.assertFalse(page_data['allow_edit'])
133 self.assertEqual('gatsby@example.com', page_data['initial_admins'])
134 self.assertEqual('sport@example.com', page_data['initial_editors'])
135 field_def_view = page_data['field_def']
136 self.assertEqual('CPU', field_def_view.field_name)
137 self.assertEqual(page_data['approval_subfields'], [])
138 self.assertEqual(page_data['initial_approvers'], '')
139
140 def testGatherPageData_Approval(self):
141 self.mr.field_name = 'UIReview'
142 page_data = self.servlet.GatherPageData(self.mr)
143 self.assertEqual(page_data['approval_subfields'], [self.sub_fd])
144 self.assertEqual(page_data['initial_approvers'], 'gatsby@example.com')
145 field_def_view = page_data['field_def']
146 self.assertEqual(field_def_view.field_name, 'UIReview')
147 self.assertEqual(field_def_view.survey, 'Question 1?')
148
149 def testProcessFormData_Permission(self):
150 """Only owners can edit fields."""
151 mr = testing_helpers.MakeMonorailRequest(
152 project=self.project,
153 perms=permissions.CONTRIBUTOR_ACTIVE_PERMISSIONSET)
154 mr.field_name = 'CPU'
155 post_data = fake.PostData(
156 name=['CPU'],
157 deletefield=['Submit'])
158 self.assertRaises(permissions.PermissionException,
159 self.servlet.ProcessFormData, mr, post_data)
160
161 self.servlet.ProcessFormData(self.mr, post_data)
162
163 def testProcessFormData_Delete(self):
164 post_data = fake.PostData(
165 name=['CPU'],
166 deletefield=['Submit'])
167 url = self.servlet.ProcessFormData(self.mr, post_data)
168 self.assertTrue('/adminLabels?deleted=1&' in url)
169 fd = tracker_bizobj.FindFieldDef('CPU', self.config)
170 self.assertEqual('CPU', fd.field_name)
171 self.assertTrue(fd.is_deleted)
172
173 def testProcessFormData_Cancel(self):
174 post_data = fake.PostData(
175 name=['CPU'],
176 cancel=['Submit'],
177 max_value=['200'])
178 url = self.servlet.ProcessFormData(self.mr, post_data)
179 logging.info(url)
180 self.assertTrue('/adminLabels?ts=' in url)
181 config = self.services.config.GetProjectConfig(
182 self.mr.cnxn, self.mr.project_id)
183
184 fd = tracker_bizobj.FindFieldDef('CPU', config)
185 self.assertIsNone(fd.max_value)
186 self.assertIsNone(fd.min_value)
187
188 def testProcessFormData_Edit(self):
189 post_data = fake.PostData(
190 name=['CPU'],
191 field_type=['INT_TYPE'],
192 min_value=['2'],
193 max_value=['98'],
194 notify_on=['never'],
195 is_required=[],
196 is_multivalued=[],
197 docstring=['It is just some field'],
198 applicable_type=['Defect'],
199 admin_names=['gatsby@example.com'])
200 url = self.servlet.ProcessFormData(self.mr, post_data)
201 self.assertTrue('/fields/detail?field=CPU&saved=1&' in url)
202 config = self.services.config.GetProjectConfig(
203 self.mr.cnxn, self.mr.project_id)
204
205 fd = tracker_bizobj.FindFieldDef('CPU', config)
206 self.assertEqual('CPU', fd.field_name)
207 self.assertEqual(2, fd.min_value)
208 self.assertEqual(98, fd.max_value)
209 self.assertEqual([111], fd.admin_ids)
210 self.assertEqual([], fd.editor_ids)
211
212 def testProcessDeleteField(self):
213 self.servlet._ProcessDeleteField(self.mr, self.config, self.fd)
214 self.assertTrue(self.fd.is_deleted)
215
216 def testProcessDeleteField_subfields(self):
217 approval_fd = tracker_bizobj.MakeFieldDef(
218 3, 789, 'Legal', tracker_pb2.FieldTypes.APPROVAL_TYPE, None,
219 '', False, False, False, None, None, '', False, '', '',
220 tracker_pb2.NotifyTriggers.NEVER, 'no_action', 'doc', False)
221 self.fd.approval_id=3
222 self.config.field_defs.append(approval_fd)
223 self.servlet._ProcessDeleteField(self.mr, self.config, approval_fd)
224 self.assertTrue(self.fd.is_deleted)
225 self.assertTrue(approval_fd.is_deleted)
226
227 def testProcessEditField_Normal(self):
228 post_data = fake.PostData(
229 name=['CPU'],
230 field_type=['INT_TYPE'],
231 min_value=['2'],
232 admin_names=['gatsby@example.com'],
233 editor_names=['sport@example.com'],
234 is_restricted_field=['Yes'])
235 self.servlet._ProcessEditField(
236 self.mr, post_data, self.config, self.fd)
237 fd = tracker_bizobj.FindFieldDef('CPU', self.config)
238 self.assertEqual('CPU', fd.field_name)
239 self.assertEqual(2, fd.min_value)
240 self.assertEqual([111], fd.admin_ids)
241 self.assertEqual([222], fd.editor_ids)
242
243 def testProcessEditField_Reject(self):
244 post_data = fake.PostData(
245 name=['CPU'],
246 field_type=['INT_TYPE'],
247 min_value=['4'],
248 max_value=['1'],
249 admin_names=[''],
250 editor_names=[''])
251
252 self.mox.StubOutWithMock(self.servlet, 'PleaseCorrect')
253 self.servlet.PleaseCorrect(
254 self.mr,
255 field_def=mox.IgnoreArg(),
256 initial_applicable_type='',
257 initial_choices='',
258 initial_admins='',
259 initial_editors='',
260 initial_approvers='',
261 initial_is_restricted_field=False)
262 self.mox.ReplayAll()
263
264 url = self.servlet._ProcessEditField(
265 self.mr, post_data, self.config, self.fd)
266 self.assertEqual('Minimum value must be less than maximum.',
267 self.mr.errors.min_value)
268 self.assertIsNone(url)
269
270 fd = tracker_bizobj.FindFieldDef('CPU', self.config)
271 self.assertIsNone(fd.min_value)
272 self.assertIsNone(fd.max_value)
273
274 def testProcessEditField_Reject_EditorsForNonRestrictedField(self):
275 # This method tests that an exception is raised
276 # when trying to add editors to a non restricted field.
277 post_data = fake.PostData(
278 name=['CPU'],
279 field_type=['INT_TYPE'],
280 min_value=['2'],
281 admin_names=[''],
282 editor_names=['gatsby@example.com'])
283
284 self.assertRaises(
285 AssertionError, self.servlet._ProcessEditField, self.mr, post_data,
286 self.config, self.fd)
287
288 def testProcessEditField_RejectAssertions_1(self):
289 # This method tests that an exception is raised
290 # when trying to add editors to an approval field.
291 post_data = fake.PostData(
292 name=['CPU'],
293 approver_names=['gatsby@example.com'],
294 admin_names=[''],
295 editor_names=['sports@example.com'])
296
297 self.assertRaises(
298 AssertionError, self.servlet._ProcessEditField, self.mr, post_data,
299 self.config, self.approval_fd)
300
301 def testProcessEditField_RejectAssertions_2(self):
302 #This method tests that an exception is raised
303 #when trying to restrict an approval field.
304 post_data = fake.PostData(
305 name=['CPU'],
306 approver_names=['gatsby@example.com'],
307 is_restricted_field=['Yes'],
308 admin_names=[''],
309 editor_names=[''])
310
311 self.assertRaises(
312 AssertionError, self.servlet._ProcessEditField, self.mr, post_data,
313 self.config, self.approval_fd)
314
315 def testProcessEditField_RejectApproval(self):
316 self.mr.field_name = 'UIReview'
317 post_data = fake.PostData(
318 name=['UIReview'],
319 admin_names=[''],
320 editor_names=[''],
321 survey=['WIll there be UI changes?'],
322 approver_names=[''])
323
324 self.mox.StubOutWithMock(self.servlet, 'PleaseCorrect')
325 self.servlet.PleaseCorrect(
326 self.mr,
327 field_def=mox.IgnoreArg(),
328 initial_applicable_type='',
329 initial_choices='',
330 initial_admins='',
331 initial_editors='',
332 initial_approvers='',
333 initial_is_restricted_field=False)
334 self.mox.ReplayAll()
335
336 url = self.servlet._ProcessEditField(
337 self.mr, post_data, self.config, self.approval_fd)
338 self.assertEqual('Please provide at least one default approver.',
339 self.mr.errors.approvers)
340 self.assertIsNone(url)
341
342 def testProcessEditField_Approval(self):
343 self.mr.field_name = 'UIReview'
344 post_data = fake.PostData(
345 name=['UIReview'],
346 admin_names=[''],
347 editor_names=[''],
348 survey=['WIll there be UI changes?'],
349 approver_names=['sport@example.com, gatsby@example.com'])
350
351
352 url = self.servlet._ProcessEditField(
353 self.mr, post_data, self.config, self.approval_fd)
354 self.assertTrue('/fields/detail?field=UIReview&saved=1&' in url)
355
356 approval_def = tracker_bizobj.FindApprovalDef('UIReview', self.config)
357 self.assertEqual(len(approval_def.approver_ids), 2)
358 self.assertEqual(sorted(approval_def.approver_ids), sorted([111, 222]))