Adrià Vilanova Martínez | f19ea43 | 2024-01-23 20:20:52 +0100 | [diff] [blame] | 1 | # 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. |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 4 | |
| 5 | """Unittests for monorail.tracker.issuereindex.""" |
| 6 | from __future__ import print_function |
| 7 | from __future__ import division |
| 8 | from __future__ import absolute_import |
| 9 | |
| 10 | import unittest |
| 11 | |
Adrià Vilanova Martínez | 9f9ade5 | 2022-10-10 23:20:11 +0200 | [diff] [blame] | 12 | try: |
| 13 | from mox3 import mox |
| 14 | except ImportError: |
| 15 | import mox |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 16 | |
| 17 | import settings |
| 18 | from framework import permissions |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 19 | from services import service_manager |
| 20 | from services import tracker_fulltext |
| 21 | from testing import fake |
| 22 | from testing import testing_helpers |
| 23 | from tracker import issuereindex |
| 24 | |
| 25 | |
| 26 | class 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ínez | f19ea43 | 2024-01-23 20:20:52 +0100 | [diff] [blame] | 46 | _, mr = testing_helpers.GetRequestObjects( |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 47 | project=self.project, perms=permission) |
Adrià Vilanova Martínez | f19ea43 | 2024-01-23 20:20:52 +0100 | [diff] [blame] | 48 | servlet = issuereindex.IssueReindex(services=self.services) |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 49 | with self.assertRaises(permissions.PermissionException) as cm: |
| 50 | servlet.AssertBasePermission(mr) |
Adrià Vilanova Martínez | f19ea43 | 2024-01-23 20:20:52 +0100 | [diff] [blame] | 51 | self.assertEqual( |
| 52 | 'You are not allowed to administer this project', str(cm.exception)) |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 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): |
Adrià Vilanova Martínez | f19ea43 | 2024-01-23 20:20:52 +0100 | [diff] [blame] | 58 | _, mr = testing_helpers.GetRequestObjects( |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 59 | project=self.project, perms=permission) |
Adrià Vilanova Martínez | f19ea43 | 2024-01-23 20:20:52 +0100 | [diff] [blame] | 60 | servlet = issuereindex.IssueReindex(services=self.services) |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 61 | servlet.AssertBasePermission(mr) |
| 62 | |
| 63 | def testGatherPageData(self): |
Adrià Vilanova Martínez | f19ea43 | 2024-01-23 20:20:52 +0100 | [diff] [blame] | 64 | servlet = issuereindex.IssueReindex(services=self.services) |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 65 | |
| 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ínez | f19ea43 | 2024-01-23 20:20:52 +0100 | [diff] [blame] | 75 | servlet = issuereindex.IssueReindex(services=self.services) |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 76 | |
| 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ínez | f19ea43 | 2024-01-23 20:20:52 +0100 | [diff] [blame] | 102 | '/p/None/issues/reindex?start=6&num=5&auto_submit=False', ret) |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 103 | |
| 104 | def testProcessFormData_LargeInputs(self): |
| 105 | post_data = {'start': 0, 'num': 10000000} |
| 106 | ret = self._callProcessFormData(post_data) |
| 107 | self.assertEqual( |
Adrià Vilanova Martínez | f19ea43 | 2024-01-23 20:20:52 +0100 | [diff] [blame] | 108 | '/p/None/issues/reindex?start=%s&num=%s&auto_submit=False' % ( |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 109 | 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ínez | f19ea43 | 2024-01-23 20:20:52 +0100 | [diff] [blame] | 116 | '/p/None/issues/reindex?start=6&num=5&auto_submit=True', ret) |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 117 | |
| 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ínez | f19ea43 | 2024-01-23 20:20:52 +0100 | [diff] [blame] | 123 | '/p/None/issues/reindex?start=11&num=5&auto_submit=False', ret) |