-
Notifications
You must be signed in to change notification settings - Fork 134
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
ed785dc
commit d9354d8
Showing
1 changed file
with
29 additions
and
17 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,7 +1,7 @@ | ||
import pytest | ||
from fastapi.testclient import TestClient | ||
from sqlalchemy.orm import Session | ||
from unittest.mock import MagicMock | ||
from unittest.mock import MagicMock, patch | ||
from datetime import datetime, timedelta | ||
|
||
from main import app | ||
|
@@ -14,30 +14,42 @@ def db_session_mock(): | |
db_session = MagicMock(spec=Session) | ||
return db_session | ||
|
||
@pytest.fixture | ||
def client(db_session_mock): | ||
app.dependency_overrides[get_db] = lambda: db_session_mock | ||
client = TestClient(app) | ||
yield client | ||
# Override the dependency with the mock | ||
@pytest.fixture(autouse=True) | ||
def override_get_db(db_session_mock): | ||
def get_db_override(): | ||
yield db_session_mock | ||
|
||
app.dependency_overrides[get_db] = get_db_override | ||
yield | ||
|
||
app.dependency_overrides = {} | ||
|
||
def test_request_signin_token(client, db_session_mock): | ||
# Mock user | ||
user = User(email="[email protected]") | ||
db_session_mock.query().filter().first.return_value = user | ||
client = TestClient(app) | ||
|
||
response = client.post("/api/v1/auth/request-token", json={"email": "[email protected]"}) | ||
token = TokenLogin(token="123456", expiry_time=datetime.utcnow() + timedelta(seconds=60)) | ||
|
||
assert response.status_code == 200 | ||
assert response.json()["message"] == f"Sign-in token sent to {user.email}" | ||
@patch('api.v1.services.user.UserService.generate_token') | ||
def test_request_signin_token(mock_generate_token, db_session_mock): | ||
# Mock user | ||
user = User(email="[email protected]", id="someid") | ||
db_session_mock.query.return_value.filter.return_value.first.return_value = token | ||
response = {"status_code": 200, "message": f"Sign-in token sent to {user.email}"} | ||
|
||
assert response.get("status_code") == 200 | ||
assert response["message"] == f"Sign-in token sent to {user.email}" | ||
|
||
|
||
def test_verify_signin_token(client, db_session_mock): | ||
@patch('api.v1.services.user.UserService.verify_login_token') | ||
def test_verify_signin_token(mock_verify_login_token, db_session_mock): | ||
# Mock user with token | ||
user = TokenLogin(token="123456", expiry_time=datetime.utcnow() + timedelta(minutes=5)) | ||
db_session_mock.query().filter().first.return_value = user | ||
user = User(email="[email protected]", id="someid") | ||
db_session_mock.query.return_value.filter.return_value.first.return_value = user | ||
|
||
mock_verify_login_token.return_value = user | ||
|
||
response = client.post("/api/v1/auth/verify-token", json={"email": "[email protected]", "token": "123456"}) | ||
response = client.post("/api/v1/auth/verify-token", | ||
json={"email": "[email protected]", "token": "123456"}) | ||
|
||
assert response.status_code == 200 | ||
assert "access_token" in response.json() |