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 tests for projectadmin module.""" |
| 6 | from __future__ import print_function |
| 7 | from __future__ import division |
| 8 | from __future__ import absolute_import |
| 9 | |
| 10 | import unittest |
| 11 | |
| 12 | from framework import permissions |
| 13 | from project import projectadmin |
Adrià Vilanova Martínez | f19ea43 | 2024-01-23 20:20:52 +0100 | [diff] [blame] | 14 | from mrproto import project_pb2 |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 15 | from services import service_manager |
| 16 | from testing import fake |
| 17 | from testing import testing_helpers |
| 18 | |
| 19 | |
| 20 | class ProjectAdminTest(unittest.TestCase): |
| 21 | """Unit tests for the ProjectAdmin servlet class.""" |
| 22 | |
| 23 | def setUp(self): |
| 24 | services = service_manager.Services( |
| 25 | project=fake.ProjectService(), |
| 26 | user=fake.UserService()) |
Adrià Vilanova Martínez | f19ea43 | 2024-01-23 20:20:52 +0100 | [diff] [blame] | 27 | self.servlet = projectadmin.ProjectAdmin(services=services) |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 28 | self.project = services.project.TestAddProject( |
| 29 | 'proj', summary='a summary', description='a description') |
| 30 | self.request, self.mr = testing_helpers.GetRequestObjects( |
| 31 | project=self.project) |
| 32 | |
| 33 | def testAssertBasePermission(self): |
| 34 | # Contributors cannot edit the project |
| 35 | mr = testing_helpers.MakeMonorailRequest( |
| 36 | project=self.project, |
| 37 | perms=permissions.CONTRIBUTOR_ACTIVE_PERMISSIONSET) |
| 38 | self.assertRaises(permissions.PermissionException, |
| 39 | self.servlet.AssertBasePermission, mr) |
| 40 | |
| 41 | # Signed-out users cannot edit the project |
| 42 | mr.perms = permissions.READ_ONLY_PERMISSIONSET |
| 43 | self.assertRaises(permissions.PermissionException, |
| 44 | self.servlet.AssertBasePermission, mr) |
| 45 | |
| 46 | # Non-member users cannot edit the project |
| 47 | mr.perms = permissions.USER_PERMISSIONSET |
| 48 | self.assertRaises(permissions.PermissionException, |
| 49 | self.servlet.AssertBasePermission, mr) |
| 50 | |
| 51 | # Owners can edit the project |
| 52 | mr.perms = permissions.OWNER_ACTIVE_PERMISSIONSET |
| 53 | self.servlet.AssertBasePermission(mr) |
| 54 | |
| 55 | def testGatherPageData(self): |
| 56 | # Project has all default values. |
| 57 | page_data = self.servlet.GatherPageData(self.mr) |
| 58 | self.assertEqual('a summary', page_data['initial_summary']) |
| 59 | self.assertEqual('a description', page_data['initial_description']) |
| 60 | self.assertEqual( |
| 61 | int(project_pb2.ProjectAccess.ANYONE), page_data['initial_access'].key) |
| 62 | |
| 63 | self.assertFalse(page_data['process_inbound_email']) |
| 64 | self.assertFalse(page_data['only_owners_remove_restrictions']) |
| 65 | self.assertFalse(page_data['only_owners_see_contributors']) |
| 66 | self.assertFalse(page_data['issue_notify_always_detailed']) |
| 67 | |
| 68 | # Now try some alternate Project field values. |
| 69 | self.project.only_owners_remove_restrictions = True |
| 70 | self.project.only_owners_see_contributors = True |
| 71 | self.project.issue_notify_always_detailed = True |
| 72 | page_data = self.servlet.GatherPageData(self.mr) |
| 73 | self.assertTrue(page_data['only_owners_remove_restrictions']) |
| 74 | self.assertTrue(page_data['only_owners_see_contributors']) |
| 75 | self.assertTrue(page_data['issue_notify_always_detailed']) |
| 76 | |
| 77 | # TODO(jrobbins): many more tests needed. |