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