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