-
Notifications
You must be signed in to change notification settings - Fork 8
Authentification API #1429
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
MaxFabre
wants to merge
6
commits into
ae-utbm:master
Choose a base branch
from
MaxFabre:Api_connexion
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Authentification API #1429
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
e20e578
Authentification API
MaxFabre 65411d7
Tests and schema added
MaxFabre 4b827fa
Now returns an UserSchema
MaxFabre d32d8fe
Update comment on core/api.py
MaxFabre 8983660
Update core/tests/test_auth_api.py
MaxFabre 4412987
core/test_auth_api updated
MaxFabre File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| import pytest | ||
| from django.contrib.auth.hashers import make_password | ||
| from django.test import Client | ||
| from django.urls import reverse | ||
| from model_bakery import baker | ||
|
|
||
| from core.models import User | ||
| from core.schemas import UserSchema | ||
| from counter.models import Customer | ||
|
|
||
|
|
||
| def post_login(client: Client, identifier: str, password: str): | ||
| return client.post( | ||
| reverse("api:login"), | ||
| data={"identifier": identifier, "password": password}, | ||
| content_type="application/json", | ||
| ) | ||
|
MaxFabre marked this conversation as resolved.
|
||
|
|
||
|
|
||
| @pytest.fixture() | ||
| def user(db) -> User: | ||
| return baker.make(User, password=make_password("plop")) | ||
|
|
||
|
|
||
| @pytest.mark.django_db | ||
| @pytest.mark.parametrize( | ||
| "identifier_getter", | ||
| [ | ||
| lambda user: user.username, | ||
| lambda user: user.email, | ||
| lambda user: Customer.get_or_create(user)[0].account_id, | ||
| ], | ||
| ) | ||
| def test_api_login_success(client: Client, user: User, identifier_getter): | ||
| response = post_login(client, identifier_getter(user), "plop") | ||
|
|
||
| assert response.status_code == 200 | ||
| assert response.json() == UserSchema.model_validate(user).model_dump(mode="json") | ||
| assert int(client.session["_auth_user_id"]) == user.id | ||
|
|
||
|
|
||
| @pytest.mark.django_db | ||
| def test_api_login_fail_invalid_credentials(client: Client, user: User): | ||
| response = post_login(client, user.username, "wrong-password") | ||
|
|
||
| assert response.status_code == 401 | ||
| assert "_auth_user_id" not in client.session | ||
|
|
||
|
|
||
| @pytest.mark.django_db | ||
| def test_api_login_fail_if_already_authenticated(client: Client, user: User): | ||
| already_logged_user = baker.make(User) | ||
| client.force_login(already_logged_user) | ||
|
|
||
| response = post_login(client, user.username, "plop") | ||
|
|
||
| assert response.status_code == 403 | ||
| assert int(client.session["_auth_user_id"]) == already_logged_user.id | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.