Debian Codegraph @

tests/test_multipart_installation.py

raw
1 import warnings
2
3 import pytest
4 from fastapi import FastAPI, File, Form, UploadFile
5 from fastapi.dependencies.utils import (
6 multipart_incorrect_install_error,
7 multipart_not_installed_error,
8 )
9
10
11 def test_incorrect_multipart_installed_form(monkeypatch):
12 monkeypatch.setattr("python_multipart.__version__", "0.0.12")
13 with warnings.catch_warnings(record=True):
14 warnings.simplefilter("always")
15 monkeypatch.delattr("multipart.multipart.parse_options_header", raising=False)
16 with pytest.raises(RuntimeError, match=multipart_incorrect_install_error):
17 app = FastAPI()
18
19 @app.post("/")
20 async def root(username: str = Form()):
21 return username # pragma: nocover
22
23
24 def test_incorrect_multipart_installed_file_upload(monkeypatch):
25 monkeypatch.setattr("python_multipart.__version__", "0.0.12")
26 with warnings.catch_warnings(record=True):
27 warnings.simplefilter("always")
28 monkeypatch.delattr("multipart.multipart.parse_options_header", raising=False)
29 with pytest.raises(RuntimeError, match=multipart_incorrect_install_error):
30 app = FastAPI()
31
32 @app.post("/")
33 async def root(f: UploadFile = File()):
34 return f # pragma: nocover
35
36
37 def test_incorrect_multipart_installed_file_bytes(monkeypatch):
38 monkeypatch.setattr("python_multipart.__version__", "0.0.12")
39 with warnings.catch_warnings(record=True):
40 warnings.simplefilter("always")
41 monkeypatch.delattr("multipart.multipart.parse_options_header", raising=False)
42 with pytest.raises(RuntimeError, match=multipart_incorrect_install_error):
43 app = FastAPI()
44
45 @app.post("/")
46 async def root(f: bytes = File()):
47 return f # pragma: nocover
48
49
50 def test_incorrect_multipart_installed_multi_form(monkeypatch):
51 monkeypatch.setattr("python_multipart.__version__", "0.0.12")
52 with warnings.catch_warnings(record=True):
53 warnings.simplefilter("always")
54 monkeypatch.delattr("multipart.multipart.parse_options_header", raising=False)
55 with pytest.raises(RuntimeError, match=multipart_incorrect_install_error):
56 app = FastAPI()
57
58 @app.post("/")
59 async def root(username: str = Form(), password: str = Form()):
60 return username # pragma: nocover
61
62
63 def test_incorrect_multipart_installed_form_file(monkeypatch):
64 monkeypatch.setattr("python_multipart.__version__", "0.0.12")
65 with warnings.catch_warnings(record=True):
66 warnings.simplefilter("always")
67 monkeypatch.delattr("multipart.multipart.parse_options_header", raising=False)
68 with pytest.raises(RuntimeError, match=multipart_incorrect_install_error):
69 app = FastAPI()
70
71 @app.post("/")
72 async def root(username: str = Form(), f: UploadFile = File()):
73 return username # pragma: nocover
74
75
76 def test_no_multipart_installed(monkeypatch):
77 monkeypatch.setattr("python_multipart.__version__", "0.0.12")
78 with warnings.catch_warnings(record=True):
79 warnings.simplefilter("always")
80 monkeypatch.delattr("multipart.__version__", raising=False)
81 with pytest.raises(RuntimeError, match=multipart_not_installed_error):
82 app = FastAPI()
83
84 @app.post("/")
85 async def root(username: str = Form()):
86 return username # pragma: nocover
87
88
89 def test_no_multipart_installed_file(monkeypatch):
90 monkeypatch.setattr("python_multipart.__version__", "0.0.12")
91 with warnings.catch_warnings(record=True):
92 warnings.simplefilter("always")
93 monkeypatch.delattr("multipart.__version__", raising=False)
94 with pytest.raises(RuntimeError, match=multipart_not_installed_error):
95 app = FastAPI()
96
97 @app.post("/")
98 async def root(f: UploadFile = File()):
99 return f # pragma: nocover
100
101
102 def test_no_multipart_installed_file_bytes(monkeypatch):
103 monkeypatch.setattr("python_multipart.__version__", "0.0.12")
104 with warnings.catch_warnings(record=True):
105 warnings.simplefilter("always")
106 monkeypatch.delattr("multipart.__version__", raising=False)
107 with pytest.raises(RuntimeError, match=multipart_not_installed_error):
108 app = FastAPI()
109
110 @app.post("/")
111 async def root(f: bytes = File()):
112 return f # pragma: nocover
113
114
115 def test_no_multipart_installed_multi_form(monkeypatch):
116 monkeypatch.setattr("python_multipart.__version__", "0.0.12")
117 with warnings.catch_warnings(record=True):
118 warnings.simplefilter("always")
119 monkeypatch.delattr("multipart.__version__", raising=False)
120 with pytest.raises(RuntimeError, match=multipart_not_installed_error):
121 app = FastAPI()
122
123 @app.post("/")
124 async def root(username: str = Form(), password: str = Form()):
125 return username # pragma: nocover
126
127
128 def test_no_multipart_installed_form_file(monkeypatch):
129 monkeypatch.setattr("python_multipart.__version__", "0.0.12")
130 with warnings.catch_warnings(record=True):
131 warnings.simplefilter("always")
132 monkeypatch.delattr("multipart.__version__", raising=False)
133 with pytest.raises(RuntimeError, match=multipart_not_installed_error):
134 app = FastAPI()
135
136 @app.post("/")
137 async def root(username: str = Form(), f: UploadFile = File()):
138 return username # pragma: nocover
139
140
141 def test_old_multipart_installed(monkeypatch):
142 monkeypatch.setattr("python_multipart.__version__", "0.0.12")
143 with warnings.catch_warnings(record=True):
144 warnings.simplefilter("always")
145 app = FastAPI()
146
147 @app.post("/")
148 async def root(username: str = Form()):
149 return username # pragma: nocover