blob: d292b6f33c26aea21ad1280fc79749b8b3621542 [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"""Unit tests for the sitewide_helpers module."""
7from __future__ import print_function
8from __future__ import division
9from __future__ import absolute_import
10
11import unittest
12
13from proto import project_pb2
14from services import service_manager
15from sitewide import sitewide_helpers
16from testing import fake
17
18
19REGULAR_USER_ID = 111
20ADMIN_USER_ID = 222
21OTHER_USER_ID = 333
22
23# Test project IDs
24REGULAR_OWNER_LIVE = 1001
25REGULAR_OWNER_ARCHIVED = 1002
26REGULAR_OWNER_DELETABLE = 1003
27REGULAR_COMMITTER_LIVE = 2001
28REGULAR_COMMITTER_ARCHIVED = 2002
29REGULAR_COMMITTER_DELETABLE = 2003
30OTHER_OWNER_LIVE = 3001
31OTHER_OWNER_ARCHIVED = 3002
32OTHER_OWNER_DELETABLE = 3003
33OTHER_COMMITTER_LIVE = 4001
34MEMBERS_ONLY = 5001
35
36
37class HelperFunctionsTest(unittest.TestCase):
38
39 def setUp(self):
40 self.services = service_manager.Services(
41 project=fake.ProjectService(),
42 user=fake.UserService(),
43 project_star=fake.ProjectStarService())
44 self.cnxn = 'fake cnxn'
45
46 for user_id in (ADMIN_USER_ID, REGULAR_USER_ID, OTHER_USER_ID):
47 self.services.user.TestAddUser('ignored_%s@gmail.com' % user_id, user_id)
48
49 self.regular_owner_live = self.services.project.TestAddProject(
50 'regular-owner-live', state=project_pb2.ProjectState.LIVE,
51 owner_ids=[REGULAR_USER_ID], project_id=REGULAR_OWNER_LIVE)
52 self.regular_owner_archived = self.services.project.TestAddProject(
53 'regular-owner-archived', state=project_pb2.ProjectState.ARCHIVED,
54 owner_ids=[REGULAR_USER_ID], project_id=REGULAR_OWNER_ARCHIVED)
55 self.regular_owner_deletable = self.services.project.TestAddProject(
56 'regular-owner-deletable', state=project_pb2.ProjectState.DELETABLE,
57 owner_ids=[REGULAR_USER_ID], project_id=REGULAR_OWNER_DELETABLE)
58 self.regular_committer_live = self.services.project.TestAddProject(
59 'regular-committer-live', state=project_pb2.ProjectState.LIVE,
60 committer_ids=[REGULAR_USER_ID], project_id=REGULAR_COMMITTER_LIVE)
61 self.regular_committer_archived = self.services.project.TestAddProject(
62 'regular-committer-archived', state=project_pb2.ProjectState.ARCHIVED,
63 committer_ids=[REGULAR_USER_ID], project_id=REGULAR_COMMITTER_ARCHIVED)
64 self.regular_committer_deletable = self.services.project.TestAddProject(
65 'regular-committer-deletable', state=project_pb2.ProjectState.DELETABLE,
66 committer_ids=[REGULAR_USER_ID], project_id=REGULAR_COMMITTER_DELETABLE)
67 self.other_owner_live = self.services.project.TestAddProject(
68 'other-owner-live', state=project_pb2.ProjectState.LIVE,
69 owner_ids=[OTHER_USER_ID], project_id=OTHER_OWNER_LIVE)
70 self.other_owner_archived = self.services.project.TestAddProject(
71 'other-owner-archived', state=project_pb2.ProjectState.ARCHIVED,
72 owner_ids=[OTHER_USER_ID], project_id=OTHER_OWNER_ARCHIVED)
73 self.other_owner_deletable = self.services.project.TestAddProject(
74 'other-owner-deletable', state=project_pb2.ProjectState.DELETABLE,
75 owner_ids=[OTHER_USER_ID], project_id=OTHER_OWNER_DELETABLE)
76 self.other_committer_live = self.services.project.TestAddProject(
77 'other-committer-live', state=project_pb2.ProjectState.LIVE,
78 committer_ids=[OTHER_USER_ID], project_id=OTHER_COMMITTER_LIVE)
79
80 self.regular_user = self.services.user.GetUser(self.cnxn, REGULAR_USER_ID)
81
82 self.admin_user = self.services.user.TestAddUser(
83 'administrator@chromium.org', ADMIN_USER_ID)
84 self.admin_user.is_site_admin = True
85
86 self.other_user = self.services.user.GetUser(self.cnxn, OTHER_USER_ID)
87
88 self.members_only_project = self.services.project.TestAddProject(
89 'members-only', owner_ids=[REGULAR_USER_ID], project_id=MEMBERS_ONLY)
90 self.members_only_project.access = project_pb2.ProjectAccess.MEMBERS_ONLY
91
92 def assertProjectsAnyOrder(self, actual_projects, *expected_projects):
93 # Check names rather than Project objects so that output is easier to read.
94 actual_names = [p.project_name for p in actual_projects]
95 expected_names = [p.project_name for p in expected_projects]
96 self.assertItemsEqual(expected_names, actual_names)
97
98 def testFilterViewableProjects_CantViewArchived(self):
99 projects = list(sitewide_helpers.FilterViewableProjects(
100 list(self.services.project.test_projects.values()),
101 self.regular_user, {REGULAR_USER_ID}))
102 self.assertProjectsAnyOrder(
103 projects, self.regular_owner_live, self.regular_committer_live,
104 self.other_owner_live, self.other_committer_live,
105 self.members_only_project)
106
107 def testFilterViewableProjects_NonMemberCantViewMembersOnly(self):
108 projects = list(sitewide_helpers.FilterViewableProjects(
109 list(self.services.project.test_projects.values()),
110 self.other_user, {OTHER_USER_ID}))
111 self.assertProjectsAnyOrder(
112 projects, self.regular_owner_live, self.regular_committer_live,
113 self.other_owner_live, self.other_committer_live)
114
115 def testFilterViewableProjects_AdminCanViewAny(self):
116 projects = list(sitewide_helpers.FilterViewableProjects(
117 list(self.services.project.test_projects.values()),
118 self.admin_user, {ADMIN_USER_ID}))
119 self.assertProjectsAnyOrder(
120 projects, self.regular_owner_live, self.regular_committer_live,
121 self.other_owner_live, self.other_committer_live,
122 self.members_only_project)
123
124 def testGetStarredProjects_OnlyViewableLiveStarred(self):
125 viewed_user_id = 123
126 for p in self.services.project.test_projects.values():
127 # We go straight to the services layer because this is a test set up
128 # rather than an actual user request.
129 self.services.project_star.SetStar(
130 self.cnxn, p.project_id, viewed_user_id, True)
131
132 self.assertProjectsAnyOrder(
133 sitewide_helpers.GetViewableStarredProjects(
134 self.cnxn, self.services, viewed_user_id,
135 {REGULAR_USER_ID}, self.regular_user),
136 self.regular_owner_live, self.regular_committer_live,
137 self.other_owner_live, self.other_committer_live,
138 self.members_only_project)
139
140 def testGetStarredProjects_MembersOnly(self):
141 # Both users were able to star the project in the past. The stars do not
142 # go away even if access to the project changes.
143 self.services.project_star.SetStar(
144 self.cnxn, self.members_only_project.project_id, REGULAR_USER_ID, True)
145 self.services.project_star.SetStar(
146 self.cnxn, self.members_only_project.project_id, OTHER_USER_ID, True)
147
148 # But now, only one of them is currently a member, so only regular_user
149 # can see the starred project in the lists.
150 self.assertProjectsAnyOrder(
151 sitewide_helpers.GetViewableStarredProjects(
152 self.cnxn, self.services, REGULAR_USER_ID, {REGULAR_USER_ID},
153 self.regular_user),
154 self.members_only_project)
155 self.assertProjectsAnyOrder(
156 sitewide_helpers.GetViewableStarredProjects(
157 self.cnxn, self.services, OTHER_USER_ID, {REGULAR_USER_ID},
158 self.regular_user),
159 self.members_only_project)
160
161 # The other user cannot see the project, so they do not see it in either
162 # list of starred projects.
163 self.assertProjectsAnyOrder(
164 sitewide_helpers.GetViewableStarredProjects(
165 self.cnxn, self.services, REGULAR_USER_ID, {OTHER_USER_ID},
166 self.other_user)) # No expected projects listed.
167 self.assertProjectsAnyOrder(
168 sitewide_helpers.GetViewableStarredProjects(
169 self.cnxn, self.services, OTHER_USER_ID, {OTHER_USER_ID},
170 self.other_user)) # No expected projects listed.