Beispiel #1
0
    def test_operation_helper_unapplicable(self, kwargs):
        """Should ignore if there are no applicable responses.

        Applicable responses are string in the defined applicable responses.
        Any other cases should pass right through without changing anything.
        """
        plugin = ResponseReferencesPlugin()
        expected = copy.deepcopy(kwargs)
        plugin.operation_helper(**kwargs)
        assert kwargs == expected  # Nothing mutated
Beispiel #2
0
    def test_multi_operation_multi_reponses(self, openapi_version):
        """Should loop all operations and all responses."""
        spec = apispec.APISpec('title', 'version', openapi_version)
        plugin = ResponseReferencesPlugin()
        plugin.init_spec(spec)

        operations = {
            'get': {
                'responses': {
                    http.HTTPStatus.OK.value:
                    http.HTTPStatus.OK.name,
                    http.HTTPStatus.NO_CONTENT.value:
                    http.HTTPStatus.NO_CONTENT.name,
                }
            },
            'post': {
                'responses': {
                    http.HTTPStatus.OK.value:
                    http.HTTPStatus.OK.name,  # Ignored repeat
                    http.HTTPStatus.CREATED.value:
                    http.HTTPStatus.CREATED.name,
                }
            },
        }

        plugin.operation_helper(operations=operations)

        components = spec.to_dict()
        if openapi_version == '3.0.2':
            components = components['components']

        assert len(components['responses']) == 3  # 200, 201, 204
Beispiel #3
0
    def test_api_registers_error_responses(self, openapi_version,
                                           http_status_code, http_status_name):
        """Responses should be added to spec."""
        spec = apispec.APISpec('title', 'version', openapi_version)
        plugin = ResponseReferencesPlugin()
        plugin.init_spec(spec)

        operations = {
            'get': {
                'responses': {
                    http_status_code: http_status_name,
                }
            }
        }

        plugin.operation_helper(operations=operations)

        components = spec.to_dict()
        if openapi_version == '3.0.2':
            components = components['components']

        assert len(components['responses']) == 1
        assert http_status_name in components['responses']
Beispiel #4
0
    def test_repeated_response(self, openapi_version):
        """Repeated response, different endpoint."""
        spec = apispec.APISpec('title', 'version', openapi_version)
        plugin = ResponseReferencesPlugin()
        plugin.init_spec(spec)

        operations = {
            'get': {
                'responses': {
                    http.HTTPStatus.OK.value: http.HTTPStatus.OK.name,
                }
            }
        }

        # operation_helper is called on each path, so this simulates
        # 2 endpoints with the same response
        plugin.operation_helper(operations=copy.deepcopy(operations))
        plugin.operation_helper(operations=copy.deepcopy(operations))

        components = spec.to_dict()
        if openapi_version == '3.0.2':
            components = components['components']

        assert len(components['responses']) == 1