Adrià Vilanova Martínez | f19ea43 | 2024-01-23 20:20:52 +0100 | [diff] [blame] | 1 | # 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. |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 4 | |
| 5 | """Unit test for User Group List servlet.""" |
| 6 | from __future__ import print_function |
| 7 | from __future__ import division |
| 8 | from __future__ import absolute_import |
| 9 | |
| 10 | import unittest |
| 11 | |
| 12 | from google.appengine.ext import testbed |
| 13 | |
| 14 | from framework import permissions |
| 15 | from services import service_manager |
| 16 | from sitewide import grouplist |
| 17 | from testing import fake |
| 18 | from testing import testing_helpers |
| 19 | |
| 20 | |
| 21 | class GroupListTest(unittest.TestCase): |
| 22 | """Tests for the GroupList servlet.""" |
| 23 | |
| 24 | def setUp(self): |
| 25 | self.services = service_manager.Services( |
| 26 | usergroup=fake.UserGroupService()) |
Adrià Vilanova Martínez | f19ea43 | 2024-01-23 20:20:52 +0100 | [diff] [blame] | 27 | self.servlet = grouplist.GroupList(services=self.services) |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 28 | self.mr = testing_helpers.MakeMonorailRequest() |
| 29 | self.testbed = testbed.Testbed() |
| 30 | self.testbed.activate() |
| 31 | self.testbed.init_memcache_stub() |
| 32 | self.testbed.init_datastore_v3_stub() |
| 33 | |
| 34 | def tearDown(self): |
| 35 | self.testbed.deactivate() |
| 36 | |
| 37 | def testAssertBasePermission_Anon(self): |
| 38 | self.mr.perms = permissions.READ_ONLY_PERMISSIONSET |
| 39 | with self.assertRaises(permissions.PermissionException): |
| 40 | self.servlet.AssertBasePermission(self.mr) |
| 41 | |
| 42 | def testAssertBasePermission_RegularUsers(self): |
| 43 | self.mr.perms = permissions.COMMITTER_ACTIVE_PERMISSIONSET |
| 44 | with self.assertRaises(permissions.PermissionException): |
| 45 | self.servlet.AssertBasePermission(self.mr) |
| 46 | |
| 47 | def testAssertBasePermission_SiteAdmin(self): |
| 48 | self.mr.perms = permissions.ADMIN_PERMISSIONSET |
| 49 | self.servlet.AssertBasePermission(self.mr) |
| 50 | |
| 51 | def testGatherPagData_ZeroGroups(self): |
| 52 | page_data = self.servlet.GatherPageData(self.mr) |
| 53 | self.assertEqual([], page_data['groups']) |
| 54 | |
| 55 | def testGatherPagData_NonzeroGroups(self): |
| 56 | self.services.usergroup.TestAddGroupSettings(777, 'group_a@example.com') |
| 57 | self.services.usergroup.TestAddGroupSettings(888, 'group_b@example.com') |
| 58 | self.services.usergroup.TestAddMembers(888, [111, 222, 333]) |
| 59 | page_data = self.servlet.GatherPageData(self.mr) |
| 60 | group_view_a, group_view_b = page_data['groups'] |
| 61 | self.assertEqual('group_a@example.com', group_view_a.name) |
| 62 | self.assertEqual(0, group_view_a.num_members) |
| 63 | self.assertEqual('group_b@example.com', group_view_b.name) |
| 64 | self.assertEqual(3, group_view_b.num_members) |
| 65 | |
| 66 | def testProcessFormData_NoPermission(self): |
| 67 | mr = testing_helpers.MakeMonorailRequest( |
| 68 | perms=permissions.USER_PERMISSIONSET) |
| 69 | post_data = fake.PostData( |
| 70 | removebtn=[1]) |
| 71 | self.assertRaises(permissions.PermissionException, |
| 72 | self.servlet.ProcessFormData, mr, post_data) |
| 73 | |
| 74 | def testProcessFormData_Normal(self): |
| 75 | self.services.usergroup.TestAddGroupSettings( |
| 76 | 888, 'group_b@example.com', friend_projects=[789]) |
| 77 | self.services.usergroup.TestAddMembers(888, [111, 222, 333]) |
| 78 | |
| 79 | post_data = fake.PostData( |
| 80 | remove=[888], |
| 81 | removebtn=[1]) |
| 82 | self.servlet.ProcessFormData(self.mr, post_data) |
| 83 | self.assertNotIn(888, self.services.usergroup.group_settings) |