107 lines
3.4 KiB
Python
107 lines
3.4 KiB
Python
from unittest.mock import patch
|
|
import pytest
|
|
from fastapi.testclient import TestClient
|
|
from fastapi import HTTPException, status
|
|
from app.main import app
|
|
|
|
client = TestClient(app)
|
|
|
|
@pytest.fixture
|
|
def mock_successful_auth():
|
|
return {
|
|
"AccessToken": "mock_access_token",
|
|
"IdToken": "mock_id_token",
|
|
"RefreshToken": "mock_refresh_token"
|
|
}
|
|
|
|
@pytest.fixture
|
|
def mock_successful_auth_no_refresh():
|
|
return {
|
|
"AccessToken": "mock_access_token",
|
|
"IdToken": "mock_id_token"
|
|
}
|
|
|
|
def test_signin_success(mock_successful_auth):
|
|
"""Test successful signin with all tokens"""
|
|
with patch('app.routers.auth.initiate_auth', return_value=mock_successful_auth):
|
|
response = client.post(
|
|
"/auth/signin",
|
|
json={"username": "testuser", "password": "testpass"}
|
|
)
|
|
|
|
assert response.status_code == 200
|
|
data = response.json()
|
|
assert data["access_token"] == "mock_access_token"
|
|
assert data["id_token"] == "mock_id_token"
|
|
assert data["refresh_token"] == "mock_refresh_token"
|
|
assert data["token_type"] == "Bearer"
|
|
|
|
def test_signin_success_no_refresh(mock_successful_auth_no_refresh):
|
|
"""Test successful signin without refresh token"""
|
|
with patch('app.routers.auth.initiate_auth', return_value=mock_successful_auth_no_refresh):
|
|
response = client.post(
|
|
"/auth/signin",
|
|
json={"username": "testuser", "password": "testpass"}
|
|
)
|
|
|
|
assert response.status_code == 200
|
|
data = response.json()
|
|
assert data["access_token"] == "mock_access_token"
|
|
assert data["id_token"] == "mock_id_token"
|
|
assert data["refresh_token"] is None
|
|
assert data["token_type"] == "Bearer"
|
|
|
|
def test_signin_invalid_input():
|
|
"""Test signin with invalid input format"""
|
|
# Missing password
|
|
response = client.post(
|
|
"/auth/signin",
|
|
json={"username": "testuser"}
|
|
)
|
|
assert response.status_code == 422
|
|
|
|
# Missing username
|
|
response = client.post(
|
|
"/auth/signin",
|
|
json={"password": "testpass"}
|
|
)
|
|
assert response.status_code == 422
|
|
|
|
# Empty payload
|
|
response = client.post(
|
|
"/auth/signin",
|
|
json={}
|
|
)
|
|
assert response.status_code == 422
|
|
|
|
def test_signin_auth_failure():
|
|
"""Test signin with authentication failure"""
|
|
with patch('app.routers.auth.initiate_auth') as mock_auth:
|
|
mock_auth.side_effect = HTTPException(
|
|
status_code=status.HTTP_401_UNAUTHORIZED,
|
|
detail="Invalid username or password"
|
|
)
|
|
response = client.post(
|
|
"/auth/signin",
|
|
json={"username": "testuser", "password": "wrongpass"}
|
|
)
|
|
|
|
assert response.status_code == 401
|
|
data = response.json()
|
|
assert data["detail"] == "Invalid username or password"
|
|
|
|
def test_signin_user_not_found():
|
|
"""Test signin with non-existent user"""
|
|
with patch('app.routers.auth.initiate_auth') as mock_auth:
|
|
mock_auth.side_effect = HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="User not found"
|
|
)
|
|
response = client.post(
|
|
"/auth/signin",
|
|
json={"username": "nonexistent", "password": "testpass"}
|
|
)
|
|
|
|
assert response.status_code == 404
|
|
data = response.json()
|
|
assert data["detail"] == "User not found" |