Adrià Vilanova Martínez | f19ea43 | 2024-01-23 20:20:52 +0100 | [diff] [blame] | 1 | # Copyright 2016 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. |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 4 | |
| 5 | """Unit tests for the componentcreate servlet.""" |
| 6 | from __future__ import print_function |
| 7 | from __future__ import division |
| 8 | from __future__ import absolute_import |
| 9 | |
| 10 | import unittest |
| 11 | |
| 12 | from framework import permissions |
| 13 | from services import service_manager |
| 14 | from testing import fake |
| 15 | from testing import testing_helpers |
| 16 | from tracker import componentcreate |
| 17 | from tracker import tracker_bizobj |
| 18 | |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 19 | |
| 20 | class ComponentCreateTest(unittest.TestCase): |
| 21 | |
| 22 | def setUp(self): |
| 23 | self.services = service_manager.Services( |
| 24 | user=fake.UserService(), |
| 25 | config=fake.ConfigService(), |
| 26 | project=fake.ProjectService()) |
Adrià Vilanova Martínez | f19ea43 | 2024-01-23 20:20:52 +0100 | [diff] [blame] | 27 | self.servlet = componentcreate.ComponentCreate(services=self.services) |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 28 | self.project = self.services.project.TestAddProject('proj') |
| 29 | self.mr = testing_helpers.MakeMonorailRequest( |
| 30 | project=self.project, perms=permissions.OWNER_ACTIVE_PERMISSIONSET) |
| 31 | self.mr.auth.email = 'b@example.com' |
| 32 | self.config = self.services.config.GetProjectConfig( |
| 33 | 'fake cnxn', self.project.project_id) |
| 34 | self.services.config.StoreConfig('fake cnxn', self.config) |
| 35 | self.cd = tracker_bizobj.MakeComponentDef( |
| 36 | 1, self.project.project_id, 'BackEnd', 'doc', False, [], [111], 0, |
| 37 | 122) |
| 38 | self.config.component_defs = [self.cd] |
| 39 | self.services.user.TestAddUser('a@example.com', 111) |
| 40 | self.services.user.TestAddUser('b@example.com', 122) |
| 41 | |
| 42 | def testAssertBasePermission(self): |
| 43 | # Anon users can never do it |
| 44 | self.mr.perms = permissions.READ_ONLY_PERMISSIONSET |
| 45 | self.assertRaises( |
| 46 | permissions.PermissionException, |
| 47 | self.servlet.AssertBasePermission, self.mr) |
| 48 | |
| 49 | # Project owner can do it. |
| 50 | self.mr.perms = permissions.OWNER_ACTIVE_PERMISSIONSET |
| 51 | self.servlet.AssertBasePermission(self.mr) |
| 52 | |
| 53 | # Project member cannot do it |
| 54 | self.mr.perms = permissions.COMMITTER_ACTIVE_PERMISSIONSET |
| 55 | self.assertRaises( |
| 56 | permissions.PermissionException, |
| 57 | self.servlet.AssertBasePermission, self.mr) |
| 58 | self.mr.perms = permissions.CONTRIBUTOR_ACTIVE_PERMISSIONSET |
| 59 | self.assertRaises( |
| 60 | permissions.PermissionException, |
| 61 | self.servlet.AssertBasePermission, self.mr) |
| 62 | |
| 63 | def testGatherPageData_CreatingAtTopLevel(self): |
| 64 | page_data = self.servlet.GatherPageData(self.mr) |
| 65 | self.assertEqual(self.servlet.PROCESS_TAB_COMPONENTS, |
| 66 | page_data['admin_tab_mode']) |
| 67 | self.assertIsNone(page_data['parent_path']) |
| 68 | |
| 69 | def testGatherPageData_CreatingASubComponent(self): |
| 70 | self.mr.component_path = 'BackEnd' |
| 71 | page_data = self.servlet.GatherPageData(self.mr) |
| 72 | self.assertEqual(self.servlet.PROCESS_TAB_COMPONENTS, |
| 73 | page_data['admin_tab_mode']) |
| 74 | self.assertEqual('BackEnd', page_data['parent_path']) |
| 75 | |
| 76 | def testProcessFormData_NotFound(self): |
| 77 | post_data = fake.PostData( |
| 78 | parent_path=['Monitoring'], |
| 79 | leaf_name=['Rules'], |
| 80 | docstring=['Detecting outages'], |
| 81 | deprecated=[False], |
| 82 | admins=[''], |
| 83 | cc=[''], |
| 84 | labels=['']) |
| 85 | self.assertRaises( |
Adrià Vilanova Martínez | f19ea43 | 2024-01-23 20:20:52 +0100 | [diff] [blame] | 86 | Exception, self.servlet.ProcessFormData, self.mr, post_data) |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 87 | |
| 88 | def testProcessFormData_Normal(self): |
| 89 | post_data = fake.PostData( |
| 90 | parent_path=['BackEnd'], |
| 91 | leaf_name=['DB'], |
| 92 | docstring=['A database'], |
| 93 | deprecated=[False], |
| 94 | admins=[''], |
| 95 | cc=[''], |
| 96 | labels=['']) |
| 97 | url = self.servlet.ProcessFormData(self.mr, post_data) |
| 98 | self.assertTrue('/adminComponents?saved=1&' in url) |
| 99 | config = self.services.config.GetProjectConfig( |
| 100 | self.mr.cnxn, self.mr.project_id) |
| 101 | |
| 102 | cd = tracker_bizobj.FindComponentDef('BackEnd>DB', config) |
| 103 | self.assertEqual('BackEnd>DB', cd.path) |
| 104 | self.assertEqual('A database', cd.docstring) |
| 105 | self.assertEqual([], cd.admin_ids) |
| 106 | self.assertEqual([], cd.cc_ids) |
| 107 | self.assertTrue(cd.created > 0) |
| 108 | self.assertEqual(122, cd.creator_id) |
| 109 | |
| 110 | |
| 111 | class ComponentCreateMethodsTest(unittest.TestCase): |
| 112 | |
| 113 | def setUp(self): |
| 114 | self.config = tracker_bizobj.MakeDefaultProjectIssueConfig(789) |
| 115 | cd1 = tracker_bizobj.MakeComponentDef( |
| 116 | 1, 789, 'BackEnd', 'doc', False, [], [111], 0, 122) |
| 117 | cd2 = tracker_bizobj.MakeComponentDef( |
| 118 | 2, 789, 'BackEnd>DB', 'doc', True, [], [111], 0, 122) |
| 119 | self.config.component_defs = [cd1, cd2] |
| 120 | |
| 121 | def testLeafNameErrorMessage_Invalid(self): |
| 122 | self.assertEqual( |
| 123 | 'Invalid component name', |
| 124 | componentcreate.LeafNameErrorMessage('', 'bad name', self.config)) |
| 125 | |
| 126 | def testLeafNameErrorMessage_AlreadyInUse(self): |
| 127 | self.assertEqual( |
| 128 | 'That name is already in use.', |
| 129 | componentcreate.LeafNameErrorMessage('', 'BackEnd', self.config)) |
| 130 | self.assertEqual( |
| 131 | 'That name is already in use.', |
| 132 | componentcreate.LeafNameErrorMessage('BackEnd', 'DB', self.config)) |
| 133 | |
| 134 | def testLeafNameErrorMessage_OK(self): |
| 135 | self.assertIsNone( |
| 136 | componentcreate.LeafNameErrorMessage('', 'FrontEnd', self.config)) |
| 137 | self.assertIsNone( |
| 138 | componentcreate.LeafNameErrorMessage('BackEnd', 'Search', self.config)) |