Debian Codegraph @

tests/test_docs/test_auth.py

raw symbols SCIP
1 from unittest.mock import Mock, patch
2
3 import pytest
4
5 from ninja import NinjaAPI
6 from ninja.testing import TestClient
7
8
9 def test_intro():
10 from docs.src.tutorial.authentication.code001 import api
11
12 client = TestClient(api)
13 assert client.get("/pets").status_code == 401
14
15 user = Mock()
16 user.is_authenticated = True
17
18 response = client.get("/pets", user=user)
19 assert response.status_code == 200
20
21
22 @pytest.mark.django_db
23 def test_examples():
24 from someapp.models import Client
25
26 api = NinjaAPI()
27 Client.objects.create(key="12345")
28
29 with patch("builtins.api", api, create=True):
30 import docs.src.tutorial.authentication.apikey01 # noqa: F401
31 import docs.src.tutorial.authentication.apikey02 # noqa: F401
32 import docs.src.tutorial.authentication.apikey03 # noqa: F401
33 import docs.src.tutorial.authentication.basic01 # noqa: F401
34 import docs.src.tutorial.authentication.bearer01 # noqa: F401
35 import docs.src.tutorial.authentication.code001 # noqa: F401
36 import docs.src.tutorial.authentication.code002 # noqa: F401
37 import docs.src.tutorial.authentication.multiple01 # noqa: F401
38 import docs.src.tutorial.authentication.schema01 # noqa: F401
39
40 client = TestClient(api)
41
42 response = client.get("/ipwhitelist", META={"REMOTE_ADDR": "127.0.0.1"})
43 assert response.status_code == 401
44 response = client.get("/ipwhitelist", META={"REMOTE_ADDR": "8.8.8.8"})
45 assert response.status_code == 200
46
47 # Api key --------------------------------
48
49 response = client.get("/apikey")
50 assert response.status_code == 401
51 response = client.get("/apikey?api_key=12345")
52 assert response.status_code == 200
53
54 response = client.get("/headerkey")
55 assert response.status_code == 401
56 response = client.get("/headerkey", headers={"X-API-Key": "supersecret"})
57 assert response.status_code == 200
58
59 response = client.get("/cookiekey")
60 assert response.status_code == 401
61 response = client.get("/cookiekey", COOKIES={"key": "supersecret"})
62 assert response.status_code == 200
63
64 # Basic http --------------------------------
65
66 response = client.get("/basic")
67 assert response.status_code == 401
68 response = client.get(
69 "/basic", headers={"Authorization": "Basic YWRtaW46c2VjcmV0"}
70 )
71 assert response.status_code == 200
72 assert response.json() == {"httpuser": "admin"}
73
74 # Bearer http --------------------------------
75
76 response = client.get("/bearer")
77 assert response.status_code == 401
78
79 response = client.get(
80 "/bearer", headers={"Authorization": "Bearer supersecret"}
81 )
82 assert response.status_code == 200
83
84 # Multiple ------------------------------------
85 assert client.get("/multiple").status_code == 401
86 assert client.get("/multiple?key=supersecret").status_code == 200
87 assert (
88 client.get("/multiple", headers={"key": "supersecret"}).status_code == 200
89 )
90
91
92 def test_global():
93 from docs.src.tutorial.authentication.global01 import api
94
95 @api.get("/somemethod")
96 def mustbeauthed(request):
97 return {"auth": request.auth}
98
99 client = TestClient(api)
100
101 assert client.get("/somemethod").status_code == 401
102
103 resp = client.post(
104 "/token", POST={"username": "admin", "password": "giraffethinnknslong"}
105 )
106 assert resp.status_code == 200
107 assert resp.json() == {"token": "supersecret"}
108
109 resp = client.get("/somemethod", headers={"Authorization": "Bearer supersecret"})
110 assert resp.status_code == 200