11from fastapi .testclient import TestClient
22from src .paste .main import app
3+ import os
34
45client = TestClient (app )
56
@@ -14,31 +15,29 @@ def test_get_health_route():
1415
1516
1617def test_get_homepage_route ():
17- response_expected_headers = ' text/html; charset=utf-8'
18+ response_expected_headers = " text/html; charset=utf-8"
1819 response = client .get ("/" )
1920 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
2222
2323
2424def test_get_web_route ():
25- response_expected_headers = ' text/html; charset=utf-8'
25+ response_expected_headers = " text/html; charset=utf-8"
2626 response = client .get ("/web" )
2727 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
3029
3130
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."
3433 response = client .get ("/paste/test" )
3534 assert response .status_code == 200
3635 assert response .text == data
3736
3837
3938def 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 }
4241 response = client .post ("/web" , data = form_data )
4342 global file
4443 file = str (response .url ).split ("/" )[- 1 ]
@@ -54,8 +53,7 @@ def test_delete_paste_route():
5453
5554
5655def 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" )})
5957 assert response .status_code == 201
6058 response_file_uuid = response .text
6159 response = client .get (f"/paste/{ response_file_uuid } " )
@@ -73,13 +71,27 @@ def test_post_file_route_failure():
7371 "detail" : [
7472 {
7573 "type" : "missing" ,
76- "loc" : [
77- "body" ,
78- "file"
79- ],
74+ "loc" : ["body" , "file" ],
8075 "msg" : "Field required" ,
8176 "input" : None ,
82- "url" : "https://errors.pydantic.dev/2.5/v/missing"
77+ "url" : "https://errors.pydantic.dev/2.5/v/missing" ,
8378 }
8479 ]
8580 }
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