|
1
|
|
|
import json |
|
2
|
|
|
|
|
3
|
|
|
from pydantic import BaseModel, ValidationError, constr |
|
4
|
|
|
|
|
5
|
|
|
from app.context import context_property |
|
6
|
|
|
from app.decorators.validation import validate |
|
7
|
|
|
from tests import BaseTestCase |
|
8
|
|
|
|
|
9
|
|
|
|
|
10
|
|
|
class TestValidate(BaseTestCase): |
|
11
|
|
|
""" |
|
12
|
|
|
validate로 데코레이팅된 함수를 만들고, |
|
13
|
|
|
요청 데이터와 함께 request context를 열어서 이를 호출하는 방식으로 테스트를 진행합니다. |
|
14
|
|
|
""" |
|
15
|
|
|
|
|
16
|
|
|
class TestSchema(BaseModel): |
|
17
|
|
|
foo: constr(min_length=1) |
|
18
|
|
|
|
|
19
|
|
|
def setUp(self): |
|
20
|
|
|
super(TestValidate, self).setUp() |
|
21
|
|
|
|
|
22
|
|
|
def initialize_function_and_call( |
|
23
|
|
|
self, |
|
24
|
|
|
decorator_kwargs: dict = None, |
|
25
|
|
|
handler_kwargs: dict = None |
|
26
|
|
|
): |
|
27
|
|
|
""" |
|
28
|
|
|
인자 정보를 통해 `validate_with_pydantic`으로 데코레이팅된 함수를 생성하고, 호출합니다. |
|
29
|
|
|
""" |
|
30
|
|
|
|
|
31
|
|
|
if decorator_kwargs is None: |
|
32
|
|
|
decorator_kwargs = {} |
|
33
|
|
|
|
|
34
|
|
|
if handler_kwargs is None: |
|
35
|
|
|
handler_kwargs = {} |
|
36
|
|
|
|
|
37
|
|
|
@validate( |
|
38
|
|
|
**decorator_kwargs |
|
39
|
|
|
) |
|
40
|
|
|
def handler(**kwargs): |
|
41
|
|
|
pass |
|
42
|
|
|
|
|
43
|
|
|
handler(**handler_kwargs) |
|
44
|
|
|
|
|
45
|
|
|
def test_path_params_validation(self): |
|
46
|
|
|
with self.app.test_request_context(): |
|
47
|
|
|
self.initialize_function_and_call( |
|
48
|
|
|
decorator_kwargs={'path_params': self.TestSchema}, |
|
49
|
|
|
handler_kwargs={'foo': 'bar'} |
|
50
|
|
|
) |
|
51
|
|
|
self.assertEqual(self.TestSchema(foo='bar'), context_property.request_path_params) |
|
52
|
|
|
|
|
53
|
|
|
def test_path_params_validation_error(self): |
|
54
|
|
|
with self.app.test_request_context(): |
|
55
|
|
|
with self.assertRaises(ValidationError): |
|
56
|
|
|
self.initialize_function_and_call( |
|
57
|
|
|
decorator_kwargs={'path_params': self.TestSchema}, |
|
58
|
|
|
handler_kwargs={'foo': ''} |
|
59
|
|
|
) |
|
60
|
|
|
|
|
61
|
|
|
self.assertIsNone(context_property.request_path_params) |
|
62
|
|
|
|
|
63
|
|
|
def test_query_params_validation(self): |
|
64
|
|
|
with self.app.test_request_context(query_string={'foo': 'bar'}): |
|
65
|
|
|
self.initialize_function_and_call( |
|
66
|
|
|
decorator_kwargs={'query_params': self.TestSchema}, |
|
67
|
|
|
) |
|
68
|
|
|
self.assertEqual(self.TestSchema(foo='bar'), context_property.request_query_params) |
|
69
|
|
|
|
|
70
|
|
|
def test_query_params_validation_error(self): |
|
71
|
|
|
with self.app.test_request_context(query_string={'foo': ''}): |
|
72
|
|
|
with self.assertRaises(ValidationError): |
|
73
|
|
|
self.initialize_function_and_call( |
|
74
|
|
|
decorator_kwargs={'query_params': self.TestSchema}, |
|
75
|
|
|
) |
|
76
|
|
|
|
|
77
|
|
|
self.assertIsNone(context_property.request_query_params) |
|
78
|
|
|
|
|
79
|
|
|
def test_json_validation(self): |
|
80
|
|
|
with self.app.test_request_context(json={'foo': 'bar'}): |
|
81
|
|
|
self.initialize_function_and_call( |
|
82
|
|
|
decorator_kwargs={'json': self.TestSchema}, |
|
83
|
|
|
) |
|
84
|
|
|
self.assertEqual(self.TestSchema(foo='bar'), context_property.request_json) |
|
85
|
|
|
|
|
86
|
|
|
def test_json_validation_error(self): |
|
87
|
|
|
with self.app.test_request_context(json={'foo': ''}): |
|
88
|
|
|
with self.assertRaises(ValidationError): |
|
89
|
|
|
self.initialize_function_and_call( |
|
90
|
|
|
decorator_kwargs={'json': self.TestSchema}, |
|
91
|
|
|
) |
|
92
|
|
|
|
|
93
|
|
|
self.assertIsNone(context_property.request_query_params) |
|
94
|
|
|
|
|
95
|
|
|
def test_json_without_content_type(self): |
|
96
|
|
|
with self.app.test_request_context(data=json.dumps({"foo": "bar"})): |
|
97
|
|
|
with self.assertRaises(ValidationError): |
|
98
|
|
|
self.initialize_function_and_call( |
|
99
|
|
|
decorator_kwargs={'json': self.TestSchema}, |
|
100
|
|
|
) |
|
101
|
|
|
|
|
102
|
|
|
self.assertIsNone(context_property.request_query_params) |
|
103
|
|
|
|
|
104
|
|
|
def test_json_without_content_type__with_json_force_load(self): |
|
105
|
|
|
with self.app.test_request_context(data=json.dumps({"foo": "bar"})): |
|
106
|
|
|
self.initialize_function_and_call( |
|
107
|
|
|
decorator_kwargs={'json': self.TestSchema, 'json_force_load': True}, |
|
108
|
|
|
) |
|
109
|
|
|
self.assertEqual(self.TestSchema(foo='bar'), context_property.request_json) |
|
110
|
|
|
|