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