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 projectadminadvanced module.""" |
| 6 | from __future__ import print_function |
| 7 | from __future__ import division |
| 8 | from __future__ import absolute_import |
| 9 | |
| 10 | import time |
| 11 | import unittest |
| 12 | from mock import patch |
| 13 | |
| 14 | from framework import permissions |
| 15 | from project import projectadminadvanced |
Adrià Vilanova Martínez | f19ea43 | 2024-01-23 20:20:52 +0100 | [diff] [blame] | 16 | from mrproto import project_pb2 |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 17 | from services import service_manager |
| 18 | from testing import fake |
| 19 | from testing import testing_helpers |
| 20 | |
| 21 | NOW = 1277762224 |
| 22 | |
| 23 | |
| 24 | class ProjectAdminAdvancedTest(unittest.TestCase): |
| 25 | """Unit tests for the ProjectAdminAdvanced servlet class.""" |
| 26 | |
| 27 | def setUp(self): |
| 28 | services = service_manager.Services( |
| 29 | project=fake.ProjectService()) |
Adrià Vilanova Martínez | f19ea43 | 2024-01-23 20:20:52 +0100 | [diff] [blame] | 30 | self.servlet = projectadminadvanced.ProjectAdminAdvanced(services=services) |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 31 | self.project = services.project.TestAddProject('proj', owner_ids=[111]) |
| 32 | self.mr = testing_helpers.MakeMonorailRequest( |
| 33 | project=self.project, |
| 34 | perms=permissions.OWNER_ACTIVE_PERMISSIONSET, |
| 35 | user_info={'user_id': 111}) |
| 36 | |
| 37 | def testAssertBasePermission(self): |
| 38 | # Signed-out users cannot edit the project |
| 39 | self.mr.perms = permissions.READ_ONLY_PERMISSIONSET |
| 40 | self.assertRaises(permissions.PermissionException, |
| 41 | self.servlet.AssertBasePermission, self.mr) |
| 42 | |
| 43 | # Non-member users cannot edit the project |
| 44 | self.mr.perms = permissions.USER_PERMISSIONSET |
| 45 | self.assertRaises(permissions.PermissionException, |
| 46 | self.servlet.AssertBasePermission, self.mr) |
| 47 | |
| 48 | # Contributors cannot edit the project |
| 49 | self.mr.perms = permissions.CONTRIBUTOR_ACTIVE_PERMISSIONSET |
| 50 | self.assertRaises( |
| 51 | permissions.PermissionException, |
| 52 | self.servlet.AssertBasePermission, self.mr) |
| 53 | |
| 54 | self.mr.perms = permissions.OWNER_ACTIVE_PERMISSIONSET |
| 55 | self.servlet.AssertBasePermission(self.mr) |
| 56 | |
| 57 | def testGatherPageData(self): |
| 58 | page_data = self.servlet.GatherPageData(self.mr) |
| 59 | self.assertEqual(self.servlet.ADMIN_TAB_ADVANCED, |
| 60 | page_data['admin_tab_mode']) |
| 61 | |
| 62 | def testGatherPublishingOptions_Live(self): |
| 63 | pub_data = self.servlet._GatherPublishingOptions(self.mr) |
| 64 | self.assertTrue(pub_data['offer_archive']) |
| 65 | self.assertTrue(pub_data['offer_move']) |
| 66 | self.assertFalse(pub_data['offer_publish']) |
| 67 | self.assertFalse(pub_data['offer_delete']) |
| 68 | self.assertEqual('http://', pub_data['moved_to']) |
| 69 | |
| 70 | def testGatherPublishingOptions_Moved(self): |
| 71 | self.project.moved_to = 'other location' |
| 72 | pub_data = self.servlet._GatherPublishingOptions(self.mr) |
| 73 | self.assertTrue(pub_data['offer_archive']) |
| 74 | self.assertTrue(pub_data['offer_move']) |
| 75 | self.assertFalse(pub_data['offer_publish']) |
| 76 | self.assertFalse(pub_data['offer_delete']) |
| 77 | self.assertEqual('other location', pub_data['moved_to']) |
| 78 | |
| 79 | def testGatherPublishingOptions_Archived(self): |
| 80 | self.project.state = project_pb2.ProjectState.ARCHIVED |
| 81 | pub_data = self.servlet._GatherPublishingOptions(self.mr) |
| 82 | self.assertFalse(pub_data['offer_archive']) |
| 83 | self.assertFalse(pub_data['offer_move']) |
| 84 | self.assertTrue(pub_data['offer_publish']) |
| 85 | self.assertTrue(pub_data['offer_delete']) |
| 86 | |
| 87 | def testGatherPublishingOptions_Doomed(self): |
| 88 | self.project.state = project_pb2.ProjectState.ARCHIVED |
| 89 | self.project.state_reason = 'you are a spammer' |
| 90 | pub_data = self.servlet._GatherPublishingOptions(self.mr) |
| 91 | self.assertFalse(pub_data['offer_archive']) |
| 92 | self.assertFalse(pub_data['offer_move']) |
| 93 | self.assertFalse(pub_data['offer_publish']) |
| 94 | self.assertTrue(pub_data['offer_delete']) |
| 95 | |
| 96 | def testGatherQuotaData(self): |
| 97 | self.mr.perms = permissions.OWNER_ACTIVE_PERMISSIONSET |
| 98 | quota_data = self.servlet._GatherQuotaData(self.mr) |
| 99 | self.assertFalse(quota_data['offer_quota_editing']) |
| 100 | |
| 101 | self.mr.perms = permissions.ADMIN_PERMISSIONSET |
| 102 | quota_data = self.servlet._GatherQuotaData(self.mr) |
| 103 | self.assertTrue(quota_data['offer_quota_editing']) |
| 104 | |
| 105 | def testBuildComponentQuota(self): |
| 106 | ezt_item = self.servlet._BuildComponentQuota( |
| 107 | 5000, 10000, 'attachments') |
| 108 | self.assertEqual(50, ezt_item.used_percent) |
| 109 | self.assertEqual('attachments', ezt_item.field_name) |
| 110 | |
| 111 | @patch('time.time') |
| 112 | def testProcessFormData_NotDeleted(self, mock_time): |
| 113 | mock_time.return_value = NOW |
| 114 | self.mr.project_name = 'proj' |
| 115 | post_data = fake.PostData() |
| 116 | next_url = self.servlet.ProcessFormData(self.mr, post_data) |
| 117 | self.assertEqual( |
| 118 | 'http://127.0.0.1/p/proj/adminAdvanced?saved=1&ts=%s' % NOW, |
| 119 | next_url) |
| 120 | |
| 121 | def testProcessFormData_AfterDeletion(self): |
| 122 | self.mr.project_name = 'proj' |
| 123 | self.project.state = project_pb2.ProjectState.ARCHIVED |
| 124 | post_data = fake.PostData(deletebtn='1') |
| 125 | next_url = self.servlet.ProcessFormData(self.mr, post_data) |
| 126 | self.assertEqual('http://127.0.0.1/hosting_old/', next_url) |