blob: 99290766793558242aed05ec91d6516de99017e5 [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"""Unittests for monorail.tracker.issuereindex."""
6from __future__ import print_function
7from __future__ import division
8from __future__ import absolute_import
9
10import unittest
11
Adrià Vilanova Martínez9f9ade52022-10-10 23:20:11 +020012try:
13 from mox3 import mox
14except ImportError:
15 import mox
Copybara854996b2021-09-07 19:36:02 +000016
17import settings
18from framework import permissions
Copybara854996b2021-09-07 19:36:02 +000019from services import service_manager
20from services import tracker_fulltext
21from testing import fake
22from testing import testing_helpers
23from tracker import issuereindex
24
25
26class IssueReindexTest(unittest.TestCase):
27
28 def setUp(self):
29 self.cnxn = 'fake cnxn'
30 self.services = service_manager.Services(
31 config=fake.ConfigService(),
32 issue=fake.IssueService(),
33 user=fake.UserService(),
34 project=fake.ProjectService())
35 self.project = self.services.project.TestAddProject('proj', project_id=987)
36 self.mox = mox.Mox()
37
38 def tearDown(self):
39 self.mox.UnsetStubs()
40 self.mox.ResetAll()
41
42 def testAssertBasePermission_NoAccess(self):
43 # Non-members and contributors do not have permission to view this page.
44 for permission in (permissions.USER_PERMISSIONSET,
45 permissions.COMMITTER_ACTIVE_PERMISSIONSET):
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +010046 _, mr = testing_helpers.GetRequestObjects(
Copybara854996b2021-09-07 19:36:02 +000047 project=self.project, perms=permission)
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +010048 servlet = issuereindex.IssueReindex(services=self.services)
Copybara854996b2021-09-07 19:36:02 +000049 with self.assertRaises(permissions.PermissionException) as cm:
50 servlet.AssertBasePermission(mr)
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +010051 self.assertEqual(
52 'You are not allowed to administer this project', str(cm.exception))
Copybara854996b2021-09-07 19:36:02 +000053
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):
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +010058 _, mr = testing_helpers.GetRequestObjects(
Copybara854996b2021-09-07 19:36:02 +000059 project=self.project, perms=permission)
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +010060 servlet = issuereindex.IssueReindex(services=self.services)
Copybara854996b2021-09-07 19:36:02 +000061 servlet.AssertBasePermission(mr)
62
63 def testGatherPageData(self):
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +010064 servlet = issuereindex.IssueReindex(services=self.services)
Copybara854996b2021-09-07 19:36:02 +000065
66 mr = testing_helpers.MakeMonorailRequest()
67 mr.auto_submit = True
68 ret = servlet.GatherPageData(mr)
69
70 self.assertTrue(ret['auto_submit'])
71 self.assertIsNone(ret['issue_tab_mode'])
72 self.assertTrue(ret['page_perms'].CreateIssue)
73
74 def _callProcessFormData(self, post_data, index_issue_1=True):
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +010075 servlet = issuereindex.IssueReindex(services=self.services)
Copybara854996b2021-09-07 19:36:02 +000076
77 mr = testing_helpers.MakeMonorailRequest(project=self.project)
78 mr.cnxn = self.cnxn
79
80 issue1 = fake.MakeTestIssue(
81 project_id=self.project.project_id, local_id=1, summary='sum',
82 status='New', owner_id=111)
83 issue1.project_name = self.project.project_name
84 self.services.issue.TestAddIssue(issue1)
85
86 self.mox.StubOutWithMock(tracker_fulltext, 'IndexIssues')
87 if index_issue_1:
88 tracker_fulltext.IndexIssues(
89 self.cnxn, [issue1], self.services.user, self.services.issue,
90 self.services.config)
91
92 self.mox.ReplayAll()
93
94 ret = servlet.ProcessFormData(mr, post_data)
95 self.mox.VerifyAll()
96 return ret
97
98 def testProcessFormData_NormalInputs(self):
99 post_data = {'start': 1, 'num': 5}
100 ret = self._callProcessFormData(post_data)
101 self.assertEqual(
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +0100102 '/p/None/issues/reindex?start=6&num=5&auto_submit=False', ret)
Copybara854996b2021-09-07 19:36:02 +0000103
104 def testProcessFormData_LargeInputs(self):
105 post_data = {'start': 0, 'num': 10000000}
106 ret = self._callProcessFormData(post_data)
107 self.assertEqual(
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +0100108 '/p/None/issues/reindex?start=%s&num=%s&auto_submit=False' % (
Copybara854996b2021-09-07 19:36:02 +0000109 settings.max_artifact_search_results_per_page,
110 settings.max_artifact_search_results_per_page), ret)
111
112 def testProcessFormData_WithAutoSubmit(self):
113 post_data = {'start': 1, 'num': 5, 'auto_submit': 1}
114 ret = self._callProcessFormData(post_data)
115 self.assertEqual(
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +0100116 '/p/None/issues/reindex?start=6&num=5&auto_submit=True', ret)
Copybara854996b2021-09-07 19:36:02 +0000117
118 def testProcessFormData_WithAutoSubmitButNoMoreIssues(self):
119 """This project has no issues 6-10, so stop autosubmitting."""
120 post_data = {'start': 6, 'num': 5, 'auto_submit': 1}
121 ret = self._callProcessFormData(post_data, index_issue_1=False)
122 self.assertEqual(
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +0100123 '/p/None/issues/reindex?start=11&num=5&auto_submit=False', ret)