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