Add local bearer authentication foundation
This commit is contained in:
@@ -5,7 +5,7 @@ from uuid import uuid4
|
||||
|
||||
from fastapi.testclient import TestClient
|
||||
|
||||
from app import database
|
||||
from app import auth, database
|
||||
from app.main import app
|
||||
|
||||
|
||||
@@ -24,6 +24,15 @@ class ConversationVersionMigrationTests(unittest.TestCase):
|
||||
id TEXT PRIMARY KEY,
|
||||
created_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
CREATE TABLE user_identities (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
user_id TEXT NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
||||
provider TEXT NOT NULL,
|
||||
provider_subject TEXT NOT NULL,
|
||||
email TEXT,
|
||||
created_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
UNIQUE(provider, provider_subject)
|
||||
);
|
||||
CREATE TABLE conversations (
|
||||
id TEXT PRIMARY KEY,
|
||||
user_id TEXT NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
||||
@@ -39,6 +48,8 @@ class ConversationVersionMigrationTests(unittest.TestCase):
|
||||
created_at TEXT NOT NULL
|
||||
);
|
||||
INSERT INTO users(id) VALUES ('00000000-0000-4000-8000-000000000001');
|
||||
INSERT INTO user_identities(user_id,provider,provider_subject,email)
|
||||
VALUES ('00000000-0000-4000-8000-000000000001','google','subject-1','old@example.test');
|
||||
INSERT INTO conversations(id, user_id, title, created_at, updated_at)
|
||||
VALUES ('conversation-1', '00000000-0000-4000-8000-000000000001',
|
||||
'قديم', '2026-01-01', '2026-01-01');
|
||||
@@ -58,6 +69,13 @@ class ConversationVersionMigrationTests(unittest.TestCase):
|
||||
|
||||
def test_old_history_migrates_and_answer_versions_round_trip(self) -> None:
|
||||
database.initialize_database()
|
||||
with database._connect() as connection:
|
||||
identity = connection.execute(
|
||||
"SELECT provider_subject,email,password_hash FROM user_identities WHERE provider='google'"
|
||||
).fetchone()
|
||||
self.assertEqual(identity["provider_subject"], "subject-1")
|
||||
self.assertEqual(identity["email"], "old@example.test")
|
||||
self.assertIsNone(identity["password_hash"])
|
||||
old_conversation = database.get_conversation(
|
||||
"00000000-0000-4000-8000-000000000001", "conversation-1"
|
||||
)
|
||||
@@ -90,6 +108,7 @@ class ConversationVersionMigrationTests(unittest.TestCase):
|
||||
|
||||
def test_api_saves_and_returns_selected_answer_version(self) -> None:
|
||||
user_id = "00000000-0000-4000-8000-000000000001"
|
||||
token, _ = auth.issue_session(user_id)
|
||||
conversation_id = "00000000-0000-4000-8000-000000000099"
|
||||
payload = {
|
||||
"title": "API version test",
|
||||
@@ -106,13 +125,13 @@ class ConversationVersionMigrationTests(unittest.TestCase):
|
||||
with TestClient(app) as client:
|
||||
saved = client.put(
|
||||
f"/v1/conversations/{conversation_id}",
|
||||
headers={"X-User-ID": user_id},
|
||||
headers={"Authorization": f"Bearer {token}"},
|
||||
json=payload,
|
||||
)
|
||||
self.assertEqual(saved.status_code, 200, saved.text)
|
||||
loaded = client.get(
|
||||
f"/v1/conversations/{conversation_id}",
|
||||
headers={"X-User-ID": user_id},
|
||||
headers={"Authorization": f"Bearer {token}"},
|
||||
)
|
||||
self.assertEqual(loaded.status_code, 200, loaded.text)
|
||||
assistant = loaded.json()["messages"][1]
|
||||
|
||||
Reference in New Issue
Block a user