blob: 4440bb8562c8fa7fbbf27b12c3a7bf7e913531ff [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 test for User Group Detail servlet."""
7from __future__ import print_function
8from __future__ import division
9from __future__ import absolute_import
10
11import unittest
12
13from framework import exceptions
14from framework import permissions
15from services import service_manager
16from sitewide import groupdetail
17from testing import fake
18from testing import testing_helpers
19
20
21class GroupDetailTest(unittest.TestCase):
22 """Tests for the GroupDetail servlet."""
23
24 def setUp(self):
25 self.services = service_manager.Services(
26 project=fake.ProjectService(),
27 user=fake.UserService(),
28 usergroup=fake.UserGroupService())
29 self.services.user.TestAddUser('a@example.com', 111)
30 self.services.user.TestAddUser('b@example.com', 222)
31 self.services.user.TestAddUser('c@example.com', 333)
32 self.services.user.TestAddUser('group@example.com', 888)
33 self.services.usergroup.TestAddGroupSettings(888, 'group@example.com')
34 self.servlet = groupdetail.GroupDetail(
35 'req', 'res', services=self.services)
36 self.mr = testing_helpers.MakeMonorailRequest()
37 self.mr.viewed_username = 'group@example.com'
38 self.mr.viewed_user_auth.user_id = 888
39
40 def testAssertBasePermission(self):
41 mr = testing_helpers.MakeMonorailRequest(
42 perms=permissions.GetPermissions(None, {}, None))
43 mr.viewed_user_auth.user_id = 888
44 mr.auth.effective_ids = set([111])
45 self.assertRaises(
46 permissions.PermissionException,
47 self.servlet.AssertBasePermission, mr)
48 self.services.usergroup.TestAddMembers(888, [111], 'member')
49 self.servlet.AssertBasePermission(mr)
50
51 def testAssertBasePermission_IgnoreNoSuchGroup(self):
52 """The permission check does not crash for non-existent user groups."""
53 mr = testing_helpers.MakeMonorailRequest(
54 perms=permissions.GetPermissions(None, {}, None))
55 mr.viewed_user_auth.user_id = 404
56 mr.auth.effective_ids = set([111])
57 self.servlet.AssertBasePermission(mr)
58
59 def testAssertBasePermission_IndirectMembership(self):
60 self.services.usergroup.TestAddGroupSettings(999, 'subgroup@example.com')
61 mr = testing_helpers.MakeMonorailRequest(
62 perms=permissions.GetPermissions(None, {}, None))
63 mr.viewed_user_auth.user_id = 888
64 mr.auth.effective_ids = set([111])
65 self.assertRaises(
66 permissions.PermissionException,
67 self.servlet.AssertBasePermission, mr)
68 self.services.usergroup.TestAddMembers(888, [999], 'member')
69 self.services.usergroup.TestAddMembers(999, [111], 'member')
70 self.servlet.AssertBasePermission(mr)
71
72 def testGatherPagData_ZeroMembers(self):
73 page_data = self.servlet.GatherPageData(self.mr)
74 pagination = page_data['pagination']
75 self.assertEqual(0, len(pagination.visible_results))
76
77 def testGatherPagData_NonzeroMembers(self):
78 self.services.usergroup.TestAddMembers(888, [111, 222, 333])
79 page_data = self.servlet.GatherPageData(self.mr)
80 pagination = page_data['pagination']
81 self.assertEqual(3, len(pagination.visible_results))
82 self.assertEqual(3, pagination.total_count)
83 self.assertEqual(1, pagination.start)
84 self.assertEqual(3, pagination.last)
85 user_view_a, user_view_b, user_view_c = pagination.visible_results
86 self.assertEqual('a@example.com', user_view_a.email)
87 self.assertEqual('b@example.com', user_view_b.email)
88 self.assertEqual('c@example.com', user_view_c.email)
89
90 def testProcessAddMembers_NoneAdded(self):
91 post_data = fake.PostData(addmembers=[''], role=['member'])
92 url = self.servlet.ProcessAddMembers(self.mr, post_data)
93 self.assertIn('/g/group@example.com/?', url)
94 members_after, _ = self.services.usergroup.LookupMembers('cnxn', [888])
95 self.assertEqual(0, len(members_after[888]))
96
97 self.services.usergroup.TestAddMembers(888, [111, 222, 333])
98 url = self.servlet.ProcessAddMembers(self.mr, post_data)
99 self.assertIn('/g/group@example.com/?', url)
100 members_after, _ = self.services.usergroup.LookupMembers('cnxn', [888])
101 self.assertEqual(3, len(members_after[888]))
102
103 def testProcessAddMembers_SomeAdded(self):
104 self.services.usergroup.TestAddMembers(888, [111])
105 post_data = fake.PostData(
106 addmembers=['b@example.com, c@example.com'], role=['member'])
107 url = self.servlet.ProcessAddMembers(self.mr, post_data)
108 self.assertIn('/g/group@example.com/?', url)
109 members_after, _ = self.services.usergroup.LookupMembers('cnxn', [888])
110 self.assertEqual(3, len(members_after[888]))
111
112 def testProcessRemoveMembers_SomeRemoved(self):
113 self.services.usergroup.TestAddMembers(888, [111, 222, 333])
114 post_data = fake.PostData(remove=['b@example.com', 'c@example.com'])
115 url = self.servlet.ProcessRemoveMembers(self.mr, post_data)
116 self.assertIn('/g/group@example.com/?', url)
117 members_after, _ = self.services.usergroup.LookupMembers('cnxn', [888])
118 self.assertEqual(1, len(members_after[888]))
119
120 def testProcessFormData_NoPermission(self):
121 """Group members cannot edit group."""
122 self.services.usergroup.TestAddMembers(888, [111], 'member')
123 mr = testing_helpers.MakeMonorailRequest(
124 perms=permissions.GetPermissions(None, {}, None))
125 mr.viewed_user_auth.user_id = 888
126 mr.auth.effective_ids = set([111])
127 self.assertRaises(permissions.PermissionException,
128 self.servlet.ProcessFormData, mr, {})
129
130 def testProcessFormData_OwnerPermission(self):
131 """Group owners cannot edit group."""
132 self.services.usergroup.TestAddMembers(888, [111], 'owner')
133 mr = testing_helpers.MakeMonorailRequest(
134 perms=permissions.GetPermissions(None, {}, None))
135 mr.viewed_user_auth.user_id = 888
136 mr.auth.effective_ids = set([111])
137 self.servlet.ProcessFormData(mr, {})
138
139 def testGatherPagData_NoSuchUserGroup(self):
140 """If there is no such user group, raise an exception."""
141 self.mr.viewed_user_auth.user_id = 404
142 self.assertRaises(
143 exceptions.NoSuchGroupException,
144 self.servlet.GatherPageData, self.mr)
145
146