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