first commit
This commit is contained in:
35
backend/tests/test_auth.py
Normal file
35
backend/tests/test_auth.py
Normal file
@@ -0,0 +1,35 @@
|
||||
from fastapi.testclient import TestClient
|
||||
|
||||
from app.main import app
|
||||
|
||||
|
||||
def test_login_accepts_form_and_json(monkeypatch, tmp_path):
|
||||
monkeypatch.setenv("DATABASE_URL", f"sqlite:///{tmp_path / 'auth.db'}")
|
||||
monkeypatch.setenv("DATA_DIR", str(tmp_path / 'data'))
|
||||
monkeypatch.setenv("SECRET_KEY", "test-secret")
|
||||
monkeypatch.setenv("DEFAULT_ADMIN_USERNAME", "admin")
|
||||
monkeypatch.setenv("DEFAULT_ADMIN_PASSWORD", "admin")
|
||||
|
||||
with TestClient(app) as client:
|
||||
form_response = client.post("/api/auth/login", data={"username": "admin", "password": "admin"})
|
||||
assert form_response.status_code == 200
|
||||
assert "access_token" in form_response.json()
|
||||
|
||||
json_response = client.post("/api/auth/login", json={"username": "admin", "password": "admin"})
|
||||
assert json_response.status_code == 200
|
||||
assert "access_token" in json_response.json()
|
||||
|
||||
|
||||
def test_auth_me(monkeypatch, tmp_path):
|
||||
monkeypatch.setenv("DATABASE_URL", f"sqlite:///{tmp_path / 'me.db'}")
|
||||
monkeypatch.setenv("DATA_DIR", str(tmp_path / 'data'))
|
||||
monkeypatch.setenv("SECRET_KEY", "test-secret")
|
||||
monkeypatch.setenv("DEFAULT_ADMIN_USERNAME", "admin")
|
||||
monkeypatch.setenv("DEFAULT_ADMIN_PASSWORD", "admin")
|
||||
|
||||
with TestClient(app) as client:
|
||||
login_response = client.post("/api/auth/login", data={"username": "admin", "password": "admin"})
|
||||
token = login_response.json()["access_token"]
|
||||
me_response = client.get("/api/auth/me", headers={"Authorization": f"Bearer {token}"})
|
||||
assert me_response.status_code == 200
|
||||
assert me_response.json()["username"] == "admin"
|
||||
Reference in New Issue
Block a user