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.search.backendnonviewable.""" |
| 6 | from __future__ import print_function |
| 7 | from __future__ import division |
| 8 | from __future__ import absolute_import |
| 9 | |
| 10 | import unittest |
Adrià Vilanova Martínez | 9f9ade5 | 2022-10-10 23:20:11 +0200 | [diff] [blame] | 11 | try: |
| 12 | from mox3 import mox |
| 13 | except ImportError: |
| 14 | import mox |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 15 | |
| 16 | from google.appengine.api import memcache |
| 17 | from google.appengine.ext import testbed |
| 18 | |
| 19 | from framework import permissions |
| 20 | from search import backendnonviewable |
| 21 | from services import service_manager |
| 22 | from testing import fake |
| 23 | from testing import testing_helpers |
| 24 | |
| 25 | |
| 26 | class BackendNonviewableTest(unittest.TestCase): |
| 27 | |
| 28 | def setUp(self): |
| 29 | self.services = service_manager.Services( |
| 30 | project=fake.ProjectService(), |
| 31 | config=fake.ConfigService(), |
| 32 | issue=fake.IssueService(), |
| 33 | ) |
| 34 | self.project = self.services.project.TestAddProject( |
| 35 | 'proj', project_id=789) |
| 36 | self.mr = testing_helpers.MakeMonorailRequest() |
| 37 | self.mr.specified_project_id = 789 |
| 38 | self.mr.shard_id = 2 |
| 39 | self.mr.invalidation_timestep = 12345 |
| 40 | |
Adrià Vilanova Martínez | 9f9ade5 | 2022-10-10 23:20:11 +0200 | [diff] [blame] | 41 | self.servlet = backendnonviewable.BackendNonviewable(services=self.services) |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 42 | |
| 43 | self.mox = mox.Mox() |
| 44 | self.testbed = testbed.Testbed() |
| 45 | self.testbed.activate() |
| 46 | self.testbed.init_memcache_stub() |
| 47 | |
| 48 | def tearDown(self): |
| 49 | self.testbed.deactivate() |
| 50 | self.mox.UnsetStubs() |
| 51 | self.mox.ResetAll() |
| 52 | |
| 53 | def testHandleRequest(self): |
| 54 | pass # TODO(jrobbins): fill in this test. |
| 55 | |
| 56 | def testGetNonviewableIIDs_OwnerOrAdmin(self): |
| 57 | """Check the special case for users who are never restricted.""" |
| 58 | perms = permissions.OWNER_ACTIVE_PERMISSIONSET |
| 59 | nonviewable_iids = self.servlet.GetNonviewableIIDs( |
| 60 | self.mr.cnxn, self.mr.auth.user_pb, {111}, self.project, perms, 2) |
| 61 | self.assertEqual([], nonviewable_iids) |
| 62 | |
| 63 | def testGetNonviewableIIDs_RegularUser(self): |
| 64 | pass # TODO(jrobbins) |
| 65 | |
| 66 | def testGetNonviewableIIDs_Anon(self): |
| 67 | pass # TODO(jrobbins) |
| 68 | |
| 69 | def testGetAtRiskIIDs_NothingEverAtRisk(self): |
| 70 | """Handle the case where the site has no restriction labels.""" |
| 71 | fake_restriction_label_rows = [] |
| 72 | fake_restriction_label_ids = [] |
| 73 | fake_at_risk_iids = [] |
| 74 | self.mox.StubOutWithMock(self.services.config, 'GetLabelDefRowsAnyProject') |
| 75 | self.services.config.GetLabelDefRowsAnyProject( |
| 76 | self.mr.cnxn, where=[('LOWER(label) LIKE %s', ['restrict-view-%'])] |
| 77 | ).AndReturn(fake_restriction_label_rows) |
| 78 | self.mox.StubOutWithMock(self.services.issue, 'GetIIDsByLabelIDs') |
| 79 | self.services.issue.GetIIDsByLabelIDs( |
| 80 | self.mr.cnxn, fake_restriction_label_ids, 789, 2 |
| 81 | ).AndReturn(fake_at_risk_iids) |
| 82 | self.mox.ReplayAll() |
| 83 | |
| 84 | at_risk_iids = self.servlet.GetAtRiskIIDs( |
| 85 | self.mr.cnxn, self.mr.auth.user_pb, self.mr.auth.effective_ids, |
| 86 | self.project, self.mr.perms, self.mr.shard_id) |
| 87 | self.mox.VerifyAll() |
| 88 | self.assertEqual([], at_risk_iids) |
| 89 | |
| 90 | def testGetAtRiskIIDs_NoIssuesAtRiskRightNow(self): |
| 91 | """Handle the case where the project has no restricted issues.""" |
| 92 | fake_restriction_label_rows = [ |
| 93 | (123, 789, 1, 'Restrict-View-A', 'doc', False), |
| 94 | (234, 789, 2, 'Restrict-View-B', 'doc', False), |
| 95 | ] |
| 96 | fake_restriction_label_ids = [123, 234] |
| 97 | fake_at_risk_iids = [] |
| 98 | self.mox.StubOutWithMock(self.services.config, 'GetLabelDefRowsAnyProject') |
| 99 | self.services.config.GetLabelDefRowsAnyProject( |
| 100 | self.mr.cnxn, where=[('LOWER(label) LIKE %s', ['restrict-view-%'])] |
| 101 | ).AndReturn(fake_restriction_label_rows) |
| 102 | self.mox.StubOutWithMock(self.services.issue, 'GetIIDsByLabelIDs') |
| 103 | self.services.issue.GetIIDsByLabelIDs( |
| 104 | self.mr.cnxn, fake_restriction_label_ids, 789, 2 |
| 105 | ).AndReturn(fake_at_risk_iids) |
| 106 | self.mox.ReplayAll() |
| 107 | |
| 108 | at_risk_iids = self.servlet.GetAtRiskIIDs( |
| 109 | self.mr.cnxn, self.mr.auth.user_pb, self.mr.auth.effective_ids, |
| 110 | self.project, self.mr.perms, self.mr.shard_id) |
| 111 | self.mox.VerifyAll() |
| 112 | self.assertEqual([], at_risk_iids) |
| 113 | |
| 114 | def testGetAtRiskIIDs_SomeAtRisk(self): |
| 115 | """Handle the case where the project has some restricted issues.""" |
| 116 | fake_restriction_label_rows = [ |
| 117 | (123, 789, 1, 'Restrict-View-A', 'doc', False), |
| 118 | (234, 789, 2, 'Restrict-View-B', 'doc', False), |
| 119 | ] |
| 120 | fake_restriction_label_ids = [123, 234] |
| 121 | fake_at_risk_iids = [432, 543] |
| 122 | self.mox.StubOutWithMock(self.services.config, 'GetLabelDefRowsAnyProject') |
| 123 | self.services.config.GetLabelDefRowsAnyProject( |
| 124 | self.mr.cnxn, where=[('LOWER(label) LIKE %s', ['restrict-view-%'])] |
| 125 | ).AndReturn(fake_restriction_label_rows) |
| 126 | self.mox.StubOutWithMock(self.services.issue, 'GetIIDsByLabelIDs') |
| 127 | self.services.issue.GetIIDsByLabelIDs( |
| 128 | self.mr.cnxn, fake_restriction_label_ids, 789, 2 |
| 129 | ).AndReturn(fake_at_risk_iids) |
| 130 | self.mox.ReplayAll() |
| 131 | |
| 132 | at_risk_iids = self.servlet.GetAtRiskIIDs( |
| 133 | self.mr.cnxn, self.mr.auth.user_pb, self.mr.auth.effective_ids, |
| 134 | self.project, self.mr.perms, self.mr.shard_id) |
| 135 | self.mox.VerifyAll() |
| 136 | self.assertEqual([432, 543], at_risk_iids) |
| 137 | |
| 138 | def testGetViewableIIDs_Anon(self): |
| 139 | """Anon users are never participants in any issues.""" |
| 140 | ok_iids = self.servlet.GetViewableIIDs( |
| 141 | self.mr.cnxn, set(), 789, 2) |
| 142 | self.assertEqual([], ok_iids) |
| 143 | |
| 144 | def testGetViewableIIDs_NoIssues(self): |
| 145 | """This visitor does not participate in any issues.""" |
| 146 | self.mox.StubOutWithMock(self.services.issue, 'GetIIDsByParticipant') |
| 147 | self.services.issue.GetIIDsByParticipant( |
| 148 | self.mr.cnxn, {111}, [789], 2).AndReturn([]) |
| 149 | self.mox.ReplayAll() |
| 150 | |
| 151 | ok_iids = self.servlet.GetViewableIIDs( |
| 152 | self.mr.cnxn, {111}, 789, 2) |
| 153 | self.mox.VerifyAll() |
| 154 | self.assertEqual([], ok_iids) |
| 155 | |
| 156 | def testGetViewableIIDs_SomeIssues(self): |
| 157 | """This visitor participates in some issues.""" |
| 158 | self.mox.StubOutWithMock(self.services.issue, 'GetIIDsByParticipant') |
| 159 | self.services.issue.GetIIDsByParticipant( |
| 160 | self.mr.cnxn, {111}, [789], 2).AndReturn([543, 654]) |
| 161 | self.mox.ReplayAll() |
| 162 | |
| 163 | ok_iids = self.servlet.GetViewableIIDs( |
| 164 | self.mr.cnxn, {111}, 789, 2) |
| 165 | self.mox.VerifyAll() |
| 166 | self.assertEqual([543, 654], ok_iids) |