def test_parameter_cascade(rf, router): request = rf.get('/cascade-parameters?a=7&b=10') request.api_info = APIInfo( router.get_path('/cascade-parameters').get_operation('get')) assert read_parameters(request) == {'a': 7, 'b': 10} request = rf.get('/cascade-parameter-override?a=yylmao') request.api_info = APIInfo( router.get_path('/cascade-parameter-override').get_operation('get')) assert read_parameters(request) == { 'a': 'yylmao' } # this would fail in the typecast if override didn't work
def test_header_parameter(rf, router): # Too bad there isn't a "requests"-like interface for testing that didn't # work by creating a `WSGIRequest` environment... Would be more truthful to test with something like that. request = rf.get('/header-parameter?blep=foo', HTTP_TOKEN='foo') request.api_info = APIInfo( router.get_path('/header-parameter').get_operation('get')) assert read_parameters(request)['token'] == 'foo'
def test_text_body_type(rf): apidoc = APIDefinition.from_data(yaml.safe_load(JSONIFY_DOC)) operation = apidoc.get_path('/jsonify').get_operation('post') request = rf.post('/jsonify', 'henlo worl', content_type='text/plain') request.api_info = APIInfo(operation=operation) params = read_parameters(request, {}) assert params['text'] == 'henlo worl'
def test_polymorphism(rf, doc_version, object): router = get_router(f'{doc_version}/schema-refs.yaml') request = rf.post('/pet', json.dumps(object), content_type='application/json') request.api_info = APIInfo(router.get_path('/pet').get_operation('post')) params = read_parameters(request, {}) assert params['pet'] == object
def test_schema_refs(rf, doc_version): router = get_router(f'{doc_version}/schema-refs.yaml') request = rf.post('/cat', json.dumps(lil_bub), content_type='application/json') request.api_info = APIInfo(router.get_path('/cat').get_operation('post')) params = read_parameters(request, {}) assert params['cat'] == lil_bub
def test_type_casting_errors(rf, router): request = rf.get('/add-numbers?a=foo&b=8') request.api_info = APIInfo( router.get_path('/add-numbers').get_operation('get')) with pytest.raises(ErroneousParameters) as ei: read_parameters(request, capture_errors=True) assert 'a' in ei.value.errors assert 'b' in ei.value.parameters
def test_files(rf, router): request = rf.post('/upload', { 'file': ContentFile(b'foo', name='foo.txt'), }) request.api_info = APIInfo( router.get_path('/upload').get_operation('post')) parameters = read_parameters(request) if OpenAPI3BodyParameter.name in parameters: # Peel into the body parameter parameters = parameters[OpenAPI3BodyParameter.name] assert isinstance(parameters['file'], UploadedFile)
def dispatch(self, request, **kwargs): try: operation = self.path.get_operation(request.method) except InvalidOperation: return self.http_method_not_allowed(request, **kwargs) request.api_info = APIInfo(operation=operation) params = dict( (snake_case(name), value) for (name, value) in read_parameters(request, kwargs).items()) handler = request.api_info.router.get_handler(operation.id) try: response = handler(request, **params) except ExceptionalResponse as er: response = er.response return self.transform_response(response)
def dispatch(self, request, **kwargs): operation = self.path.mapping.get(request.method.lower()) if not operation: return self.http_method_not_allowed(request, **kwargs) operation_id = operation['operationId'] request.api_info = APIInfo(api=self.api, path=self.path, operation=operation) params = read_parameters(request.api_info, request, kwargs) handler = self.api.get_handler(operation_id) response = handler(request, **params) if isinstance(response, HttpResponse): # TODO: validate against responses return response return JsonResponse(response, safe=False) # TODO: maybe less TIMTOWDI here?
def test_invalid_collection_format(rf, router): request = rf.get('/invalid-collection-format?blep=foo') request.api_info = APIInfo( router.get_path('/invalid-collection-format').get_operation('get')) with pytest.raises((NotImplementedError, ImproperlyConfigured)): read_parameters(request)
def test_required(rf, router): request = rf.get('/greet') request.api_info = APIInfo(router.get_path('/greet').get_operation('get')) with pytest.raises(ErroneousParameters) as ei: read_parameters(request) assert isinstance(ei.value.errors['greetee'], MissingParameter)
def test_default(rf, router): request = rf.get('/greet?greetee=doggo') request.api_info = APIInfo(router.get_path('/greet').get_operation('get')) parameters = read_parameters(request) assert parameters == {'greeting': 'henlo', 'greetee': 'doggo'}
def test_multi(rf, router): request = rf.get('/multiple-tags?tag=a&tag=b&tag=c') request.api_info = APIInfo( router.get_path('/multiple-tags').get_operation('get')) parameters = read_parameters(request) assert parameters['tag'] == ['a', 'b', 'c']
def test_parameters_ref(rf, router): # /parameters-reference refers the entire parameters object from parameter-reference, so the test is equivalent request = rf.get('/parameters-reference?age=86') request.api_info = APIInfo( router.get_path('/parameters-reference').get_operation('get')) assert read_parameters(request) == {'age': 86}
def test_parameter_ref(rf, router): request = rf.get('/parameter-reference?age=86') request.api_info = APIInfo( router.get_path('/parameter-reference').get_operation('get')) assert read_parameters(request) == {'age': 86}
def make_request_for_operation(operation, method='GET', query_string=''): request = RequestFactory().generic(method=method, path=operation.path.path, QUERY_STRING=query_string) request.api_info = APIInfo(operation) return request