def test_custom_route_object_creation(self): test_methods = ['POST', 'HEAD', 'FOO', 'BAR'] test_response_type = 'foobar' test_headers = { 'Content-Type': 'text/html', 'Foo-Bar': 'not-even-exist' } route = Route( self.route_name, self.route_url, response_type=test_response_type, headers=test_headers, methods=test_methods, url_prefix='/test' ) self.assertEqual(route.name, self.route_name) self.assertEqual(route.url, self.route_url) self.assertEqual(route.response_type, test_response_type) self.assertIsInstance(route.headers, dict) self.assertEqual(len(route.headers), len(test_headers)) for h in test_headers: self.assertIn(h, route.headers) self.assertIsInstance(route.methods, list) self.assertEqual(len(route.methods), len(test_methods)) for m in test_methods: self.assertIn(m, route.methods)
def test_method_add_route(self): route_manager = RouteManager() self.assertEqual(route_manager.ROUTES, {}) # Route #1 is added route_1 = Route('test_route_1', '/api/v1/test/route-1/') route_manager.add_route(route_1) self.assertEqual(len(route_manager.ROUTES), 1) self.assertEqual(route_1.name, 'test_route_1') self.assertIn(route_1.name, route_manager.ROUTES.keys()) # Route #2 - with a DIFFERENT name - is added route_2 = Route('test_route_2', '/api/v1/test/route-2/') route_manager.add_route(route_2) self.assertEqual(len(route_manager.ROUTES), 2) self.assertEqual(route_2.name, 'test_route_2') self.assertIn(route_2.name, route_manager.ROUTES.keys()) # Route #3 - with the SAME name of Route #2 - is added route_3 = Route('test_route_2', '/api/v1/test/route-3/') self.assertEqual(route_3.name, 'test_route_2') with self.assertRaises(KeyError): route_manager.add_route(route_3) self.assertEqual(len(route_manager.ROUTES), 2) self.assertIsNot(route_3, route_manager.ROUTES['test_route_2']) # Route #3 - with the SAME name of Route #2 - is added AGAIN & AGAIN for i in range(10): with self.assertRaises(KeyError): route_manager.add_route(route_3) self.assertEqual(len(route_manager.ROUTES), 2) self.assertIsNot(route_3, route_manager.ROUTES['test_route_2']) # Route #4 - with a DIFFERENT name - is added route_4 = Route('test_route_4', '/api/v1/test/route-4/') route_manager.add_route(route_4) self.assertEqual(len(route_manager.ROUTES), 3) self.assertEqual(route_4.name, 'test_route_4') self.assertIn(route_4.name, route_manager.ROUTES.keys())
def test_default_route_object_creation(self): route = Route(self.route_name, self.route_url) self.assertEqual(route.name, self.route_name) self.assertEqual(route.url, self.route_url) self.assertIsInstance(route.methods, list) self.assertEqual(len(route.methods), 1) self.assertIn('GET', route.methods) self.assertEqual(route.response_type, 'json') self.assertIsInstance(route.headers, dict) self.assertEqual(len(route.headers), 0)
class TestRouteObjectInternalMethodSplitURL(openerp.tests.TransactionCase): def setUp(self): super(TestRouteObjectInternalMethodSplitURL, self).setUp() self.route = Route('test_route', '') def test_internal_method_split_url_with_no_trailing_slash(self): url = '/api/v1/test/url' split_url = self.route._split_url(url) self.assertIsInstance(split_url, list) self.assertEqual(len(split_url), 4) self.assertIn('api', split_url) self.assertIn('v1', split_url) self.assertIn('test', split_url) self.assertIn('url', split_url) def test_internal_method_split_url_with_trailing_slash(self): url = '/api/v1/url/' split_url = self.route._split_url(url) self.assertIsInstance(split_url, list) self.assertEqual(len(split_url), 3) self.assertIn('api', split_url) self.assertIn('v1', split_url) self.assertIn('url', split_url) def test_internal_method_split_url_with_multiple_slashes(self): url = '/api//v1/test///url///split' split_url = self.route._split_url(url) self.assertIsInstance(split_url, list) self.assertEqual(len(split_url), 5) self.assertIn('api', split_url) self.assertIn('v1', split_url) self.assertIn('test', split_url) self.assertIn('url', split_url) self.assertIn('split', split_url) def test_internal_method_split_url_with_no_string_argument(self): url = 56 self.assertIsInstance(url, int) with self.assertRaises(ValueError): self.route._split_url(url)
def _generate_route_objects(number=1): """Internal utility method to generate multiple Route objects. :param number: Number of Route objects to generate :return: List of Route objects """ route_list = [] for n in range(int(number)): r = Route( 'test_route_{}'.format(n), 'api/v1/test/resource_{}'.format(n) ) route_list.append(r) return route_list # TODO: return a generator instead ?
def test_method_get_route(self): route_manager = RouteManager() route_name_list = ['route_{}'.format(n) for n in range(10)] for route_name in route_name_list: r = Route(route_name, '/api/v1/test/') route_manager.add_route(r) self.assertIn(route_name, route_manager.ROUTES.keys()) self.assertEqual(len(route_manager.ROUTES), len(route_name_list)) for route_name in route_name_list: r = route_manager.get_route(route_name) self.assertIsInstance(r, Route) self.assertEqual(r.name, route_name) not_existing_route = route_manager.get_route('non_existing_route_name') self.assertNotIsInstance(not_existing_route, Route) self.assertIsNone(not_existing_route)
def test_get_javascript_routes_passing_two_url_prefixes(self): diffr_prefix_route = Route( 'prefix', '/prefix/', url_prefix='/test/url/' ) self.route_manager.add_route(diffr_prefix_route) r_list = self.all_route_list js_string = self.route_manager.get_javascript_routes( self.name_of_template, self.path_to_template, route_list=r_list ) # need to make sure url prefix is done properly for r in r_list: self.assertIn( r.name, js_string, 'Route object "{}" was not rendered in the template.'.format( r.name ) )
def setUp(self): super(TestGetArgumentsFromRouteURL, self).setUp() self.route = Route('test_route', '') self.assertFalse(self.route.args)
class TestGetArgumentsFromRouteURL(openerp.tests.TransactionCase): def setUp(self): super(TestGetArgumentsFromRouteURL, self).setUp() self.route = Route('test_route', '') self.assertFalse(self.route.args) def test_get_args_from_url_with_no_arguments(self): url = '/api/v1/patients/' args = self.route._get_args(url) self.assertNotIsInstance(args, list) self.assertEqual(args, False) def test_get_args_from_url_with_one_single_argument(self): url = '/api/v1/patients/<patient_id>/' args = self.route._get_args(url) self.assertEqual(len(args), 1) self.assertIn('patient_id', args) def test_get_args_from_url_with_multiple_arguments(self): url = '/api/v1/patients/<patient_id>/observation/<observation_id>/' args = self.route._get_args(url) self.assertEqual(len(args), 2) self.assertIn('patient_id', args) self.assertNotIn('<patient_id>', args) self.assertIn('observation_id', args) self.assertNotIn('<observation_id>', args) def test_get_args_from_url_with_two_consecutive_arguments(self): url = '/api/v1/patient/submit_ajax/<observation_type>/<patient_id>/' args = self.route._get_args(url) self.assertEqual(len(args), 2) self.assertIn('patient_id', args) self.assertNotIn('<patient_id>', args) self.assertIn('observation_type', args) self.assertNotIn('<observation_type>', args) def test_get_args_from_url_with_multiple_consecutive_arguments(self): url = '/api/v1/<multiple>/<consecutive_parameters>/' \ '<in_this>/<url>/test/' args = self.route._get_args(url) self.assertEqual(len(args), 4) self.assertIn('multiple', args) self.assertNotIn('<multiple>', args) self.assertIn('consecutive_parameters', args) self.assertNotIn('<consecutive_parameters>', args) self.assertIn('in_this', args) self.assertNotIn('<in_this>', args) self.assertIn('url', args) self.assertNotIn('<url>', args) def test_get_args_from_url_with_wrong_written_arguments(self): url = '/api/v1/patients/<_id>/observation/<observation_id_>/' args = self.route._get_args(url) self.assertNotIsInstance(args, list) self.assertEqual(args, False) def test_get_args_from_url_with_mix_cased_arguments(self): url = '/api/v1/patients/<paTieNT_ID>/observation/<obSERvatioN_iD>/' args = self.route._get_args(url) self.assertEqual(len(args), 2) self.assertIn('paTieNT_ID', args) self.assertNotIn('<paTieNT_ID>', args) self.assertIn('obSERvatioN_iD', args) self.assertNotIn('<obSERvatioN_iD>', args) def test_get_args_from_url_with_arguments_having_digits(self): url = '/api/v1/patients/<pa7ien7_id>/observation/<ob5ervation_id>/' args = self.route._get_args(url) self.assertNotIsInstance(args, list) self.assertEqual(args, False) def test_get_args_from_url_with_args_having_underscores_and_hyphens(self): url = '/api/v1/patients/<patient_id>/observation/<observation-id>/' args = self.route._get_args(url) self.assertEqual(len(args), 2) self.assertIn('patient_id', args) self.assertNotIn('<patient_id>', args) self.assertIn('observation-id', args) self.assertNotIn('<observation-id>', args) def test_get_args_from_url_with_args_having___and_hyphens_no_chevron(self): url = '/api/v1/patients/patient_id/observation/observation-id/' args = self.route._get_args(url) self.assertNotIsInstance(args, list) self.assertEqual(args, False) def test_get_args_from_url_with_no_trailing_slash(self): url = '/api/v1/patients/<patient_id>/observation/<observation_id>' args = self.route._get_args(url) self.assertEqual(len(args), 2) self.assertIn('patient_id', args) self.assertNotIn('<patient_id>', args) self.assertIn('observation_id', args) self.assertNotIn('<observation_id>', args) def test_get_args_from_url_with_every_possible_valid_combination(self): url = '/api/v1/patients/<Patient_strange-ID>/' \ 'observation/<obSERvation_typE>/<obserVATion-very_Strange_Id>' args = self.route._get_args(url) self.assertEqual(len(args), 3) self.assertIn('obSERvation_typE', args) self.assertNotIn('<obSERvation_typE>', args) self.assertIn('Patient_strange-ID', args) self.assertNotIn('<Patient_strange-ID>', args) self.assertIn('obserVATion-very_Strange_Id', args) self.assertNotIn('<obserVATion-very-Strange_Id>', args)
def setUp(self): super(TestRouteObjectGetURLComponents, self).setUp() self.route = Route('test_route', '')
class TestRouteObjectGetURLComponents(openerp.tests.TransactionCase): def setUp(self): super(TestRouteObjectGetURLComponents, self).setUp() self.route = Route('test_route', '') def test_get_url_components_from_url_with_no_argument(self): url = '/api/v1/patients/' # Create a generator to check each different components of # the URL in the assertions below name_check_generator = (t for t in ['api', 'v1', 'patients']) # Fetch the components list from the method under test components_list = self.route._get_url_components(url) self.assertEqual(len(components_list), 3) for c in components_list: self.assertIsInstance(c, dict) self.assertEqual(len(c), 2) self.assertIn('type', c) self.assertIn('name', c) self.assertEqual(c['type'], 'string') self.assertEqual(c['name'], name_check_generator.next()) def test_get_url_components_from_url_with_single_argument(self): url = '/api/v1/patients/<id>' # Create generators to check each different components of # the URL in the assertions below type_check_generator = (t for t in ['string', 'string', 'string', 'func']) name_check_generator = (n for n in ['api', 'v1', 'patients', 'id']) # Fetch the components list from the method under test components_list = self.route._get_url_components(url) self.assertEqual(len(components_list), 4) for c in components_list: self.assertIsInstance(c, dict) self.assertEqual(len(c), 2) self.assertIn('type', c) self.assertIn('name', c) self.assertEqual(c['type'], type_check_generator.next()) self.assertEqual(c['name'], name_check_generator.next()) def test_get_url_components_from_url_with_multiple_arguments(self): url = '/api/<version_number>/location/<ward_code>/<bed_number>/' # Create generators to check each different components of the # URL in the assertions below type_check_generator = (t for t in ['string', 'func', 'string', 'func', 'func']) name_check_generator = (n for n in ['api', 'version_number', 'location', 'ward_code', 'bed_number']) # Fetch the components list from the method under test components_list = self.route._get_url_components(url) self.assertEqual(len(components_list), 5) for c in components_list: self.assertIsInstance(c, dict) self.assertEqual(len(c), 2) self.assertIn('type', c) self.assertIn('name', c) self.assertEqual(c['type'], type_check_generator.next()) self.assertEqual(c['name'], name_check_generator.next()) def test_get_url_components_from_url_with_multiple_slashes(self): url = 'api///<version_number>/location/<ward_code>///<bed_number>//' # Create generators to check each different components of the URL # in the assertions below type_check_generator = (t for t in ['string', 'func', 'string', 'func', 'func']) name_check_generator = (n for n in ['api', 'version_number', 'location', 'ward_code', 'bed_number']) # Fetch the components list from the method under test components_list = self.route._get_url_components(url) self.assertEqual(len(components_list), 5) for c in components_list: self.assertIsInstance(c, dict) self.assertEqual(len(c), 2) self.assertIn('type', c) self.assertIn('name', c) self.assertEqual(c['type'], type_check_generator.next()) self.assertEqual(c['name'], name_check_generator.next())
def setUp(self): super(TestRouteObjectInternalMethodSplitURL, self).setUp() self.route = Route('test_route', '')
###################### # ROUTING SYSTEM TESTS ###################### # Define server's routing constants to be used for the routing tests SERVER_PROTOCOL = "http" SERVER_ADDRESS = "localhost" SERVER_PORT = "{0}".format(config['xmlrpc_port']) MOBILE_URL_PREFIX = '/mobile' BASE_URL = SERVER_PROTOCOL + '://' + SERVER_ADDRESS + ':' + SERVER_PORT BASE_MOBILE_URL = BASE_URL + MOBILE_URL_PREFIX # Create the RouteManager and the Route objects for the tests route_manager_test = RouteManager() no_args_route = Route('no_args_route', '/no/args/route/', auth='none') no_args_route_only_post = Route( 'no_args_route_only_post', '/no/args/route/post/', auth='none', methods=['POST'] ) no_args_route_auth_as_user = Route( 'no_args_route_auth_as_user', '/no/args/route/auth/user/', auth='user' ) single_arg_route = Route( 'single_arg_route', '/single/arg/<arg_id>/',
from openerp import http from openerp.addons.nh_eobs_api.routing import ResponseJSON from openerp.addons.nh_eobs_api.routing import Route from openerp.addons.nh_eobs_api.routing import RouteManager from openerp.http import request from openerp.modules.module import get_module_path from openerp.osv import osv from openerp.tools import DEFAULT_SERVER_DATETIME_FORMAT as DTF from werkzeug import exceptions _logger = logging.getLogger(__name__) # Create the RouteManager and the Route objects for the tests route_manager = RouteManager(url_prefix='/api/v1') route_list = [ Route('json_share_patients', '/staff/assign/', methods=['POST']), Route('json_claim_patients', '/staff/unassign/', methods=['POST']), Route('json_colleagues_list', '/staff/colleagues/'), Route('json_invite_patients', '/staff/invite/<activity_id>/'), Route('json_accept_patients', '/staff/accept/<activity_id>/', methods=['POST']), Route('json_reject_patients', '/staff/reject/<activity_id>/', methods=['POST']), Route('json_take_task', '/tasks/take_ajax/<task_id>/', methods=['POST']), Route('json_cancel_take_task', '/tasks/cancel_take_ajax/<task_id>/', methods=['POST']), Route('json_task_form_action', '/tasks/submit_ajax/<observation>/<task_id>/',
# -*- coding: utf-8 -*- from openerp import http from openerp.addons.nh_eobs_api.controllers.route_api import route_manager from openerp.addons.nh_eobs_api.routing import ResponseJSON from openerp.addons.nh_eobs_api.routing import Route from openerp.addons.nh_eobs_mobile.controllers.main import MobileFrontend from openerp.http import request if not route_manager.get_route('rapid_tranq'): route_toggle_rapid_tranq = Route('rapid_tranq', '/patient/<patient_id>/rapid_tranq', methods=['GET', 'POST'], url_prefix='/mobile') route_manager.add_route(route_toggle_rapid_tranq) class MobileFrontendMentalHealth(MobileFrontend): @http.route(**route_manager.expose_route('rapid_tranq')) def rapid_tranq(self, *args, **kwargs): """ Endpoint for reading and writing the `rapid_tranq` field on a patient's spell. Let's the API model create the JSON encoded response and simply wraps it in a response object to be returned to the client. :param args: :param kwargs: :return: """ api_model = request.registry('nh.eobs.api')