Skip to content

Commit

Permalink
fix: modifed the tests accordingly
Browse files Browse the repository at this point in the history
  • Loading branch information
johnson-oragui committed Aug 14, 2024
1 parent ed785dc commit d9354d8
Showing 1 changed file with 29 additions and 17 deletions.
46 changes: 29 additions & 17 deletions tests/v1/auth/test_token_auth.py
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
Expand All @@ -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()

0 comments on commit d9354d8

Please sign in to comment.