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