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 servlet base class helper functions.""" |
| 7 | from __future__ import print_function |
| 8 | from __future__ import division |
| 9 | from __future__ import absolute_import |
| 10 | |
| 11 | import unittest |
Adrià Vilanova Martínez | ac4a644 | 2022-05-15 19:05:13 +0200 | [diff] [blame^] | 12 | import settings |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 13 | |
| 14 | from google.appengine.ext import testbed |
| 15 | |
| 16 | |
| 17 | from framework import permissions |
| 18 | from framework import servlet_helpers |
| 19 | from proto import project_pb2 |
| 20 | from proto import tracker_pb2 |
| 21 | from testing import testing_helpers |
| 22 | |
| 23 | |
| 24 | class EztDataTest(unittest.TestCase): |
| 25 | |
| 26 | def testGetBannerTime(self): |
| 27 | """Tests GetBannerTime method.""" |
| 28 | timestamp = [2019, 6, 13, 18, 30] |
| 29 | |
| 30 | banner_time = servlet_helpers.GetBannerTime(timestamp) |
| 31 | self.assertEqual(1560450600, banner_time) |
| 32 | |
| 33 | |
| 34 | class AssertBasePermissionTest(unittest.TestCase): |
| 35 | |
| 36 | def testAccessGranted(self): |
| 37 | _, mr = testing_helpers.GetRequestObjects(path='/hosting') |
| 38 | # No exceptions should be raised. |
| 39 | servlet_helpers.AssertBasePermission(mr) |
| 40 | |
| 41 | mr.auth.user_id = 123 |
| 42 | # No exceptions should be raised. |
| 43 | servlet_helpers.AssertBasePermission(mr) |
| 44 | servlet_helpers.AssertBasePermissionForUser( |
| 45 | mr.auth.user_pb, mr.auth.user_view) |
| 46 | |
| 47 | def testBanned(self): |
| 48 | _, mr = testing_helpers.GetRequestObjects(path='/hosting') |
| 49 | mr.auth.user_pb.banned = 'spammer' |
| 50 | self.assertRaises( |
| 51 | permissions.BannedUserException, |
| 52 | servlet_helpers.AssertBasePermissionForUser, |
| 53 | mr.auth.user_pb, mr.auth.user_view) |
| 54 | self.assertRaises( |
| 55 | permissions.BannedUserException, |
| 56 | servlet_helpers.AssertBasePermission, mr) |
| 57 | |
| 58 | def testPlusAddressAccount(self): |
| 59 | _, mr = testing_helpers.GetRequestObjects(path='/hosting') |
| 60 | mr.auth.user_pb.email = 'mailinglist+spammer@chromium.org' |
| 61 | self.assertRaises( |
| 62 | permissions.BannedUserException, |
| 63 | servlet_helpers.AssertBasePermissionForUser, |
| 64 | mr.auth.user_pb, mr.auth.user_view) |
| 65 | self.assertRaises( |
| 66 | permissions.BannedUserException, |
| 67 | servlet_helpers.AssertBasePermission, mr) |
| 68 | |
| 69 | def testNoAccessToProject(self): |
| 70 | project = project_pb2.Project() |
| 71 | project.project_name = 'proj' |
| 72 | project.access = project_pb2.ProjectAccess.MEMBERS_ONLY |
| 73 | _, mr = testing_helpers.GetRequestObjects(path='/p/proj/', project=project) |
| 74 | mr.perms = permissions.EMPTY_PERMISSIONSET |
| 75 | self.assertRaises( |
| 76 | permissions.PermissionException, |
| 77 | servlet_helpers.AssertBasePermission, mr) |
| 78 | |
Adrià Vilanova Martínez | ac4a644 | 2022-05-15 19:05:13 +0200 | [diff] [blame^] | 79 | def testPermForProject(self): |
| 80 | project = project_pb2.Project() |
| 81 | project.project_name = 'proj' |
| 82 | project.access = project_pb2.ProjectAccess.MEMBERS_ONLY |
| 83 | _, mr = testing_helpers.GetRequestObjects(path='/p/proj/', project=project) |
| 84 | mr.auth.user_pb.email = settings.borg_service_account |
| 85 | project_perm = servlet_helpers.CheckPermForProject( |
| 86 | mr, permissions.CREATE_GROUP, project) |
| 87 | self.assertTrue(project_perm) |
| 88 | |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 89 | |
| 90 | FORM_URL = 'http://example.com/issues/form.php' |
| 91 | |
| 92 | |
| 93 | class ComputeIssueEntryURLTest(unittest.TestCase): |
| 94 | |
| 95 | def setUp(self): |
| 96 | self.project = project_pb2.Project() |
| 97 | self.project.project_name = 'proj' |
| 98 | self.config = tracker_pb2.ProjectIssueConfig() |
| 99 | self.testbed = testbed.Testbed() |
| 100 | self.testbed.activate() |
| 101 | self.testbed.init_user_stub() |
| 102 | self.testbed.init_memcache_stub() |
| 103 | self.testbed.init_datastore_v3_stub() |
| 104 | |
| 105 | def tearDown(self): |
| 106 | self.testbed.deactivate() |
| 107 | |
| 108 | def testComputeIssueEntryURL_Normal(self): |
| 109 | _request, mr = testing_helpers.GetRequestObjects( |
| 110 | path='/p/proj/issues/detail?id=123&q=term', |
| 111 | project=self.project) |
| 112 | |
| 113 | url = servlet_helpers.ComputeIssueEntryURL(mr, self.config) |
| 114 | self.assertEqual('/p/proj/issues/entry', url) |
| 115 | |
| 116 | def testComputeIssueEntryURL_Customized(self): |
| 117 | _request, mr = testing_helpers.GetRequestObjects( |
| 118 | path='/p/proj/issues/detail?id=123&q=term', |
| 119 | project=self.project) |
| 120 | mr.auth.user_id = 111 |
| 121 | self.config.custom_issue_entry_url = FORM_URL |
| 122 | |
| 123 | url = servlet_helpers.ComputeIssueEntryURL(mr, self.config) |
Adrià Vilanova Martínez | ac4a644 | 2022-05-15 19:05:13 +0200 | [diff] [blame^] | 124 | self.assertIn('/issues/wizard', url) |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 125 | |
| 126 | class IssueListURLTest(unittest.TestCase): |
| 127 | |
| 128 | def setUp(self): |
| 129 | self.project = project_pb2.Project() |
| 130 | self.project.project_name = 'proj' |
| 131 | self.project.owner_ids = [111] |
| 132 | self.config = tracker_pb2.ProjectIssueConfig() |
| 133 | self.testbed = testbed.Testbed() |
| 134 | self.testbed.activate() |
| 135 | self.testbed.init_user_stub() |
| 136 | self.testbed.init_memcache_stub() |
| 137 | self.testbed.init_datastore_v3_stub() |
| 138 | |
| 139 | def tearDown(self): |
| 140 | self.testbed.deactivate() |
| 141 | |
| 142 | def testIssueListURL_NotCustomized(self): |
| 143 | _request, mr = testing_helpers.GetRequestObjects( |
| 144 | path='/p/proj/issues', project=self.project) |
| 145 | |
| 146 | url = servlet_helpers.IssueListURL(mr, self.config) |
| 147 | self.assertEqual('/p/proj/issues/list', url) |
| 148 | |
| 149 | def testIssueListURL_Customized_Nonmember(self): |
| 150 | _request, mr = testing_helpers.GetRequestObjects( |
| 151 | path='/p/proj/issues', project=self.project) |
| 152 | self.config.member_default_query = 'owner:me' |
| 153 | |
| 154 | url = servlet_helpers.IssueListURL(mr, self.config) |
| 155 | self.assertEqual('/p/proj/issues/list', url) |
| 156 | |
| 157 | def testIssueListURL_Customized_Member(self): |
| 158 | _request, mr = testing_helpers.GetRequestObjects( |
| 159 | path='/p/proj/issues', project=self.project, |
| 160 | user_info={'effective_ids': {111}}) |
| 161 | self.config.member_default_query = 'owner:me' |
| 162 | |
| 163 | url = servlet_helpers.IssueListURL(mr, self.config) |
| 164 | self.assertEqual('/p/proj/issues/list?q=owner%3Ame', url) |
| 165 | |
| 166 | def testIssueListURL_Customized_RetainQS(self): |
| 167 | _request, mr = testing_helpers.GetRequestObjects( |
| 168 | path='/p/proj/issues', project=self.project, |
| 169 | user_info={'effective_ids': {111}}) |
| 170 | self.config.member_default_query = 'owner:me' |
| 171 | |
| 172 | url = servlet_helpers.IssueListURL(mr, self.config, query_string='') |
| 173 | self.assertEqual('/p/proj/issues/list?q=owner%3Ame', url) |
| 174 | |
| 175 | url = servlet_helpers.IssueListURL(mr, self.config, query_string='q=Pri=1') |
| 176 | self.assertEqual('/p/proj/issues/list?q=Pri=1', url) |
Adrià Vilanova Martínez | ac4a644 | 2022-05-15 19:05:13 +0200 | [diff] [blame^] | 177 | |
| 178 | class ProjectIsRestrictedTest(unittest.TestCase): |
| 179 | |
| 180 | def setUp(self): |
| 181 | self.project = project_pb2.Project() |
| 182 | self.project.project_name = 'proj' |
| 183 | self.config = tracker_pb2.ProjectIssueConfig() |
| 184 | self.testbed = testbed.Testbed() |
| 185 | self.testbed.activate() |
| 186 | self.testbed.init_user_stub() |
| 187 | self.testbed.init_memcache_stub() |
| 188 | self.testbed.init_datastore_v3_stub() |
| 189 | |
| 190 | def tearDown(self): |
| 191 | self.testbed.deactivate() |
| 192 | |
| 193 | def testProjectIsRestricted(self): |
| 194 | self.project.access = project_pb2.ProjectAccess.MEMBERS_ONLY |
| 195 | _request, mr = testing_helpers.GetRequestObjects( |
| 196 | path='/p/proj/issues/detail?id=123&q=term', project=self.project) |
| 197 | isRestrict = servlet_helpers.ProjectIsRestricted(mr) |
| 198 | self.assertTrue(isRestrict) |
| 199 | |
| 200 | def testProjectIsNotRestricted(self): |
| 201 | self.project.access = project_pb2.ProjectAccess.ANYONE |
| 202 | _request, mr = testing_helpers.GetRequestObjects( |
| 203 | path='/p/proj/issues/detail?id=123&q=term', project=self.project) |
| 204 | isRestrict = servlet_helpers.ProjectIsRestricted(mr) |
| 205 | self.assertFalse(isRestrict) |
| 206 | |
| 207 | |
| 208 | class ComputerCreateUrl(unittest.TestCase): |
| 209 | |
| 210 | def setUp(self): |
| 211 | self.project = project_pb2.Project() |
| 212 | self.project.project_name = 'proj' |
| 213 | self.config = tracker_pb2.ProjectIssueConfig() |
| 214 | self.testbed = testbed.Testbed() |
| 215 | self.testbed.activate() |
| 216 | self.testbed.init_user_stub() |
| 217 | self.testbed.init_memcache_stub() |
| 218 | self.testbed.init_datastore_v3_stub() |
| 219 | |
| 220 | def tearDown(self): |
| 221 | self.testbed.deactivate() |
| 222 | |
| 223 | def testCreateLoginUrl(self): |
| 224 | _, mr = testing_helpers.GetRequestObjects( |
| 225 | path='/p/proj/issues/detail?id=123&q=term', project=self.project) |
| 226 | url = servlet_helpers.SafeCreateLoginURL(mr, '/continue') |
| 227 | self.assertIn('/continue', url) |
| 228 | |
| 229 | def testCreateLogoutUrl(self): |
| 230 | _, mr = testing_helpers.GetRequestObjects( |
| 231 | path='/p/proj/issues/detail?id=123&q=term', project=self.project) |
| 232 | url = servlet_helpers.SafeCreateLogoutURL(mr) |
| 233 | self.assertIn('/Logout', url) |