-
Notifications
You must be signed in to change notification settings - Fork 43
/
tests.py
161 lines (130 loc) · 5.01 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
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
# vim: set ts=4 sw=4 et:
import json
import zlib
from functools import wraps
from unittest import TestCase
from uuid import uuid4
try:
from urlparse import parse_qsl, urlparse
except ImportError:
from urllib.parse import parse_qsl, urlparse
from mock import patch
from sanction import Client, transport_headers
from sanction.test import with_patched_client
AUTH_ENDPOINT = "http://example.com"
TOKEN_ENDPOINT = "http://example.com/token"
RESOURCE_ENDPOINT = "http://example.com/resource"
CLIENT_ID = "client_id"
CLIENT_SECRET = "client_secret"
REDIRECT_URI = "redirect_uri"
SCOPE = 'foo,bar'
STATE = str(uuid4())
ACCESS_TOKEN = 'access_token'
class TestClient(TestCase):
def setUp(self):
self.client = Client(auth_endpoint=AUTH_ENDPOINT,
token_endpoint=TOKEN_ENDPOINT,
resource_endpoint=RESOURCE_ENDPOINT,
client_id=CLIENT_ID)
def test_init(self):
map(lambda c: self.assertEqual(*c),
((self.client.auth_endpoint, AUTH_ENDPOINT),
(self.client.token_endpoint, TOKEN_ENDPOINT),
(self.client.resource_endpoint, RESOURCE_ENDPOINT),
(self.client.client_id, CLIENT_ID),))
def test_auth_uri(self):
parsed = urlparse(self.client.auth_uri(redirect_uri=REDIRECT_URI))
qs = dict(parse_qsl(parsed.query))
map(lambda c: self.assertEqual(*c),
((qs['redirect_uri'], REDIRECT_URI),
(qs['response_type'], 'code'),
(qs['client_id'], CLIENT_ID)))
parsed = urlparse(self.client.auth_uri(scope=SCOPE))
qs = dict(parse_qsl(parsed.query))
self.assertEqual(qs['scope'], SCOPE)
parsed = urlparse(self.client.auth_uri(state=STATE))
qs = dict(parse_qsl(parsed.query))
self.assertEqual(qs['state'], STATE)
@with_patched_client(json.dumps({
'access_token':'test_token',
'expires_in': 300,
}))
def test_request_token_json(self):
self.client.request_token()
self.assertEqual(self.client.access_token, 'test_token')
self.client.request_token(redirect_uri=REDIRECT_URI)
self.assertEqual(self.client.access_token, 'test_token')
@with_patched_client('access_token=test_token')
def test_request_token_url(self):
self.client.request_token()
self.assertEqual(self.client.access_token, 'test_token')
@with_patched_client(json.dumps({
'access_token': 'refreshed_token',
}))
def test_refresh_token(self):
self.client.refresh_token = 'refresh_token'
self.client.refresh()
self.assertEqual(self.client.access_token, 'refreshed_token')
@with_patched_client(json.dumps({
'userid': 1234
}))
def test_request(self):
self.client.access_token = 'foo'
data = self.client.request('/foo')
self.assertEqual(data['userid'], 1234)
@with_patched_client(zlib.compress(json.dumps({
'userid': 1234
}).encode('utf8')))
def test_request_custom_parser(self):
def _decompress(buf):
return json.loads(zlib.decompress(buf).decode())
self.client.access_token = 'foo'
data = self.client.request('/foo', parser=_decompress)
self.assertEqual(data['userid'], 1234)
@with_patched_client(json.dumps({
'userid': 1234
}))
def test_request_transport_headers(self):
self.client.token_transport = transport_headers
self.client.access_token = 'foo'
data = self.client.request('/foo')
self.assertEqual(data['userid'], 1234)
@with_patched_client(json.dumps({
'userid': 1234
}), headers={
'Content-Type': 'text/html; charset=utf-8',
})
def test_request_with_charset(self):
self.client.access_token = 'foo'
data = self.client.request('/foo')
self.assertEqual(data['userid'], 1234)
@with_patched_client(json.dumps({
'userid': 1234
}))
def test_custom_transport(self):
def _transport(url, access_token, data=None, method=None,
headers=None):
self.assertEqual(url, 'http://example.com/resource/foo')
self.assertEqual(access_token, 'foo')
self.client.access_token = 'foo'
self.client.token_transport = _transport
data = self.client.request('/foo', headers={
'Content-Type': 'application/json'})
self.assertEqual(data['userid'], 1234)
@with_patched_client(json.dumps({
'userid': 1234
}))
def test_query_transport_with_headers(self):
self.client.access_token = 'foo'
data = self.client.request('/foo', headers={'Content-Type':
'application/json'})
self.assertEqual(data['userid'], 1234)
@with_patched_client(json.dumps({
'userid': 1234
}))
def test_header_transport_with_headers(self):
self.client.access_token = 'foo'
self.client.token_transport = transport_headers
data = self.client.request('/foo', headers={'Content-Type':
'application/json'})
self.assertEqual(data['userid'], 1234)