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