File: test_django.py

package info (click to toggle)
python-openapi-core 0.19.4-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 2,008 kB
  • sloc: python: 18,868; makefile: 47
file content (197 lines) | stat: -rw-r--r-- 7,171 bytes parent folder | download | duplicates (2)
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import pytest
from werkzeug.datastructures import Headers
from werkzeug.datastructures import ImmutableMultiDict

from openapi_core.contrib.django import DjangoOpenAPIRequest
from openapi_core.contrib.django import DjangoOpenAPIResponse
from openapi_core.datatypes import RequestParameters


class BaseTestDjango:
    @pytest.fixture(autouse=True, scope="module")
    def django_settings(self):
        import django
        from django.conf import settings
        from django.contrib import admin
        from django.urls import path
        from django.urls import re_path

        if settings.configured:
            from django.utils.functional import empty

            settings._wrapped = empty

        settings.configure(
            SECRET_KEY="secretkey",
            ALLOWED_HOSTS=[
                "testserver",
            ],
            INSTALLED_APPS=[
                "django.contrib.admin",
                "django.contrib.auth",
                "django.contrib.contenttypes",
                "django.contrib.messages",
                "django.contrib.sessions",
            ],
            MIDDLEWARE=[
                "django.contrib.sessions.middleware.SessionMiddleware",
                "django.contrib.auth.middleware.AuthenticationMiddleware",
                "django.contrib.messages.middleware.MessageMiddleware",
            ],
        )
        django.setup()
        settings.ROOT_URLCONF = (
            path("admin/", admin.site.urls),
            re_path("^test/test-regexp/$", lambda d: None),
            re_path("^object/(?P<pk>[^/.]+)/action/$", lambda d: None),
        )

    @pytest.fixture
    def request_factory(self):
        from django.test.client import RequestFactory

        return RequestFactory()

    @pytest.fixture
    def response_factory(self):
        from django.http import HttpResponse

        def create(content=b"", status_code=None):
            return HttpResponse(content, status=status_code)

        return create


class TestDjangoOpenAPIRequest(BaseTestDjango):
    def test_type_invalid(self):
        with pytest.raises(TypeError):
            DjangoOpenAPIRequest(None)

    def test_no_resolver(self, request_factory):
        data = {"test1": "test2"}
        request = request_factory.get("/admin/", data)

        openapi_request = DjangoOpenAPIRequest(request)

        assert openapi_request.parameters == RequestParameters(
            path={},
            query=ImmutableMultiDict([("test1", "test2")]),
            header=Headers({"Cookie": ""}),
            cookie={},
        )
        assert openapi_request.method == request.method.lower()
        assert openapi_request.host_url == request._current_scheme_host
        assert openapi_request.path == request.path
        assert openapi_request.path_pattern is None
        assert openapi_request.body == b""
        assert openapi_request.content_type == request.content_type

    def test_simple(self, request_factory):
        from django.urls import resolve

        request = request_factory.get("/admin/")
        request.resolver_match = resolve(request.path)

        openapi_request = DjangoOpenAPIRequest(request)

        assert openapi_request.parameters == RequestParameters(
            path={},
            query={},
            header=Headers({"Cookie": ""}),
            cookie={},
        )
        assert openapi_request.method == request.method.lower()
        assert openapi_request.host_url == request._current_scheme_host
        assert openapi_request.path == request.path
        assert openapi_request.path_pattern == request.path
        assert openapi_request.body == b""
        assert openapi_request.content_type == request.content_type

    def test_url_rule(self, request_factory):
        from django.urls import resolve

        request = request_factory.get("/admin/auth/group/1/")
        request.resolver_match = resolve(request.path)

        openapi_request = DjangoOpenAPIRequest(request)

        assert openapi_request.parameters == RequestParameters(
            path={"object_id": "1"},
            query={},
            header=Headers({"Cookie": ""}),
            cookie={},
        )
        assert openapi_request.method == request.method.lower()
        assert openapi_request.host_url == request._current_scheme_host
        assert openapi_request.path == request.path
        assert openapi_request.path_pattern == "/admin/auth/group/{object_id}/"
        assert openapi_request.body == b""
        assert openapi_request.content_type == request.content_type

    def test_url_regexp_pattern(self, request_factory):
        from django.urls import resolve

        request = request_factory.get("/test/test-regexp/")
        request.resolver_match = resolve(request.path)

        openapi_request = DjangoOpenAPIRequest(request)

        assert openapi_request.parameters == RequestParameters(
            path={},
            query={},
            header=Headers({"Cookie": ""}),
            cookie={},
        )
        assert openapi_request.method == request.method.lower()
        assert openapi_request.host_url == request._current_scheme_host
        assert openapi_request.path == request.path
        assert openapi_request.path_pattern == request.path
        assert openapi_request.body == b""
        assert openapi_request.content_type == request.content_type

    def test_drf_default_value_pattern(self, request_factory):
        from django.urls import resolve

        request = request_factory.get("/object/123/action/")
        request.resolver_match = resolve(request.path)

        openapi_request = DjangoOpenAPIRequest(request)

        assert openapi_request.parameters == RequestParameters(
            path={"pk": "123"},
            query={},
            header=Headers({"Cookie": ""}),
            cookie={},
        )
        assert openapi_request.method == request.method.lower()
        assert openapi_request.host_url == request._current_scheme_host
        assert openapi_request.path == request.path
        assert openapi_request.path_pattern == "/object/{pk}/action/"
        assert openapi_request.body == b""
        assert openapi_request.content_type == request.content_type


class TestDjangoOpenAPIResponse(BaseTestDjango):
    def test_type_invalid(self):
        with pytest.raises(TypeError):
            DjangoOpenAPIResponse(None)

    def test_stream_response(self, response_factory):
        response = response_factory()
        response.writelines(["foo\n", "bar\n", "baz\n"])

        openapi_response = DjangoOpenAPIResponse(response)

        assert openapi_response.data == b"foo\nbar\nbaz\n"
        assert openapi_response.status_code == response.status_code
        assert openapi_response.content_type == response["Content-Type"]

    def test_redirect_response(self, response_factory):
        data = b"/redirected/"
        response = response_factory(data, status_code=302)

        openapi_response = DjangoOpenAPIResponse(response)

        assert openapi_response.data == data
        assert openapi_response.status_code == response.status_code
        assert openapi_response.content_type == response["Content-Type"]