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 |
| 12 | |
| 13 | from google.appengine.ext import testbed |
| 14 | |
| 15 | |
| 16 | from framework import permissions |
| 17 | from framework import servlet_helpers |
| 18 | from proto import project_pb2 |
| 19 | from proto import tracker_pb2 |
| 20 | from testing import testing_helpers |
| 21 | |
| 22 | |
| 23 | class EztDataTest(unittest.TestCase): |
| 24 | |
| 25 | def testGetBannerTime(self): |
| 26 | """Tests GetBannerTime method.""" |
| 27 | timestamp = [2019, 6, 13, 18, 30] |
| 28 | |
| 29 | banner_time = servlet_helpers.GetBannerTime(timestamp) |
| 30 | self.assertEqual(1560450600, banner_time) |
| 31 | |
| 32 | |
| 33 | class AssertBasePermissionTest(unittest.TestCase): |
| 34 | |
| 35 | def testAccessGranted(self): |
| 36 | _, mr = testing_helpers.GetRequestObjects(path='/hosting') |
| 37 | # No exceptions should be raised. |
| 38 | servlet_helpers.AssertBasePermission(mr) |
| 39 | |
| 40 | mr.auth.user_id = 123 |
| 41 | # No exceptions should be raised. |
| 42 | servlet_helpers.AssertBasePermission(mr) |
| 43 | servlet_helpers.AssertBasePermissionForUser( |
| 44 | mr.auth.user_pb, mr.auth.user_view) |
| 45 | |
| 46 | def testBanned(self): |
| 47 | _, mr = testing_helpers.GetRequestObjects(path='/hosting') |
| 48 | mr.auth.user_pb.banned = 'spammer' |
| 49 | self.assertRaises( |
| 50 | permissions.BannedUserException, |
| 51 | servlet_helpers.AssertBasePermissionForUser, |
| 52 | mr.auth.user_pb, mr.auth.user_view) |
| 53 | self.assertRaises( |
| 54 | permissions.BannedUserException, |
| 55 | servlet_helpers.AssertBasePermission, mr) |
| 56 | |
| 57 | def testPlusAddressAccount(self): |
| 58 | _, mr = testing_helpers.GetRequestObjects(path='/hosting') |
| 59 | mr.auth.user_pb.email = 'mailinglist+spammer@chromium.org' |
| 60 | self.assertRaises( |
| 61 | permissions.BannedUserException, |
| 62 | servlet_helpers.AssertBasePermissionForUser, |
| 63 | mr.auth.user_pb, mr.auth.user_view) |
| 64 | self.assertRaises( |
| 65 | permissions.BannedUserException, |
| 66 | servlet_helpers.AssertBasePermission, mr) |
| 67 | |
| 68 | def testNoAccessToProject(self): |
| 69 | project = project_pb2.Project() |
| 70 | project.project_name = 'proj' |
| 71 | project.access = project_pb2.ProjectAccess.MEMBERS_ONLY |
| 72 | _, mr = testing_helpers.GetRequestObjects(path='/p/proj/', project=project) |
| 73 | mr.perms = permissions.EMPTY_PERMISSIONSET |
| 74 | self.assertRaises( |
| 75 | permissions.PermissionException, |
| 76 | servlet_helpers.AssertBasePermission, mr) |
| 77 | |
| 78 | |
| 79 | FORM_URL = 'http://example.com/issues/form.php' |
| 80 | |
| 81 | |
| 82 | class ComputeIssueEntryURLTest(unittest.TestCase): |
| 83 | |
| 84 | def setUp(self): |
| 85 | self.project = project_pb2.Project() |
| 86 | self.project.project_name = 'proj' |
| 87 | self.config = tracker_pb2.ProjectIssueConfig() |
| 88 | self.testbed = testbed.Testbed() |
| 89 | self.testbed.activate() |
| 90 | self.testbed.init_user_stub() |
| 91 | self.testbed.init_memcache_stub() |
| 92 | self.testbed.init_datastore_v3_stub() |
| 93 | |
| 94 | def tearDown(self): |
| 95 | self.testbed.deactivate() |
| 96 | |
| 97 | def testComputeIssueEntryURL_Normal(self): |
| 98 | _request, mr = testing_helpers.GetRequestObjects( |
| 99 | path='/p/proj/issues/detail?id=123&q=term', |
| 100 | project=self.project) |
| 101 | |
| 102 | url = servlet_helpers.ComputeIssueEntryURL(mr, self.config) |
| 103 | self.assertEqual('/p/proj/issues/entry', url) |
| 104 | |
| 105 | def testComputeIssueEntryURL_Customized(self): |
| 106 | _request, mr = testing_helpers.GetRequestObjects( |
| 107 | path='/p/proj/issues/detail?id=123&q=term', |
| 108 | project=self.project) |
| 109 | mr.auth.user_id = 111 |
| 110 | self.config.custom_issue_entry_url = FORM_URL |
| 111 | |
| 112 | url = servlet_helpers.ComputeIssueEntryURL(mr, self.config) |
| 113 | self.assertTrue(url.startswith(FORM_URL)) |
| 114 | self.assertIn('token=', url) |
| 115 | self.assertIn('role=', url) |
| 116 | self.assertIn('continue=', url) |
| 117 | |
| 118 | class IssueListURLTest(unittest.TestCase): |
| 119 | |
| 120 | def setUp(self): |
| 121 | self.project = project_pb2.Project() |
| 122 | self.project.project_name = 'proj' |
| 123 | self.project.owner_ids = [111] |
| 124 | self.config = tracker_pb2.ProjectIssueConfig() |
| 125 | self.testbed = testbed.Testbed() |
| 126 | self.testbed.activate() |
| 127 | self.testbed.init_user_stub() |
| 128 | self.testbed.init_memcache_stub() |
| 129 | self.testbed.init_datastore_v3_stub() |
| 130 | |
| 131 | def tearDown(self): |
| 132 | self.testbed.deactivate() |
| 133 | |
| 134 | def testIssueListURL_NotCustomized(self): |
| 135 | _request, mr = testing_helpers.GetRequestObjects( |
| 136 | path='/p/proj/issues', project=self.project) |
| 137 | |
| 138 | url = servlet_helpers.IssueListURL(mr, self.config) |
| 139 | self.assertEqual('/p/proj/issues/list', url) |
| 140 | |
| 141 | def testIssueListURL_Customized_Nonmember(self): |
| 142 | _request, mr = testing_helpers.GetRequestObjects( |
| 143 | path='/p/proj/issues', project=self.project) |
| 144 | self.config.member_default_query = 'owner:me' |
| 145 | |
| 146 | url = servlet_helpers.IssueListURL(mr, self.config) |
| 147 | self.assertEqual('/p/proj/issues/list', url) |
| 148 | |
| 149 | def testIssueListURL_Customized_Member(self): |
| 150 | _request, mr = testing_helpers.GetRequestObjects( |
| 151 | path='/p/proj/issues', project=self.project, |
| 152 | user_info={'effective_ids': {111}}) |
| 153 | self.config.member_default_query = 'owner:me' |
| 154 | |
| 155 | url = servlet_helpers.IssueListURL(mr, self.config) |
| 156 | self.assertEqual('/p/proj/issues/list?q=owner%3Ame', url) |
| 157 | |
| 158 | def testIssueListURL_Customized_RetainQS(self): |
| 159 | _request, mr = testing_helpers.GetRequestObjects( |
| 160 | path='/p/proj/issues', project=self.project, |
| 161 | user_info={'effective_ids': {111}}) |
| 162 | self.config.member_default_query = 'owner:me' |
| 163 | |
| 164 | url = servlet_helpers.IssueListURL(mr, self.config, query_string='') |
| 165 | self.assertEqual('/p/proj/issues/list?q=owner%3Ame', url) |
| 166 | |
| 167 | url = servlet_helpers.IssueListURL(mr, self.config, query_string='q=Pri=1') |
| 168 | self.assertEqual('/p/proj/issues/list?q=Pri=1', url) |