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