-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
⚡️(api) cache logged-in user database request
Getting request user from database is the most frequent request as it's accessed by every API endpoint. Caching logged user object for a limited time can significantly decrease database queries.
- Loading branch information
Showing
11 changed files
with
262 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,9 +8,10 @@ | |
|
||
from qualicharge.auth.factories import IDTokenFactory | ||
from qualicharge.auth.models import IDToken, UserCreate, UserRead | ||
from qualicharge.auth.oidc import discover_provider, get_public_keys | ||
from qualicharge.auth.oidc import discover_provider, get_public_keys, get_user_from_db | ||
from qualicharge.auth.schemas import User | ||
from qualicharge.conf import settings | ||
from qualicharge.db import SAQueryCounter | ||
|
||
|
||
def setup_function(): | ||
|
@@ -51,6 +52,42 @@ def test_whoami_auth(client_auth): | |
assert user.is_staff is True | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"client_auth", | ||
((True, {"email": "[email protected]", "username": "jdoe"}),), | ||
indirect=True, | ||
) | ||
def test_whoami_auth_get_user_cache(client_auth, db_session): | ||
"""Test the get_user cache on the whoami endpoint.""" | ||
cache_info = get_user_from_db.cache_info() | ||
assert cache_info.hits == 0 | ||
assert cache_info.currsize == 0 | ||
|
||
with SAQueryCounter(db_session.connection()) as counter: | ||
response = client_auth.get("/auth/whoami") | ||
expected = 2 | ||
assert counter.count == expected | ||
assert response.status_code == status.HTTP_200_OK | ||
cache_info = get_user_from_db.cache_info() | ||
assert cache_info.hits == 0 | ||
assert cache_info.currsize == 1 | ||
|
||
user = UserRead(**response.json()) | ||
assert user.email == "[email protected]" | ||
|
||
# Now we should be using cache 10 times | ||
for hit in range(1, 10): | ||
with SAQueryCounter(db_session.connection()) as counter: | ||
response = client_auth.get("/auth/whoami") | ||
cache_info = get_user_from_db.cache_info() | ||
assert counter.count == 0 | ||
assert cache_info.hits == hit | ||
assert cache_info.currsize == 1 | ||
assert response.status_code == status.HTTP_200_OK | ||
user = UserRead(**response.json()) | ||
assert user.email == "[email protected]" | ||
|
||
|
||
def test_whoami_expired_signature( | ||
client, id_token_factory: IDTokenFactory, httpx_mock, monkeypatch | ||
): | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters