|
|
@ -1,4 +1,4 @@ |
|
|
|
from fastapi import Depends, FastAPI |
|
|
|
from fastapi import Depends, FastAPI, Security |
|
|
|
from fastapi.testclient import TestClient |
|
|
|
|
|
|
|
app = FastAPI() |
|
|
@ -35,6 +35,19 @@ async def get_sub_counter_no_cache( |
|
|
|
return {"counter": count, "subcounter": subcount} |
|
|
|
|
|
|
|
|
|
|
|
@app.get("/scope-counter") |
|
|
|
async def get_scope_counter( |
|
|
|
count: int = Security(dep_counter), |
|
|
|
scope_count_1: int = Security(dep_counter, scopes=["scope"]), |
|
|
|
scope_count_2: int = Security(dep_counter, scopes=["scope"]), |
|
|
|
): |
|
|
|
return { |
|
|
|
"counter": count, |
|
|
|
"scope_counter_1": scope_count_1, |
|
|
|
"scope_counter_2": scope_count_2, |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
client = TestClient(app) |
|
|
|
|
|
|
|
|
|
|
@ -66,3 +79,13 @@ def test_sub_counter_no_cache(): |
|
|
|
response = client.get("/sub-counter-no-cache/") |
|
|
|
assert response.status_code == 200, response.text |
|
|
|
assert response.json() == {"counter": 4, "subcounter": 3} |
|
|
|
|
|
|
|
|
|
|
|
def test_security_cache(): |
|
|
|
counter_holder["counter"] = 0 |
|
|
|
response = client.get("/scope-counter/") |
|
|
|
assert response.status_code == 200, response.text |
|
|
|
assert response.json() == {"counter": 1, "scope_counter_1": 2, "scope_counter_2": 2} |
|
|
|
response = client.get("/scope-counter/") |
|
|
|
assert response.status_code == 200, response.text |
|
|
|
assert response.json() == {"counter": 3, "scope_counter_1": 4, "scope_counter_2": 4} |
|
|
|