-
-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update deprecated syntax Update Status codes and responses for register_user
- Loading branch information
Showing
9 changed files
with
112 additions
and
66 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,7 @@ class LoginUserDTO(BaseModel): | |
password: str | ||
|
||
class Config: | ||
schema_extra = { | ||
json_schema_extra = { | ||
"example": { | ||
"email": "[email protected]", | ||
"password": "password", | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -181,7 +181,7 @@ class AddMemberSchema(BaseModel): | |
class Config: | ||
extra = "forbid" | ||
schema_extra = { | ||
json_schema_extra = { | ||
"example": { | ||
"user_email": "[email protected]", | ||
"role": "ADMIN", | ||
|
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 |
---|---|---|
@@ -1,103 +1,142 @@ | ||
import flask_user | ||
import pytest | ||
from unittest.mock import patch, MagicMock | ||
from backend.database import User | ||
from flask_jwt_extended import decode_token | ||
|
||
|
||
@pytest.mark.parametrize( | ||
("email", "password", "expected_status_code"), | ||
( | ||
"email", "password", "firstname", "lastname", | ||
"phone_number", "expected_status_code" | ||
), | ||
[ | ||
("[email protected]", "my_password", 200), | ||
("bad_email", "bad_password", 422), | ||
(None, None, 422), | ||
("[email protected]", "my_password", "John", "Doe", "1234567890", 200), | ||
("[email protected]", "my_password", "John", "Doe", "1234567890", 409), | ||
("bad_email", "bad_password", None, None, None, 422), | ||
(None, None, None, None, None, 422), | ||
], | ||
) | ||
def test_register(db_session, client, email, password, expected_status_code): | ||
def test_register( | ||
db_session, client, email, password, | ||
firstname, lastname, phone_number, | ||
expected_status_code | ||
): | ||
res = client.post( | ||
"api/v1/auth/register", | ||
json={ | ||
"email": email, | ||
"password": password, | ||
}, | ||
"firstname": firstname, | ||
"lastname": lastname, | ||
"phone_number": phone_number, | ||
} | ||
) | ||
|
||
db_user = User.get_by_email(email) | ||
if db_user is not None: | ||
add_test_label(db_user) | ||
assert ("Set-Cookie" in res.headers) == (expected_status_code == 200) | ||
assert (db_user is not None) == (expected_status_code == 200) | ||
assert res.status_code == expected_status_code | ||
|
||
if expected_status_code == 200: | ||
assert res.json["status"] == "ok" | ||
assert res.json["message"] == "Successfully registered." | ||
assert res.json["access_token"] == "mocked_access_token" | ||
assert "Set-Cookie" in res.headers | ||
elif expected_status_code == 409 and email == "[email protected]": | ||
assert res.json["status"] == "Conflict" | ||
assert res.json["message"] == "Error. Email matches existing account." | ||
elif expected_status_code == 422: | ||
assert res.json["status"] == "Unprocessable Entity" | ||
assert "Invalid request body" in res.json["message"] | ||
else: | ||
assert res.json["status"] == "ok" | ||
assert "Failed to register" in res.json["message"] | ||
|
||
|
||
@pytest.mark.parametrize( | ||
("password", "expected_status_code"), | ||
[("my_password", 200), ("bad_password", 401), (None, 422)], | ||
) | ||
def test_login( | ||
db_session, | ||
mock_db_session, | ||
client, example_user, password, expected_status_code | ||
): | ||
res = client.post( | ||
"api/v1/auth/login", | ||
json={ | ||
"email": example_user.email, | ||
"password": password, | ||
}, | ||
) | ||
with patch('backend.database.User.get_by_email') as mock_get_by_email: | ||
mock_get_by_email.return_value = example_user | ||
|
||
assert ("Set-Cookie" in res.headers) == (expected_status_code == 200) | ||
assert res.status_code == expected_status_code | ||
res = client.post( | ||
"api/v1/auth/login", | ||
json={ | ||
"email": example_user.email, | ||
"password": password, | ||
}, | ||
) | ||
|
||
assert ("Set-Cookie" in res.headers) == (expected_status_code == 200) | ||
assert res.status_code == expected_status_code | ||
|
||
def test_jwt(client, db_session, example_user): | ||
res = client.post( | ||
"api/v1/auth/login", | ||
json={ | ||
"email": "[email protected]", | ||
"password": "my_password", | ||
}, | ||
) | ||
|
||
assert res.status_code == 200 | ||
def test_jwt(client, mock_db_session, example_user, mock_access_token): | ||
with patch('backend.database.User.nodes.get_or_none') as mock_get_or_none, \ | ||
patch('flask_jwt_extended.decode_token') as mock_decode_token: | ||
|
||
mock_get_or_none.return_value = example_user | ||
mock_decode_token.return_value = {"sub": "mock_user_uid"} | ||
|
||
res = client.post( | ||
"api/v1/auth/login", | ||
json={ | ||
"email": "[email protected]", | ||
"password": "my_password", | ||
}, | ||
) | ||
|
||
user_uid = decode_token(res.json["access_token"])["sub"] | ||
db_user = User.nodes.get_or_none(uid=user_uid) | ||
assert res.status_code == 200 | ||
|
||
assert db_user.email == example_user.email | ||
assert res.status_code == 200 | ||
user_uid = decode_token(res.json["access_token"])["sub"] | ||
db_user = User.nodes.get_or_none(uid=user_uid) | ||
|
||
assert db_user.email == example_user.email | ||
assert res.status_code == 200 | ||
|
||
def test_auth_test_header(db_session, client, example_user): | ||
|
||
def test_auth_test_header( | ||
mock_db_session, client, example_user, mock_access_token): | ||
login_res = client.post( | ||
"api/v1/auth/login", | ||
json={"email": example_user.email, "password": "my_password"}, | ||
) | ||
|
||
client.set_cookie(domain="localhost", key="access_token_cookie", value="") | ||
|
||
test_res = client.get( | ||
"api/v1/auth/whoami", | ||
headers={ | ||
"Authorization": "Bearer {0}".format(login_res.json["access_token"]) | ||
}, | ||
) | ||
with patch('backend.auth.jwt_required') as mock_jwt_required: | ||
mock_jwt_required.return_value = None # Mock the jwt_required decorator | ||
test_res = client.get( | ||
"api/v1/auth/whoami", | ||
headers={ | ||
"Authorization": f"Bearer {mock_access_token}" | ||
}, | ||
) | ||
|
||
assert test_res.status_code == 200 | ||
|
||
|
||
def test_auth_test_cookie(db_session, client, example_user): | ||
def test_auth_test_cookie(mock_db_session, client, example_user): | ||
client.post( | ||
"api/v1/auth/login", | ||
json={"email": example_user.email, "password": "my_password"}, | ||
) | ||
|
||
test_res = client.get( | ||
"api/v1/auth/whoami", | ||
) | ||
with patch('backend.auth.jwt_required') as mock_jwt_required: | ||
mock_jwt_required.return_value = None # Mock the jwt_required decorator | ||
test_res = client.get( | ||
"api/v1/auth/whoami", | ||
) | ||
|
||
assert test_res.status_code == 200 | ||
|
||
|
||
def test_access_token_fixture(mock_db_session, mock_access_token): | ||
assert len(mock_access_token) > 0 | ||
|
||
|
||
# @pytest.mark.parametrize(("use_correct_email"), [(True), (False)]) | ||
# def test_forgot_email(mocker, client, example_user, use_correct_email): | ||
# mock_send_reset_password_email = mocker.spy( | ||
|
@@ -146,7 +185,3 @@ def test_auth_test_cookie(db_session, client, example_user): | |
# ) | ||
|
||
# assert (login_res.status_code == 200) == use_correct_token | ||
|
||
|
||
def test_access_token_fixture(db_session, access_token): | ||
assert len(access_token) > 0 |