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 helpers module.""" |
| 6 | from __future__ import print_function |
| 7 | from __future__ import division |
| 8 | from __future__ import absolute_import |
| 9 | |
| 10 | import unittest |
| 11 | |
| 12 | from mock import patch |
| 13 | |
| 14 | from framework import framework_views |
| 15 | from framework import permissions |
| 16 | from project import project_constants |
| 17 | from project import project_helpers |
Adrià Vilanova Martínez | f19ea43 | 2024-01-23 20:20:52 +0100 | [diff] [blame^] | 18 | from mrproto import project_pb2 |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 19 | from services import service_manager |
| 20 | from testing import fake |
| 21 | |
| 22 | |
| 23 | class HelpersUnitTest(unittest.TestCase): |
| 24 | |
| 25 | def setUp(self): |
| 26 | self.cnxn = 'fake sql connection' |
| 27 | self.services = service_manager.Services( |
| 28 | project=fake.ProjectService(), |
| 29 | user=fake.UserService()) |
| 30 | self.services.user.TestAddUser('a@example.com', 111) |
| 31 | self.services.user.TestAddUser('b@example.com', 222) |
| 32 | self.services.user.TestAddUser('c@example.com', 333) |
| 33 | self.users_by_id = framework_views.MakeAllUserViews( |
| 34 | 'cnxn', self.services.user, [111, 222, 333]) |
| 35 | self.effective_ids_by_user = {user: set() for user in {111, 222, 333}} |
| 36 | |
| 37 | def testBuildProjectMembers(self): |
| 38 | project = project_pb2.MakeProject( |
| 39 | 'proj', owner_ids=[111], committer_ids=[222], |
| 40 | contributor_ids=[333]) |
| 41 | page_data = project_helpers.BuildProjectMembers( |
| 42 | self.cnxn, project, self.services.user) |
| 43 | self.assertEqual(111, page_data['owners'][0].user_id) |
| 44 | self.assertEqual(222, page_data['committers'][0].user_id) |
| 45 | self.assertEqual(333, page_data['contributors'][0].user_id) |
| 46 | self.assertEqual(3, len(page_data['all_members'])) |
| 47 | |
| 48 | def testParseUsernames(self): |
| 49 | # Form field was not present in post data. |
| 50 | id_set = project_helpers.ParseUsernames( |
| 51 | self.cnxn, self.services.user, None) |
| 52 | self.assertEqual(set(), id_set) |
| 53 | |
| 54 | # Form field was present, but empty. |
| 55 | id_set = project_helpers.ParseUsernames( |
| 56 | self.cnxn, self.services.user, '') |
| 57 | self.assertEqual(set(), id_set) |
| 58 | |
| 59 | # Parsing valid user names. |
| 60 | id_set = project_helpers.ParseUsernames( |
| 61 | self.cnxn, self.services.user, 'a@example.com, c@example.com') |
| 62 | self.assertEqual({111, 333}, id_set) |
| 63 | |
| 64 | def testParseProjectAccess_NotOffered(self): |
| 65 | project = project_pb2.MakeProject('proj') |
| 66 | access = project_helpers.ParseProjectAccess(project, None) |
| 67 | self.assertEqual(None, access) |
| 68 | |
| 69 | def testParseProjectAccess_AllowedChoice(self): |
| 70 | project = project_pb2.MakeProject('proj') |
| 71 | access = project_helpers.ParseProjectAccess(project, '1') |
| 72 | self.assertEqual(project_pb2.ProjectAccess.ANYONE, access) |
| 73 | |
| 74 | access = project_helpers.ParseProjectAccess(project, '3') |
| 75 | self.assertEqual(project_pb2.ProjectAccess.MEMBERS_ONLY, access) |
| 76 | |
| 77 | def testParseProjectAccess_BogusChoice(self): |
| 78 | project = project_pb2.MakeProject('proj') |
| 79 | access = project_helpers.ParseProjectAccess(project, '9') |
| 80 | self.assertEqual(None, access) |
| 81 | |
| 82 | def testUsersWithPermsInProject_StandardPermission(self): |
| 83 | project = project_pb2.MakeProject('proj', committer_ids=[111]) |
| 84 | perms_needed = {permissions.VIEW, permissions.EDIT_ISSUE} |
| 85 | actual = project_helpers.UsersWithPermsInProject( |
| 86 | project, perms_needed, self.users_by_id, self.effective_ids_by_user) |
| 87 | self.assertEqual( |
| 88 | {permissions.VIEW: {111, 222, 333}, |
| 89 | permissions.EDIT_ISSUE: {111}}, |
| 90 | actual) |
| 91 | |
| 92 | def testUsersWithPermsInProject_IndirectPermission(self): |
| 93 | perms_needed = {permissions.EDIT_ISSUE} |
| 94 | # User 111 has the EDIT_ISSUE permission. |
| 95 | project = project_pb2.MakeProject('proj', committer_ids=[111]) |
| 96 | # User 222 has the EDIT_ISSUE permission, because 111 is included in its |
| 97 | # effective IDs. |
| 98 | self.effective_ids_by_user[222] = {111} |
| 99 | # User 333 doesn't have the EDIT_ISSUE permission, since only direct |
| 100 | # effective IDs are taken into account. |
| 101 | self.effective_ids_by_user[333] = {222} |
| 102 | actual = project_helpers.UsersWithPermsInProject( |
| 103 | project, perms_needed, self.users_by_id, self.effective_ids_by_user) |
| 104 | self.assertEqual( |
| 105 | {permissions.EDIT_ISSUE: {111, 222}}, |
| 106 | actual) |
| 107 | |
| 108 | def testUsersWithPermsInProject_CustomPermission(self): |
| 109 | project = project_pb2.MakeProject('proj') |
| 110 | project.extra_perms = [ |
| 111 | project_pb2.Project.ExtraPerms( |
| 112 | member_id=111, |
| 113 | perms=['FooPerm', 'BarPerm']), |
| 114 | project_pb2.Project.ExtraPerms( |
| 115 | member_id=222, |
| 116 | perms=['BarPerm'])] |
| 117 | perms_needed = {'FooPerm', 'BarPerm'} |
| 118 | actual = project_helpers.UsersWithPermsInProject( |
| 119 | project, perms_needed, self.users_by_id, self.effective_ids_by_user) |
| 120 | self.assertEqual( |
| 121 | {'FooPerm': {111}, |
| 122 | 'BarPerm': {111, 222}}, |
| 123 | actual) |
| 124 | |
| 125 | @patch('google.appengine.api.app_identity.get_default_gcs_bucket_name') |
| 126 | @patch('framework.gcs_helpers.SignUrl') |
| 127 | def testGetThumbnailUrl(self, mock_SignUrl, mock_get_default_gcs_bucket_name): |
| 128 | bucket_name = 'testbucket' |
| 129 | expected_url = 'signed/url' |
| 130 | |
| 131 | mock_get_default_gcs_bucket_name.return_value = bucket_name |
| 132 | mock_SignUrl.return_value = expected_url |
| 133 | |
| 134 | self.assertEqual(expected_url, project_helpers.GetThumbnailUrl('xyz')) |
| 135 | mock_get_default_gcs_bucket_name.assert_called_once() |
| 136 | mock_SignUrl.assert_called_once_with(bucket_name, 'xyz' + '-thumbnail') |
| 137 | |
| 138 | def testIsValidProjectName_BadChars(self): |
| 139 | self.assertFalse(project_helpers.IsValidProjectName('spa ce')) |
| 140 | self.assertFalse(project_helpers.IsValidProjectName('under_score')) |
| 141 | self.assertFalse(project_helpers.IsValidProjectName('name.dot')) |
| 142 | self.assertFalse(project_helpers.IsValidProjectName('pie#sign$')) |
| 143 | self.assertFalse(project_helpers.IsValidProjectName('(who?)')) |
| 144 | |
| 145 | def testIsValidProjectName_BadHyphen(self): |
| 146 | self.assertFalse(project_helpers.IsValidProjectName('name-')) |
| 147 | self.assertFalse(project_helpers.IsValidProjectName('-name')) |
| 148 | self.assertTrue(project_helpers.IsValidProjectName('project-name')) |
| 149 | |
| 150 | def testIsValidProjectName_MinimumLength(self): |
| 151 | self.assertFalse(project_helpers.IsValidProjectName('x')) |
| 152 | self.assertTrue(project_helpers.IsValidProjectName('xy')) |
| 153 | |
| 154 | def testIsValidProjectName_MaximumLength(self): |
| 155 | self.assertFalse( |
| 156 | project_helpers.IsValidProjectName( |
| 157 | 'x' * (project_constants.MAX_PROJECT_NAME_LENGTH + 1))) |
| 158 | self.assertTrue( |
| 159 | project_helpers.IsValidProjectName( |
| 160 | 'x' * (project_constants.MAX_PROJECT_NAME_LENGTH))) |
| 161 | |
| 162 | def testIsValidProjectName_InvalidName(self): |
| 163 | self.assertFalse(project_helpers.IsValidProjectName('')) |
| 164 | self.assertFalse(project_helpers.IsValidProjectName('000')) |
| 165 | |
| 166 | def testIsValidProjectName_ValidName(self): |
| 167 | self.assertTrue(project_helpers.IsValidProjectName('098asd')) |
| 168 | self.assertTrue(project_helpers.IsValidProjectName('one-two-three')) |
| 169 | |
| 170 | def testAllProjectMembers(self): |
| 171 | p = project_pb2.Project() |
| 172 | self.assertEqual(project_helpers.AllProjectMembers(p), []) |
| 173 | |
| 174 | p.owner_ids.extend([1, 2, 3]) |
| 175 | p.committer_ids.extend([4, 5, 6]) |
| 176 | p.contributor_ids.extend([7, 8, 9]) |
| 177 | self.assertEqual( |
| 178 | project_helpers.AllProjectMembers(p), [1, 2, 3, 4, 5, 6, 7, 8, 9]) |