| 1 |
import asyncio |
| 2 |
|
| 3 |
import pytest |
| 4 |
|
| 5 |
from ninja import NinjaAPI |
| 6 |
from ninja.security import APIKeyQuery, HttpBearer |
| 7 |
from ninja.testing import TestAsyncClient, TestClient |
| 8 |
|
| 9 |
|
| 10 |
@pytest.mark.asyncio |
| 11 |
async def test_async_view_handles_async_auth_func(): |
| 12 |
api = NinjaAPI() |
| 13 |
|
| 14 |
async def auth(request): |
| 15 |
key = request.GET.get("key") |
| 16 |
if key == "secret": |
| 17 |
return key |
| 18 |
|
| 19 |
@api.get("/async", auth=auth) |
| 20 |
async def view(request): |
| 21 |
await asyncio.sleep(0) |
| 22 |
return {"key": request.auth} |
| 23 |
|
| 24 |
client = TestAsyncClient(api) |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
res = await client.get("/async") |
| 30 |
assert res.status_code == 401 |
| 31 |
|
| 32 |
|
| 33 |
res = await client.get("/async?key=secret") |
| 34 |
assert res.json() == {"key": "secret"} |
| 35 |
|
| 36 |
|
| 37 |
@pytest.mark.asyncio |
| 38 |
async def test_async_view_handles_async_auth_cls(): |
| 39 |
api = NinjaAPI() |
| 40 |
|
| 41 |
class Auth: |
| 42 |
async def __call__(self, request): |
| 43 |
key = request.GET.get("key") |
| 44 |
if key == "secret": |
| 45 |
return key |
| 46 |
|
| 47 |
@api.get("/async", auth=Auth()) |
| 48 |
async def view(request): |
| 49 |
await asyncio.sleep(0) |
| 50 |
return {"key": request.auth} |
| 51 |
|
| 52 |
client = TestAsyncClient(api) |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
res = await client.get("/async") |
| 58 |
assert res.status_code == 401 |
| 59 |
|
| 60 |
|
| 61 |
res = await client.get("/async?key=secret") |
| 62 |
assert res.json() == {"key": "secret"} |
| 63 |
|
| 64 |
|
| 65 |
@pytest.mark.asyncio |
| 66 |
async def test_async_view_handles_multi_auth(): |
| 67 |
api = NinjaAPI() |
| 68 |
|
| 69 |
def auth_1(request): |
| 70 |
return None |
| 71 |
|
| 72 |
async def auth_2(request): |
| 73 |
return None |
| 74 |
|
| 75 |
async def auth_3(request): |
| 76 |
key = request.GET.get("key") |
| 77 |
if key == "secret": |
| 78 |
return key |
| 79 |
|
| 80 |
@api.get("/async", auth=[auth_1, auth_2, auth_3]) |
| 81 |
async def view(request): |
| 82 |
await asyncio.sleep(0) |
| 83 |
return {"key": request.auth} |
| 84 |
|
| 85 |
client = TestAsyncClient(api) |
| 86 |
|
| 87 |
res = await client.get("/async?key=secret") |
| 88 |
assert res.json() == {"key": "secret"} |
| 89 |
|
| 90 |
|
| 91 |
@pytest.mark.asyncio |
| 92 |
async def test_async_view_handles_auth_errors(): |
| 93 |
api = NinjaAPI() |
| 94 |
|
| 95 |
async def auth(request): |
| 96 |
raise Exception("boom") |
| 97 |
|
| 98 |
@api.get("/async", auth=auth) |
| 99 |
async def view(request): |
| 100 |
await asyncio.sleep(0) |
| 101 |
return {"key": request.auth} |
| 102 |
|
| 103 |
@api.exception_handler(Exception) |
| 104 |
def on_custom_error(request, exc): |
| 105 |
return api.create_response(request, {"custom": True}, status=401) |
| 106 |
|
| 107 |
client = TestAsyncClient(api) |
| 108 |
|
| 109 |
res = await client.get("/async?key=secret") |
| 110 |
assert res.json() == {"custom": True} |
| 111 |
|
| 112 |
|
| 113 |
@pytest.mark.asyncio |
| 114 |
async def test_sync_authenticate_method(): |
| 115 |
class KeyAuth(APIKeyQuery): |
| 116 |
async def authenticate(self, request, key): |
| 117 |
await asyncio.sleep(0) |
| 118 |
if key == "secret": |
| 119 |
return key |
| 120 |
|
| 121 |
api = NinjaAPI(auth=KeyAuth()) |
| 122 |
|
| 123 |
@api.get("/async") |
| 124 |
async def async_view(request): |
| 125 |
return {"auth": request.auth} |
| 126 |
|
| 127 |
client = TestAsyncClient(api) |
| 128 |
|
| 129 |
res = await client.get("/async") |
| 130 |
assert res.json() == {"detail": "Unauthorized"} |
| 131 |
|
| 132 |
res = await client.get("/async?key=secret") |
| 133 |
assert res.json() == {"auth": "secret"} |
| 134 |
|
| 135 |
|
| 136 |
def test_async_authenticate_method_in_sync_context(): |
| 137 |
class KeyAuth(APIKeyQuery): |
| 138 |
async def authenticate(self, request, key): |
| 139 |
await asyncio.sleep(0) |
| 140 |
if key == "secret": |
| 141 |
return key |
| 142 |
|
| 143 |
api = NinjaAPI(auth=KeyAuth()) |
| 144 |
|
| 145 |
@api.get("/sync") |
| 146 |
def sync_view(request): |
| 147 |
return {"auth": request.auth} |
| 148 |
|
| 149 |
client = TestClient(api) |
| 150 |
|
| 151 |
res = client.get("/sync") |
| 152 |
assert res.json() == {"detail": "Unauthorized"} |
| 153 |
|
| 154 |
res = client.get("/sync?key=secret") |
| 155 |
assert res.json() == {"auth": "secret"} |
| 156 |
|
| 157 |
|
| 158 |
@pytest.mark.asyncio |
| 159 |
async def test_async_with_bearer(): |
| 160 |
class BearerAuth(HttpBearer): |
| 161 |
async def authenticate(self, request, key): |
| 162 |
await asyncio.sleep(0) |
| 163 |
if key == "secret": |
| 164 |
return key |
| 165 |
|
| 166 |
api = NinjaAPI(auth=BearerAuth()) |
| 167 |
|
| 168 |
@api.get("/async") |
| 169 |
async def async_view(request): |
| 170 |
return {"auth": request.auth} |
| 171 |
|
| 172 |
client = TestAsyncClient(api) |
| 173 |
|
| 174 |
res = await client.get("/async") |
| 175 |
assert res.json() == {"detail": "Unauthorized"} |
| 176 |
|
| 177 |
res = await client.get("/async", headers={"Authorization": "Bearer secret"}) |
| 178 |
assert res.json() == {"auth": "secret"} |