blob: d1f7e0fadd3ea5307bdcbbb2d3d0ac1f692096e0 [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')
37 self.servlet = groupadmin.GroupAdmin(
38 'req', 'res', services=self.services)
39 self.mr = testing_helpers.MakeMonorailRequest()
40 self.mr.viewed_username = 'group@example.com'
41 self.mr.viewed_user_auth.user_id = 888
42
43 def testAssertBasePermission(self):
44 mr = testing_helpers.MakeMonorailRequest(
45 perms=permissions.GetPermissions(None, {}, None))
46 mr.viewed_user_auth.user_id = 888
47 self.assertRaises(
48 permissions.PermissionException,
49 self.servlet.AssertBasePermission, mr)
50 self.services.usergroup.TestAddMembers(888, [111], 'owner')
51 self.servlet.AssertBasePermission(self.mr)
52
53 def testGatherPageData_Normal(self):
54 page_data = self.servlet.GatherPageData(self.mr)
55 self.assertEqual('group@example.com', page_data['groupname'])
56 self.assertEqual('Group Members', page_data['initial_visibility'].name)
57 self.assertEqual(3, len(page_data['visibility_levels']))
58
59 def testGatherPageData_Import(self):
60 mr = testing_helpers.MakeMonorailRequest()
61 mr.viewed_username = 'importgroup@example.com'
62 mr.viewed_user_auth.user_id = 999
63 page_data = self.servlet.GatherPageData(mr)
64 self.assertEqual('importgroup@example.com', page_data['groupname'])
65 self.assertTrue(page_data['import_group'])
66 self.assertEqual('MDB', page_data['initial_group_type'].name)
67
68 def testProcessFormData_Normal(self):
69 post_data = fake.PostData(visibility='0')
70 url = self.servlet.ProcessFormData(self.mr, post_data)
71 self.assertIn('/g/group@example.com/groupadmin', url)
72 group_settings = self.services.usergroup.GetGroupSettings(None, 888)
73 self.assertEqual(usergroup_pb2.MemberVisibility.OWNERS,
74 group_settings.who_can_view_members)
75
76 def testProcessFormData_Import(self):
77 post_data = fake.PostData(
78 group_type='1', import_group=['on'])
79 url = self.servlet.ProcessFormData(self.mr, post_data)
80 self.assertIn('/g/group@example.com/groupadmin', url)
81 group_settings = self.services.usergroup.GetGroupSettings(None, 888)
82 self.assertEqual(usergroup_pb2.MemberVisibility.OWNERS,
83 group_settings.who_can_view_members)
84 self.assertEqual(usergroup_pb2.GroupType.MDB,
85 group_settings.ext_group_type)