File: test_django.py

package info (click to toggle)
python-cross-web 0.4.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 744 kB
  • sloc: python: 2,262; sh: 23; makefile: 10
file content (160 lines) | stat: -rw-r--r-- 5,001 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import json
import pytest

from cross_web import AsyncDjangoHTTPRequestAdapter, DjangoHTTPRequestAdapter

pytestmark = [pytest.mark.django]


@pytest.fixture(scope="module", autouse=True)
def django_setup() -> None:
    import django
    from django.conf import settings

    if not settings.configured:
        settings.configure(
            DEBUG=True,
            SECRET_KEY="test-secret-key",
            DEFAULT_CHARSET="utf-8",
            USE_TZ=True,
            ALLOWED_HOSTS=["*"],
            INSTALLED_APPS=[
                "django.contrib.contenttypes",
                "django.contrib.auth",
            ],
            MIDDLEWARE=[],
            ROOT_URLCONF="tests.request.django_urls",
            DATABASES={
                "default": {
                    "ENGINE": "django.db.backends.sqlite3",
                    "NAME": ":memory:",
                }
            },
        )
        django.setup()


def test_sync_django_adapter() -> None:
    from django.test import RequestFactory
    from django.core.files.uploadedfile import SimpleUploadedFile

    factory = RequestFactory()

    # Create a POST request with form data and files
    request = factory.post(
        "/test?query=test",
        data={
            "form": "data",
            "file": SimpleUploadedFile(
                "test.txt", b"upload", content_type="text/plain"
            ),
        },
        HTTP_COOKIE="session=123",
    )

    adapter = DjangoHTTPRequestAdapter(request)

    assert adapter.query_params == {"query": "test"}
    # Django doesn't consume body for multipart, it's still available
    assert adapter.body  # Body contains multipart data
    assert adapter.method == "POST"
    assert "Content-Type" in adapter.headers
    assert adapter.headers["Content-Type"].startswith("multipart/form-data")
    assert adapter.post_data["form"] == "data"
    assert "file" in adapter.files
    assert adapter.content_type is not None and adapter.content_type.startswith(
        "multipart/form-data"
    )
    assert adapter.url == "http://testserver/test?query=test"
    assert adapter.cookies == {"session": "123"}

    form_data = adapter.get_form_data()
    assert "file" in form_data.files
    assert form_data.form["form"] == "data"


def test_sync_django_adapter_json() -> None:
    from django.test import RequestFactory

    factory = RequestFactory()

    # Create a POST request with JSON data
    request = factory.post(
        "/test?query=test",
        data=json.dumps({"key": "value"}),
        content_type="application/json",
        HTTP_COOKIE="session=123",
    )

    adapter = DjangoHTTPRequestAdapter(request)

    assert adapter.query_params == {"query": "test"}
    assert adapter.body == '{"key": "value"}'
    assert adapter.method == "POST"
    assert adapter.headers["Content-Type"] == "application/json"
    assert adapter.content_type == "application/json"
    assert adapter.url == "http://testserver/test?query=test"
    assert adapter.cookies == {"session": "123"}


@pytest.mark.asyncio
async def test_async_django_adapter() -> None:
    from django.test import RequestFactory
    from django.core.files.uploadedfile import SimpleUploadedFile

    factory = RequestFactory()

    # Create a POST request with form data and files
    request = factory.post(
        "/test?query=test",
        data={
            "form": "data",
            "file": SimpleUploadedFile(
                "test.txt", b"upload", content_type="text/plain"
            ),
        },
        HTTP_COOKIE="session=123",
    )

    adapter = AsyncDjangoHTTPRequestAdapter(request)

    assert adapter.query_params == {"query": "test"}
    body = await adapter.get_body()
    assert body  # Body contains multipart data
    assert adapter.method == "POST"
    assert "Content-Type" in adapter.headers
    assert adapter.content_type is not None and adapter.content_type.startswith(
        "multipart/form-data"
    )
    assert adapter.url == "http://testserver/test?query=test"
    assert adapter.cookies == {"session": "123"}

    form_data = await adapter.get_form_data()
    assert "file" in form_data.files
    assert form_data.form["form"] == "data"


@pytest.mark.asyncio
async def test_async_django_adapter_json() -> None:
    from django.test import RequestFactory

    factory = RequestFactory()

    # Create a POST request with JSON data
    request = factory.post(
        "/test?query=test",
        data=json.dumps({"key": "value"}),
        content_type="application/json",
        HTTP_COOKIE="session=123",
    )

    adapter = AsyncDjangoHTTPRequestAdapter(request)

    assert adapter.query_params == {"query": "test"}
    body = await adapter.get_body()
    assert body == b'{"key": "value"}'
    assert adapter.method == "POST"
    assert adapter.headers["Content-Type"] == "application/json"
    assert adapter.content_type == "application/json"
    assert adapter.url == "http://testserver/test?query=test"
    assert adapter.cookies == {"session": "123"}