def main(): parser = build_parser() args = parser.parse_args() check_arguments(args) # Overall setup pattern taken from: # http://docs.cherrypy.org/en/latest/config.html # Set up global site configuration cherrypy.config.update(args.config) controller = controllers.MainController() # Mount the app and pass it its own configuration application = cherrypy.tree.mount( controller, "/slugs", config=args.config ) plugins.FileMonitoringPlugin( cherrypy.engine, application.config.get('data').get('user_group_mapping'), controller.update ).subscribe() if hasattr(cherrypy.engine, 'block'): # CherryPy 3.1 syntax cherrypy.engine.start() cherrypy.engine.block() else: # CherryPy 3.0 syntax cherrypy.server.quickstart() cherrypy.engine.start()
def test_index(self): """ Test that a MainController can process a general index query without error. """ controller = controllers.MainController() controller._users = mock.MagicMock(spec=controllers.UsersController) controller._groups = mock.MagicMock(spec=controllers.GroupsController) controller._users.list.return_value = ['Adam', 'Eve'] controller._groups.list.return_value = ['Male', 'Female', 'Human'] results = controller.index() self.assertIsInstance(results, dict) self.assertEqual(2, len(results.keys())) self.assertIn('users', results.keys()) self.assertIn('groups', results.keys()) self.assertEqual(2, len(results.get('users'))) self.assertIn('Adam', results.get('users')) self.assertIn('Eve', results.get('users')) self.assertEqual(3, len(results.get('groups'))) self.assertIn('Male', results.get('groups')) self.assertIn('Female', results.get('groups')) self.assertIn('Human', results.get('groups'))
def test_cp_dispatch_level_three(self): """ Test that MainController dispatching routes a Level 3 query to the right object controller with the right request parameters. """ controller = controllers.MainController() users_mock = mock.MagicMock(spec=controllers.UsersController) groups_mock = mock.MagicMock(spec=controllers.GroupsController) controller._users = users_mock controller._groups = groups_mock result = controller._cp_dispatch(['users', 'Adam', 'groups']) self.assertEqual(result, users_mock) self.assertEqual(cherrypy.request.params['user'], 'Adam') self.assertEqual(cherrypy.request.params['groups'], True) args = [['users', 'Adam', 'invalid']] self.assertRaisesRegex(cherrypy.HTTPError, "User attribute not found.", controller._cp_dispatch, *args) result = controller._cp_dispatch(['groups', 'Human', 'users']) self.assertEqual(result, groups_mock) self.assertEqual(cherrypy.request.params['group'], 'Human') self.assertEqual(cherrypy.request.params['users'], True) args = [['groups', 'Human', 'invalid']] self.assertRaisesRegex(cherrypy.HTTPError, "Group attribute not found.", controller._cp_dispatch, *args)
def test_init(self): """ Test that a MainController can be built without error. """ controller = controllers.MainController() self.assertIsInstance(controller._users, controllers.UsersController) self.assertIsInstance(controller._groups, controllers.GroupsController)
def test_update_with_data(self): """ Test that a MainController can be updated with data without error. """ controller = controllers.MainController() controller._users = mock.MagicMock(spec=controllers.UsersController) controller._groups = mock.MagicMock(spec=controllers.GroupsController) controller.update('test') controller._users.update.assert_called_once_with('test') controller._groups.update.assert_called_once_with('test')
def test_cp_dispatch_invalid_query(self): """ Test that MainController dispatching handles an invalid query correctly. """ controller = controllers.MainController() users_mock = mock.MagicMock(spec=controllers.UsersController) groups_mock = mock.MagicMock(spec=controllers.GroupsController) controller._users = users_mock controller._groups = groups_mock args = [['groups', 'Human', 'users', 'Adam', 'invalid']] self.assertRaisesRegex(cherrypy.HTTPError, "Resource not found.", controller._cp_dispatch, *args)
def test_cp_dispatch_level_two(self): """ Test that MainController dispatching routes a Level 2 query to the right object controller with the right request parameters. """ controller = controllers.MainController() users_mock = mock.MagicMock(spec=controllers.UsersController) groups_mock = mock.MagicMock(spec=controllers.GroupsController) controller._users = users_mock controller._groups = groups_mock result = controller._cp_dispatch(['users', 'Adam']) self.assertEqual(result, users_mock) self.assertEqual(cherrypy.request.params['user'], 'Adam') result = controller._cp_dispatch(['groups', 'Human']) self.assertEqual(result, groups_mock) self.assertEqual(cherrypy.request.params['group'], 'Human')
def test_cp_dispatch_level_one(self): """ Test that MainController dispatching routes a Level 1 query to the right object controller """ controller = controllers.MainController() users_mock = mock.MagicMock(spec=controllers.UsersController) groups_mock = mock.MagicMock(spec=controllers.GroupsController) controller._users = users_mock controller._groups = groups_mock result = controller._cp_dispatch(['users']) self.assertEqual(result, users_mock) result = controller._cp_dispatch(['groups']) self.assertEqual(result, groups_mock) args = [['invalid']] self.assertRaisesRegex(cherrypy.HTTPError, "Collection not found.", controller._cp_dispatch, *args)