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