-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtests.py
55 lines (49 loc) · 1.82 KB
/
tests.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
import unittest
from coreapi import Document, Field, Link
from django.conf import settings
settings.configure(ROOT_URLCONF='test_app', DEBUG=True, REST_FRAMEWORK={'UNAUTHENTICATED_USER': None}, ALLOWED_HOSTS='*')
import django
django.setup()
from django_coreapi import mock
from django_coreapi.client import DjangoCoreAPIClient
from django_coreapi.transports import DjangoTestHTTPTransport
class Tests(unittest.TestCase):
def test_client(self):
client = DjangoCoreAPIClient()
doc = client.get('/')
self.assertIsNotNone(doc)
def test_client_headers(self):
transport = DjangoTestHTTPTransport(headers={'authorization': 'token'})
client = DjangoCoreAPIClient(transports=[transport])
doc = client.get('/headers/')
self.assertIsNotNone(doc)
def test_post_data(self):
content = {
'test': {
'post_data': Link(url='/post_data/', action='post', fields=[
Field('data', location='body')
]),
}
}
schema = Document(title='test', content=content)
client = DjangoCoreAPIClient()
doc = client.action(schema, ['test', 'post_data'], params={'data': {
'test': 'cat'
}})
self.assertIsNotNone(doc)
@mock.activate
def test_mocking(self):
content = {
'test': {
'post_data': Link(url='/post_data/', action='post', fields=[
Field('data', location='body')
]),
}
}
schema = Document(title='test', content=content)
mock.add(schema, ['test', 'post_data'], {"a": 1})
client = DjangoCoreAPIClient()
doc = client.action(schema, ['test', 'post_data'], params={'data': {
'test': 'cat'
}})
self.assertEqual(doc, {"a": 1})