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