54 lines
1.7 KiB
Python
54 lines
1.7 KiB
Python
def test_login_success(client, admin_user):
|
|
response = client.post(
|
|
"/api/v1/auth/login",
|
|
json={"username_or_email": "test_admin", "password": "AdminPass123!"}
|
|
)
|
|
assert response.status_code == 200
|
|
data = response.json()
|
|
assert "access_token" in data
|
|
assert data["token_type"] == "Bearer"
|
|
assert data["user"]["username"] == "test_admin"
|
|
assert data["user"]["role"] == "super_admin"
|
|
|
|
|
|
def test_login_invalid_password(client, admin_user):
|
|
response = client.post(
|
|
"/api/v1/auth/login",
|
|
json={"username_or_email": "test_admin", "password": "WrongPassword!"}
|
|
)
|
|
assert response.status_code == 401
|
|
assert "Incorrect username or password" in response.json()["detail"]
|
|
|
|
|
|
def test_login_nonexistent_user(client):
|
|
response = client.post(
|
|
"/api/v1/auth/login",
|
|
json={"username_or_email": "nonexistent_user", "password": "any_password"}
|
|
)
|
|
assert response.status_code == 401
|
|
|
|
|
|
def test_get_current_user_me(client, admin_token):
|
|
response = client.get(
|
|
"/api/v1/auth/me",
|
|
headers={"Authorization": f"Bearer {admin_token}"}
|
|
)
|
|
assert response.status_code == 200
|
|
data = response.json()
|
|
assert data["username"] == "test_admin"
|
|
assert data["email"] == "admin@test.com"
|
|
|
|
|
|
def test_get_current_user_unauthorized(client):
|
|
response = client.get("/api/v1/auth/me")
|
|
assert response.status_code == 401
|
|
|
|
|
|
def test_logout(client, admin_token):
|
|
response = client.post(
|
|
"/api/v1/auth/logout",
|
|
headers={"Authorization": f"Bearer {admin_token}"}
|
|
)
|
|
assert response.status_code == 200
|
|
assert response.json()["message"] == "Successfully logged out"
|