blob: 13dc97b262b9cff0c763e3e5405d3c2f13fda9cc [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"""Unittests for the Project Creation servlet."""
7from __future__ import print_function
8from __future__ import division
9from __future__ import absolute_import
10
11import unittest
12
13import settings
14from framework import permissions
15from proto import project_pb2
16from proto import site_pb2
17from services import service_manager
18from sitewide import projectcreate
19from testing import fake
20from testing import testing_helpers
21
22
23class ProjectCreateTest(unittest.TestCase):
24
25 def setUp(self):
26 services = service_manager.Services()
Adrià Vilanova Martínez9f9ade52022-10-10 23:20:11 +020027 self.servlet = projectcreate.ProjectCreate(services=services)
Copybara854996b2021-09-07 19:36:02 +000028
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'])