blob: f82fb7453fb8dab9fc6579f509bf8a0fab546924 [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())
Adrià Vilanova Martínez9f9ade52022-10-10 23:20:11 +020031 self.servlet = groupcreate.GroupCreate(services=self.services)
Copybara854996b2021-09-07 19:36:02 +000032 self.mr = testing_helpers.MakeMonorailRequest()
33
34 def CheckAssertBasePermissions(
35 self, restriction, expect_admin_ok, expect_nonadmin_ok):
36 old_group_creation_restriction = settings.group_creation_restriction
37 settings.group_creation_restriction = restriction
38
39 # Anon users can never do it
40 mr = testing_helpers.MakeMonorailRequest(
41 perms=permissions.GetPermissions(None, {}, None))
42 self.assertRaises(
43 permissions.PermissionException,
44 self.servlet.AssertBasePermission, mr)
45
46 mr = testing_helpers.MakeMonorailRequest()
47 if expect_admin_ok:
48 self.servlet.AssertBasePermission(mr)
49 else:
50 self.assertRaises(
51 permissions.PermissionException,
52 self.servlet.AssertBasePermission, mr)
53
54 mr = testing_helpers.MakeMonorailRequest(
55 perms=permissions.GetPermissions(mr.auth.user_pb, {111}, None))
56 if expect_nonadmin_ok:
57 self.servlet.AssertBasePermission(mr)
58 else:
59 self.assertRaises(
60 permissions.PermissionException,
61 self.servlet.AssertBasePermission, mr)
62
63 settings.group_creation_restriction = old_group_creation_restriction
64
65 def testAssertBasePermission(self):
66 self.CheckAssertBasePermissions(
67 site_pb2.UserTypeRestriction.ANYONE, True, True)
68 self.CheckAssertBasePermissions(
69 site_pb2.UserTypeRestriction.ADMIN_ONLY, True, False)
70 self.CheckAssertBasePermissions(
71 site_pb2.UserTypeRestriction.NO_ONE, False, False)
72
73 def testGatherPageData(self):
74 page_data = self.servlet.GatherPageData(self.mr)
75 self.assertEqual('', page_data['initial_name'])
76
77 def testProcessFormData_Normal(self):
78 post_data = fake.PostData(
79 groupname=['group@example.com'], visibility='1')
80 url = self.servlet.ProcessFormData(self.mr, post_data)
81 self.assertIn('/g/3444127190/', url)
82 group_id = self.services.user.LookupUserID('cnxn', 'group@example.com')
83 group_settings = self.services.usergroup.GetGroupSettings('cnxn', group_id)
84 self.assertIsNotNone(group_settings)
85 members_after, owners_after = self.services.usergroup.LookupMembers(
86 'cnxn', [group_id])
87 self.assertEqual(0, len(members_after[group_id] + owners_after[group_id]))
88
89 def testProcessFormData_Import(self):
90 post_data = fake.PostData(
91 groupname=['group@example.com'], group_type='1',
92 import_group=['on'])
93 self.servlet.ProcessFormData(self.mr, post_data)
94 group_id = self.services.user.LookupUserID('cnxn', 'group@example.com')
95 group_settings = self.services.usergroup.GetGroupSettings('cnxn', group_id)
96 self.assertIsNotNone(group_settings)
97 self.assertEqual(usergroup_pb2.MemberVisibility.OWNERS,
98 group_settings.who_can_view_members)
99 self.assertEqual(usergroup_pb2.GroupType.MDB,
100 group_settings.ext_group_type)