blob: fe033b84857bbcee7b8eb094c520a4dce0d62dff [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.issuereindex."""
7from __future__ import print_function
8from __future__ import division
9from __future__ import absolute_import
10
11import unittest
12
13import mox
14
15import settings
16from framework import permissions
17from framework import template_helpers
18from services import service_manager
19from services import tracker_fulltext
20from testing import fake
21from testing import testing_helpers
22from tracker import issuereindex
23
24
25class IssueReindexTest(unittest.TestCase):
26
27 def setUp(self):
28 self.cnxn = 'fake cnxn'
29 self.services = service_manager.Services(
30 config=fake.ConfigService(),
31 issue=fake.IssueService(),
32 user=fake.UserService(),
33 project=fake.ProjectService())
34 self.project = self.services.project.TestAddProject('proj', project_id=987)
35 self.mox = mox.Mox()
36
37 def tearDown(self):
38 self.mox.UnsetStubs()
39 self.mox.ResetAll()
40
41 def testAssertBasePermission_NoAccess(self):
42 # Non-members and contributors do not have permission to view this page.
43 for permission in (permissions.USER_PERMISSIONSET,
44 permissions.COMMITTER_ACTIVE_PERMISSIONSET):
45 request, mr = testing_helpers.GetRequestObjects(
46 project=self.project, perms=permission)
47 servlet = issuereindex.IssueReindex(
48 request, 'res', services=self.services)
49 with self.assertRaises(permissions.PermissionException) as cm:
50 servlet.AssertBasePermission(mr)
51 self.assertEqual('You are not allowed to administer this project',
52 cm.exception.message)
53
54 def testAssertBasePermission_WithAccess(self):
55 # Owners and admins have permission to view this page.
56 for permission in (permissions.OWNER_ACTIVE_PERMISSIONSET,
57 permissions.ADMIN_PERMISSIONSET):
58 request, mr = testing_helpers.GetRequestObjects(
59 project=self.project, perms=permission)
60 servlet = issuereindex.IssueReindex(
61 request, 'res', services=self.services)
62 servlet.AssertBasePermission(mr)
63
64 def testGatherPageData(self):
65 servlet = issuereindex.IssueReindex('req', 'res', services=self.services)
66
67 mr = testing_helpers.MakeMonorailRequest()
68 mr.auto_submit = True
69 ret = servlet.GatherPageData(mr)
70
71 self.assertTrue(ret['auto_submit'])
72 self.assertIsNone(ret['issue_tab_mode'])
73 self.assertTrue(ret['page_perms'].CreateIssue)
74
75 def _callProcessFormData(self, post_data, index_issue_1=True):
76 servlet = issuereindex.IssueReindex('req', 'res', services=self.services)
77
78 mr = testing_helpers.MakeMonorailRequest(project=self.project)
79 mr.cnxn = self.cnxn
80
81 issue1 = fake.MakeTestIssue(
82 project_id=self.project.project_id, local_id=1, summary='sum',
83 status='New', owner_id=111)
84 issue1.project_name = self.project.project_name
85 self.services.issue.TestAddIssue(issue1)
86
87 self.mox.StubOutWithMock(tracker_fulltext, 'IndexIssues')
88 if index_issue_1:
89 tracker_fulltext.IndexIssues(
90 self.cnxn, [issue1], self.services.user, self.services.issue,
91 self.services.config)
92
93 self.mox.ReplayAll()
94
95 ret = servlet.ProcessFormData(mr, post_data)
96 self.mox.VerifyAll()
97 return ret
98
99 def testProcessFormData_NormalInputs(self):
100 post_data = {'start': 1, 'num': 5}
101 ret = self._callProcessFormData(post_data)
102 self.assertEqual(
103 '/p/None/issues/reindex?start=6&auto_submit=False&num=5', ret)
104
105 def testProcessFormData_LargeInputs(self):
106 post_data = {'start': 0, 'num': 10000000}
107 ret = self._callProcessFormData(post_data)
108 self.assertEqual(
109 '/p/None/issues/reindex?start=%s&auto_submit=False&num=%s' % (
110 settings.max_artifact_search_results_per_page,
111 settings.max_artifact_search_results_per_page), ret)
112
113 def testProcessFormData_WithAutoSubmit(self):
114 post_data = {'start': 1, 'num': 5, 'auto_submit': 1}
115 ret = self._callProcessFormData(post_data)
116 self.assertEqual(
117 '/p/None/issues/reindex?start=6&auto_submit=True&num=5', ret)
118
119 def testProcessFormData_WithAutoSubmitButNoMoreIssues(self):
120 """This project has no issues 6-10, so stop autosubmitting."""
121 post_data = {'start': 6, 'num': 5, 'auto_submit': 1}
122 ret = self._callProcessFormData(post_data, index_issue_1=False)
123 self.assertEqual(
124 '/p/None/issues/reindex?start=11&auto_submit=False&num=5', ret)