This repository has been archived by the owner on Dec 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
test.py
134 lines (115 loc) · 4.98 KB
/
test.py
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
from unittest import TestCase
from unittest.mock import MagicMock
from unittest import mock
import django
from django.conf import settings
from django.test import RequestFactory
from django.http import HttpResponse, HttpResponseRedirect
import os
from inertia.share import share
settings.configure(
VERSION=1, DEBUG=True,
TEMPLATES= [{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'APP_DIRS': True,
'DIRS': [ os.path.join('testutils'), ],
}],
INERTIA_SHARE = "test.share_custom_func"
)
django.setup()
from inertia.version import get_version
from inertia.views import location, render_inertia
from inertia.middleware import InertiaMiddleware
def share_custom_func(request):
share(request, "custom_data", "custom_value")
class TestInertia(TestCase):
def test_views(self):
requestfactory = RequestFactory()
request = requestfactory.get("/")
self.set_session(request)
response = render_inertia(request, "Index")
self.assertTrue(b'id="page"' in response.content)
def set_session(self, request):
dict_sessions = {
'share': {}
}
request.session = MagicMock()
request.session.__getitem__.side_effect = lambda key: dict_sessions[key]
def test_simple_view(self):
request = RequestFactory().get("/")
self.set_session(request)
response = InertiaMiddleware(lambda x: HttpResponse())(request)
self.assertTrue(response.status_code==200, response.status_code)
def test_middlware_missing_header(self):
view = lambda x: HttpResponse()
defaults = {
'X-Inertia': 'true',
'X-Requested-With': 'XMLHttpRequest',
'X-Inertia-Version': str(get_version()+1),
}
request = RequestFactory().get("/")
request.headers = defaults
self.set_session(request)
response = InertiaMiddleware(view)(request)
self.assertTrue(response.status_code == 409, response.status_code)
def test_middleware(self):
view = lambda request: HttpResponse()
defaults = {
'x-Inertia': 'true',
'X-Inertia-Version': get_version(),
'x-Requested-With': 'XMLHttpRequest'
}
request = RequestFactory().get("/") #, **defaults)
request.headers = defaults
self.set_session(request)
response = InertiaMiddleware(view)(request)
self.assertTrue(response.status_code == 200, response.status_code)
def test_share_custom_data(self):
requestfactory = RequestFactory()
request = requestfactory.get("/")
self.set_session(request)
response = render_inertia(request, "Index")
self.assertDictEqual({"custom_data": "custom_value"}, request.session["share"])
# self.assertTrue(b'share_custom_value"' in response.content)
def test_redirect_303_for_put_patch_delete_requests(self):
request = RequestFactory().put("/users/1")
self.set_session(request)
response = InertiaMiddleware(lambda x: HttpResponseRedirect(redirect_to="/users"))(request)
self.assertTrue(response.status_code==303, response.status_code)
request = RequestFactory().patch("/users/1")
self.set_session(request)
response = InertiaMiddleware(lambda x: HttpResponseRedirect(redirect_to="/users"))(request)
self.assertTrue(response.status_code==303, response.status_code)
request = RequestFactory().delete("/users/1")
self.set_session(request)
response = InertiaMiddleware(lambda x: HttpResponseRedirect(redirect_to="/users"))(request)
self.assertTrue(response.status_code==303, response.status_code)
def test_resolve_lazy_loading_props(self):
requestfactory = RequestFactory()
request = requestfactory.get("/")
self.set_session(request)
def lazy_loaded_prop():
return "2"
response = render_inertia(request, "Index", {"a": "1", "b": lazy_loaded_prop})
self.assertTrue(b'"props": {"a": "1", "b": "2"}' in response.content)
def test_partial_loading(self):
defaults = {
'X-Inertia': 'true',
'X-Inertia-Version': get_version(),
'X-Requested-With': 'XMLHttpRequest',
'X-Inertia-Partial-Data': ["a"],
"X-Inertia-Partial-Component": "Index"
}
requestfactory = RequestFactory()
request = requestfactory.get("/")
request.headers = defaults
self.set_session(request)
def lazy_loaded_prop():
return "2"
response = render_inertia(request, "Index", {"a": "1", "b": lazy_loaded_prop})
# check that b is not returned because we only ask for a
self.assertIn(b'"props": {"a": "1"},', response.content)
def test_location(self):
response = location("https://github.com")
self.assertEqual(409, response.status_code)
self.assertEqual(('X-Inertia-Location', 'https://github.com'), response._headers['x-inertia-location'])