Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 1 | # 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 component_helpers module.""" |
| 7 | from __future__ import print_function |
| 8 | from __future__ import division |
| 9 | from __future__ import absolute_import |
| 10 | |
| 11 | import unittest |
| 12 | |
| 13 | from proto import tracker_pb2 |
| 14 | from services import service_manager |
| 15 | from testing import fake |
| 16 | from tracker import component_helpers |
| 17 | from tracker import tracker_bizobj |
| 18 | |
| 19 | |
| 20 | class ComponentHelpersTest(unittest.TestCase): |
| 21 | |
| 22 | def setUp(self): |
| 23 | self.config = tracker_bizobj.MakeDefaultProjectIssueConfig(789) |
| 24 | self.cd1 = tracker_bizobj.MakeComponentDef( |
| 25 | 1, 789, 'FrontEnd', 'doc', False, [], [111], 0, 0) |
| 26 | self.cd2 = tracker_bizobj.MakeComponentDef( |
| 27 | 2, 789, 'FrontEnd>Splash', 'doc', False, [], [222], 0, 0) |
| 28 | self.cd3 = tracker_bizobj.MakeComponentDef( |
| 29 | 3, 789, 'BackEnd', 'doc', True, [], [111, 333], 0, 0) |
| 30 | self.config.component_defs = [self.cd1, self.cd2, self.cd3] |
| 31 | self.services = service_manager.Services( |
| 32 | user=fake.UserService(), |
| 33 | config=fake.ConfigService()) |
| 34 | self.services.user.TestAddUser('a@example.com', 111) |
| 35 | self.services.user.TestAddUser('b@example.com', 222) |
| 36 | self.services.user.TestAddUser('c@example.com', 333) |
| 37 | self.mr = fake.MonorailRequest(self.services) |
| 38 | self.mr.cnxn = fake.MonorailConnection() |
| 39 | |
| 40 | def testParseComponentRequest_Empty(self): |
| 41 | post_data = fake.PostData(admins=[''], cc=[''], labels=['']) |
| 42 | parsed = component_helpers.ParseComponentRequest( |
| 43 | self.mr, post_data, self.services) |
| 44 | self.assertEqual('', parsed.leaf_name) |
| 45 | self.assertEqual('', parsed.docstring) |
| 46 | self.assertEqual([], parsed.admin_usernames) |
| 47 | self.assertEqual([], parsed.cc_usernames) |
| 48 | self.assertEqual([], parsed.admin_ids) |
| 49 | self.assertEqual([], parsed.cc_ids) |
| 50 | self.assertFalse(self.mr.errors.AnyErrors()) |
| 51 | |
| 52 | def testParseComponentRequest_Normal(self): |
| 53 | post_data = fake.PostData( |
| 54 | leaf_name=['FrontEnd'], |
| 55 | docstring=['The server-side app that serves pages'], |
| 56 | deprecated=[False], |
| 57 | admins=['a@example.com'], |
| 58 | cc=['b@example.com, c@example.com'], |
| 59 | labels=['Hot, Cold']) |
| 60 | parsed = component_helpers.ParseComponentRequest( |
| 61 | self.mr, post_data, self.services) |
| 62 | self.assertEqual('FrontEnd', parsed.leaf_name) |
| 63 | self.assertEqual('The server-side app that serves pages', parsed.docstring) |
| 64 | self.assertEqual(['a@example.com'], parsed.admin_usernames) |
| 65 | self.assertEqual(['b@example.com', 'c@example.com'], parsed.cc_usernames) |
| 66 | self.assertEqual(['Hot', 'Cold'], parsed.label_strs) |
| 67 | self.assertEqual([111], parsed.admin_ids) |
| 68 | self.assertEqual([222, 333], parsed.cc_ids) |
| 69 | self.assertEqual([0, 1], parsed.label_ids) |
| 70 | self.assertFalse(self.mr.errors.AnyErrors()) |
| 71 | |
| 72 | def testParseComponentRequest_InvalidUser(self): |
| 73 | post_data = fake.PostData( |
| 74 | leaf_name=['FrontEnd'], |
| 75 | docstring=['The server-side app that serves pages'], |
| 76 | deprecated=[False], |
| 77 | admins=['a@example.com, invalid_user'], |
| 78 | cc=['b@example.com, c@example.com'], |
| 79 | labels=['']) |
| 80 | parsed = component_helpers.ParseComponentRequest( |
| 81 | self.mr, post_data, self.services) |
| 82 | self.assertEqual('FrontEnd', parsed.leaf_name) |
| 83 | self.assertEqual('The server-side app that serves pages', parsed.docstring) |
| 84 | self.assertEqual(['a@example.com', 'invalid_user'], parsed.admin_usernames) |
| 85 | self.assertEqual(['b@example.com', 'c@example.com'], parsed.cc_usernames) |
| 86 | self.assertEqual([111], parsed.admin_ids) |
| 87 | self.assertEqual([222, 333], parsed.cc_ids) |
| 88 | self.assertTrue(self.mr.errors.AnyErrors()) |
| 89 | self.assertEqual('invalid_user unrecognized', self.mr.errors.member_admins) |
| 90 | |
| 91 | def testGetComponentCcIDs(self): |
| 92 | issue = tracker_pb2.Issue() |
| 93 | issues_components_cc_ids = component_helpers.GetComponentCcIDs( |
| 94 | issue, self.config) |
| 95 | self.assertEqual(set(), issues_components_cc_ids) |
| 96 | |
| 97 | issue.component_ids = [1, 2] |
| 98 | issues_components_cc_ids = component_helpers.GetComponentCcIDs( |
| 99 | issue, self.config) |
| 100 | self.assertEqual({111, 222}, issues_components_cc_ids) |
| 101 | |
| 102 | def testGetCcIDsForComponentAndAncestors(self): |
| 103 | components_cc_ids = component_helpers.GetCcIDsForComponentAndAncestors( |
| 104 | self.config, self.cd1) |
| 105 | self.assertEqual({111}, components_cc_ids) |
| 106 | |
| 107 | components_cc_ids = component_helpers.GetCcIDsForComponentAndAncestors( |
| 108 | self.config, self.cd2) |
| 109 | self.assertEqual({111, 222}, components_cc_ids) |
| 110 | |
| 111 | components_cc_ids = component_helpers.GetCcIDsForComponentAndAncestors( |
| 112 | self.config, self.cd3) |
| 113 | self.assertEqual({111, 333}, components_cc_ids) |