OLD | NEW |
(Empty) | |
| 1 # Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is govered 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 the fielddetail servlet.""" |
| 7 |
| 8 import unittest |
| 9 |
| 10 import webapp2 |
| 11 |
| 12 from framework import permissions |
| 13 from proto import project_pb2 |
| 14 from proto import tracker_pb2 |
| 15 from services import service_manager |
| 16 from testing import fake |
| 17 from testing import testing_helpers |
| 18 from tracker import fielddetail |
| 19 from tracker import tracker_bizobj |
| 20 |
| 21 |
| 22 class FieldDetailTest(unittest.TestCase): |
| 23 |
| 24 def setUp(self): |
| 25 self.services = service_manager.Services( |
| 26 user=fake.UserService(), |
| 27 config=fake.ConfigService(), |
| 28 project=fake.ProjectService()) |
| 29 self.servlet = fielddetail.FieldDetail( |
| 30 'req', 'res', services=self.services) |
| 31 self.project = self.services.project.TestAddProject('proj') |
| 32 self.mr = testing_helpers.MakeMonorailRequest( |
| 33 project=self.project, perms=permissions.OWNER_ACTIVE_PERMISSIONSET) |
| 34 self.config = self.services.config.GetProjectConfig( |
| 35 'fake cnxn', self.project.project_id) |
| 36 self.services.config.StoreConfig('fake cnxn', self.config) |
| 37 self.fd = tracker_bizobj.MakeFieldDef( |
| 38 123, 789, 'CPU', tracker_pb2.FieldTypes.INT_TYPE, None, |
| 39 '', False, False, None, None, '', False, '', '', |
| 40 tracker_pb2.NotifyTriggers.NEVER, 'doc', False) |
| 41 self.config.field_defs.append(self.fd) |
| 42 self.mr.field_name = 'CPU' |
| 43 |
| 44 def testGetFieldDef_NotFound(self): |
| 45 self.mr.field_name = 'NeverHeardOfIt' |
| 46 self.assertRaises( |
| 47 webapp2.HTTPException, |
| 48 self.servlet._GetFieldDef, self.mr) |
| 49 |
| 50 def testGetFieldDef_Normal(self): |
| 51 actual_config, actual_fd = self.servlet._GetFieldDef(self.mr) |
| 52 self.assertEqual(self.config, actual_config) |
| 53 self.assertEqual(self.fd, actual_fd) |
| 54 |
| 55 def testAssertBasePermission_AnyoneCanView(self): |
| 56 self.servlet.AssertBasePermission(self.mr) |
| 57 self.mr.perms = permissions.COMMITTER_ACTIVE_PERMISSIONSET |
| 58 self.servlet.AssertBasePermission(self.mr) |
| 59 self.mr.perms = permissions.CONTRIBUTOR_ACTIVE_PERMISSIONSET |
| 60 self.servlet.AssertBasePermission(self.mr) |
| 61 self.mr.perms = permissions.READ_ONLY_PERMISSIONSET |
| 62 self.servlet.AssertBasePermission(self.mr) |
| 63 |
| 64 def testAssertBasePermission_MembersOnly(self): |
| 65 self.project.access = project_pb2.ProjectAccess.MEMBERS_ONLY |
| 66 # The project members can view the field definition. |
| 67 self.servlet.AssertBasePermission(self.mr) |
| 68 self.mr.perms = permissions.COMMITTER_ACTIVE_PERMISSIONSET |
| 69 self.servlet.AssertBasePermission(self.mr) |
| 70 self.mr.perms = permissions.CONTRIBUTOR_ACTIVE_PERMISSIONSET |
| 71 self.servlet.AssertBasePermission(self.mr) |
| 72 # Non-member is not allowed to view anything in the project. |
| 73 self.mr.perms = permissions.EMPTY_PERMISSIONSET |
| 74 self.assertRaises( |
| 75 permissions.PermissionException, |
| 76 self.servlet.AssertBasePermission, self.mr) |
| 77 |
| 78 def testGatherPageData_ReadWrite(self): |
| 79 page_data = self.servlet.GatherPageData(self.mr) |
| 80 self.assertEqual(self.servlet.PROCESS_TAB_LABELS, |
| 81 page_data['admin_tab_mode']) |
| 82 self.assertTrue(page_data['allow_edit']) |
| 83 self.assertEqual('', page_data['initial_admins']) |
| 84 field_def_view = page_data['field_def'] |
| 85 self.assertEqual('CPU', field_def_view.field_name) |
| 86 |
| 87 def testGatherPageData_ReadOnly(self): |
| 88 self.mr.perms = permissions.READ_ONLY_PERMISSIONSET |
| 89 page_data = self.servlet.GatherPageData(self.mr) |
| 90 self.assertEqual(self.servlet.PROCESS_TAB_LABELS, |
| 91 page_data['admin_tab_mode']) |
| 92 self.assertFalse(page_data['allow_edit']) |
| 93 self.assertEqual('', page_data['initial_admins']) |
| 94 field_def_view = page_data['field_def'] |
| 95 self.assertEqual('CPU', field_def_view.field_name) |
| 96 |
| 97 def testProcessFormData_Permission(self): |
| 98 """Only owners can edit fields.""" |
| 99 mr = testing_helpers.MakeMonorailRequest( |
| 100 project=self.project, |
| 101 perms=permissions.CONTRIBUTOR_ACTIVE_PERMISSIONSET) |
| 102 mr.field_name = 'CPU' |
| 103 post_data = fake.PostData( |
| 104 name=['CPU'], |
| 105 deletefield=['Submit']) |
| 106 self.assertRaises(permissions.PermissionException, |
| 107 self.servlet.ProcessFormData, mr, post_data) |
| 108 |
| 109 self.servlet.ProcessFormData(self.mr, post_data) |
| 110 |
| 111 def testProcessFormData_Delete(self): |
| 112 post_data = fake.PostData( |
| 113 name=['CPU'], |
| 114 deletefield=['Submit']) |
| 115 url = self.servlet.ProcessFormData(self.mr, post_data) |
| 116 self.assertTrue('/adminLabels?deleted=1&' in url) |
| 117 fd = tracker_bizobj.FindFieldDef('CPU', self.config) |
| 118 self.assertEqual('CPU', fd.field_name) |
| 119 self.assertTrue(fd.is_deleted) |
| 120 |
| 121 def testProcessFormData_Edit(self): |
| 122 post_data = fake.PostData( |
| 123 name=['CPU'], |
| 124 field_type=['INT_TYPE'], |
| 125 min_value=['2'], |
| 126 max_value=['98'], |
| 127 notify_on=['never'], |
| 128 is_required=[], |
| 129 is_multivalued=[], |
| 130 docstring=['It is just some field'], |
| 131 applicable_type=['Defect'], |
| 132 admin_names=['']) |
| 133 url = self.servlet.ProcessFormData(self.mr, post_data) |
| 134 self.assertTrue('/fields/detail?field=CPU&saved=1&' in url) |
| 135 config = self.services.config.GetProjectConfig( |
| 136 self.mr.cnxn, self.mr.project_id) |
| 137 |
| 138 fd = tracker_bizobj.FindFieldDef('CPU', config) |
| 139 self.assertEqual('CPU', fd.field_name) |
| 140 self.assertEqual(2, fd.min_value) |
| 141 self.assertEqual(98, fd.max_value) |
| 142 |
| 143 def testProcessDeleteField(self): |
| 144 self.servlet._ProcessDeleteField(self.mr, self.fd) |
| 145 self.assertTrue(self.fd.is_deleted) |
| 146 |
| 147 def testProcessEditField(self): |
| 148 post_data = fake.PostData( |
| 149 name=['CPU'], field_type=['INT_TYPE'], min_value=['2'], |
| 150 admin_names=['']) |
| 151 self.servlet._ProcessEditField( |
| 152 self.mr, post_data, self.config, self.fd) |
| 153 fd = tracker_bizobj.FindFieldDef('CPU', self.config) |
| 154 self.assertEqual('CPU', fd.field_name) |
| 155 self.assertEqual(2, fd.min_value) |
| 156 |
| 157 |
| 158 if __name__ == '__main__': |
| 159 unittest.main() |
OLD | NEW |