blob: a79b9df640a0a93d23d6b424341d6ebc5cf03740 [file] [log] [blame]
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +01001# 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.
Copybara854996b2021-09-07 19:36:02 +00004
5"""Unittests for the Project Creation servlet."""
6from __future__ import print_function
7from __future__ import division
8from __future__ import absolute_import
9
10import unittest
11
12import settings
13from framework import permissions
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +010014from mrproto import project_pb2
15from mrproto import site_pb2
Copybara854996b2021-09-07 19:36:02 +000016from services import service_manager
17from sitewide import projectcreate
18from testing import fake
19from testing import testing_helpers
20
21
22class ProjectCreateTest(unittest.TestCase):
23
24 def setUp(self):
25 services = service_manager.Services()
Adrià Vilanova Martínez9f9ade52022-10-10 23:20:11 +020026 self.servlet = projectcreate.ProjectCreate(services=services)
Copybara854996b2021-09-07 19:36:02 +000027
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'])