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 | """Unittests for the Project Creation servlet.""" |
| 6 | from __future__ import print_function |
| 7 | from __future__ import division |
| 8 | from __future__ import absolute_import |
| 9 | |
| 10 | import unittest |
| 11 | |
| 12 | import settings |
| 13 | from framework import permissions |
Adrià Vilanova Martínez | f19ea43 | 2024-01-23 20:20:52 +0100 | [diff] [blame^] | 14 | from mrproto import project_pb2 |
| 15 | from mrproto import site_pb2 |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 16 | from services import service_manager |
| 17 | from sitewide import projectcreate |
| 18 | from testing import fake |
| 19 | from testing import testing_helpers |
| 20 | |
| 21 | |
| 22 | class ProjectCreateTest(unittest.TestCase): |
| 23 | |
| 24 | def setUp(self): |
| 25 | services = service_manager.Services() |
Adrià Vilanova Martínez | 9f9ade5 | 2022-10-10 23:20:11 +0200 | [diff] [blame] | 26 | self.servlet = projectcreate.ProjectCreate(services=services) |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 27 | |
| 28 | def CheckAssertBasePermissions( |
| 29 | self, restriction, expect_admin_ok, expect_nonadmin_ok): |
| 30 | old_project_creation_restriction = settings.project_creation_restriction |
| 31 | settings.project_creation_restriction = restriction |
| 32 | |
| 33 | # Anon users can never do it |
| 34 | mr = testing_helpers.MakeMonorailRequest( |
| 35 | perms=permissions.GetPermissions(None, {}, None)) |
| 36 | self.assertRaises( |
| 37 | permissions.PermissionException, |
| 38 | self.servlet.AssertBasePermission, mr) |
| 39 | |
| 40 | mr = testing_helpers.MakeMonorailRequest() |
| 41 | if expect_admin_ok: |
| 42 | self.servlet.AssertBasePermission(mr) |
| 43 | else: |
| 44 | self.assertRaises( |
| 45 | permissions.PermissionException, |
| 46 | self.servlet.AssertBasePermission, mr) |
| 47 | |
| 48 | mr = testing_helpers.MakeMonorailRequest( |
| 49 | perms=permissions.GetPermissions(mr.auth.user_pb, {111}, None)) |
| 50 | if expect_nonadmin_ok: |
| 51 | self.servlet.AssertBasePermission(mr) |
| 52 | else: |
| 53 | self.assertRaises( |
| 54 | permissions.PermissionException, |
| 55 | self.servlet.AssertBasePermission, mr) |
| 56 | |
| 57 | settings.project_creation_restriction = old_project_creation_restriction |
| 58 | |
| 59 | def testAssertBasePermission(self): |
| 60 | self.CheckAssertBasePermissions( |
| 61 | site_pb2.UserTypeRestriction.ANYONE, True, True) |
| 62 | self.CheckAssertBasePermissions( |
| 63 | site_pb2.UserTypeRestriction.ADMIN_ONLY, True, False) |
| 64 | self.CheckAssertBasePermissions( |
| 65 | site_pb2.UserTypeRestriction.NO_ONE, False, False) |
| 66 | |
| 67 | def testGatherPageData(self): |
| 68 | mr = testing_helpers.MakeMonorailRequest() |
| 69 | page_data = self.servlet.GatherPageData(mr) |
| 70 | self.assertEqual('', page_data['initial_name']) |
| 71 | self.assertEqual('', page_data['initial_summary']) |
| 72 | self.assertEqual('', page_data['initial_description']) |
| 73 | self.assertEqual([], page_data['labels']) |