Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 1 | # Copyright 2018 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 | """Unittest for the template helpers module.""" |
| 7 | from __future__ import print_function |
| 8 | from __future__ import division |
| 9 | from __future__ import absolute_import |
| 10 | |
| 11 | import logging |
| 12 | import unittest |
| 13 | |
| 14 | import settings |
| 15 | |
| 16 | from services import service_manager |
| 17 | from testing import fake |
| 18 | from testing import testing_helpers |
| 19 | from tracker import template_helpers |
| 20 | from tracker import tracker_bizobj |
| 21 | from proto import tracker_pb2 |
| 22 | |
| 23 | |
| 24 | class 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) |
| 90 | self.assertItemsEqual(parsed.phase_names, ['', '', '', '', '', '']) |
| 91 | 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') |
| 142 | self.assertItemsEqual(parsed.phase_names, |
| 143 | ['Canary', 'Stable-Exp', 'Stable', '', '', 'Oops']) |
| 144 | self.assertEqual(parsed.approvals_to_phase_idx, {3: 2, 4: None}) |
| 145 | self.assertItemsEqual(parsed.required_approval_ids, [3, 4]) |
| 146 | |
| 147 | def testGetTemplateInfoFromParsed_Normal(self): |
| 148 | self.config.field_defs.extend([self.fd_1, self.fd_2]) |
| 149 | self.config.component_defs.append(self.cd_1) |
| 150 | parsed = template_helpers.ParsedTemplate( |
| 151 | 'template', True, 'summary', True, 'content', 'Available', |
| 152 | '1@ex.com', ['label1', 'label1'], {1: ['NO'], 2: ['MOOD']}, |
| 153 | ['BackEnd'], True, True, '2@ex.com', False, [], {}, []) |
| 154 | (admin_ids, owner_id, component_ids, |
| 155 | field_values, phases, |
| 156 | approval_values) = template_helpers.GetTemplateInfoFromParsed( |
| 157 | self.mr, self.services, parsed, self.config) |
| 158 | self.assertEqual(admin_ids, [222]) |
| 159 | self.assertEqual(owner_id, 111) |
| 160 | self.assertEqual(component_ids, [1]) |
| 161 | self.assertEqual(field_values[0].str_value, 'NO') |
| 162 | self.assertEqual(field_values[1].str_value, 'MOOD') |
| 163 | self.assertEqual(phases, []) |
| 164 | self.assertEqual(approval_values, []) |
| 165 | |
| 166 | def testGetTemplateInfoFromParsed_Errors(self): |
| 167 | self.config.field_defs.extend([self.fd_1, self.fd_2]) |
| 168 | parsed = template_helpers.ParsedTemplate( |
| 169 | 'template', True, 'summary', True, 'content', 'Available', |
| 170 | '4@ex.com', ['label1', 'label1'], {1: ['NO'], 2: ['MOOD']}, |
| 171 | ['BackEnd'], True, True, '2@ex.com', False, [], {}, []) |
| 172 | (admin_ids, _owner_id, _component_ids, |
| 173 | field_values, phases, |
| 174 | approval_values) = template_helpers.GetTemplateInfoFromParsed( |
| 175 | self.mr, self.services, parsed, self.config) |
| 176 | self.assertEqual(admin_ids, [222]) |
| 177 | self.assertEqual(field_values[0].str_value, 'NO') |
| 178 | self.assertEqual(field_values[1].str_value, 'MOOD') |
| 179 | self.assertEqual(self.mr.errors.owner, 'Owner not found.') |
| 180 | self.assertEqual(self.mr.errors.components, 'Unknown component BackEnd') |
| 181 | self.assertEqual(phases, []) |
| 182 | self.assertEqual(approval_values, []) |
| 183 | |
| 184 | def testGetPhasesAndApprovalsFromParsed_Normal(self): |
| 185 | self.config.field_defs.extend([self.fd_1, self.fd_2]) |
| 186 | self.config.approval_defs.extend([self.ad_3, self.ad_4, self.ad_5]) |
| 187 | |
| 188 | phase_names = ['Canary', '', 'Stable-Exp', '', '', ''] |
| 189 | approvals_to_phase_idx = {3: 0, 4: None, 5: 2} |
| 190 | required_approval_ids = [3, 5] |
| 191 | |
| 192 | phases, approval_values = template_helpers._GetPhasesAndApprovalsFromParsed( |
| 193 | self.mr, phase_names, approvals_to_phase_idx, required_approval_ids) |
| 194 | self.assertEqual(len(phases), 2) |
| 195 | self.assertEqual(len(approval_values), 3) |
| 196 | |
| 197 | canary = tracker_bizobj.FindPhase('canary', phases) |
| 198 | self.assertEqual(canary.rank, 0) |
| 199 | av_3 = tracker_bizobj.FindApprovalValueByID(3, approval_values) |
| 200 | self.assertEqual(av_3.status, tracker_pb2.ApprovalStatus.NEEDS_REVIEW) |
| 201 | self.assertEqual(av_3.phase_id, canary.phase_id) |
| 202 | |
| 203 | av_4 = tracker_bizobj.FindApprovalValueByID(4, approval_values) |
| 204 | self.assertEqual(av_4.status, tracker_pb2.ApprovalStatus.NOT_SET) |
| 205 | self.assertIsNone(av_4.phase_id) |
| 206 | |
| 207 | stable_exp = tracker_bizobj.FindPhase('stable-exp', phases) |
| 208 | self.assertEqual(stable_exp.rank, 2) |
| 209 | av_5 = tracker_bizobj.FindApprovalValueByID(5, approval_values) |
| 210 | self.assertEqual(av_5.status, tracker_pb2.ApprovalStatus.NEEDS_REVIEW) |
| 211 | self.assertEqual(av_5.phase_id, stable_exp.phase_id) |
| 212 | |
| 213 | self.assertIsNone(self.mr.errors.phase_approvals) |
| 214 | |
| 215 | def testGetPhasesAndApprovalsFromParsed_Errors(self): |
| 216 | self.config.field_defs.extend([self.fd_1, self.fd_2]) |
| 217 | self.config.approval_defs.extend([self.ad_3, self.ad_4, self.ad_5]) |
| 218 | required_approval_ids = [] |
| 219 | |
| 220 | phase_names = ['Canary', 'Extra', 'Stable-Exp', '', '', ''] |
| 221 | approvals_to_phase_idx = {3: 0, 4: None, 5: 2} |
| 222 | |
| 223 | template_helpers._GetPhasesAndApprovalsFromParsed( |
| 224 | self.mr, phase_names, approvals_to_phase_idx, required_approval_ids) |
| 225 | self.assertEqual(self.mr.errors.phase_approvals, |
| 226 | 'Defined gates must have assigned approvals.') |
| 227 | |
| 228 | def testGetPhasesAndApprovalsFromParsed_DupsErrors(self): |
| 229 | self.config.field_defs.extend([self.fd_1, self.fd_2]) |
| 230 | self.config.approval_defs.extend([self.ad_3, self.ad_4, self.ad_5]) |
| 231 | required_approval_ids = [] |
| 232 | |
| 233 | phase_names = ['Canary', 'canary', 'Stable-Exp', '', '', ''] |
| 234 | approvals_to_phase_idx = {3: 0, 4: None, 5: 2} |
| 235 | |
| 236 | template_helpers._GetPhasesAndApprovalsFromParsed( |
| 237 | self.mr, phase_names, approvals_to_phase_idx, required_approval_ids) |
| 238 | self.assertEqual(self.mr.errors.phase_approvals, |
| 239 | 'Duplicate gate names.') |
| 240 | |
| 241 | def testGetPhasesAndApprovalsFromParsed_InvalidPhaseName(self): |
| 242 | self.config.field_defs.extend([self.fd_1, self.fd_2]) |
| 243 | self.config.approval_defs.extend([self.ad_3, self.ad_4, self.ad_5]) |
| 244 | required_approval_ids = [] |
| 245 | |
| 246 | phase_names = ['Canary', 'A B', 'Stable-Exp', '', '', ''] |
| 247 | approvals_to_phase_idx = {3: 0, 4: None, 5: 2} |
| 248 | |
| 249 | template_helpers._GetPhasesAndApprovalsFromParsed( |
| 250 | self.mr, phase_names, approvals_to_phase_idx, required_approval_ids) |
| 251 | self.assertEqual(self.mr.errors.phase_approvals, |
| 252 | 'Invalid gate name(s).') |
| 253 | |
| 254 | def testGatherApprovalsPageData(self): |
| 255 | self.fd_3.is_deleted = True |
| 256 | self.config.field_defs = [self.fd_3, self.fd_4, self.fd_5] |
| 257 | approval_values = [ |
| 258 | tracker_pb2.ApprovalValue(approval_id=3, phase_id=8), |
| 259 | tracker_pb2.ApprovalValue( |
| 260 | approval_id=4, phase_id=9, |
| 261 | status=tracker_pb2.ApprovalStatus.NEEDS_REVIEW), |
| 262 | tracker_pb2.ApprovalValue(approval_id=5) |
| 263 | ] |
| 264 | tmpl_phases = [ |
| 265 | tracker_pb2.Phase(phase_id=8, rank=1, name='deletednoshow'), |
| 266 | tracker_pb2.Phase(phase_id=9, rank=2, name='notdeleted') |
| 267 | ] |
| 268 | |
| 269 | (prechecked_approvals, required_approval_ids, |
| 270 | phases) = template_helpers.GatherApprovalsPageData( |
| 271 | approval_values, tmpl_phases, self.config) |
| 272 | self.assertItemsEqual(prechecked_approvals, |
| 273 | ['4_phase_0', '5']) |
| 274 | 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) |
| 283 | self.assertItemsEqual(checked, |
| 284 | ['23_phase_0', '25_phase_1', '26']) |
| 285 | |
| 286 | def testGetIssueFromTemplate(self): |
| 287 | """Can fill and return the templated issue""" |
| 288 | expected_fvs = [ |
| 289 | tracker_pb2.FieldValue(field_id=123, str_value='fv_1_value'), |
| 290 | tracker_pb2.FieldValue(field_id=124, str_value='fv_2_value'), |
| 291 | ] |
| 292 | expected_phases = [ |
| 293 | tracker_pb2.Phase(phase_id=123, name='phase_1_name', rank=1) |
| 294 | ] |
| 295 | expected_avs = [ |
| 296 | tracker_pb2.ApprovalValue( |
| 297 | approval_id=1, |
| 298 | setter_id=111, |
| 299 | set_on=1232352, |
| 300 | approver_ids=[111], |
| 301 | phase_id=123), |
| 302 | ] |
| 303 | input_template = tracker_pb2.TemplateDef( |
| 304 | summary='expected_summary', |
| 305 | owner_id=111, |
| 306 | status='expected_status', |
| 307 | labels=['expected-label_1, expected-label_2'], |
| 308 | field_values=expected_fvs, |
| 309 | component_ids=[987], |
| 310 | phases=expected_phases, |
| 311 | approval_values=expected_avs) |
| 312 | reporter_id = 321 |
| 313 | project_id = 1 |
| 314 | |
| 315 | actual = template_helpers.GetIssueFromTemplate( |
| 316 | input_template, project_id, reporter_id) |
| 317 | expected = tracker_pb2.Issue( |
| 318 | project_id=project_id, |
| 319 | summary='expected_summary', |
| 320 | status='expected_status', |
| 321 | owner_id=111, |
| 322 | labels=['expected-label_1, expected-label_2'], |
| 323 | component_ids=[987], |
| 324 | reporter_id=reporter_id, |
| 325 | field_values=expected_fvs, |
| 326 | phases=expected_phases, |
| 327 | approval_values=expected_avs) |
| 328 | self.assertEqual(actual, expected) |
| 329 | |
| 330 | def testGetIssueFromTemplate_NoOwner(self): |
| 331 | """Uses reporter as owner when owner_defaults_to_member""" |
| 332 | input_template = tracker_pb2.TemplateDef(owner_defaults_to_member=False) |
| 333 | |
| 334 | actual = template_helpers.GetIssueFromTemplate(input_template, 1, 1) |
| 335 | self.assertEqual(actual.owner_id, None) |
| 336 | |
| 337 | def testGetIssueFromTemplate_DefaultsOwnerToReporter(self): |
| 338 | """Uses reporter as owner when owner_defaults_to_member""" |
| 339 | input_template = tracker_pb2.TemplateDef(owner_defaults_to_member=True) |
| 340 | reporter_id = 321 |
| 341 | |
| 342 | actual = template_helpers.GetIssueFromTemplate( |
| 343 | input_template, 1, reporter_id) |
| 344 | self.assertEqual(actual.owner_id, reporter_id) |
| 345 | |
| 346 | def testGetIssueFromTemplate_SpecifiedOwnerOverridesReporter(self): |
| 347 | """Specified owner overrides owner_defaults_to_member""" |
| 348 | expected_owner_id = 111 |
| 349 | input_template = tracker_pb2.TemplateDef( |
| 350 | owner_id=expected_owner_id, owner_defaults_to_member=True) |
| 351 | reporter_id = 321 |
| 352 | |
| 353 | actual = template_helpers.GetIssueFromTemplate( |
| 354 | input_template, 1, reporter_id) |
| 355 | self.assertEqual(actual.owner_id, expected_owner_id) |