""" LinkSyncServer - Link API Tests """ import pytest from fastapi.testclient import TestClient class TestLinks: def test_create_bookmark(self, client: TestClient, auth_headers: dict, sample_bookmark_data: dict): response = client.post("/api/links/", json=sample_bookmark_data, headers=auth_headers) assert response.status_code == 201 data = response.json() assert data["url"] == sample_bookmark_data["url"] assert data["title"] == sample_bookmark_data["title"] assert data["tags"] == sample_bookmark_data["tags"] def test_list_bookmarks(self, client: TestClient, auth_headers: dict, sample_bookmark_data: dict): client.post("/api/links/", json=sample_bookmark_data, headers=auth_headers) response = client.get("/api/links/", headers=auth_headers) assert response.status_code == 200 assert isinstance(response.json(), list) def test_get_bookmark(self, client: TestClient, auth_headers: dict, sample_bookmark_data: dict): create_resp = client.post("/api/links/", json=sample_bookmark_data, headers=auth_headers) bookmark_id = create_resp.json()["id"] response = client.get(f"/api/links/{bookmark_id}", headers=auth_headers) assert response.status_code == 200 assert response.json()["id"] == bookmark_id def test_get_bookmark_not_found(self, client: TestClient, auth_headers: dict): response = client.get("/api/links/nonexistent-id", headers=auth_headers) assert response.status_code == 404 def test_update_bookmark(self, client: TestClient, auth_headers: dict, sample_bookmark_data: dict): create_resp = client.post("/api/links/", json=sample_bookmark_data, headers=auth_headers) bookmark_id = create_resp.json()["id"] response = client.put( f"/api/links/{bookmark_id}", json={"title": "Updated Title"}, headers=auth_headers, ) assert response.status_code == 200 assert response.json()["title"] == "Updated Title" def test_delete_bookmark(self, client: TestClient, auth_headers: dict, sample_bookmark_data: dict): create_resp = client.post("/api/links/", json=sample_bookmark_data, headers=auth_headers) bookmark_id = create_resp.json()["id"] response = client.delete(f"/api/links/{bookmark_id}", headers=auth_headers) assert response.status_code == 200 assert response.json()["deleted_id"] == bookmark_id def test_add_tags(self, client: TestClient, auth_headers: dict, sample_bookmark_data: dict): create_resp = client.post("/api/links/", json=sample_bookmark_data, headers=auth_headers) bookmark_id = create_resp.json()["id"] response = client.post( f"/api/links/{bookmark_id}/tags", json={"tags": ["new-tag", "another-tag"]}, headers=auth_headers, ) assert response.status_code == 200 tags = response.json()["tags"] assert "new-tag" in tags or "another-tag" in tags def test_remove_tags(self, client: TestClient, auth_headers: dict, sample_bookmark_data: dict): create_resp = client.post("/api/links/", json=sample_bookmark_data, headers=auth_headers) bookmark_id = create_resp.json()["id"] response = client.request( "DELETE", f"/api/links/{bookmark_id}/tags", json={"tags": ["test"]}, headers=auth_headers, ) assert response.status_code in (200, 422) def test_search_bookmarks(self, client: TestClient, auth_headers: dict, sample_bookmark_data: dict): client.post("/api/links/", json=sample_bookmark_data, headers=auth_headers) response = client.get("/api/links/", params={"search": "example"}, headers=auth_headers) assert response.status_code == 200 assert len(response.json()) >= 1 def test_pagination(self, client: TestClient, auth_headers: dict, sample_bookmark_data: dict): for i in range(5): data = sample_bookmark_data.copy() data["url"] = f"https://example{i}.com" data["title"] = f"Example {i}" client.post("/api/links/", json=data, headers=auth_headers) response = client.get("/api/links/", params={"limit": 2, "offset": 0}, headers=auth_headers) assert response.status_code == 200 assert len(response.json()) <= 2