-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtests.py
More file actions
70 lines (58 loc) · 2.99 KB
/
tests.py
File metadata and controls
70 lines (58 loc) · 2.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import unittest
from unittest.mock import MagicMock, patch
from api import Api
class ApiTests(unittest.TestCase):
def setUp(self):
self.detector = MagicMock()
self.log = MagicMock()
self.api = Api(("localhost", 8080), self.detector, self.log)
def test_handle_request_no_request_method(self):
# Purpose: Test handling a request with no 'REQUEST_METHOD' field
# Expected behavior: The API should respond with a status code 400 and an error message indicating the missing field
environ = {}
response = self.api.handle_request(environ)
self.assertEqual(response.status, 400)
self.assertEqual(response.body, "Can not find required field: 'REQUEST_METHOD'\n")
def test_handle_request_no_document_uri(self):
# Purpose: Test handling a request with no 'DOCUMENT_URI' field
# Expected behavior: The API should respond with a status code 400 and an error message indicating the missing field
environ = {"REQUEST_METHOD": "POST"}
response = self.api.handle_request(environ)
self.assertEqual(response.status, 400)
self.assertEqual(response.body, "Can not find required field: 'DOCUMENT_URI'\n")
def test_handle_request_no_such_method(self):
# Purpose: Test handling a request with an unknown method
# Expected behavior: The API should respond with a status code 400 and an error message indicating the unknown method
environ = {"REQUEST_METHOD": "POST", "DOCUMENT_URI": "/unknown"}
response = self.api.handle_request(environ)
self.assertEqual(response.status, 400)
self.assertEqual(response.body, "Can not find requested method: 'POST: /unknown'\n")
def test_detect_invalid_content_type(self):
# Purpose: Test handling a request with an invalid content type
# Expected behavior: The API should respond with a status code 400 and an error message indicating the invalid content type
environ = {"REQUEST_METHOD": "POST", "DOCUMENT_URI": "/find_signatures", "CONTENT_TYPE": "application/json"}
response = self.api.handle_request(environ)
self.assertEqual(response.status, 400)
self.assertEqual(response.body,
"Invalid content type:\nProvided: 'application/json'\nExpected: 'multipart/form-data'\n")
@patch("api.main.np.frombuffer")
@patch("api.main.cv2.imdecode")
def test_detect_no_image_field(self, mock_imdecode, mock_frombuffer):
# Purpose: Test handling a request with no image field in the input stream
# Expected behavior: The API should respond with a status code 400 and an error message indicating the missing image field
mock_imdecode.return_value = MagicMock()
mock_frombuffer.return_value = MagicMock()
environ = {
"REQUEST_METHOD": "POST",
"DOCUMENT_URI": "/trace_signatures",
"CONTENT_TYPE": "multipart/form-data",
"wsgi.input": MagicMock()
}
form = MagicMock()
form.getvalue.return_value = None
environ["wsgi.input"].read.return_value = form
response = self.api.handle_request(environ)
self.assertEqual(response.status, 400)
self.assertEqual(response.body, "Invalid input stream field storage\n")
if __name__ == "__main__":
unittest.main()