-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi_test.py
266 lines (231 loc) · 8.26 KB
/
api_test.py
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
import json
from unittest2 import TestCase
from werkzeug.wrappers import Response
from werkzeug.test import Client as TestClient
from cosmic.api import API
from cosmic.http import Server
from cosmic.models import BaseModel
from cosmic.globals import cosmos
from cosmic.types import *
cookbook_spec = {
u'name': u'cookbook',
u'actions': {
u'map': {
u'cabbage': {
u'accepts': {
u'type': u"Struct",
u"param": {
u"map": {
u"spicy": {
u"required": True,
u"schema": {u"type": u"Boolean"}
},
u"capitalize": {
u"required": False,
u"schema": {u"type": u"Boolean"}
}
},
u"order": [u"spicy", u"capitalize"]
}
},
u'returns': {u'type': u'String'},
u'doc': u"Yay cabbage"
},
u'noop': {
u'doc': u"Does not do anything"
}
},
u'order': [u'cabbage', u'noop']
},
u"models": {
u"map": {
u"Recipe": {
u"properties": {
u"map": {
u"name": {
u"required": True,
u"schema": {u"type": u"String"}
},
},
u"order": [u"name"]
},
u"links": {
u"map": {},
u"order": []
},
u"query_fields": {
u"map": {},
u"order": []
},
u"list_metadata": {
u"map": {},
u"order": []
},
u'methods': {
u'get_by_id': False,
u'get_list': False,
u'create': False,
u'update': False,
u'delete': False,
},
},
u"Author": {
u"properties": {
u"map": {
u"is_gordon_ramsay": {
u"required": True,
u"schema": {u"type": u"Boolean"}
},
},
u"order": [u"is_gordon_ramsay"]
},
u"links": {
u"map": {},
u"order": []
},
u"query_fields": {
u"map": {
u"is_gordon_ramsay": {
u"required": True,
u"schema": {u"type": u"Boolean"}
},
},
u"order": [u"is_gordon_ramsay"]
},
u"list_metadata": {
u"map": {},
u"order": []
},
u'methods': {
u'get_by_id': False,
u'get_list': True,
u'create': False,
u'update': False,
u'delete': False,
},
}
},
u"order": [u"Recipe", u"Author"]
}
}
class TestAPI(TestCase):
def setUp(self):
self.maxDiff = None
self._old_cosmos = cosmos.data
cosmos.data = {}
self.cookbook = cookbook = API(u'cookbook')
@cookbook.action(
accepts=Struct([
required(u"spicy", Boolean),
optional(u"capitalize", Boolean)
]),
returns=String)
def cabbage(spicy, capitalize=False):
"Yay cabbage"
if spicy:
c = "kimchi"
else:
c = "sauerkraut"
if capitalize:
return c.capitalize()
else:
return c
@cookbook.action(accepts=None, returns=None)
def noop():
"Does not do anything"
pass
@cookbook.model
class Recipe(BaseModel):
properties = [
required(u"name", String)
]
@classmethod
def validate_patch(cls, datum):
if datum["name"] == "bacon":
raise ValidationError("Not kosher")
@cookbook.model
class Author(BaseModel):
methods = ['get_list']
properties = [
required(u"is_gordon_ramsay", Boolean)
]
query_fields = [
required(u"is_gordon_ramsay", Boolean)
]
@classmethod
def get_list(cls, is_gordon_ramsey):
return [("0", {"is_gordon_ramsey": True})]
self.Author = Author
self.server = Server(self.cookbook)
self.server.debug = True
self.app = self.server.wsgi_app
self.client = TestClient(self.app, response_wrapper=Response)
def tearDown(self):
cosmos.data = self._old_cosmos
def test_get_list_missing(self):
resp = self.client.get('/Author')
self.assertEqual(resp.status_code, 400)
def test_model(self):
d = {
"_links": {
"self": {"href": "/Recipe/24"}
},
"name": "pancake"
}
(id, rep) = Representation(Model('cookbook.Recipe')).from_json(d)
self.assertEqual(rep['name'], "pancake")
self.assertEqual(Representation(Model('cookbook.Recipe')).to_json((id, rep)), d)
def test_model_deserialize_okay(self):
(id, rep) = Representation(Model('cookbook.Recipe')).from_json({
"_links": {
"self": {"href": "/Recipe/14"}
},
"name": "turkey"
})
self.assertEqual(rep['name'], "turkey")
def test_subclassing_hook(self):
self.assertEqual(set(self.cookbook.models.__dict__.keys()), set(["Recipe", "Author"]))
def test_recursive_subclassing_hook(self):
@self.cookbook.model
class ChocolateAuthor(self.Author):
pass
self.assertEqual(set(self.cookbook.models.__dict__.keys()), set(["Recipe", "Author", "ChocolateAuthor"]))
def test_model_schema_validation(self):
with self.assertRaises(ValidationError):
Representation(Model('cookbook.Recipe')).from_json(1.1)
def test_model_custom_validation(self):
with self.assertRaisesRegexp(ValidationError, "kosher"):
(id, rep) = Representation(Model('cookbook.Recipe')).from_json({
"_links": {
"self": {"href": "/Recipe/123"}
},
"name": "bacon"
})
self.cookbook.models.Recipe.validate_patch(rep)
def test_serialize(self):
self.assertEqual(APISpec.to_json(self.cookbook.spec), cookbook_spec)
def test_call_action_with_args(self):
self.assertEqual(self.cookbook.actions.cabbage(spicy=False), "sauerkraut")
def test_spec_endpoint(self):
res = self.client.get('/spec.json')
self.assertEqual(json.loads(res.data), cookbook_spec)
def test_spec_wrong_method(self):
res = self.client.get('/actions/noop')
self.assertEqual(res.status_code, 404)
res = self.client.post('/spec.json')
self.assertEqual(res.status_code, 404)
def test_wrong_content_type(self):
res = self.client.post('/actions/cabbage', data="1", content_type="application/xml")
self.assertEqual(res.status_code, 400)
self.assertRegexpMatches(res.data, "Content-Type")
def test_action_okay(self):
data = json.dumps({"spicy": True})
res = self.client.post('/actions/cabbage', data=data, content_type="application/json")
self.assertEqual(res.status_code, 200)
self.assertEqual(res.data, '"kimchi"')
def test_noop_action_okay(self):
res = self.client.post('/actions/noop', data='')
self.assertEqual(res.status_code, 204)
self.assertEqual(res.data, '')
def test_schema(self):
APISpec.from_json(APISpec.to_json(self.cookbook.spec))