blob: 982e8bd6bbf527c0c3db30502411307caaa5ffe1 [file] [log] [blame]
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +01001# Copyright 2018 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"""Unittest for the template helpers module."""
6from __future__ import print_function
7from __future__ import division
8from __future__ import absolute_import
9
10import logging
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +010011import six
Copybara854996b2021-09-07 19:36:02 +000012import unittest
13
14import settings
15
16from services import service_manager
17from testing import fake
18from testing import testing_helpers
19from tracker import template_helpers
20from tracker import tracker_bizobj
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +010021from mrproto import tracker_pb2
Copybara854996b2021-09-07 19:36:02 +000022
23
24class TemplateHelpers(unittest.TestCase):
25
26 def setUp(self):
27 self.services = service_manager.Services(
28 user=fake.UserService(),
29 config=fake.ConfigService(),
30 project=fake.ProjectService(),
31 usergroup=fake.UserGroupService())
32 self.project = self.services.project.TestAddProject('proj')
33 self.mr = testing_helpers.MakeMonorailRequest(
34 project=self.project)
35 self.config = self.services.config.GetProjectConfig(
36 'fake cnxn', self.project.project_id)
37 self.fd_1 = tracker_bizobj.MakeFieldDef(
38 1, 789, 'UXReview', tracker_pb2.FieldTypes.STR_TYPE, None,
39 '', False, False, False, None, None, '', False, '', '',
40 tracker_pb2.NotifyTriggers.NEVER, 'no_action',
41 'Approval for UX review', False)
42 self.fd_2 = tracker_bizobj.MakeFieldDef(
43 2, 789, 'UXReview', tracker_pb2.FieldTypes.STR_TYPE, None,
44 '', False, False, False, None, None, '', False, '', '',
45 tracker_pb2.NotifyTriggers.NEVER, 'no_action',
46 'Approval for UX review', False)
47 self.fd_3 = tracker_bizobj.MakeFieldDef(
48 3, 789, 'UXApproval', tracker_pb2.FieldTypes.APPROVAL_TYPE, None,
49 '', False, False, False, None, None, '', False, '', '',
50 tracker_pb2.NotifyTriggers.NEVER, 'no_action',
51 'Approval for UX review', False)
52 self.fd_4 = tracker_bizobj.MakeFieldDef(
53 4, 789, 'TestApproval', tracker_pb2.FieldTypes.APPROVAL_TYPE, None,
54 '', False, False, False, None, None, '', False, '', '',
55 tracker_pb2.NotifyTriggers.NEVER, 'no_action',
56 'Approval for Test review', False)
57 self.fd_5 = tracker_bizobj.MakeFieldDef(
58 5, 789, 'SomeApproval', tracker_pb2.FieldTypes.APPROVAL_TYPE, None,
59 '', False, False, False, None, None, '', False, '', '',
60 tracker_pb2.NotifyTriggers.NEVER, 'no_action',
61 'Approval for Test review', False)
62 self.ad_3 = tracker_pb2.ApprovalDef(approval_id=3)
63 self.ad_4 = tracker_pb2.ApprovalDef(approval_id=4)
64 self.ad_5 = tracker_pb2.ApprovalDef(approval_id=5)
65 self.cd_1 = tracker_bizobj.MakeComponentDef(
66 1, 789, 'BackEnd', 'doc', False, [111], [], 100000, 222)
67
68 self.services.user.TestAddUser('1@ex.com', 111)
69 self.services.user.TestAddUser('2@ex.com', 222)
70 self.services.user.TestAddUser('3@ex.com', 333)
71 self.services.project.TestAddProjectMembers(
72 [111], self.project, 'OWNER_ROLE')
73
74 def testParseTemplateRequest_Empty(self):
75 post_data = fake.PostData()
76 parsed = template_helpers.ParseTemplateRequest(post_data, self.config)
77 self.assertEqual(parsed.name, '')
78 self.assertFalse(parsed.members_only)
79 self.assertEqual(parsed.summary, '')
80 self.assertFalse(parsed.summary_must_be_edited)
81 self.assertEqual(parsed.content, '')
82 self.assertEqual(parsed.status, '')
83 self.assertEqual(parsed.owner_str, '')
84 self.assertEqual(parsed.labels, [])
85 self.assertEqual(parsed.field_val_strs, {})
86 self.assertEqual(parsed.component_paths, [])
87 self.assertFalse(parsed.component_required)
88 self.assertFalse(parsed.owner_defaults_to_member)
89 self.assertFalse(parsed.add_approvals)
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +010090 six.assertCountEqual(self, parsed.phase_names, ['', '', '', '', '', ''])
Copybara854996b2021-09-07 19:36:02 +000091 self.assertEqual(parsed.approvals_to_phase_idx, {})
92 self.assertEqual(parsed.required_approval_ids, [])
93
94 def testParseTemplateRequest_Normal(self):
95 self.config.field_defs.extend([self.fd_1, self.fd_2])
96 self.config.approval_defs.extend([self.ad_3, self.ad_4])
97 post_data = fake.PostData(
98 name=['sometemplate'],
99 members_only=['on'],
100 summary=['TLDR'],
101 summary_must_be_edited=['on'],
102 content=['HEY WHY'],
103 status=['Accepted'],
104 owner=['someone@world.com'],
105 label=['label-One', 'label-Two'],
106 custom_1=['NO'],
107 custom_2=['MOOD'],
108 components=['hey, hey2,he3'],
109 component_required=['on'],
110 owner_defaults_to_memeber=['no'],
111 admin_names=['jojwang@test.com, annajo@test.com'],
112 add_approvals=['on'],
113 phase_0=['Canary'],
114 phase_1=['Stable-Exp'],
115 phase_2=['Stable'],
116 phase_3=[''],
117 phase_4=[''],
118 phase_5=['Oops'],
119 approval_3=['phase_2'],
120 approval_4=['no_phase'],
121 approval_3_required=['on'],
122 approval_4_required=['on'],
123 # ignore required cb for omitted approvals
124 approval_5_required=['on']
125 )
126
127 parsed = template_helpers.ParseTemplateRequest(post_data, self.config)
128 self.assertEqual(parsed.name, 'sometemplate')
129 self.assertTrue(parsed.members_only)
130 self.assertEqual(parsed.summary, 'TLDR')
131 self.assertTrue(parsed.summary_must_be_edited)
132 self.assertEqual(parsed.content, 'HEY WHY')
133 self.assertEqual(parsed.status, 'Accepted')
134 self.assertEqual(parsed.owner_str, 'someone@world.com')
135 self.assertEqual(parsed.labels, ['label-One', 'label-Two'])
136 self.assertEqual(parsed.field_val_strs, {1: ['NO'], 2: ['MOOD']})
137 self.assertEqual(parsed.component_paths, ['hey', 'hey2', 'he3'])
138 self.assertTrue(parsed.component_required)
139 self.assertFalse(parsed.owner_defaults_to_member)
140 self.assertTrue(parsed.add_approvals)
141 self.assertEqual(parsed.admin_str, 'jojwang@test.com, annajo@test.com')
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +0100142 six.assertCountEqual(
143 self, parsed.phase_names,
144 ['Canary', 'Stable-Exp', 'Stable', '', '', 'Oops'])
Copybara854996b2021-09-07 19:36:02 +0000145 self.assertEqual(parsed.approvals_to_phase_idx, {3: 2, 4: None})
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +0100146 six.assertCountEqual(self, parsed.required_approval_ids, [3, 4])
Copybara854996b2021-09-07 19:36:02 +0000147
148 def testGetTemplateInfoFromParsed_Normal(self):
149 self.config.field_defs.extend([self.fd_1, self.fd_2])
150 self.config.component_defs.append(self.cd_1)
151 parsed = template_helpers.ParsedTemplate(
152 'template', True, 'summary', True, 'content', 'Available',
153 '1@ex.com', ['label1', 'label1'], {1: ['NO'], 2: ['MOOD']},
154 ['BackEnd'], True, True, '2@ex.com', False, [], {}, [])
155 (admin_ids, owner_id, component_ids,
156 field_values, phases,
157 approval_values) = template_helpers.GetTemplateInfoFromParsed(
158 self.mr, self.services, parsed, self.config)
159 self.assertEqual(admin_ids, [222])
160 self.assertEqual(owner_id, 111)
161 self.assertEqual(component_ids, [1])
162 self.assertEqual(field_values[0].str_value, 'NO')
163 self.assertEqual(field_values[1].str_value, 'MOOD')
164 self.assertEqual(phases, [])
165 self.assertEqual(approval_values, [])
166
167 def testGetTemplateInfoFromParsed_Errors(self):
168 self.config.field_defs.extend([self.fd_1, self.fd_2])
169 parsed = template_helpers.ParsedTemplate(
170 'template', True, 'summary', True, 'content', 'Available',
171 '4@ex.com', ['label1', 'label1'], {1: ['NO'], 2: ['MOOD']},
172 ['BackEnd'], True, True, '2@ex.com', False, [], {}, [])
173 (admin_ids, _owner_id, _component_ids,
174 field_values, phases,
175 approval_values) = template_helpers.GetTemplateInfoFromParsed(
176 self.mr, self.services, parsed, self.config)
177 self.assertEqual(admin_ids, [222])
178 self.assertEqual(field_values[0].str_value, 'NO')
179 self.assertEqual(field_values[1].str_value, 'MOOD')
180 self.assertEqual(self.mr.errors.owner, 'Owner not found.')
181 self.assertEqual(self.mr.errors.components, 'Unknown component BackEnd')
182 self.assertEqual(phases, [])
183 self.assertEqual(approval_values, [])
184
185 def testGetPhasesAndApprovalsFromParsed_Normal(self):
186 self.config.field_defs.extend([self.fd_1, self.fd_2])
187 self.config.approval_defs.extend([self.ad_3, self.ad_4, self.ad_5])
188
189 phase_names = ['Canary', '', 'Stable-Exp', '', '', '']
190 approvals_to_phase_idx = {3: 0, 4: None, 5: 2}
191 required_approval_ids = [3, 5]
192
193 phases, approval_values = template_helpers._GetPhasesAndApprovalsFromParsed(
194 self.mr, phase_names, approvals_to_phase_idx, required_approval_ids)
195 self.assertEqual(len(phases), 2)
196 self.assertEqual(len(approval_values), 3)
197
198 canary = tracker_bizobj.FindPhase('canary', phases)
199 self.assertEqual(canary.rank, 0)
200 av_3 = tracker_bizobj.FindApprovalValueByID(3, approval_values)
201 self.assertEqual(av_3.status, tracker_pb2.ApprovalStatus.NEEDS_REVIEW)
202 self.assertEqual(av_3.phase_id, canary.phase_id)
203
204 av_4 = tracker_bizobj.FindApprovalValueByID(4, approval_values)
205 self.assertEqual(av_4.status, tracker_pb2.ApprovalStatus.NOT_SET)
206 self.assertIsNone(av_4.phase_id)
207
208 stable_exp = tracker_bizobj.FindPhase('stable-exp', phases)
209 self.assertEqual(stable_exp.rank, 2)
210 av_5 = tracker_bizobj.FindApprovalValueByID(5, approval_values)
211 self.assertEqual(av_5.status, tracker_pb2.ApprovalStatus.NEEDS_REVIEW)
212 self.assertEqual(av_5.phase_id, stable_exp.phase_id)
213
214 self.assertIsNone(self.mr.errors.phase_approvals)
215
216 def testGetPhasesAndApprovalsFromParsed_Errors(self):
217 self.config.field_defs.extend([self.fd_1, self.fd_2])
218 self.config.approval_defs.extend([self.ad_3, self.ad_4, self.ad_5])
219 required_approval_ids = []
220
221 phase_names = ['Canary', 'Extra', 'Stable-Exp', '', '', '']
222 approvals_to_phase_idx = {3: 0, 4: None, 5: 2}
223
224 template_helpers._GetPhasesAndApprovalsFromParsed(
225 self.mr, phase_names, approvals_to_phase_idx, required_approval_ids)
226 self.assertEqual(self.mr.errors.phase_approvals,
227 'Defined gates must have assigned approvals.')
228
229 def testGetPhasesAndApprovalsFromParsed_DupsErrors(self):
230 self.config.field_defs.extend([self.fd_1, self.fd_2])
231 self.config.approval_defs.extend([self.ad_3, self.ad_4, self.ad_5])
232 required_approval_ids = []
233
234 phase_names = ['Canary', 'canary', 'Stable-Exp', '', '', '']
235 approvals_to_phase_idx = {3: 0, 4: None, 5: 2}
236
237 template_helpers._GetPhasesAndApprovalsFromParsed(
238 self.mr, phase_names, approvals_to_phase_idx, required_approval_ids)
239 self.assertEqual(self.mr.errors.phase_approvals,
240 'Duplicate gate names.')
241
242 def testGetPhasesAndApprovalsFromParsed_InvalidPhaseName(self):
243 self.config.field_defs.extend([self.fd_1, self.fd_2])
244 self.config.approval_defs.extend([self.ad_3, self.ad_4, self.ad_5])
245 required_approval_ids = []
246
247 phase_names = ['Canary', 'A B', 'Stable-Exp', '', '', '']
248 approvals_to_phase_idx = {3: 0, 4: None, 5: 2}
249
250 template_helpers._GetPhasesAndApprovalsFromParsed(
251 self.mr, phase_names, approvals_to_phase_idx, required_approval_ids)
252 self.assertEqual(self.mr.errors.phase_approvals,
253 'Invalid gate name(s).')
254
255 def testGatherApprovalsPageData(self):
256 self.fd_3.is_deleted = True
257 self.config.field_defs = [self.fd_3, self.fd_4, self.fd_5]
258 approval_values = [
259 tracker_pb2.ApprovalValue(approval_id=3, phase_id=8),
260 tracker_pb2.ApprovalValue(
261 approval_id=4, phase_id=9,
262 status=tracker_pb2.ApprovalStatus.NEEDS_REVIEW),
263 tracker_pb2.ApprovalValue(approval_id=5)
264 ]
265 tmpl_phases = [
266 tracker_pb2.Phase(phase_id=8, rank=1, name='deletednoshow'),
267 tracker_pb2.Phase(phase_id=9, rank=2, name='notdeleted')
268 ]
269
270 (prechecked_approvals, required_approval_ids,
271 phases) = template_helpers.GatherApprovalsPageData(
272 approval_values, tmpl_phases, self.config)
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +0100273 six.assertCountEqual(self, prechecked_approvals, ['4_phase_0', '5'])
Copybara854996b2021-09-07 19:36:02 +0000274 self.assertEqual(required_approval_ids, [4])
275 self.assertEqual(phases[0], tmpl_phases[1])
276 self.assertIsNone(phases[1].name)
277 self.assertEqual(len(phases), 6)
278
279 def testGetCheckedApprovalsFromParsed(self):
280 approvals_to_phase_idx = {23: 0, 25: 1, 26: None}
281 checked = template_helpers.GetCheckedApprovalsFromParsed(
282 approvals_to_phase_idx)
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +0100283 six.assertCountEqual(self, checked, ['23_phase_0', '25_phase_1', '26'])
Copybara854996b2021-09-07 19:36:02 +0000284
285 def testGetIssueFromTemplate(self):
286 """Can fill and return the templated issue"""
287 expected_fvs = [
288 tracker_pb2.FieldValue(field_id=123, str_value='fv_1_value'),
289 tracker_pb2.FieldValue(field_id=124, str_value='fv_2_value'),
290 ]
291 expected_phases = [
292 tracker_pb2.Phase(phase_id=123, name='phase_1_name', rank=1)
293 ]
294 expected_avs = [
295 tracker_pb2.ApprovalValue(
296 approval_id=1,
297 setter_id=111,
298 set_on=1232352,
299 approver_ids=[111],
300 phase_id=123),
301 ]
302 input_template = tracker_pb2.TemplateDef(
303 summary='expected_summary',
304 owner_id=111,
305 status='expected_status',
306 labels=['expected-label_1, expected-label_2'],
307 field_values=expected_fvs,
308 component_ids=[987],
309 phases=expected_phases,
310 approval_values=expected_avs)
311 reporter_id = 321
312 project_id = 1
313
314 actual = template_helpers.GetIssueFromTemplate(
315 input_template, project_id, reporter_id)
316 expected = tracker_pb2.Issue(
317 project_id=project_id,
318 summary='expected_summary',
319 status='expected_status',
320 owner_id=111,
321 labels=['expected-label_1, expected-label_2'],
322 component_ids=[987],
323 reporter_id=reporter_id,
324 field_values=expected_fvs,
325 phases=expected_phases,
326 approval_values=expected_avs)
327 self.assertEqual(actual, expected)
328
329 def testGetIssueFromTemplate_NoOwner(self):
330 """Uses reporter as owner when owner_defaults_to_member"""
331 input_template = tracker_pb2.TemplateDef(owner_defaults_to_member=False)
332
333 actual = template_helpers.GetIssueFromTemplate(input_template, 1, 1)
334 self.assertEqual(actual.owner_id, None)
335
336 def testGetIssueFromTemplate_DefaultsOwnerToReporter(self):
337 """Uses reporter as owner when owner_defaults_to_member"""
338 input_template = tracker_pb2.TemplateDef(owner_defaults_to_member=True)
339 reporter_id = 321
340
341 actual = template_helpers.GetIssueFromTemplate(
342 input_template, 1, reporter_id)
343 self.assertEqual(actual.owner_id, reporter_id)
344
345 def testGetIssueFromTemplate_SpecifiedOwnerOverridesReporter(self):
346 """Specified owner overrides owner_defaults_to_member"""
347 expected_owner_id = 111
348 input_template = tracker_pb2.TemplateDef(
349 owner_id=expected_owner_id, owner_defaults_to_member=True)
350 reporter_id = 321
351
352 actual = template_helpers.GetIssueFromTemplate(
353 input_template, 1, reporter_id)
354 self.assertEqual(actual.owner_id, expected_owner_id)