File: test_request_unmarshallers.py

package info (click to toggle)
python-openapi-core 0.19.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,008 kB
  • sloc: python: 18,868; makefile: 47
file content (136 lines) | stat: -rw-r--r-- 4,536 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import enum

import pytest
from jsonschema_path import SchemaPath

from openapi_core import V30RequestUnmarshaller
from openapi_core import V31RequestUnmarshaller
from openapi_core.datatypes import Parameters
from openapi_core.testing import MockRequest


class Colors(enum.Enum):

    YELLOW = "yellow"
    BLUE = "blue"
    RED = "red"

    @classmethod
    def of(cls, v: str):
        for it in cls:
            if it.value == v:
                return it
        raise ValueError(f"Invalid value: {v}")


class TestRequestUnmarshaller:

    @pytest.fixture(scope="session")
    def spec_dict(self):
        return {
            "openapi": "3.1.0",
            "info": {
                "title": "Test request body unmarshaller",
                "version": "0.1",
            },
            "paths": {
                "/resources": {
                    "post": {
                        "description": "POST resources test request",
                        "requestBody": {
                            "description": "",
                            "content": {
                                "application/json": {
                                    "schema": {
                                        "$ref": "#/components/schemas/createResource"
                                    }
                                }
                            },
                        },
                        "responses": {
                            "201": {"description": "Resource was created."}
                        },
                    },
                    "get": {
                        "description": "POST resources test request",
                        "parameters": [
                            {
                                "name": "color",
                                "in": "query",
                                "required": False,
                                "schema": {
                                    "$ref": "#/components/schemas/colors"
                                },
                            },
                        ],
                        "responses": {
                            "default": {
                                "description": "Returned resources matching request."
                            }
                        },
                    },
                }
            },
            "components": {
                "schemas": {
                    "colors": {
                        "type": "string",
                        "enum": ["yellow", "blue", "red"],
                        "format": "enum_Colors",
                    },
                    "createResource": {
                        "type": "object",
                        "properties": {
                            "resId": {"type": "integer"},
                            "color": {"$ref": "#/components/schemas/colors"},
                        },
                        "required": ["resId", "color"],
                    },
                }
            },
        }

    @pytest.fixture(scope="session")
    def spec(self, spec_dict):
        return SchemaPath.from_dict(spec_dict)

    @pytest.mark.parametrize(
        "req_unmarshaller_cls",
        [V30RequestUnmarshaller, V31RequestUnmarshaller],
    )
    def test_request_body_extra_unmarshaller(self, spec, req_unmarshaller_cls):
        ru = req_unmarshaller_cls(
            spec=spec, extra_format_unmarshallers={"enum_Colors": Colors.of}
        )
        request = MockRequest(
            host_url="http://example.com",
            method="post",
            path="/resources",
            data=b'{"resId": 23498572, "color": "blue"}',
        )
        result = ru.unmarshal(request)

        assert not result.errors
        assert result.body == {"resId": 23498572, "color": Colors.BLUE}
        assert result.parameters == Parameters()

    @pytest.mark.parametrize(
        "req_unmarshaller_cls",
        [V30RequestUnmarshaller, V31RequestUnmarshaller],
    )
    def test_request_param_extra_unmarshaller(
        self, spec, req_unmarshaller_cls
    ):
        ru = req_unmarshaller_cls(
            spec=spec, extra_format_unmarshallers={"enum_Colors": Colors.of}
        )
        request = MockRequest(
            host_url="http://example.com",
            method="get",
            path="/resources",
            args={"color": "blue"},
        )
        result = ru.unmarshal(request)

        assert not result.errors
        assert result.parameters == Parameters(query=dict(color=Colors.BLUE))