|
1 | 1 | from fastapi.testclient import TestClient |
2 | 2 | from src.paste.main import app |
3 | 3 | from typing import Optional |
4 | | -import os |
5 | 4 |
|
6 | 5 | client: TestClient = TestClient(app) |
7 | 6 |
|
8 | | -file: Optional[str] = None |
| 7 | +paste_id: Optional[str] = None |
9 | 8 |
|
10 | 9 |
|
11 | 10 | def test_get_health_route() -> None: |
12 | | - data: dict = {"status": "ok"} |
13 | 11 | response = client.get("/health") |
14 | 12 | assert response.status_code == 200 |
15 | | - assert response.json() == data |
16 | 13 |
|
17 | 14 |
|
18 | | -def test_get_homepage_route() -> None: |
19 | | - response_expected_headers: str = "text/html; charset=utf-8" |
20 | | - response = client.get("/") |
21 | | - assert response.status_code == 200 |
22 | | - assert response.headers.get("Content-Type", "") == response_expected_headers |
23 | | - |
24 | | - |
25 | | -def test_get_web_route() -> None: |
26 | | - response_expected_headers: str = "text/html; charset=utf-8" |
27 | | - response = client.get("/web") |
28 | | - assert response.status_code == 200 |
29 | | - assert response.headers.get("Content-Type", "") == response_expected_headers |
30 | | - |
31 | | - |
32 | | -def test_get_paste_data_route() -> None: |
33 | | - data: str = "This is a test file." |
34 | | - response = client.get("/paste/test") |
35 | | - assert response.status_code == 200 |
36 | | - assert data in response.text |
37 | | - |
38 | | - |
39 | | -def test_post_web_route() -> None: |
40 | | - data: str = "This is a test data" |
41 | | - form_data: dict = {"content": data, "extension": ".txt"} |
42 | | - response = client.post("/web", data=form_data) |
43 | | - global file |
44 | | - file = str(response.url).split("/")[-1] |
45 | | - assert response.status_code == 200 |
46 | | - assert data in response.text |
47 | | - |
48 | | - |
49 | | -def test_delete_paste_route() -> None: |
50 | | - expected_response: str = f"File successfully deleted {file}" |
51 | | - response = client.delete(f"/paste/{file}") |
52 | | - assert response.status_code == 200 |
53 | | - assert response.text == expected_response |
54 | | - |
55 | | - |
56 | | -def test_post_file_route() -> None: |
57 | | - response = client.post("/file", files={"file": ("test.txt", b"test file content")}) |
58 | | - assert response.status_code == 201 |
59 | | - response_file_uuid: str = response.text |
60 | | - response = client.get(f"/paste/{response_file_uuid}") |
61 | | - assert response.status_code == 200 |
62 | | - assert "test file content" in response.text |
63 | | - response = client.delete(f"/paste/{response_file_uuid}") |
64 | | - assert response.status_code == 200 |
65 | | - assert f"File successfully deleted {response_file_uuid}" in response.text |
66 | | - |
67 | | - |
68 | | -def test_post_file_route_failure() -> None: |
69 | | - response = client.post("/file") |
70 | | - assert response.status_code == 422 # Unprocessable Entity |
71 | | - # Add body assertion in future. |
72 | | - |
73 | | - |
74 | | -def test_post_file_route_size_limit() -> None: |
75 | | - large_file_name: str = "large_file.txt" |
76 | | - file_size: int = 20 * 1024 * 1024 # 20 MB in bytes |
77 | | - additional_bytes: int = 100 # Adding some extra bytes to exceed 20 MB |
78 | | - content: bytes = b"This is a line in the file.\n" |
79 | | - with open(large_file_name, "wb") as file: |
80 | | - while file.tell() < file_size: |
81 | | - file.write(content) |
82 | | - file.write(b"Extra bytes to exceed 20 MB\n" * additional_bytes) |
83 | | - file.close() |
84 | | - f = open(large_file_name, "rb") |
85 | | - files: dict = {"file": f} |
86 | | - response = client.post("/file", files=files) |
87 | | - f.close() |
88 | | - # cleanup |
89 | | - os.remove(large_file_name) |
90 | | - assert response.status_code == 413 |
91 | | - assert "File is too large" in response.text |
92 | | - |
93 | | - |
94 | | -def test_post_api_paste_route() -> None: |
95 | | - paste_data = {"content": "This is a test paste content", "extension": "txt"} |
96 | | - response = client.post("/api/paste", json=paste_data) |
97 | | - assert response.status_code == 201 |
98 | | - response_json = response.json() |
99 | | - assert "uuid" in response_json |
100 | | - assert "url" in response_json |
101 | | - assert response_json["uuid"].endswith(".txt") |
102 | | - assert response_json["url"].startswith("http://paste.fosscu.org/paste/") |
103 | | - |
104 | | - # Clean up: delete the created paste |
105 | | - uuid = response_json["uuid"] |
106 | | - delete_response = client.delete(f"/paste/{uuid}") |
107 | | - assert delete_response.status_code == 200 |
108 | | - |
109 | | - |
110 | | -def test_get_api_paste_route() -> None: |
111 | | - # First, create a paste |
112 | | - paste_data = {"content": "This is a test paste content for GET", "extension": "md"} |
113 | | - create_response = client.post("/api/paste", json=paste_data) |
114 | | - assert create_response.status_code == 201 |
115 | | - created_uuid = create_response.json()["uuid"] |
116 | | - |
117 | | - # Now, test getting the paste |
118 | | - response = client.get(f"/api/paste/{created_uuid}") |
119 | | - assert response.status_code == 200 |
120 | | - response_json = response.json() |
121 | | - assert response_json["uuid"] == created_uuid |
122 | | - assert response_json["content"] == paste_data["content"] |
123 | | - assert response_json["extension"] == paste_data["extension"] |
124 | | - |
125 | | - # Clean up: delete the created paste |
126 | | - delete_response = client.delete(f"/paste/{created_uuid}") |
127 | | - assert delete_response.status_code == 200 |
| 15 | +def test_paste_api_route() -> None: |
| 16 | + respose = client.post( |
| 17 | + "/api/paste", |
| 18 | + json={ |
| 19 | + "content": "Hello-World", |
| 20 | + }, |
| 21 | + ) |
| 22 | + paste_id = respose.text |
| 23 | + assert respose.status_code == 201 |
128 | 24 |
|
129 | 25 |
|
130 | | -def test_get_api_paste_route_not_found() -> None: |
131 | | - response = client.get("/api/paste/nonexistent_uuid.txt") |
132 | | - assert response.status_code == 404 |
133 | | - assert response.json()["detail"] == "Paste not found" |
| 26 | +print(paste_id) |
0 commit comments