blob: 870de40c30952b999e0657dc922860a264361cfd [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 servlet base class helper functions."""
7from __future__ import print_function
8from __future__ import division
9from __future__ import absolute_import
10
11import unittest
Adrià Vilanova Martínezac4a6442022-05-15 19:05:13 +020012import settings
Copybara854996b2021-09-07 19:36:02 +000013
14from google.appengine.ext import testbed
15
16
17from framework import permissions
18from framework import servlet_helpers
19from proto import project_pb2
20from proto import tracker_pb2
21from testing import testing_helpers
22
23
24class 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
34class 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ínezac4a6442022-05-15 19:05:13 +020079 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
Copybara854996b2021-09-07 19:36:02 +000089
90FORM_URL = 'http://example.com/issues/form.php'
91
92
93class 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
Adrià Vilanova Martínezde942802022-07-15 14:06:55 +0200113 url = servlet_helpers.ComputeIssueEntryURL(mr)
Copybara854996b2021-09-07 19:36:02 +0000114 self.assertEqual('/p/proj/issues/entry', url)
115
Adrià Vilanova Martínezde942802022-07-15 14:06:55 +0200116 def testComputeIssueEntryURL_Chromium(self):
Copybara854996b2021-09-07 19:36:02 +0000117 _request, mr = testing_helpers.GetRequestObjects(
Adrià Vilanova Martínezde942802022-07-15 14:06:55 +0200118 path='/p/chromium/issues/detail?id=123&q=term', project=self.project)
Copybara854996b2021-09-07 19:36:02 +0000119
Adrià Vilanova Martínezde942802022-07-15 14:06:55 +0200120 url = servlet_helpers.ComputeIssueEntryURL(mr)
Adrià Vilanova Martínezac4a6442022-05-15 19:05:13 +0200121 self.assertIn('/issues/wizard', url)
Copybara854996b2021-09-07 19:36:02 +0000122
123class IssueListURLTest(unittest.TestCase):
124
125 def setUp(self):
126 self.project = project_pb2.Project()
127 self.project.project_name = 'proj'
128 self.project.owner_ids = [111]
129 self.config = tracker_pb2.ProjectIssueConfig()
130 self.testbed = testbed.Testbed()
131 self.testbed.activate()
132 self.testbed.init_user_stub()
133 self.testbed.init_memcache_stub()
134 self.testbed.init_datastore_v3_stub()
135
136 def tearDown(self):
137 self.testbed.deactivate()
138
139 def testIssueListURL_NotCustomized(self):
140 _request, mr = testing_helpers.GetRequestObjects(
141 path='/p/proj/issues', project=self.project)
142
143 url = servlet_helpers.IssueListURL(mr, self.config)
144 self.assertEqual('/p/proj/issues/list', url)
145
146 def testIssueListURL_Customized_Nonmember(self):
147 _request, mr = testing_helpers.GetRequestObjects(
148 path='/p/proj/issues', project=self.project)
149 self.config.member_default_query = 'owner:me'
150
151 url = servlet_helpers.IssueListURL(mr, self.config)
152 self.assertEqual('/p/proj/issues/list', url)
153
154 def testIssueListURL_Customized_Member(self):
155 _request, mr = testing_helpers.GetRequestObjects(
156 path='/p/proj/issues', project=self.project,
157 user_info={'effective_ids': {111}})
158 self.config.member_default_query = 'owner:me'
159
160 url = servlet_helpers.IssueListURL(mr, self.config)
161 self.assertEqual('/p/proj/issues/list?q=owner%3Ame', url)
162
163 def testIssueListURL_Customized_RetainQS(self):
164 _request, mr = testing_helpers.GetRequestObjects(
165 path='/p/proj/issues', project=self.project,
166 user_info={'effective_ids': {111}})
167 self.config.member_default_query = 'owner:me'
168
169 url = servlet_helpers.IssueListURL(mr, self.config, query_string='')
170 self.assertEqual('/p/proj/issues/list?q=owner%3Ame', url)
171
172 url = servlet_helpers.IssueListURL(mr, self.config, query_string='q=Pri=1')
173 self.assertEqual('/p/proj/issues/list?q=Pri=1', url)
Adrià Vilanova Martínezac4a6442022-05-15 19:05:13 +0200174
175class ProjectIsRestrictedTest(unittest.TestCase):
176
177 def setUp(self):
178 self.project = project_pb2.Project()
179 self.project.project_name = 'proj'
180 self.config = tracker_pb2.ProjectIssueConfig()
181 self.testbed = testbed.Testbed()
182 self.testbed.activate()
183 self.testbed.init_user_stub()
184 self.testbed.init_memcache_stub()
185 self.testbed.init_datastore_v3_stub()
186
187 def tearDown(self):
188 self.testbed.deactivate()
189
190 def testProjectIsRestricted(self):
191 self.project.access = project_pb2.ProjectAccess.MEMBERS_ONLY
192 _request, mr = testing_helpers.GetRequestObjects(
193 path='/p/proj/issues/detail?id=123&q=term', project=self.project)
194 isRestrict = servlet_helpers.ProjectIsRestricted(mr)
195 self.assertTrue(isRestrict)
196
197 def testProjectIsNotRestricted(self):
198 self.project.access = project_pb2.ProjectAccess.ANYONE
199 _request, mr = testing_helpers.GetRequestObjects(
200 path='/p/proj/issues/detail?id=123&q=term', project=self.project)
201 isRestrict = servlet_helpers.ProjectIsRestricted(mr)
202 self.assertFalse(isRestrict)
203
204
205class ComputerCreateUrl(unittest.TestCase):
206
207 def setUp(self):
208 self.project = project_pb2.Project()
209 self.project.project_name = 'proj'
210 self.config = tracker_pb2.ProjectIssueConfig()
211 self.testbed = testbed.Testbed()
212 self.testbed.activate()
213 self.testbed.init_user_stub()
214 self.testbed.init_memcache_stub()
215 self.testbed.init_datastore_v3_stub()
216
217 def tearDown(self):
218 self.testbed.deactivate()
219
220 def testCreateLoginUrl(self):
221 _, mr = testing_helpers.GetRequestObjects(
222 path='/p/proj/issues/detail?id=123&q=term', project=self.project)
Adrià Vilanova Martínezde942802022-07-15 14:06:55 +0200223 url = servlet_helpers.SafeCreateLoginURL(mr, 'current.url.to.return.to')
224 # Ensure that users can pick their account to use with Monorail.
225 self.assertIn('/AccountChooser', url)
226 self.assertIn('current.url.to.return.to', url)
227
228 def testCreateLoginUrl(self):
229 _, mr = testing_helpers.GetRequestObjects(
230 path='/p/proj/issues/detail?id=123&q=term', project=self.project)
231 url = servlet_helpers.SafeCreateLoginURL(mr, 'current.url.to.return.to')
232 # Ensure that users can pick their account to use with Monorail.
233 self.assertIn('/AccountChooser', url)
234 self.assertIn('current.url.to.return.to', url)
235
236 def testCreateEscapedLoginUrlFromMR(self):
237 _, mr = testing_helpers.GetRequestObjects(
238 path='/p/proj/issues/detail?id=123&q=term', project=self.project)
239 mr.current_page_url_encoded = (
240 'https%3A%2F%2Fbugs.chromium.org'
241 '%2Fp%2Fchromium%2Fissues%2Fentry')
242 url = servlet_helpers.SafeCreateLoginURL(mr)
243 self.assertIn('https%3A%2F%2Fbugs.chromium.org%2Fp', url)
Adrià Vilanova Martínezac4a6442022-05-15 19:05:13 +0200244
245 def testCreateLogoutUrl(self):
246 _, mr = testing_helpers.GetRequestObjects(
247 path='/p/proj/issues/detail?id=123&q=term', project=self.project)
248 url = servlet_helpers.SafeCreateLogoutURL(mr)
249 self.assertIn('/Logout', url)