1
1
from fastapi .testclient import TestClient
2
2
from src .paste .main import app
3
+ import os
3
4
4
5
client = TestClient (app )
5
6
@@ -14,31 +15,29 @@ def test_get_health_route():
14
15
15
16
16
17
def test_get_homepage_route ():
17
- response_expected_headers = ' text/html; charset=utf-8'
18
+ response_expected_headers = " text/html; charset=utf-8"
18
19
response = client .get ("/" )
19
20
assert response .status_code == 200
20
- assert response .headers .get (
21
- 'Content-Type' , '' ) == response_expected_headers
21
+ assert response .headers .get ("Content-Type" , "" ) == response_expected_headers
22
22
23
23
24
24
def test_get_web_route ():
25
- response_expected_headers = ' text/html; charset=utf-8'
25
+ response_expected_headers = " text/html; charset=utf-8"
26
26
response = client .get ("/web" )
27
27
assert response .status_code == 200
28
- assert response .headers .get (
29
- 'Content-Type' , '' ) == response_expected_headers
28
+ assert response .headers .get ("Content-Type" , "" ) == response_expected_headers
30
29
31
30
32
- def test_get_paste_route ():
33
- data = ' This is a test file.'
31
+ def test_get_paste_data_route ():
32
+ data = " This is a test file."
34
33
response = client .get ("/paste/test" )
35
34
assert response .status_code == 200
36
35
assert response .text == data
37
36
38
37
39
38
def test_post_web_route ():
40
- data = ' This is a test data'
41
- form_data = {' content' : data }
39
+ data = " This is a test data"
40
+ form_data = {" content" : data }
42
41
response = client .post ("/web" , data = form_data )
43
42
global file
44
43
file = str (response .url ).split ("/" )[- 1 ]
@@ -54,8 +53,7 @@ def test_delete_paste_route():
54
53
55
54
56
55
def test_post_file_route ():
57
- response = client .post (
58
- "/file" , files = {"file" : ("test.txt" , b"test file content" )})
56
+ response = client .post ("/file" , files = {"file" : ("test.txt" , b"test file content" )})
59
57
assert response .status_code == 201
60
58
response_file_uuid = response .text
61
59
response = client .get (f"/paste/{ response_file_uuid } " )
@@ -73,13 +71,27 @@ def test_post_file_route_failure():
73
71
"detail" : [
74
72
{
75
73
"type" : "missing" ,
76
- "loc" : [
77
- "body" ,
78
- "file"
79
- ],
74
+ "loc" : ["body" , "file" ],
80
75
"msg" : "Field required" ,
81
76
"input" : None ,
82
- "url" : "https://errors.pydantic.dev/2.5/v/missing"
77
+ "url" : "https://errors.pydantic.dev/2.5/v/missing" ,
83
78
}
84
79
]
85
80
}
81
+
82
+
83
+ def test_post_file_route_size_limit ():
84
+ large_file_name = "large_file.txt"
85
+ file_size = 20 * 1024 * 1024 # 20 MB in bytes
86
+ additional_bytes = 100 # Adding some extra bytes to exceed 20 MB
87
+ content = b"This is a line in the file.\n "
88
+ with open (large_file_name , "wb" ) as file :
89
+ while file .tell () < file_size :
90
+ file .write (content )
91
+ file .write (b"Extra bytes to exceed 20 MB\n " * additional_bytes )
92
+ files = {"file" : open (large_file_name , "rb" )}
93
+ response = client .post ("/file" , files = files )
94
+ # cleanup
95
+ os .remove (large_file_name )
96
+ assert response .status_code == 413
97
+ assert response .text == "File is too large"
0 commit comments