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