blob: bf7be8dd67b6b9c679629e2a57b56e73d76fc415 [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 creation servlet."""
7from __future__ import print_function
8from __future__ import division
9from __future__ import absolute_import
10
11import unittest
12
13import settings
14from framework import permissions
15from proto import site_pb2
16from proto import usergroup_pb2
17from services import service_manager
18from sitewide import groupcreate
19from testing import fake
20from testing import testing_helpers
21
22
23class GroupCreateTest(unittest.TestCase):
24 """Tests for the GroupCreate servlet."""
25
26 def setUp(self):
27 self.services = service_manager.Services(
28 user=fake.UserService(),
29 usergroup=fake.UserGroupService(),
30 project=fake.ProjectService())
31 self.servlet = groupcreate.GroupCreate(
32 'req', 'res', services=self.services)
33 self.mr = testing_helpers.MakeMonorailRequest()
34
35 def CheckAssertBasePermissions(
36 self, restriction, expect_admin_ok, expect_nonadmin_ok):
37 old_group_creation_restriction = settings.group_creation_restriction
38 settings.group_creation_restriction = restriction
39
40 # Anon users can never do it
41 mr = testing_helpers.MakeMonorailRequest(
42 perms=permissions.GetPermissions(None, {}, None))
43 self.assertRaises(
44 permissions.PermissionException,
45 self.servlet.AssertBasePermission, mr)
46
47 mr = testing_helpers.MakeMonorailRequest()
48 if expect_admin_ok:
49 self.servlet.AssertBasePermission(mr)
50 else:
51 self.assertRaises(
52 permissions.PermissionException,
53 self.servlet.AssertBasePermission, mr)
54
55 mr = testing_helpers.MakeMonorailRequest(
56 perms=permissions.GetPermissions(mr.auth.user_pb, {111}, None))
57 if expect_nonadmin_ok:
58 self.servlet.AssertBasePermission(mr)
59 else:
60 self.assertRaises(
61 permissions.PermissionException,
62 self.servlet.AssertBasePermission, mr)
63
64 settings.group_creation_restriction = old_group_creation_restriction
65
66 def testAssertBasePermission(self):
67 self.CheckAssertBasePermissions(
68 site_pb2.UserTypeRestriction.ANYONE, True, True)
69 self.CheckAssertBasePermissions(
70 site_pb2.UserTypeRestriction.ADMIN_ONLY, True, False)
71 self.CheckAssertBasePermissions(
72 site_pb2.UserTypeRestriction.NO_ONE, False, False)
73
74 def testGatherPageData(self):
75 page_data = self.servlet.GatherPageData(self.mr)
76 self.assertEqual('', page_data['initial_name'])
77
78 def testProcessFormData_Normal(self):
79 post_data = fake.PostData(
80 groupname=['group@example.com'], visibility='1')
81 url = self.servlet.ProcessFormData(self.mr, post_data)
82 self.assertIn('/g/3444127190/', url)
83 group_id = self.services.user.LookupUserID('cnxn', 'group@example.com')
84 group_settings = self.services.usergroup.GetGroupSettings('cnxn', group_id)
85 self.assertIsNotNone(group_settings)
86 members_after, owners_after = self.services.usergroup.LookupMembers(
87 'cnxn', [group_id])
88 self.assertEqual(0, len(members_after[group_id] + owners_after[group_id]))
89
90 def testProcessFormData_Import(self):
91 post_data = fake.PostData(
92 groupname=['group@example.com'], group_type='1',
93 import_group=['on'])
94 self.servlet.ProcessFormData(self.mr, post_data)
95 group_id = self.services.user.LookupUserID('cnxn', 'group@example.com')
96 group_settings = self.services.usergroup.GetGroupSettings('cnxn', group_id)
97 self.assertIsNotNone(group_settings)
98 self.assertEqual(usergroup_pb2.MemberVisibility.OWNERS,
99 group_settings.who_can_view_members)
100 self.assertEqual(usergroup_pb2.GroupType.MDB,
101 group_settings.ext_group_type)