import json from types import SimpleNamespace from fastapi import FastAPI from fastapi.testclient import TestClient import pytest from deeptutor.api.routers import co_writer from deeptutor.capabilities.notes.capability import NotesCapability from deeptutor.capabilities.notes.tools import NoteReadTool, NoteSuggestTool from deeptutor.co_writer.storage import CoWriterStorage from deeptutor.notes.review import due_cards, grade_card from tests.notes.test_block_pages import Paths, block @pytest.fixture def storage(tmp_path): return CoWriterStorage(Paths(tmp_path)) @pytest.fixture def client(storage, monkeypatch): monkeypatch.setattr(co_writer, "get_co_writer_storage", lambda: storage) app = FastAPI() app.include_router(co_writer.router, prefix="/api") return TestClient(app) def test_invalid_blocks_and_parent_are_client_errors(client): assert client.post("/api/documents", json={"parent_id": "aaaaaaaa"}).status_code == 400 assert ( client.post("/api/documents", json={"blocks": [{"type": "paragraph"}]}).status_code == 400 ) bad = {**block(), "type": "heading", "props": {"level": "bad"}} assert client.post("/api/documents", json={"blocks": [bad]}).status_code == 400 bad = {**block(), "content": [{"type": "link", "href": "javascript:alert(1)", "content": []}]} assert client.post("/api/documents", json={"blocks": [bad]}).status_code == 400 def test_atomic_review_and_stale_editor_api(client): page = client.post("/api/documents", json={"blocks": [block()]}).json() root = "/api/documents/" + page["id"] suggestion = client.post( root + "/suggestions", json={"operation": "replace", "block_id": "a", "markdown": "changed"} ).json() accepted = client.post( root + "/suggestions/" + suggestion["id"], json={"accept": True, "blocks": [block(text="changed")]}, ) assert accepted.status_code == 200 assert accepted.json()["content"] == "changed" assert client.put(root, json={"blocks": [block()], "expected_revision": 0}).status_code == 409 assert client.get(root).json()["content"] == "changed" assert ( client.post( root + "/clips", json={"title": "Original", "markdown": "source", "source_url": "/chat/test"}, ).status_code == 200 ) assert ( client.post( root + "/clips", json={"title": "Original", "markdown": "source", "source_url": "javascript:alert(1)"}, ).status_code == 400 ) def test_assets_are_scoped_and_cannot_escape(client, storage): page = storage.create_document(blocks=[block()]) other = storage.create_document(blocks=[block()]) response = client.post( f"/api/documents/{page.id}/assets", files={"file": ("x.png", b"png-data", "image/png")} ) assert response.status_code == 200 url = response.json()["url"] assert client.get(url).content == b"png-data" assert client.get(url.replace(page.id, other.id)).status_code == 404 assert ( client.post( f"/api/documents/{page.id}/assets", files={"file": ("x.html", "