Skip to content

Commit

Permalink
Merge pull request #546 from okfn/546-get-actions-no-params
Browse files Browse the repository at this point in the history
GETable actions always need a URL parameter even if not required
  • Loading branch information
domoritz committed Mar 6, 2013
2 parents 34f1e4a + f824f4e commit c19b6ae
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 4 deletions.
7 changes: 5 additions & 2 deletions ckan/controllers/api.py
Expand Up @@ -832,8 +832,11 @@ def make_unicode(entity):
raise ValueError(msg)
cls.log.debug('Retrieved request body: %r' % request.body)
if not request_data:
msg = "No request body data"
raise ValueError(msg)
if not try_url_params:
msg = "No request body data"
raise ValueError(msg)
else:
request_data = {}
if request_data:
try:
request_data = h.json.loads(request_data, encoding='utf8')
Expand Down
43 changes: 41 additions & 2 deletions ckan/tests/logic/test_action.py
@@ -1,5 +1,6 @@
import re
import json
import urllib
from pprint import pprint
from nose.tools import assert_equal, assert_raises
from nose.plugins.skip import SkipTest
Expand Down Expand Up @@ -66,6 +67,12 @@ def test_01_package_list(self):
assert res['help'].startswith(
"Return a list of the names of the site's datasets (packages).")

# Test GET request
res = json.loads(self.app.get('/api/action/package_list').body)
assert len(res['result']) == 2
assert 'warandpeace' in res['result']
assert 'annakarenina' in res['result']

def test_01_package_show(self):
anna_id = model.Package.by_name(u'annakarenina').id
postparams = '%s=1' % json.dumps({'id': anna_id})
Expand Down Expand Up @@ -453,6 +460,11 @@ def test_13_group_list(self):
assert res_obj['help'].startswith(
"Return a list of the names of the site's groups.")

# Test GET request
res = self.app.get('/api/action/group_list')
res_obj = json.loads(res.body)
assert res_obj['result'] == ['david', 'roger']

#Get all fields
postparams = '%s=1' % json.dumps({'all_fields':True})
res = self.app.post('/api/action/group_list', params=postparams)
Expand Down Expand Up @@ -1176,19 +1188,46 @@ def teardown_class(self):
model.repo.rebuild_db()

def test_1_basic(self):
postparams = '%s=1' % json.dumps({
params = {
'q':'tolstoy',
'facet.field': ('groups', 'tags', 'res_format', 'license'),
'rows': 20,
'start': 0,
})
}
postparams = '%s=1' % json.dumps(params)
res = self.app.post('/api/action/package_search', params=postparams)
res = json.loads(res.body)
result = res['result']
assert_equal(res['success'], True)
assert_equal(result['count'], 1)
assert_equal(result['results'][0]['name'], 'annakarenina')

# Test GET request
url_params = urllib.urlencode(params)
res = self.app.get('/api/action/package_search?{0}'.format(url_params))
res = json.loads(res.body)
result = res['result']
assert_equal(res['success'], True)
assert_equal(result['count'], 1)
assert_equal(result['results'][0]['name'], 'annakarenina')

def test_1_basic_no_params(self):
postparams = '%s=1' % json.dumps({})
res = self.app.post('/api/action/package_search', params=postparams)
res = json.loads(res.body)
result = res['result']
assert_equal(res['success'], True)
assert_equal(result['count'], 2)
assert_equal(result['results'][0]['name'], 'annakarenina')

# Test GET request
res = self.app.get('/api/action/package_search')
res = json.loads(res.body)
result = res['result']
assert_equal(res['success'], True)
assert_equal(result['count'], 2)
assert_equal(result['results'][0]['name'], 'annakarenina')

def test_2_bad_param(self):
postparams = '%s=1' % json.dumps({
'sort':'metadata_modified',
Expand Down

0 comments on commit c19b6ae

Please sign in to comment.