Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 1 | # 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.""" |
| 7 | from __future__ import print_function |
| 8 | from __future__ import division |
| 9 | from __future__ import absolute_import |
| 10 | |
| 11 | import unittest |
| 12 | |
| 13 | from framework import permissions |
| 14 | from proto import usergroup_pb2 |
| 15 | from services import service_manager |
| 16 | from sitewide import groupadmin |
| 17 | from testing import fake |
| 18 | from testing import testing_helpers |
| 19 | |
| 20 | |
| 21 | class 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') |
Adrià Vilanova MartÃnez | 9f9ade5 | 2022-10-10 23:20:11 +0200 | [diff] [blame^] | 37 | self.servlet = groupadmin.GroupAdmin(services=self.services) |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 38 | self.mr = testing_helpers.MakeMonorailRequest() |
| 39 | self.mr.viewed_username = 'group@example.com' |
| 40 | self.mr.viewed_user_auth.user_id = 888 |
| 41 | |
| 42 | def testAssertBasePermission(self): |
| 43 | mr = testing_helpers.MakeMonorailRequest( |
| 44 | perms=permissions.GetPermissions(None, {}, None)) |
| 45 | mr.viewed_user_auth.user_id = 888 |
| 46 | self.assertRaises( |
| 47 | permissions.PermissionException, |
| 48 | self.servlet.AssertBasePermission, mr) |
| 49 | self.services.usergroup.TestAddMembers(888, [111], 'owner') |
| 50 | self.servlet.AssertBasePermission(self.mr) |
| 51 | |
| 52 | def testGatherPageData_Normal(self): |
| 53 | page_data = self.servlet.GatherPageData(self.mr) |
| 54 | self.assertEqual('group@example.com', page_data['groupname']) |
| 55 | self.assertEqual('Group Members', page_data['initial_visibility'].name) |
| 56 | self.assertEqual(3, len(page_data['visibility_levels'])) |
| 57 | |
| 58 | def testGatherPageData_Import(self): |
| 59 | mr = testing_helpers.MakeMonorailRequest() |
| 60 | mr.viewed_username = 'importgroup@example.com' |
| 61 | mr.viewed_user_auth.user_id = 999 |
| 62 | page_data = self.servlet.GatherPageData(mr) |
| 63 | self.assertEqual('importgroup@example.com', page_data['groupname']) |
| 64 | self.assertTrue(page_data['import_group']) |
| 65 | self.assertEqual('MDB', page_data['initial_group_type'].name) |
| 66 | |
| 67 | def testProcessFormData_Normal(self): |
| 68 | post_data = fake.PostData(visibility='0') |
| 69 | url = self.servlet.ProcessFormData(self.mr, post_data) |
| 70 | self.assertIn('/g/group@example.com/groupadmin', url) |
| 71 | group_settings = self.services.usergroup.GetGroupSettings(None, 888) |
| 72 | self.assertEqual(usergroup_pb2.MemberVisibility.OWNERS, |
| 73 | group_settings.who_can_view_members) |
| 74 | |
| 75 | def testProcessFormData_Import(self): |
| 76 | post_data = fake.PostData( |
| 77 | group_type='1', import_group=['on']) |
| 78 | url = self.servlet.ProcessFormData(self.mr, post_data) |
| 79 | self.assertIn('/g/group@example.com/groupadmin', url) |
| 80 | group_settings = self.services.usergroup.GetGroupSettings(None, 888) |
| 81 | self.assertEqual(usergroup_pb2.MemberVisibility.OWNERS, |
| 82 | group_settings.who_can_view_members) |
| 83 | self.assertEqual(usergroup_pb2.GroupType.MDB, |
| 84 | group_settings.ext_group_type) |