blob: 54809f1cecf8f8b836e3835a761262c72640273e [file] [log] [blame]
# Copyright 2016 The Chromium Authors. All rights reserved.
# Use of this source code is govered by a BSD-style
# license that can be found in the LICENSE file or at
# https://developers.google.com/open-source/licenses/bsd
"""Unit tests for the componentcreate servlet."""
import unittest
from framework import permissions
from services import service_manager
from testing import fake
from testing import testing_helpers
from tracker import componentcreate
from tracker import tracker_bizobj
import webapp2
class ComponentCreateTest(unittest.TestCase):
def setUp(self):
self.services = service_manager.Services(
user=fake.UserService(),
config=fake.ConfigService(),
project=fake.ProjectService())
self.servlet = componentcreate.ComponentCreate(
'req', 'res', services=self.services)
self.project = self.services.project.TestAddProject('proj')
self.mr = testing_helpers.MakeMonorailRequest(
project=self.project, perms=permissions.OWNER_ACTIVE_PERMISSIONSET)
self.mr.auth.email = 'b@example.com'
self.config = self.services.config.GetProjectConfig(
'fake cnxn', self.project.project_id)
self.services.config.StoreConfig('fake cnxn', self.config)
self.cd = tracker_bizobj.MakeComponentDef(
1, self.project.project_id, 'BackEnd', 'doc', False, [], [111L], 0,
122L)
self.config.component_defs = [self.cd]
self.services.user.TestAddUser('a@example.com', 111L)
self.services.user.TestAddUser('b@example.com', 122L)
def testAssertBasePermission(self):
# Anon users can never do it
self.mr.perms = permissions.READ_ONLY_PERMISSIONSET
self.assertRaises(
permissions.PermissionException,
self.servlet.AssertBasePermission, self.mr)
# Project owner can do it.
self.mr.perms = permissions.OWNER_ACTIVE_PERMISSIONSET
self.servlet.AssertBasePermission(self.mr)
# Project member cannot do it
self.mr.perms = permissions.COMMITTER_ACTIVE_PERMISSIONSET
self.assertRaises(
permissions.PermissionException,
self.servlet.AssertBasePermission, self.mr)
self.mr.perms = permissions.CONTRIBUTOR_ACTIVE_PERMISSIONSET
self.assertRaises(
permissions.PermissionException,
self.servlet.AssertBasePermission, self.mr)
def testGatherPageData_CreatingAtTopLevel(self):
page_data = self.servlet.GatherPageData(self.mr)
self.assertEqual(self.servlet.PROCESS_TAB_COMPONENTS,
page_data['admin_tab_mode'])
self.assertIsNone(page_data['parent_path'])
def testGatherPageData_CreatingASubComponent(self):
self.mr.component_path = 'BackEnd'
page_data = self.servlet.GatherPageData(self.mr)
self.assertEqual(self.servlet.PROCESS_TAB_COMPONENTS,
page_data['admin_tab_mode'])
self.assertEqual('BackEnd', page_data['parent_path'])
def testProcessFormData_NotFound(self):
post_data = fake.PostData(
parent_path=['Monitoring'],
leaf_name=['Rules'],
docstring=['Detecting outages'],
deprecated=[False],
admins=[''],
cc=[''])
self.assertRaises(
webapp2.HTTPException,
self.servlet.ProcessFormData, self.mr, post_data)
def testProcessFormData_Normal(self):
post_data = fake.PostData(
parent_path=['BackEnd'],
leaf_name=['DB'],
docstring=['A database'],
deprecated=[False],
admins=[''],
cc=[''])
url = self.servlet.ProcessFormData(self.mr, post_data)
self.assertTrue('/adminComponents?saved=1&' in url)
config = self.services.config.GetProjectConfig(
self.mr.cnxn, self.mr.project_id)
cd = tracker_bizobj.FindComponentDef('BackEnd>DB', config)
self.assertEqual('BackEnd>DB', cd.path)
self.assertEqual('A database', cd.docstring)
self.assertEqual([], cd.admin_ids)
self.assertEqual([], cd.cc_ids)
self.assertTrue(cd.created > 0)
self.assertEqual(122L, cd.creator_id)
class CheckComponentNameJSONTest(unittest.TestCase):
def setUp(self):
self.services = service_manager.Services(
config=fake.ConfigService(),
project=fake.ProjectService())
self.servlet = componentcreate.CheckComponentNameJSON(
'req', 'res', services=self.services)
self.project = self.services.project.TestAddProject('proj')
self.config = self.services.config.GetProjectConfig(
'fake cnxn', self.project.project_id)
self.cd = tracker_bizobj.MakeComponentDef(
1, self.project.project_id, 'BackEnd', 'doc', False, [], [111L], 0,
122L)
self.config.component_defs = [self.cd]
self.services.config.StoreConfig('fake cnxn', self.config)
def testHandleRequest_NewComponent(self):
mr = testing_helpers.MakeMonorailRequest(
project=self.project, perms=permissions.OWNER_ACTIVE_PERMISSIONSET,
path='/p/proj/components/checkname?leaf_name=DB')
page_data = self.servlet.HandleRequest(mr)
self.assertItemsEqual(['error_message'], page_data.keys())
self.assertIsNone(page_data['error_message'])
def testHandleRequest_NameAlreadyUsed(self):
mr = testing_helpers.MakeMonorailRequest(
project=self.project, perms=permissions.OWNER_ACTIVE_PERMISSIONSET,
path='/p/proj/components/checkname?leaf_name=BackEnd')
page_data = self.servlet.HandleRequest(mr)
self.assertItemsEqual(['error_message'], page_data.keys())
self.assertEqual('That name is already in use.',
page_data['error_message'])
class ComponentCreateMethodsTest(unittest.TestCase):
def setUp(self):
self.config = tracker_bizobj.MakeDefaultProjectIssueConfig(789)
cd1 = tracker_bizobj.MakeComponentDef(
1, 789, 'BackEnd', 'doc', False, [], [111L], 0, 122L)
cd2 = tracker_bizobj.MakeComponentDef(
2, 789, 'BackEnd>DB', 'doc', True, [], [111L], 0, 122L)
self.config.component_defs = [cd1, cd2]
def testLeafNameErrorMessage_Invalid(self):
self.assertEqual(
'Invalid component name',
componentcreate.LeafNameErrorMessage('', 'bad name', self.config))
def testLeafNameErrorMessage_AlreadyInUse(self):
self.assertEqual(
'That name is already in use.',
componentcreate.LeafNameErrorMessage('', 'BackEnd', self.config))
self.assertEqual(
'That name is already in use.',
componentcreate.LeafNameErrorMessage('BackEnd', 'DB', self.config))
def testLeafNameErrorMessage_OK(self):
self.assertIsNone(
componentcreate.LeafNameErrorMessage('', 'FrontEnd', self.config))
self.assertIsNone(
componentcreate.LeafNameErrorMessage('BackEnd', 'Search', self.config))
if __name__ == '__main__':
unittest.main()