-
Notifications
You must be signed in to change notification settings - Fork 2k
/
test_api.py
358 lines (295 loc) · 11.8 KB
/
test_api.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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
'''
NB Don't test logic functions here. This is just for the mechanics of the API
controller itself.
'''
import json
import re
from ckan.lib.helpers import url_for
from nose.tools import assert_equal, eq_
import ckan.tests.helpers as helpers
from ckan.tests.helpers import assert_in
from ckan.tests import factories
from ckan import model
class TestApiController(helpers.FunctionalTestBase):
def test_unicode_in_error_message_works_ok(self):
# Use tag_delete to echo back some unicode
app = self._get_test_app()
org_url = '/api/action/tag_delete'
data_dict = {'id': u'Delta symbol: \u0394'} # unicode gets rec'd ok
postparams = '%s=1' % json.dumps(data_dict)
response = app.post(url=org_url, params=postparams, status=404)
# The unicode is backslash encoded (because that is the default when
# you do str(exception) )
assert 'Delta symbol: \\u0394' in response.body
def test_dataset_autocomplete_name(self):
dataset = factories.Dataset(name='rivers')
app = self._get_test_app()
with app.flask_app.test_request_context():
url = url_for(controller='api', action='dataset_autocomplete',
ver='/2')
assert_equal(url, '/api/2/util/dataset/autocomplete')
response = app.get(
url=url,
params={
'incomplete': u'rive',
},
status=200,
)
results = json.loads(response.body)
assert_equal(results, {"ResultSet": {"Result": [{
'match_field': 'name',
"name": "rivers",
'match_displayed': 'rivers',
'title': dataset['title'],
}]}})
assert_equal(response.headers['Content-Type'],
'application/json;charset=utf-8')
def test_dataset_autocomplete_title(self):
dataset = factories.Dataset(name='test_ri', title='Rivers')
app = self._get_test_app()
with app.flask_app.test_request_context():
url = url_for(controller='api', action='dataset_autocomplete',
ver='/2')
assert_equal(url, '/api/2/util/dataset/autocomplete')
app = self._get_test_app()
response = app.get(
url=url,
params={
'incomplete': u'riv',
},
status=200,
)
results = json.loads(response.body)
assert_equal(results, {"ResultSet": {"Result": [{
'match_field': 'title',
"name": dataset['name'],
'match_displayed': 'Rivers (test_ri)',
'title': 'Rivers',
}]}})
assert_equal(response.headers['Content-Type'],
'application/json;charset=utf-8')
def test_tag_autocomplete(self):
factories.Dataset(tags=[{'name': 'rivers'}])
app = self._get_test_app()
with app.flask_app.test_request_context():
url = url_for(controller='api', action='tag_autocomplete',
ver='/2')
assert_equal(url, '/api/2/util/tag/autocomplete')
response = app.get(
url=url,
params={
'incomplete': u'rive',
},
status=200,
)
results = json.loads(response.body)
assert_equal(results, {"ResultSet": {"Result": [{"Name": "rivers"}]}})
assert_equal(response.headers['Content-Type'],
'application/json;charset=utf-8')
def test_group_autocomplete_by_name(self):
factories.Group(name='rivers', title='Bridges')
app = self._get_test_app()
with app.flask_app.test_request_context():
url = url_for(controller='api', action='group_autocomplete',
ver='/2')
assert_equal(url, '/api/2/util/group/autocomplete')
response = app.get(
url=url,
params={
'q': u'rive',
},
status=200,
)
results = json.loads(response.body)
assert_equal(len(results), 1)
assert_equal(results[0]['name'], 'rivers')
assert_equal(results[0]['title'], 'Bridges')
assert_equal(response.headers['Content-Type'],
'application/json;charset=utf-8')
def test_group_autocomplete_by_title(self):
factories.Group(name='frogs', title='Bugs')
app = self._get_test_app()
with app.flask_app.test_request_context():
url = url_for(controller='api', action='group_autocomplete',
ver='/2')
response = app.get(
url=url,
params={
'q': u'bug',
},
status=200,
)
results = json.loads(response.body)
assert_equal(len(results), 1)
assert_equal(results[0]['name'], 'frogs')
def test_organization_autocomplete_by_name(self):
org = factories.Organization(name='simple-dummy-org')
app = self._get_test_app()
with app.flask_app.test_request_context():
url = url_for(controller='api', action='organization_autocomplete',
ver='/2')
assert_equal(url, '/api/2/util/organization/autocomplete')
response = app.get(
url=url,
params={
'q': u'simple',
},
status=200,
)
results = json.loads(response.body)
assert_equal(len(results), 1)
assert_equal(results[0]['name'], 'simple-dummy-org')
assert_equal(results[0]['title'], org['title'])
assert_equal(response.headers['Content-Type'],
'application/json;charset=utf-8')
def test_organization_autocomplete_by_title(self):
factories.Organization(title='Simple dummy org')
app = self._get_test_app()
with app.flask_app.test_request_context():
url = url_for(controller='api', action='organization_autocomplete',
ver='/2')
response = app.get(
url=url,
params={
'q': u'simple dum',
},
status=200,
)
results = json.loads(response.body)
assert_equal(len(results), 1)
assert_equal(results[0]['title'], 'Simple dummy org')
def test_config_option_list_access_sysadmin(self):
user = factories.Sysadmin()
app = self._get_test_app()
with app.flask_app.test_request_context():
url = url_for(
controller='api',
action='action',
logic_function='config_option_list',
ver='/3')
app.get(
url=url,
params={},
extra_environ={'REMOTE_USER': user['name'].encode('ascii')},
status=200,
)
def test_config_option_list_access_sysadmin_jsonp(self):
user = factories.Sysadmin()
app = self._get_test_app()
with app.flask_app.test_request_context():
url = url_for(
controller='api',
action='action',
logic_function='config_option_list',
ver='/3')
app.get(
url=url,
params={'callback': 'myfn'},
extra_environ={'REMOTE_USER': user['name'].encode('ascii')},
status=403,
)
def test_jsonp_works_on_get_requests(self):
dataset1 = factories.Dataset()
dataset2 = factories.Dataset()
app = self._get_test_app()
with app.flask_app.test_request_context():
url = url_for(
controller='api',
action='action',
logic_function='package_list',
ver='/3')
res = app.get(
url=url,
params={'callback': 'my_callback'},
)
assert re.match('my_callback\(.*\);', res.body), res
# Unwrap JSONP callback (we want to look at the data).
msg = res.body[len('my_callback') + 1:-2]
res_dict = json.loads(msg)
eq_(res_dict['success'], True)
eq_(sorted(res_dict['result']),
sorted([dataset1['name'], dataset2['name']]))
def test_jsonp_does_not_work_on_post_requests(self):
dataset1 = factories.Dataset()
dataset2 = factories.Dataset()
app = self._get_test_app()
with app.flask_app.test_request_context():
url = url_for(
controller='api',
action='action',
logic_function='package_list',
ver='/3',
callback='my_callback',
)
res = app.post(
url=url,
)
# The callback param is ignored and the normal response is returned
assert not res.body.startswith('my_callback')
res_dict = json.loads(res.body)
eq_(res_dict['success'], True)
eq_(sorted(res_dict['result']),
sorted([dataset1['name'], dataset2['name']]))
class TestRevisionSearch(helpers.FunctionalTestBase):
# Error cases
def test_no_search_term(self):
app = self._get_test_app()
response = app.get('/api/search/revision', status=400)
assert_in('Bad request - Missing search term', response.body)
def test_no_search_term_api_v2(self):
app = self._get_test_app()
response = app.get('/api/2/search/revision', status=400)
assert_in('Bad request - Missing search term', response.body)
def test_date_instead_of_revision(self):
app = self._get_test_app()
response = app.get('/api/search/revision'
'?since_id=2010-01-01T00:00:00', status=404)
assert_in('Not found - There is no revision', response.body)
def test_date_invalid(self):
app = self._get_test_app()
response = app.get('/api/search/revision'
'?since_time=2010-02-31T00:00:00', status=400)
assert_in('Bad request - ValueError: day is out of range for month',
response.body)
def test_no_value(self):
app = self._get_test_app()
response = app.get('/api/search/revision?since_id=', status=400)
assert_in('Bad request - No revision specified', response.body)
def test_revision_doesnt_exist(self):
app = self._get_test_app()
response = app.get('/api/search/revision?since_id=1234', status=404)
assert_in('Not found - There is no revision', response.body)
def test_revision_doesnt_exist_api_v2(self):
app = self._get_test_app()
response = app.get('/api/2/search/revision?since_id=1234', status=404)
assert_in('Not found - There is no revision', response.body)
# Normal usage
@classmethod
def _create_revisions(cls, num_revisions):
rev_ids = []
for i in xrange(num_revisions):
rev = model.repo.new_revision()
rev.id = unicode(i)
model.Session.commit()
rev_ids.append(rev.id)
return rev_ids
def test_revision_since_id(self):
rev_ids = self._create_revisions(4)
app = self._get_test_app()
response = app.get('/api/2/search/revision?since_id=%s' % rev_ids[1])
res = json.loads(response.body)
assert_equal(res, rev_ids[2:])
def test_revision_since_time(self):
rev_ids = self._create_revisions(4)
app = self._get_test_app()
rev1 = model.Session.query(model.Revision).get(rev_ids[1])
response = app.get('/api/2/search/revision?since_time=%s'
% rev1.timestamp.isoformat())
res = json.loads(response.body)
assert_equal(res, rev_ids[2:])
def test_revisions_returned_are_limited(self):
rev_ids = self._create_revisions(55)
app = self._get_test_app()
response = app.get('/api/2/search/revision?since_id=%s' % rev_ids[1])
res = json.loads(response.body)
assert_equal(res, rev_ids[2:52]) # i.e. limited to 50