-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f184e96
commit f983817
Showing
2 changed files
with
41 additions
and
26 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,19 @@ | ||
import pytest | ||
from werkzeug.security import generate_password_hash | ||
|
||
from src.models.user import User | ||
from src.tests.factories.factories import UserFactory | ||
from werkzeug.security import generate_password_hash | ||
|
||
|
||
@pytest.fixture | ||
def user_in_db(db_session) -> User: | ||
def test_jwt_authentification_should_return_user_payload_with_tokens( | ||
client, db_session | ||
): | ||
# Given | ||
user: User = UserFactory( | ||
email="[email protected]", password=generate_password_hash("password123") | ||
) | ||
db_session.add(user) | ||
db_session.commit() | ||
return user | ||
|
||
|
||
def test_jwt_authentification_should_return_user_payload_with_tokens( | ||
client, user_in_db | ||
): | ||
# When | ||
response = client.post( | ||
"/api/v1/auth/login", | ||
|
@@ -26,14 +23,21 @@ def test_jwt_authentification_should_return_user_payload_with_tokens( | |
result = response.json | ||
|
||
# Then | ||
assert result["user"]["id"] == user_in_db.id | ||
assert result["user"]["email"] == user_in_db.email | ||
assert result["user"]["username"] == user_in_db.username | ||
assert result["user"]["id"] == user.id | ||
assert result["user"]["email"] == user.email | ||
assert result["user"]["username"] == user.username | ||
assert result["user"]["access"] != "" | ||
assert result["user"]["refresh"] != "" | ||
|
||
|
||
def test_jwt_authentification_should_return_401_unauthorized(client, user_in_db): | ||
def test_jwt_authentification_should_return_401_unauthorized(client, db_session): | ||
# Given | ||
user: User = UserFactory( | ||
email="[email protected]", password=generate_password_hash("password123") | ||
) | ||
db_session.add(user) | ||
db_session.commit() | ||
|
||
# When | ||
response = client.post( | ||
"/api/v1/auth/login", | ||
|
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 |
---|---|---|
|
@@ -7,38 +7,49 @@ | |
from werkzeug.security import generate_password_hash | ||
|
||
|
||
@pytest.fixture | ||
def user_in_db(db_session) -> User: | ||
def test_validate_user_should_return_user(db_session): | ||
# Given | ||
user: User = UserFactory( | ||
email="test@example.com", password=generate_password_hash("password123") | ||
email="test1@example.com", password=generate_password_hash("password123") | ||
) | ||
db_session.add(user) | ||
db_session.commit() | ||
return user | ||
|
||
|
||
def test_validate_user_should_return_user(user_in_db): | ||
# Act | ||
result = validate_user("test@example.com", "password123") | ||
result = validate_user("test1@example.com", "password123") | ||
|
||
# Assert | ||
assert result == user_in_db | ||
assert result == user | ||
|
||
|
||
def test_validate_user_should_create_email_exception(db_session): | ||
# Given | ||
user: User = UserFactory( | ||
email="[email protected]", password=generate_password_hash("password123") | ||
) | ||
db_session.add(user) | ||
db_session.commit() | ||
|
||
def test_validate_user_should_create_email_exception(user_in_db): | ||
# Act & Assert | ||
with pytest.raises(AuthenticationException) as exc_info: | ||
validate_user("bad.email@gmail.com", "password123") | ||
validate_user("bad.email2@gmail.com", "password123") | ||
assert str(exc_info.value) == "User not found" | ||
|
||
|
||
def test_validate_user_should_create_password_exception(user_in_db): | ||
def test_validate_user_should_create_password_exception(db_session): | ||
# Given | ||
user: User = UserFactory( | ||
email="[email protected]", password=generate_password_hash("password123") | ||
) | ||
db_session.add(user) | ||
db_session.commit() | ||
|
||
with pytest.raises(AuthenticationException) as exc_info: | ||
validate_user("test@example.com", "badPassword") | ||
validate_user("test3@example.com", "badPassword") | ||
assert str(exc_info.value) == "Incorrect password" | ||
|
||
|
||
def test_validate_user_should_create_missing_email_or_password_exception(user_in_db): | ||
def test_validate_user_should_create_missing_email_or_password_exception(): | ||
# Act & Assert | ||
with pytest.raises(AuthenticationException) as exc_info: | ||
validate_user("", "password") | ||
|