Spaces:
Sleeping
Sleeping
File size: 2,301 Bytes
3286713 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 | import os
import tempfile
import unittest
from pathlib import Path
from fastapi.testclient import TestClient
class TestApiAuthFlow(unittest.TestCase):
@classmethod
def setUpClass(cls):
tmp_dir = tempfile.TemporaryDirectory()
cls._tmp_dir = tmp_dir
db_path = Path(tmp_dir.name) / "test_echo.db"
os.environ["ECHO_DB_PATH"] = str(db_path)
os.environ["ECHO_SKIP_PIPELINE_PRELOAD"] = "1"
from pipeline.api.main import app
from pipeline.api.persistence import init_db
init_db()
cls.client = TestClient(app)
@classmethod
def tearDownClass(cls):
cls._tmp_dir.cleanup()
def test_register_login_and_data_flow(self):
register = self.client.post(
"/api/auth/register",
json={"username": "alice", "password": "secret123"},
)
self.assertEqual(register.status_code, 200)
token = register.json()["token"]
auth_header = {"Authorization": f"Bearer {token}"}
me = self.client.get("/api/auth/me", headers=auth_header)
self.assertEqual(me.status_code, 200)
self.assertEqual(me.json()["username"], "alice")
save_draft = self.client.post(
"/api/data/drafts",
headers=auth_header,
json={"text": "draft text", "mood": "Sad"},
)
self.assertEqual(save_draft.status_code, 200)
drafts = self.client.get("/api/data/drafts", headers=auth_header)
self.assertEqual(drafts.status_code, 200)
self.assertGreaterEqual(len(drafts.json()), 1)
reset_req = self.client.post("/api/auth/password-reset/request", json={"username": "alice"})
self.assertEqual(reset_req.status_code, 200)
reset_token = reset_req.json()["reset_token"]
self.assertTrue(reset_token)
reset_confirm = self.client.post(
"/api/auth/password-reset/confirm",
json={"token": reset_token, "new_password": "newpass123"},
)
self.assertEqual(reset_confirm.status_code, 200)
login = self.client.post(
"/api/auth/login",
json={"username": "alice", "password": "newpass123"},
)
self.assertEqual(login.status_code, 200)
if __name__ == "__main__":
unittest.main()
|