blob: d3b83273f1dd644660fbb5deb2fff9f61ceee2df [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"""Unittests for monorail.tracker.issuedetailezt."""
7from __future__ import print_function
8from __future__ import division
9from __future__ import absolute_import
10
11import logging
12import mock
13import mox
14import time
15import unittest
16
17import settings
18from businesslogic import work_env
19from proto import features_pb2
20from features import hotlist_views
21from features import send_notifications
22from framework import authdata
23from framework import exceptions
24from framework import framework_views
25from framework import framework_helpers
26from framework import urls
27from framework import permissions
28from framework import profiler
29from framework import sorting
30from framework import template_helpers
31from proto import project_pb2
32from proto import tracker_pb2
33from proto import user_pb2
34from services import service_manager
35from services import issue_svc
36from services import tracker_fulltext
37from testing import fake
38from testing import testing_helpers
39from tracker import issuedetailezt
40from tracker import tracker_constants
41from tracker import tracker_helpers
42
43
44class GetAdjacentIssueTest(unittest.TestCase):
45
46 def setUp(self):
47 self.cnxn = 'fake cnxn'
48 self.services = service_manager.Services(
49 config=fake.ConfigService(),
50 issue=fake.IssueService(),
51 user=fake.UserService(),
52 project=fake.ProjectService(),
53 issue_star=fake.IssueStarService(),
54 spam=fake.SpamService())
55 self.services.project.TestAddProject('proj', project_id=789)
56 self.mr = testing_helpers.MakeMonorailRequest()
57 self.mr.auth.user_id = 111
58 self.mr.auth.effective_ids = {111}
59 self.mr.me_user_id = 111
60 self.work_env = work_env.WorkEnv(
61 self.mr, self.services, 'Testing phase')
62
63 def testGetAdjacentIssue_PrevIssue(self):
64 cur_issue = fake.MakeTestIssue(789, 2, 'sum', 'New', 111, issue_id=78902)
65 next_issue = fake.MakeTestIssue(789, 3, 'sum', 'New', 111, issue_id=78903)
66 prev_issue = fake.MakeTestIssue(789, 1, 'sum', 'New', 111, issue_id=78901)
67 self.services.issue.TestAddIssue(cur_issue)
68 self.services.issue.TestAddIssue(next_issue)
69 self.services.issue.TestAddIssue(prev_issue)
70
71 with self.work_env as we:
72 we.FindIssuePositionInSearch = mock.Mock(
73 return_value=[78901, 1, 78903, 3])
74
75 actual_issue = issuedetailezt.GetAdjacentIssue(
76 self.mr, we, cur_issue)
77 self.assertEqual(prev_issue, actual_issue)
78 we.FindIssuePositionInSearch.assert_called_once_with(cur_issue)
79
80 def testGetAdjacentIssue_NextIssue(self):
81 cur_issue = fake.MakeTestIssue(789, 2, 'sum', 'New', 111, issue_id=78902)
82 next_issue = fake.MakeTestIssue(789, 3, 'sum', 'New', 111, issue_id=78903)
83 prev_issue = fake.MakeTestIssue(789, 1, 'sum', 'New', 111, issue_id=78901)
84 self.services.issue.TestAddIssue(cur_issue)
85 self.services.issue.TestAddIssue(next_issue)
86 self.services.issue.TestAddIssue(prev_issue)
87
88 with self.work_env as we:
89 we.FindIssuePositionInSearch = mock.Mock(
90 return_value=[78901, 1, 78903, 3])
91
92 actual_issue = issuedetailezt.GetAdjacentIssue(
93 self.mr, we, cur_issue, next_issue=True)
94 self.assertEqual(next_issue, actual_issue)
95 we.FindIssuePositionInSearch.assert_called_once_with(cur_issue)
96
97 def testGetAdjacentIssue_NotFound(self):
98 cur_issue = fake.MakeTestIssue(789, 2, 'sum', 'New', 111, issue_id=78902)
99 prev_issue = fake.MakeTestIssue(789, 1, 'sum', 'New', 111, issue_id=78901)
100 self.services.issue.TestAddIssue(cur_issue)
101 self.services.issue.TestAddIssue(prev_issue)
102
103 with self.work_env as we:
104 we.FindIssuePositionInSearch = mock.Mock(
105 return_value=[78901, 1, 78903, 3])
106
107 with self.assertRaises(exceptions.NoSuchIssueException):
108 issuedetailezt.GetAdjacentIssue(
109 self.mr, we, cur_issue, next_issue=True)
110 we.FindIssuePositionInSearch.assert_called_once_with(cur_issue)
111
112 def testGetAdjacentIssue_Hotlist(self):
113 cur_issue = fake.MakeTestIssue(789, 2, 'sum', 'New', 111, issue_id=78902)
114 next_issue = fake.MakeTestIssue(789, 3, 'sum', 'New', 111, issue_id=78903)
115 prev_issue = fake.MakeTestIssue(789, 1, 'sum', 'New', 111, issue_id=78901)
116 self.services.issue.TestAddIssue(cur_issue)
117 self.services.issue.TestAddIssue(next_issue)
118 self.services.issue.TestAddIssue(prev_issue)
119 hotlist = fake.Hotlist('name', 678, owner_ids=[111])
120
121 with self.work_env as we:
122 we.GetIssuePositionInHotlist = mock.Mock(
123 return_value=[78901, 1, 78903, 3])
124
125 actual_issue = issuedetailezt.GetAdjacentIssue(
126 self.mr, we, cur_issue, hotlist=hotlist, next_issue=True)
127 self.assertEqual(next_issue, actual_issue)
128 we.GetIssuePositionInHotlist.assert_called_once_with(
129 cur_issue, hotlist, self.mr.can, self.mr.sort_spec,
130 self.mr.group_by_spec)
131
132
133class FlipperRedirectTest(unittest.TestCase):
134
135 def setUp(self):
136 self.services = service_manager.Services(
137 config=fake.ConfigService(),
138 features=fake.FeaturesService(),
139 issue=fake.IssueService(),
140 user=fake.UserService(),
141 project=fake.ProjectService())
142 self.project = self.services.project.TestAddProject(
143 'proj', project_id=987, committer_ids=[111])
144 self.next_servlet = issuedetailezt.FlipperNext(
145 'req', 'res', services=self.services)
146 self.prev_servlet = issuedetailezt.FlipperPrev(
147 'req', 'res', services=self.services)
148 self.list_servlet = issuedetailezt.FlipperList(
149 'req', 'res', services=self.services)
150 mr = testing_helpers.MakeMonorailRequest(project=self.project)
151 mr.local_id = 123
152 mr.me_user_id = 111
153
154 self.next_servlet.mr = mr
155 self.prev_servlet.mr = mr
156 self.list_servlet.mr = mr
157
158 self.fake_issue_1 = fake.MakeTestIssue(987, 123, 'summary', 'New', 111,
159 project_name='rutabaga')
160 self.services.issue.TestAddIssue(self.fake_issue_1)
161 self.fake_issue_2 = fake.MakeTestIssue(987, 456, 'summary', 'New', 111,
162 project_name='rutabaga')
163 self.services.issue.TestAddIssue(self.fake_issue_2)
164 self.fake_issue_3 = fake.MakeTestIssue(987, 789, 'summary', 'New', 111,
165 project_name='potato')
166 self.services.issue.TestAddIssue(self.fake_issue_3)
167
168 self.next_servlet.redirect = mock.Mock()
169 self.prev_servlet.redirect = mock.Mock()
170 self.list_servlet.redirect = mock.Mock()
171
172 @mock.patch('tracker.issuedetailezt.GetAdjacentIssue')
173 def testFlipperNext(self, patchGetAdjacentIssue):
174 patchGetAdjacentIssue.return_value = self.fake_issue_2
175 self.next_servlet.mr.GetIntParam = mock.Mock(return_value=None)
176
177 self.next_servlet.get(project_name='proj', viewed_username=None)
178 self.next_servlet.mr.GetIntParam.assert_called_once_with('hotlist_id')
179 patchGetAdjacentIssue.assert_called_once()
180 self.next_servlet.redirect.assert_called_once_with(
181 '/p/rutabaga/issues/detail?id=456')
182
183 @mock.patch('tracker.issuedetailezt.GetAdjacentIssue')
184 def testFlipperNext_Hotlist(self, patchGetAdjacentIssue):
185 patchGetAdjacentIssue.return_value = self.fake_issue_3
186 self.next_servlet.mr.GetIntParam = mock.Mock(return_value=123)
187 # TODO(jeffcarp): Mock hotlist_id param on path here.
188
189 self.next_servlet.get(project_name='proj', viewed_username=None)
190 self.next_servlet.mr.GetIntParam.assert_called_with('hotlist_id')
191 self.next_servlet.redirect.assert_called_once_with(
192 '/p/potato/issues/detail?id=789')
193
194 @mock.patch('tracker.issuedetailezt.GetAdjacentIssue')
195 def testFlipperPrev(self, patchGetAdjacentIssue):
196 patchGetAdjacentIssue.return_value = self.fake_issue_2
197 self.next_servlet.mr.GetIntParam = mock.Mock(return_value=None)
198
199 self.prev_servlet.get(project_name='proj', viewed_username=None)
200 self.prev_servlet.mr.GetIntParam.assert_called_with('hotlist_id')
201 patchGetAdjacentIssue.assert_called_once()
202 self.prev_servlet.redirect.assert_called_once_with(
203 '/p/rutabaga/issues/detail?id=456')
204
205 @mock.patch('tracker.issuedetailezt.GetAdjacentIssue')
206 def testFlipperPrev_Hotlist(self, patchGetAdjacentIssue):
207 patchGetAdjacentIssue.return_value = self.fake_issue_3
208 self.prev_servlet.mr.GetIntParam = mock.Mock(return_value=123)
209 # TODO(jeffcarp): Mock hotlist_id param on path here.
210
211 self.prev_servlet.get(project_name='proj', viewed_username=None)
212 self.prev_servlet.mr.GetIntParam.assert_called_with('hotlist_id')
213 self.prev_servlet.redirect.assert_called_once_with(
214 '/p/potato/issues/detail?id=789')
215
216 @mock.patch('tracker.issuedetailezt._ComputeBackToListURL')
217 def testFlipperList(self, patch_ComputeBackToListURL):
218 patch_ComputeBackToListURL.return_value = '/p/test/issues/list'
219 self.list_servlet.mr.GetIntParam = mock.Mock(return_value=None)
220
221 self.list_servlet.get()
222
223 self.list_servlet.mr.GetIntParam.assert_called_with('hotlist_id')
224 patch_ComputeBackToListURL.assert_called_once()
225 self.list_servlet.redirect.assert_called_once_with(
226 '/p/test/issues/list')
227
228 @mock.patch('tracker.issuedetailezt._ComputeBackToListURL')
229 def testFlipperList_Hotlist(self, patch_ComputeBackToListURL):
230 patch_ComputeBackToListURL.return_value = '/p/test/issues/list'
231 self.list_servlet.mr.GetIntParam = mock.Mock(return_value=123)
232
233 self.list_servlet.get()
234
235 self.list_servlet.mr.GetIntParam.assert_called_with('hotlist_id')
236 self.list_servlet.redirect.assert_called_once_with(
237 '/p/test/issues/list')
238
239
240class ShouldShowFlipperTest(unittest.TestCase):
241
242 def setUp(self):
243 self.cnxn = 'fake cnxn'
244
245 def VerifyShouldShowFlipper(
246 self, expected, query, sort_spec, can, create_issues=0):
247 """Instantiate a _Flipper and check if makes a pipeline or not."""
248 services = service_manager.Services(
249 config=fake.ConfigService(),
250 issue=fake.IssueService(),
251 project=fake.ProjectService(),
252 user=fake.UserService())
253 project = services.project.TestAddProject(
254 'proj', project_id=987, committer_ids=[111])
255 mr = testing_helpers.MakeMonorailRequest(project=project)
256 mr.query = query
257 mr.sort_spec = sort_spec
258 mr.can = can
259 mr.project_name = project.project_name
260 mr.project = project
261
262 for idx in range(create_issues):
263 _created_issue = fake.MakeTestIssue(
264 project.project_id,
265 idx,
266 'summary_%d' % idx,
267 'status',
268 111,
269 reporter_id=111)
270 services.issue.TestAddIssue(_created_issue)
271
272 self.assertEqual(expected, issuedetailezt._ShouldShowFlipper(mr, services))
273
274 def testShouldShowFlipper_RegularSizedProject(self):
275 # If the user is looking for a specific issue, no flipper.
276 self.VerifyShouldShowFlipper(
277 False, '123', '', tracker_constants.OPEN_ISSUES_CAN)
278 self.VerifyShouldShowFlipper(False, '123', '', 5)
279 self.VerifyShouldShowFlipper(
280 False, '123', 'priority', tracker_constants.OPEN_ISSUES_CAN)
281
282 # If the user did a search or sort or all in a small can, show flipper.
283 self.VerifyShouldShowFlipper(
284 True, 'memory leak', '', tracker_constants.OPEN_ISSUES_CAN)
285 self.VerifyShouldShowFlipper(
286 True, 'id=1,2,3', '', tracker_constants.OPEN_ISSUES_CAN)
287 # Any can other than 1 or 2 is doing a query and so it should have a
288 # failry narrow result set size. 5 is issues starred by me.
289 self.VerifyShouldShowFlipper(True, '', '', 5)
290 self.VerifyShouldShowFlipper(
291 True, '', 'status', tracker_constants.OPEN_ISSUES_CAN)
292
293 # In a project without a huge number of issues, still show the flipper even
294 # if there was no specific query.
295 self.VerifyShouldShowFlipper(
296 True, '', '', tracker_constants.OPEN_ISSUES_CAN)
297
298 def testShouldShowFlipper_LargeSizedProject(self):
299 settings.threshold_to_suppress_prev_next = 1
300
301 # In a project that has tons of issues, save time by not showing the
302 # flipper unless there was a specific query, sort, or can.
303 self.VerifyShouldShowFlipper(
304 False, '', '', tracker_constants.ALL_ISSUES_CAN, create_issues=3)
305 self.VerifyShouldShowFlipper(
306 False, '', '', tracker_constants.OPEN_ISSUES_CAN, create_issues=3)