blob: 49c327002f5ad3a7f33c85c2e730a74693cb575d [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 issuelist module."""
7from __future__ import print_function
8from __future__ import division
9from __future__ import absolute_import
10
11import mox
12import mock
13import unittest
14import time
15
16from google.appengine.ext import testbed
17import ezt
18
19from features import hotlistissues
20from features import hotlist_helpers
21from framework import framework_views
22from framework import permissions
23from framework import sorting
24from framework import template_helpers
25from framework import xsrf
26from services import service_manager
27from testing import fake
28from testing import testing_helpers
29
30
31class HotlistIssuesUnitTest(unittest.TestCase):
32
33 def setUp(self):
34 self.testbed = testbed.Testbed()
35 self.testbed.activate()
36 self.testbed.init_memcache_stub()
37 self.testbed.init_datastore_v3_stub()
38 self.services = service_manager.Services(
39 issue_star=fake.IssueStarService(),
40 config=fake.ConfigService(),
41 user=fake.UserService(),
42 issue=fake.IssueService(),
43 project=fake.ProjectService(),
44 features=fake.FeaturesService(),
45 cache_manager=fake.CacheManager(),
46 hotlist_star=fake.HotlistStarService())
47 self.servlet = hotlistissues.HotlistIssues(
48 'req', 'res', services=self.services)
49 self.user1 = self.services.user.TestAddUser('testuser@gmail.com', 111)
50 self.user2 = self.services.user.TestAddUser('testuser2@gmail.com', 222, )
51 self.services.project.TestAddProject('project-name', project_id=1)
52 self.issue1 = fake.MakeTestIssue(
53 1, 1, 'issue_summary', 'New', 111, project_name='project-name')
54 self.services.issue.TestAddIssue(self.issue1)
55 self.issue2 = fake.MakeTestIssue(
56 1, 2, 'issue_summary2', 'New', 111, project_name='project-name')
57 self.services.issue.TestAddIssue(self.issue2)
58 self.issue3 = fake.MakeTestIssue(
59 1, 3, 'issue_summary3', 'New', 222, project_name='project-name')
60 self.services.issue.TestAddIssue(self.issue3)
61 self.issues = [self.issue1, self.issue2, self.issue3]
62 self.hotlist_item_fields = [
63 (issue.issue_id, rank, 111, 1205079300, '') for
64 rank, issue in enumerate(self.issues)]
65 self.test_hotlist = self.services.features.TestAddHotlist(
66 'hotlist', hotlist_id=123, owner_ids=[222], editor_ids=[111],
67 hotlist_item_fields=self.hotlist_item_fields)
68 self.hotlistissues = self.test_hotlist.items
69 # Unless perms is specified,
70 # MakeMonorailRequest will return an mr with admin permissions.
71 self.mr = testing_helpers.MakeMonorailRequest(
72 hotlist=self.test_hotlist, path='/u/222/hotlists/123',
73 services=self.services, perms=permissions.EMPTY_PERMISSIONSET)
74 self.mr.hotlist_id = self.test_hotlist.hotlist_id
75 self.mr.auth.user_id = 111
76 self.mr.auth.effective_ids = {111}
77 self.mr.viewed_user_auth.user_id = 111
78 sorting.InitializeArtValues(self.services)
79
80 self.mox = mox.Mox()
81
82 def tearDown(self):
83 self.mox.UnsetStubs()
84 self.testbed.deactivate()
85
86 def testAssertBasePermissions(self):
87 private_hotlist = self.services.features.TestAddHotlist(
88 'privateHotlist', hotlist_id=321, owner_ids=[222],
89 hotlist_item_fields=self.hotlist_item_fields, is_private=True)
90 # non-members cannot view private hotlists
91 mr = testing_helpers.MakeMonorailRequest(
92 hotlist=private_hotlist, perms=permissions.EMPTY_PERMISSIONSET)
93 mr.auth.effective_ids = {333}
94 mr.hotlist_id = private_hotlist.hotlist_id
95 self.assertRaises(permissions.PermissionException,
96 self.servlet.AssertBasePermission, mr)
97
98 # members can view private hotlists
99 mr = testing_helpers.MakeMonorailRequest(
100 hotlist=private_hotlist, perms=permissions.EMPTY_PERMISSIONSET)
101 mr.auth.effective_ids = {222, 444}
102 mr.hotlist_id = private_hotlist.hotlist_id
103 self.servlet.AssertBasePermission(mr)
104
105 # non-members can view public hotlists
106 mr = testing_helpers.MakeMonorailRequest(
107 hotlist=self.test_hotlist, perms=permissions.EMPTY_PERMISSIONSET)
108 mr.auth.effective_ids = {333, 444}
109 mr.hotlist_id = self.test_hotlist.hotlist_id
110 self.servlet.AssertBasePermission(mr)
111
112 # members can view public hotlists
113 mr = testing_helpers.MakeMonorailRequest(
114 hotlist=self.test_hotlist, perms=permissions.EMPTY_PERMISSIONSET)
115 mr.auth.effective_ids = {111, 333}
116 mr.hotlist_id = self.test_hotlist.hotlist_id
117 self.servlet.AssertBasePermission(mr)
118
119 def testGatherPageData(self):
120 self.mr.mode = 'list'
121 self.mr.auth.effective_ids = {111}
122 self.mr.auth.user_id = 111
123 self.mr.sort_spec = 'rank stars'
124 page_data = self.servlet.GatherPageData(self.mr)
125 self.assertEqual(ezt.boolean(False), page_data['owner_permissions'])
126 self.assertEqual(ezt.boolean(True), page_data['editor_permissions'])
127 self.assertEqual(ezt.boolean(False), page_data['grid_mode'])
128 self.assertEqual(ezt.boolean(True), page_data['allow_rerank'])
129
130 self.mr.sort_spec = 'stars ranks'
131 page_data = self.servlet.GatherPageData(self.mr)
132 self.assertEqual(ezt.boolean(False), page_data['allow_rerank'])
133
134 def testGetTableViewData(self):
135 now = time.time()
136 self.mox.StubOutWithMock(time, 'time')
137 time.time().MultipleTimes().AndReturn(now)
138 self.mox.ReplayAll()
139
140 self.mr.auth.user_id = 222
141 self.mr.col_spec = 'Stars Projects Rank'
142 table_view_data = self.servlet.GetTableViewData(self.mr)
143 self.assertEqual(table_view_data['edit_hotlist_token'], xsrf.GenerateToken(
144 self.mr.auth.user_id, '/u/222/hotlists/hotlist.do'))
145 self.assertEqual(table_view_data['add_issues_selected'], ezt.boolean(False))
146
147 self.user2.obscure_email = False
148 table_view_data = self.servlet.GetTableViewData(self.mr)
149 self.assertEqual(table_view_data['edit_hotlist_token'], xsrf.GenerateToken(
150 self.mr.auth.user_id, '/u/222/hotlists/hotlist.do'))
151 self.mox.VerifyAll()
152
153 def testGetGridViewData(self):
154 # TODO(jojwang): Write this test
155 pass
156
157 def testProcessFormData_NoNewIssues(self):
158 post_data = fake.PostData(remove=['false'], add_local_ids=[''])
159 url = self.servlet.ProcessFormData(self.mr, post_data)
160 self.assertTrue(url.endswith('u/222/hotlists/hotlist'))
161 self.assertEqual(self.test_hotlist.items, self.hotlistissues)
162
163 def testProcessFormData_AddBadIssueRef(self):
164 self.servlet.PleaseCorrect = mock.Mock()
165 post_data = fake.PostData(
166 remove=['false'], add_local_ids=['no-such-project:999'])
167 url = self.servlet.ProcessFormData(self.mr, post_data)
168 self.assertIsNone(url)
169 self.servlet.PleaseCorrect.assert_called_once()
170
171 def testProcessFormData_RemoveBadIssueRef(self):
172 post_data = fake.PostData(
173 remove=['true'], add_local_ids=['no-such-project:999'])
174 url = self.servlet.ProcessFormData(self.mr, post_data)
175 self.assertIn('u/222/hotlists/hotlist', url)
176 self.assertEqual(self.test_hotlist.items, self.hotlistissues)
177
178 def testProcessFormData_NormalEditIssues(self):
179 issue4 = fake.MakeTestIssue(
180 1, 4, 'issue_summary4', 'New', 222, project_name='project-name')
181 self.services.issue.TestAddIssue(issue4)
182 issue5 = fake.MakeTestIssue(
183 1, 5, 'issue_summary5', 'New', 222, project_name='project-name')
184 self.services.issue.TestAddIssue(issue5)
185
186 post_data = fake.PostData(remove=['false'],
187 add_local_ids=['project-name:4, project-name:5'])
188 url = self.servlet.ProcessFormData(self.mr, post_data)
189 self.assertTrue('u/222/hotlists/hotlist' in url)
190 self.assertEqual(len(self.test_hotlist.items), 5)
191 self.assertEqual(
192 self.test_hotlist.items[3].issue_id, issue4.issue_id)
193 self.assertEqual(
194 self.test_hotlist.items[4].issue_id, issue5.issue_id)
195
196 post_data = fake.PostData(remove=['true'], remove_local_ids=[
197 'project-name:4, project-name:1, project-name:2'])
198 url = self.servlet.ProcessFormData(self.mr, post_data)
199 self.assertTrue('u/222/hotlists/hotlist' in url)
200 self.assertTrue(len(self.test_hotlist.items), 2)
201 issue_ids = [issue.issue_id for issue in self.test_hotlist.items]
202 self.assertTrue(issue5.issue_id in issue_ids)
203 self.assertTrue(self.issue3.issue_id in issue_ids)
204
205 def testProcessFormData_NoPermissions(self):
206 post_data = fake.PostData(remove=['false'],
207 add_local_ids=['project-name:4, project-name:5'])
208 self.mr.auth.effective_ids = {333}
209 self.mr.auth.user_id = 333
210 with self.assertRaises(permissions.PermissionException):
211 self.servlet.ProcessFormData(self.mr, post_data)