def op(minimal_swagger_spec): return Operation( minimal_swagger_spec, '/foo', 'get', op_spec={'produces': ['application/json']}, )
def test_raise_error_when_no_default_and_no_status_code_match( empty_swagger_spec, op_spec): del op_spec['responses']['default'] op = Operation(empty_swagger_spec, '/pet/{petId}', 'get', op_spec) with pytest.raises(MatchingResponseNotFound) as excinfo: get_response_spec(404, op) assert 'not found' in str(excinfo.value)
def test_returns_consumes_from_op(minimal_swagger_dict): op_spec = { 'consumes': ['multipart/form-data'], } minimal_swagger_spec = Spec.from_dict(minimal_swagger_dict) op = Operation(minimal_swagger_spec, '/foo', 'get', op_spec) assert ['multipart/form-data'] == op.consumes
def test_produces_on_op_overrides_produces_from_swagger_spec( minimal_swagger_dict, ): op_spec = {'produces': ['application/xml']} minimal_swagger_dict['produces'] = ['application/json'] minimal_swagger_spec = Spec.from_dict(minimal_swagger_dict) op = Operation(minimal_swagger_spec, '/foo', 'get', op_spec) assert ['application/xml'] == op.produces
def mock_operation(simple_operation_dict): return Operation( swagger_spec=mock.Mock(), path_name='/endpoint', http_method='get', op_spec=simple_operation_dict, )
def test_path_param_and_op_param(minimal_swagger_dict): op_spec = { 'operationId': 'get_pet_by_id', 'parameters': [{ 'name': 'pet_id', 'in': 'query', 'required': True, 'type': 'integer', }], 'responses': { '200': {} } } path_spec = { 'get': op_spec, 'parameters': [{ 'name': 'sort_key', 'in': 'query', 'required': False, 'type': 'string', }], } minimal_swagger_dict['paths']['/pets'] = path_spec swagger_spec = Spec(minimal_swagger_dict) op = Operation(swagger_spec, '/pets', 'get', op_spec) params = build_params(op) assert len(params) == 2 assert 'pet_id' in params assert 'sort_key' in params
def test_sanitized_param(minimal_swagger_dict): op_spec = { 'operationId': 'get_pet_by_id', # op params would go here 'responses': { '200': {} } } path_spec = { 'get': op_spec, 'parameters': [{ 'name': 'pet-id', 'in': 'headers', 'required': 'true', 'type': 'integer', }], } minimal_swagger_dict['paths']['/pets'] = path_spec swagger_spec = Spec(minimal_swagger_dict) op = Operation(swagger_spec, '/pets', 'get', op_spec) params = build_params(op) assert len(params) == 1 assert [p for p in params] == ['pet_id'] assert 'pet-id' in params assert params['pet_id'] is params['pet-id']
def test_success_json_response(minimal_swagger_spec): response_spec = { 'description': 'Address', 'schema': { 'type': 'object', 'properties': { 'first_name': { 'type': 'string', }, 'last_name': { 'type': 'string', }, }, }, } op = Operation( minimal_swagger_spec, '/foo', 'get', op_spec={'produces': ['application/json']}, ) response = Mock( spec=OutgoingResponse, content_type='application/json', json=Mock( spec=dict, return_value={ 'first_name': 'darwin', 'last_name': 'niwrad', }, ), ) validate_response_body(op, response_spec, response)
def test_success_msgpack_response(minimal_swagger_spec): response_spec = { 'description': 'Address', 'schema': { 'type': 'object', 'properties': { 'first_name': { 'type': 'string', }, 'last_name': { 'type': 'string', }, }, }, } op = Operation( minimal_swagger_spec, '/foo', 'get', op_spec={'produces': [APP_MSGPACK]}, ) response = Mock( spec=OutgoingResponse, content_type=APP_MSGPACK, raw_bytes=msgpack.dumps( { 'first_name': 'darwin', 'last_name': 'niwrad', }, use_bin_type=True, ), ) validate_response_body(op, response_spec, response)
def test_op_param_overrides_path_param(minimal_swagger_dict): # Override 'required' to be True for the sort_key param op_spec = { 'operationId': 'get_pet_by_id', 'parameters': [{ 'name': 'sort_key', 'in': 'query', 'required': True, 'type': 'string', }], 'responses': { '200': {} } } path_spec = { 'get': op_spec, 'parameters': [{ 'name': 'sort_key', 'in': 'query', 'required': False, 'type': 'string', }], } minimal_swagger_dict['paths']['/pets'] = path_spec swagger_spec = Spec(minimal_swagger_dict) op = Operation(swagger_spec, '/pets', 'get', op_spec) params = build_params(op) assert len(params) == 1 assert 'sort_key' in params assert params['sort_key'].required
def test_path_param_only(minimal_swagger_dict): op_spec = { 'operationId': 'get_pet_by_id', # op params would go here 'responses': { '200': {}, }, } path_spec = { 'get': op_spec, 'parameters': [ { 'name': 'pet_id', 'in': 'query', 'required': 'true', 'type': 'integer', }, ], } minimal_swagger_dict['paths']['/pets'] = path_spec swagger_spec = Spec(minimal_swagger_dict) op = Operation(swagger_spec, '/pets', 'get', op_spec) params = build_params(op) assert len(params) == 1 assert 'pet_id' in params
def test_consumes_on_op_overrides_consumes_from_swagger_spec( minimal_swagger_dict): op_spec = {'consumes': ['application/x-www-form-urlencoded']} minimal_swagger_dict['consumes'] = ['multipart/form-data'] minimal_swagger_spec = Spec.from_dict(minimal_swagger_dict) op = Operation(minimal_swagger_spec, '/foo', 'get', op_spec) assert ['application/x-www-form-urlencoded'] == op.consumes
def test_returns_produces_from_swagger_spec_when_not_present_on_op( minimal_swagger_dict, ): op_spec = { # 'produces' left out intentionally } minimal_swagger_dict['produces'] = ['application/json'] minimal_swagger_spec = Spec.from_dict(minimal_swagger_dict) op = Operation(minimal_swagger_spec, '/foo', 'get', op_spec) assert ['application/json'] == op.produces
def test_returns_consumes_from_swagger_spec_when_not_present_on_op( minimal_swagger_dict, ): op_spec = { # 'consumes' left out intentionally } minimal_swagger_dict['consumes'] = ['multipart/form-data'] minimal_swagger_spec = Spec.from_dict(minimal_swagger_dict) op = Operation(minimal_swagger_spec, '/foo', 'get', op_spec) assert ['multipart/form-data'] == op.consumes
def test_consumes_not_present_on_swagger_spec_returns_empty_array( minimal_swagger_dict, ): # The point being, None should never be returned op_spec = { # 'consumes' left out intentionally } minimal_swagger_spec = Spec.from_dict(minimal_swagger_dict) op = Operation(minimal_swagger_spec, '/foo', 'get', op_spec) assert [] == op.consumes
def test_operation_id_raises_when_missing_operation_id_and_possible_sanitization_results_in_empty_string( http_method): spec = Spec(spec_dict={}) operation_spec = {} operation = Operation(spec, '/', http_method, operation_spec) with pytest.raises(ValueError) as excinfo: operation.operation_id assert 'empty operation id starting from operation_id=None, http_method={} and path_name=/'.format( http_method, ) in str(excinfo.value)
def test_success_spec_empty_and_body_empty(minimal_swagger_spec): response_spec = { 'description': 'I do not return anything hence I have no "schema" key', } for empty_body in EMPTY_BODIES: response = Mock(spec=OutgoingResponse, text=empty_body) op = Operation(minimal_swagger_spec, '/foo', 'get', op_spec={}) # no exception raised == success validate_response_body(op, response_spec, response)
def test_success_text_plain_response(minimal_swagger_spec): response_spec = {'description': 'Plain Text Response', 'schema': {}} op = Operation(minimal_swagger_spec, '/foo', 'get', op_spec={'produces': ['text/plain']}) response = Mock(spec=OutgoingResponse, content_type='text/plain', text="Plain Text") validate_response_body(op, response_spec, response)
def test_no_description(op_spec, empty_swagger_spec): expected = \ "[GET] Finds Pets by status\n\n" \ ":param status: the status, yo! (Default: available) (optional)\n" \ ":type status: array\n" \ ":returns: 200: successful operation\n" \ ":rtype: array:#/definitions/Pet\n" \ ":returns: 400: Invalid status value\n" del op_spec['description'] op = Operation(empty_swagger_spec, '/pet', 'get', op_spec) assert expected == create_operation_docstring(op)
def test_no_parameters(op_spec, empty_swagger_spec): del op_spec['parameters'] expected = \ "[GET] Finds Pets by status\n\n" \ "Multiple status values can be provided with comma " \ "seperated strings\n\n" \ ":returns: 200: successful operation\n" \ ":rtype: array:#/definitions/Pet\n" \ ":returns: 400: Invalid status value\n" op = Operation(empty_swagger_spec, '/pet', 'get', op_spec) assert expected == create_operation_docstring(op)
def test_failure_spec_empty_with_body_not_empty(minimal_swagger_spec): response_spec = { 'description': 'I do not return anything hence I have no "schema" key' } op = Operation(minimal_swagger_spec, '/foo', 'get', op_spec={}) response = Mock( spec=OutgoingResponse, text="I am the body and I am not empty even though the response_spec " "says I should be") with pytest.raises(SwaggerMappingError) as excinfo: validate_response_body(op, response_spec, response) assert 'should be empty' in str(excinfo.value)
def test_no_summary(op_spec, empty_swagger_spec): expected = \ "Multiple status values can be provided with comma " \ "seperated strings\n\n" \ ":param status: the status, yo! (Default: available) (optional)\n" \ ":type status: array\n" \ ":returns: 200: successful operation\n" \ ":rtype: array:#/definitions/Pet\n" \ ":returns: 400: Invalid status value\n" del op_spec['summary'] op = Operation(empty_swagger_spec, '/pet', 'get', op_spec) assert expected == create_operation_docstring(op)
def test_notype(op_spec, empty_swagger_spec): del op_spec['responses']['200']['schema']['type'] expected = \ "[GET] Finds Pets by status\n\n" \ "Multiple status values can be provided with comma " \ "seperated strings\n\n" \ ":param status: the status, yo! (Default: available) (optional)\n" \ ":type status: array\n" \ ":returns: 200: successful operation\n" \ ":rtype: notype\n" \ ":returns: 400: Invalid status value\n" op = Operation(empty_swagger_spec, '/pet', 'get', op_spec) assert expected == create_operation_docstring(op)
def test_failure_response_content_type_not_supported_by_bravado_core( minimal_swagger_spec): response_spec = { 'description': 'I return an int', 'schema': { 'type': 'integer', } } op = Operation(minimal_swagger_spec, '/foo', 'get', op_spec={'produces': ['application/xml']}) response = Mock(spec=OutgoingResponse, content_type='application/xml') with pytest.raises(SwaggerMappingError) as excinfo: validate_response_body(op, response_spec, response) assert 'Unsupported content-type' in str(excinfo.value)
def test_deprecated(op_spec, empty_swagger_spec): expected = \ "** DEPRECATED **\n" \ "[GET] Finds Pets by status\n\n" \ "Multiple status values can be provided with comma " \ "seperated strings\n\n" \ ":param status: the status, yo! (Default: available) (optional)\n" \ ":type status: array\n" \ ":returns: 200: successful operation\n" \ ":rtype: array:#/definitions/Pet\n" \ ":returns: 400: Invalid status value\n" op_spec['deprecated'] = True op = Operation(empty_swagger_spec, '/pet', 'get', op_spec) assert expected == create_operation_docstring(op)
def test_no_params(minimal_swagger_dict): op_spec = { 'operationId': 'get_all_pets', # op params would go here 'responses': { '200': {} } } path_spec = { 'get': op_spec # path params would go here } minimal_swagger_dict['paths']['/pets'] = path_spec swagger_spec = Spec(minimal_swagger_dict) op = Operation(swagger_spec, '/pets', 'get', op_spec) params = build_params(op) assert len(params) == 0
def test_path_param_and_op_param_refs(minimal_swagger_dict): # pet_id param is #/parameters/PetId # sort_key param is #/parameters/SortKey parameters = { 'PetId': { 'name': 'pet_id', 'in': 'query', 'required': True, 'type': 'integer', }, 'SortKey': { 'name': 'sort_key', 'in': 'query', 'required': False, 'type': 'string', }, } op_spec = { 'operationId': 'get_pet_by_id', 'parameters': [ { '$ref': '#/parameters/PetId' }, ], 'responses': { '200': {} } } path_spec = { 'get': op_spec, 'parameters': [ { '$ref': '#/parameters/SortKey' }, ], } minimal_swagger_dict['paths']['/pets'] = path_spec minimal_swagger_dict['parameters'] = parameters swagger_spec = Spec(minimal_swagger_dict) op = Operation(swagger_spec, '/pets', 'get', op_spec) params = build_params(op) assert len(params) == 2 assert 'pet_id' in params assert 'sort_key' in params
def test_unicode(op_spec, empty_swagger_spec): # Only test freeform fields (those most likely to contain unicode) op_spec['summary'] = u'Ümlaut1' op_spec['description'] = u'Ümlaut2' op_spec['parameters'][0]['name'] = u'Ümlaut3' op_spec['parameters'][0]['description'] = u'Ümlaut4' op_spec['parameters'][0]['default'] = u'Ümlaut5' op_spec['responses']['200']['description'] = u'Ümlaut6' op_spec['responses']['400']['description'] = u'Ümlaut7' expected = \ u"[GET] Ümlaut1\n\n" \ u"Ümlaut2\n\n" \ u":param Ümlaut3: Ümlaut4 (Default: Ümlaut5) (optional)\n" \ u":type Ümlaut3: array\n" \ u":returns: 200: Ümlaut6\n" \ u":rtype: array:#/definitions/Pet\n" \ u":returns: 400: Ümlaut7\n" op = Operation(empty_swagger_spec, '/pet', 'get', op_spec) assert expected == create_operation_docstring(op)
def test_returns_operation_id_from_operation_spec(): spec = Spec(spec_dict={}) operation_spec = {'operationId': 'getPetById'} operation = Operation(spec, '/pet/{petId}', 'get', operation_spec) assert 'getPetById' == operation.operation_id
def test_returns_sanitized_operation_id_when_using_illegal_chars( input, expected): spec = Spec(spec_dict={}) operation_spec = {'operationId': input} operation = Operation(spec, '/pet/{petId}', 'get', operation_spec) assert expected == operation.operation_id