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