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