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