blob: 72dfa9dd5caeeb6ff75a6878d5d91ffdbe11ba52 [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 admin servlet."""
7from __future__ import print_function
8from __future__ import division
9from __future__ import absolute_import
10
11import unittest
12
13from framework import permissions
14from proto import usergroup_pb2
15from services import service_manager
16from sitewide import groupadmin
17from testing import fake
18from testing import testing_helpers
19
20
21class GrouAdminTest(unittest.TestCase):
22 """Tests for the GroupAdmin servlet."""
23
24 def setUp(self):
25 self.services = service_manager.Services(
26 user=fake.UserService(),
27 usergroup=fake.UserGroupService(),
28 project=fake.ProjectService())
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.user.TestAddUser('importgroup@example.com', 999)
34 self.services.usergroup.TestAddGroupSettings(888, 'group@example.com')
35 self.services.usergroup.TestAddGroupSettings(
36 999, 'importgroup@example.com', external_group_type='mdb')
Adrià Vilanova Martínez9f9ade52022-10-10 23:20:11 +020037 self.servlet = groupadmin.GroupAdmin(services=self.services)
Copybara854996b2021-09-07 19:36:02 +000038 self.mr = testing_helpers.MakeMonorailRequest()
39 self.mr.viewed_username = 'group@example.com'
40 self.mr.viewed_user_auth.user_id = 888
41
42 def testAssertBasePermission(self):
43 mr = testing_helpers.MakeMonorailRequest(
44 perms=permissions.GetPermissions(None, {}, None))
45 mr.viewed_user_auth.user_id = 888
46 self.assertRaises(
47 permissions.PermissionException,
48 self.servlet.AssertBasePermission, mr)
49 self.services.usergroup.TestAddMembers(888, [111], 'owner')
50 self.servlet.AssertBasePermission(self.mr)
51
52 def testGatherPageData_Normal(self):
53 page_data = self.servlet.GatherPageData(self.mr)
54 self.assertEqual('group@example.com', page_data['groupname'])
55 self.assertEqual('Group Members', page_data['initial_visibility'].name)
56 self.assertEqual(3, len(page_data['visibility_levels']))
57
58 def testGatherPageData_Import(self):
59 mr = testing_helpers.MakeMonorailRequest()
60 mr.viewed_username = 'importgroup@example.com'
61 mr.viewed_user_auth.user_id = 999
62 page_data = self.servlet.GatherPageData(mr)
63 self.assertEqual('importgroup@example.com', page_data['groupname'])
64 self.assertTrue(page_data['import_group'])
65 self.assertEqual('MDB', page_data['initial_group_type'].name)
66
67 def testProcessFormData_Normal(self):
68 post_data = fake.PostData(visibility='0')
69 url = self.servlet.ProcessFormData(self.mr, post_data)
70 self.assertIn('/g/group@example.com/groupadmin', url)
71 group_settings = self.services.usergroup.GetGroupSettings(None, 888)
72 self.assertEqual(usergroup_pb2.MemberVisibility.OWNERS,
73 group_settings.who_can_view_members)
74
75 def testProcessFormData_Import(self):
76 post_data = fake.PostData(
77 group_type='1', import_group=['on'])
78 url = self.servlet.ProcessFormData(self.mr, post_data)
79 self.assertIn('/g/group@example.com/groupadmin', url)
80 group_settings = self.services.usergroup.GetGroupSettings(None, 888)
81 self.assertEqual(usergroup_pb2.MemberVisibility.OWNERS,
82 group_settings.who_can_view_members)
83 self.assertEqual(usergroup_pb2.GroupType.MDB,
84 group_settings.ext_group_type)