| 1 |
from ninja import NinjaAPI, Router |
| 2 |
from ninja.security import APIKeyQuery |
| 3 |
from ninja.testing import TestClient |
| 4 |
|
| 5 |
|
| 6 |
class KeyQuery1(APIKeyQuery): |
| 7 |
def authenticate(self, request, key): |
| 8 |
if key == "k1": |
| 9 |
return key |
| 10 |
|
| 11 |
|
| 12 |
class KeyQuery2(APIKeyQuery): |
| 13 |
def authenticate(self, request, key): |
| 14 |
if key == "k2": |
| 15 |
return key |
| 16 |
|
| 17 |
|
| 18 |
api = NinjaAPI(auth=KeyQuery1()) |
| 19 |
|
| 20 |
|
| 21 |
@api.get("/default") |
| 22 |
def default(request): |
| 23 |
return {"auth": request.auth} |
| 24 |
|
| 25 |
|
| 26 |
@api.api_operation(["POST", "PATCH"], "/multi-method-no-auth") |
| 27 |
def multi_no_auth(request): |
| 28 |
return {"auth": request.auth} |
| 29 |
|
| 30 |
|
| 31 |
@api.api_operation(["POST", "PATCH"], "/multi-method-auth", auth=KeyQuery2()) |
| 32 |
def multi_auth(request): |
| 33 |
return {"auth": request.auth} |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
router = Router() |
| 39 |
|
| 40 |
|
| 41 |
@router.get("/router-operation") |
| 42 |
def router_operation(request): |
| 43 |
return {"auth": str(request.auth)} |
| 44 |
|
| 45 |
|
| 46 |
@router.get("/router-operation-auth", auth=KeyQuery2()) |
| 47 |
def router_operation_auth(request): |
| 48 |
return {"auth": str(request.auth)} |
| 49 |
|
| 50 |
|
| 51 |
api.add_router("", router) |
| 52 |
|
| 53 |
|
| 54 |
router_noauth = Router(auth=None) |
| 55 |
|
| 56 |
|
| 57 |
@router_noauth.get("/router-no-auth") |
| 58 |
def router_operation_no_auth(request): |
| 59 |
return {"auth": str(request.auth)} |
| 60 |
|
| 61 |
|
| 62 |
api.add_router("/no-auth/", router_noauth) |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
client = TestClient(api) |
| 67 |
|
| 68 |
|
| 69 |
def test_multi(): |
| 70 |
assert client.get("/default").status_code == 401 |
| 71 |
assert client.get("/default?key=k1").json() == {"auth": "k1"} |
| 72 |
|
| 73 |
assert client.post("/multi-method-no-auth").status_code == 401 |
| 74 |
assert client.post("/multi-method-no-auth?key=k1").json() == {"auth": "k1"} |
| 75 |
|
| 76 |
assert client.patch("/multi-method-no-auth").status_code == 401 |
| 77 |
assert client.patch("/multi-method-no-auth?key=k1").json() == {"auth": "k1"} |
| 78 |
|
| 79 |
assert client.post("/multi-method-auth?key=k1").status_code == 401 |
| 80 |
assert client.patch("/multi-method-auth?key=k1").status_code == 401 |
| 81 |
|
| 82 |
assert client.post("/multi-method-auth?key=k2").json() == {"auth": "k2"} |
| 83 |
assert client.patch("/multi-method-auth?key=k2").json() == {"auth": "k2"} |
| 84 |
|
| 85 |
|
| 86 |
def test_router_auth(): |
| 87 |
assert client.get("/router-operation").status_code == 401 |
| 88 |
assert client.get("/router-operation?key=k1").json() == {"auth": "k1"} |
| 89 |
|
| 90 |
assert client.get("/router-operation-auth?key=k1").status_code == 401 |
| 91 |
assert client.get("/router-operation-auth?key=k2").json() == {"auth": "k2"} |
| 92 |
|
| 93 |
|
| 94 |
def test_router_no_auth(): |
| 95 |
assert client.get("/no-auth/router-no-auth").json() == {"auth": "None"} |