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