Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
0a48d21
fix a b0rked code example in the permissions section of api guide
wimglenn Aug 12, 2016
b508bc8
Merge pull request #4396 from wimglenn/docs_bugfix
jpadilla Aug 13, 2016
075a0bd
Fix template syntax error for `as_list_of_strings` (#4403)
jamesbeith Aug 15, 2016
785b206
Tweak doctsring. Closes #4404 [ci skip]
lovelydinosaur Aug 15, 2016
101fd29
Do not include uploads in request.POST (#4407)
lovelydinosaur Aug 15, 2016
e3f8d06
Include .action attribute on viewsets when generating schemas (#4408)
lovelydinosaur Aug 15, 2016
966330a
Replace utf8 character ' with its ascii counterpart, makes bdist_rpm.…
nevun Aug 17, 2016
b76984d
Allow custom CSRF_HEADER_NAME setting. (#4415)
lovelydinosaur Aug 18, 2016
382ea77
Improve debug error handling (#4416)
lovelydinosaur Aug 18, 2016
59ca61a
Add django-rest-framework-roles to third party packages in permission…
r1b Aug 19, 2016
e5b4498
Initial tests for API client
lovelydinosaur Aug 19, 2016
63342e8
Version 3.4.5 (#4421)
lovelydinosaur Aug 19, 2016
a335309
Add __str__ method to PKOnlyObject (#4423)
lovelydinosaur Aug 19, 2016
d540f02
Improve Create to show the original exception traceback (#3508)
orf Aug 19, 2016
c5e80e1
Merge branch 'master' into api-client
lovelydinosaur Aug 19, 2016
1a73c1c
Initial test cases for API client
lovelydinosaur Aug 19, 2016
e615f6d
Tests for API clients
lovelydinosaur Aug 22, 2016
341fa58
Support raw file uplaods with requests client / api client.
lovelydinosaur Aug 22, 2016
fe706eb
Tweak to tests for py3
lovelydinosaur Aug 22, 2016
b56e7d3
Upload and download support
lovelydinosaur Sep 9, 2016
b41ec30
Py3 compat
lovelydinosaur Sep 9, 2016
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Tests for API clients
  • Loading branch information
lovelydinosaur committed Aug 22, 2016
commit e615f6d611979ce549f4859df76e109ceffdb72b
2 changes: 1 addition & 1 deletion requirements/requirements-optionals.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
markdown==2.6.4
django-guardian==1.4.3
django-filter==0.13.0
coreapi==1.32.0
coreapi==1.32.2
151 changes: 120 additions & 31 deletions tests/test_api_client.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import unicode_literals

import tempfile
import unittest

from django.conf.urls import url
Expand All @@ -17,19 +18,29 @@ def get_schema():
url='https://api.example.com/',
title='Example API',
content={
'simple_link': coreapi.Link('/example/'),
'query_params': coreapi.Link('/example/', fields=[
coreapi.Field(name='example')
]),
'form_params': coreapi.Link('/example/', action='post', fields=[
coreapi.Field(name='example')
]),
'body_params': coreapi.Link('/example/', action='post', fields=[
coreapi.Field(name='example', location='body')
]),
'path_params': coreapi.Link('/example/{id}', fields=[
coreapi.Field(name='id', location='path')
]),
'simple_link': coreapi.Link('/example/', description='example link'),
'location': {
'query': coreapi.Link('/example/', fields=[
coreapi.Field(name='example', description='example field')
]),
'form': coreapi.Link('/example/', action='post', fields=[
coreapi.Field(name='example'),
]),
'body': coreapi.Link('/example/', action='post', fields=[
coreapi.Field(name='example', location='body')
]),
'path': coreapi.Link('/example/{id}', fields=[
coreapi.Field(name='id', location='path')
])
},
'encoding': {
'multipart': coreapi.Link('/example/', action='post', encoding='multipart/form-data', fields=[
coreapi.Field(name='example')
]),
'urlencoded': coreapi.Link('/example/', action='post', encoding='application/x-www-form-urlencoded', fields=[
coreapi.Field(name='example')
]),
}
}
)

Expand All @@ -46,15 +57,39 @@ class ListView(APIView):
def get(self, request):
return Response({
'method': request.method,
'query_params': request.query_params,
'data': request.data
'query_params': request.query_params
})

def post(self, request):
if request.content_type:
content_type = request.content_type.split(';')[0]
else:
content_type = None

if isinstance(request.data, dict):
# Coerce multidict into regular dict, and remove files to
# make assertions simpler.
data = {
key: value for key, value in request.data.items()
if key not in request.FILES
}
else:
data = request.data

if request.FILES:
files = {
key: {'name': value.name, 'contents': value.read()}
for key, value in request.FILES.items()
}
else:
files = None

return Response({
'method': request.method,
'query_params': request.query_params,
'data': request.data
'data': data,
'files': files,
'content_type': content_type
})


Expand All @@ -63,8 +98,7 @@ def get(self, request, id):
return Response({
'id': id,
'method': request.method,
'query_params': request.query_params,
'data': request.data
'query_params': request.query_params
})


Expand All @@ -82,33 +116,88 @@ def test_api_client(self):
client = get_api_client()
schema = client.get('http://api.example.com/')
assert schema.title == 'Example API'
assert schema.url == 'https://api.example.com/'
assert schema['simple_link'].description == 'example link'
assert schema['location']['query'].fields[0].description == 'example field'
data = client.action(schema, ['simple_link'])
assert data == {'method': 'GET', 'query_params': {}, 'data': {}}
expected = {
'method': 'GET',
'query_params': {}
}
assert data == expected

def test_query_params(self):
client = get_api_client()
schema = client.get('http://api.example.com/')
assert schema.title == 'Example API'
data = client.action(schema, ['query_params'], params={'example': 123})
assert data == {'method': 'GET', 'query_params': {'example': '123'}, 'data': {}}
data = client.action(schema, ['location', 'query'], params={'example': 123})
expected = {
'method': 'GET',
'query_params': {'example': '123'}
}
assert data == expected

def test_form_params(self):
client = get_api_client()
schema = client.get('http://api.example.com/')
assert schema.title == 'Example API'
data = client.action(schema, ['form_params'], params={'example': 123})
assert data == {'method': 'POST', 'query_params': {}, 'data': {'example': 123}}
data = client.action(schema, ['location', 'form'], params={'example': 123})
expected = {
'method': 'POST',
'content_type': 'application/json',
'query_params': {},
'data': {'example': 123},
'files': None
}
assert data == expected

def test_body_params(self):
client = get_api_client()
schema = client.get('http://api.example.com/')
assert schema.title == 'Example API'
data = client.action(schema, ['body_params'], params={'example': 123})
assert data == {'method': 'POST', 'query_params': {}, 'data': 123}
data = client.action(schema, ['location', 'body'], params={'example': 123})
expected = {
'method': 'POST',
'content_type': 'application/json',
'query_params': {},
'data': 123,
'files': None
}
assert data == expected

def test_path_params(self):
client = get_api_client()
schema = client.get('http://api.example.com/')
assert schema.title == 'Example API'
data = client.action(schema, ['path_params'], params={'id': 123})
assert data == {'method': 'GET', 'query_params': {}, 'data': {}, 'id': '123'}
data = client.action(schema, ['location', 'path'], params={'id': 123})
expected = {
'method': 'GET',
'query_params': {},
'id': '123'
}
assert data == expected

def test_multipart_encoding(self):
client = get_api_client()
schema = client.get('http://api.example.com/')
temp = tempfile.TemporaryFile()
temp.write('example file contents')
temp.seek(0)
data = client.action(schema, ['encoding', 'multipart'], params={'example': temp})
expected = {
'method': 'POST',
'content_type': 'multipart/form-data',
'query_params': {},
'data': {},
'files': {'example': {'name': 'example', 'contents': 'example file contents'}}
}
assert data == expected

def test_urlencoded_encoding(self):
client = get_api_client()
schema = client.get('http://api.example.com/')
data = client.action(schema, ['encoding', 'urlencoded'], params={'example': 123})
expected = {
'method': 'POST',
'content_type': 'application/x-www-form-urlencoded',
'query_params': {},
'data': {'example': '123'},
'files': None
}
assert data == expected