def test_resource_not_authorized(self): class Controller(object): def index(self, req): raise exception.NotAuthorized() req = webob.Request.blank('/tests') app = fakes.TestRouter(Controller()) response = req.get_response(app) self.assertEqual(403, response.status_int)
def test_resource_call(self): class Controller(object): def index(self, req): return 'off' req = webob.Request.blank('/tests') app = fakes.TestRouter(Controller()) response = req.get_response(app) self.assertEqual(b'off', response.body) self.assertEqual(200, response.status_int)
def test_versions_version_not_found(self): api_version_request_4_0 = api_version_request.APIVersionRequest('4.0') self.mock_object(api_version_request, 'max_api_version', mock.Mock(return_value=api_version_request_4_0)) class Controller(wsgi.Controller): @wsgi.Controller.api_version('3.0', '3.0') def index(self, req): return 'off' req = self.build_request(header_version='3.5') app = fakes.TestRouter(Controller()) response = req.get_response(app) self.assertEqual(404, response.status_int)
def test_versions_inheritance_of_non_base_controller( self, version, call, expected, controller): """Test ControllerMetaclass works inheriting from non base class.""" class ControllerParent(wsgi.Controller): @wsgi.Controller.api_version('3.1') def index(self, req): return 'parent' # We create this class in between to confirm that we don't leave # undesired versioned methods in the wsgi.Controller class. class Controller(wsgi.Controller): @wsgi.Controller.api_version('3.1') def index(self, req): return 'single' class ControllerChild(ControllerParent): # We don't add max version to confirm that once we set a newer # version it doesn't really matter because the newest one will be # called. @wsgi.Controller.api_version('3.2') def index(self, req): return 'child 3.2' @index.api_version('3.3') def index(self, req): return 'child 3.3' @index.api_version('3.4') def index(self, req): return 'child 3.4' @wsgi.Controller.api_version('3.3') def show(self, req, *args, **kwargs): return 'show' base_dir = '/tests' if call == 'index' else '/tests/123' req = self.build_request(base_dir=base_dir, header_version=version) app = fakes.TestRouter(locals()[controller]()) response = req.get_response(app) resp = encodeutils.safe_decode(response.body, incoming='utf-8') if isinstance(expected, str): self.assertEqual(HTTPStatus.OK, response.status_int) self.assertEqual(expected, resp) else: self.assertEqual(expected, response.status_int)
def test_versions_version_not_found(self): api_version_request_4_0 = api_version_request.APIVersionRequest('4.0') self.mock_object(api_version_request, 'max_api_version', mock.Mock(return_value=api_version_request_4_0)) class Controller(wsgi.Controller): @wsgi.Controller.api_version('3.0', '3.0') def index(self, req): return 'off' req = fakes.HTTPRequest.blank('/tests', base_url='http://localhost/v3') req.headers = {version_header_name: '3.5'} app = fakes.TestRouter(Controller()) response = req.get_response(app) self.assertEqual(404, response.status_int)
def test_req_version_matches_with_None_None(self): version_request = api_version_request.APIVersionRequest('3.39') self.mock_object(api_version_request, 'max_api_version', mock.Mock(return_value=version_request)) class Controller(wsgi.Controller): def index(self, req): req_version = req.api_version_request # This case is artificial, and will return True if req_version.matches(None, None): return "Pass" req = self.build_request(base_dir='/tests', header_version='3.39') app = fakes.TestRouter(Controller()) response = req.get_response(app) resp = encodeutils.safe_decode(response.body, incoming='utf-8') self.assertEqual("Pass", resp) self.assertEqual(200, response.status_int)
def test_req_version_matches_with_None(self, version, ret_val): version_request = api_version_request.APIVersionRequest(version) self.mock_object(api_version_request, 'max_api_version', mock.Mock(return_value=version_request)) class Controller(wsgi.Controller): def index(self, req): req_version = req.api_version_request if req_version.matches(None, '3.8'): return 'older' if req_version.matches('3.9', None): return 'newer' req = self.build_request(base_dir='/tests', header_version=version) app = fakes.TestRouter(Controller()) response = req.get_response(app) resp = encodeutils.safe_decode(response.body, incoming='utf-8') self.assertEqual(ret_val, resp) self.assertEqual(200, response.status_int)
def test_req_version_matches(self, version, HTTP_ret): version_request = api_version_request.APIVersionRequest(version) self.mock_object(api_version_request, 'max_api_version', mock.Mock(return_value=version_request)) class Controller(wsgi.Controller): @wsgi.Controller.api_version('3.0', '3.6') def index(self, req): return 'off' req = self.build_request(base_dir='/tests', header_version=version) app = fakes.TestRouter(Controller()) response = req.get_response(app) resp = encodeutils.safe_decode(response.body, incoming='utf-8') if HTTP_ret == 200: self.assertEqual('off', resp) elif HTTP_ret == 404: self.assertNotEqual('off', resp) self.assertEqual(HTTP_ret, response.status_int)