This repository was archived by the owner on Oct 2, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtests.py
172 lines (144 loc) · 7.17 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
162
163
164
165
166
167
168
169
170
171
import unittest
import os
import urlparse
import toopher
class HttpClientMock(object):
def __init__(self, paths):
self.paths = paths
def request(self, uri, method, data):
self.last_called_uri = uri
self.last_called_method = method
self.last_called_data = urlparse.parse_qs(data)
if uri in self.paths:
return self.paths[uri]
else:
return {'status':400}, None
class ToopherTests(unittest.TestCase):
def test_constructor(self):
with self.assertRaises(TypeError):
api = toopher.ToopherApi()
api = toopher.ToopherApi('key', 'secret', api_url='http://testonly')
def test_create_pairing(self):
api = toopher.ToopherApi('key', 'secret', api_url='http://testonly')
api.client = HttpClientMock({
'http://testonly/pairings/create':(
{'status':'200'},
'{"id":"1", "enabled":true, "user":{"id":"1","name":"some user"}}'
)
})
pairing = api.pair('awkward turtle', 'some user')
self.assertEqual(api.client.last_called_method, 'POST')
self.assertEqual(api.client.last_called_data['pairing_phrase'], ['awkward turtle'])
with self.assertRaises(KeyError):
self.assertEqual(api.client.last_called_data['test_param'], ['42'])
def test_pairing_status(self):
api = toopher.ToopherApi('key', 'secret', api_url='http://testonly')
api.client = HttpClientMock({
'http://testonly/pairings/1':(
{'status':'200'},
'{"id":"1", "enabled":true, "user":{"id":"1","name":"some user"}}'
)
})
pairing = api.get_pairing_status('1')
self.assertEqual(api.client.last_called_method, 'GET')
self.assertEqual(pairing.id, '1')
self.assertEqual(pairing.user_name, 'some user')
self.assertEqual(pairing.user_id, '1')
self.assertTrue(pairing.enabled)
with self.assertRaises(KeyError):
foo = pairing.random_key
def test_create_authentication_request(self):
api = toopher.ToopherApi('key', 'secret', api_url='http://testonly')
api.client = HttpClientMock({
'http://testonly/authentication_requests/initiate':(
{'status':'200'},
'{"id":"1", "pending":false, "granted":true, "automated":false, "reason":"its a test", "terminal":{"id":"1", "name":"test terminal"}}'
)
})
auth_request = api.authenticate('1', 'test terminal')
self.assertEqual(api.client.last_called_method, 'POST')
self.assertEqual(api.client.last_called_data['pairing_id'], ['1'])
self.assertEqual(api.client.last_called_data['terminal_name'], ['test terminal'])
with self.assertRaises(KeyError):
self.assertEqual(api.client.last_called_data['test_param'], ['42'])
def test_authentication_status(self):
api = toopher.ToopherApi('key', 'secret', api_url='http://testonly')
api.client = HttpClientMock({
'http://testonly/authentication_requests/1':(
{'status':'200'},
'{"id":"1", "pending":false, "granted":true, "automated":false, "reason":"its a test", "terminal":{"id":"1", "name":"test terminal"}}'
)
})
auth_request = api.get_authentication_status('1')
self.assertEqual(api.client.last_called_method, 'GET')
self.assertEqual(auth_request.id, '1')
self.assertFalse(auth_request.pending, False)
self.assertTrue(auth_request.granted)
self.assertFalse(auth_request.automated)
self.assertEqual(auth_request.reason, 'its a test')
self.assertEqual(auth_request.terminal_id, '1')
self.assertEqual(auth_request.terminal_name, 'test terminal')
with self.assertRaises(KeyError):
foo = auth_request.random_key
def test_pass_arbitrary_parameters_on_pair(self):
api = toopher.ToopherApi('key', 'secret', api_url='http://testonly')
api.client = HttpClientMock({
'http://testonly/pairings/create':(
{'status':'200'},
'{"id":"1", "enabled":true, "user":{"id":"1","name":"some user"}}'
)
})
pairing = api.pair('awkward turtle', 'some user', test_param='42')
self.assertEqual(api.client.last_called_method, 'POST')
self.assertEqual(api.client.last_called_data['pairing_phrase'], ['awkward turtle'])
self.assertEqual(api.client.last_called_data['test_param'], ['42'])
def test_pass_arbitrary_parameters_on_authenticate(self):
api = toopher.ToopherApi('key', 'secret', api_url='http://testonly')
api.client = HttpClientMock({
'http://testonly/authentication_requests/initiate':(
{'status':'200'},
'{"id":"1", "pending":false, "granted":true, "automated":false, "reason":"its a test", "terminal":{"id":"1", "name":"test terminal"}}'
)
})
auth_request = api.authenticate('1', 'test terminal', test_param='42')
self.assertEqual(api.client.last_called_method, 'POST')
self.assertEqual(api.client.last_called_data['pairing_id'], ['1'])
self.assertEqual(api.client.last_called_data['terminal_name'], ['test terminal'])
self.assertEqual(api.client.last_called_data['test_param'], ['42'])
def test_access_arbitrary_keys_in_pairing_status(self):
api = toopher.ToopherApi('key', 'secret', api_url='http://testonly')
api.client = HttpClientMock({
'http://testonly/pairings/1':(
{'status':'200'},
'{"id":"1", "enabled":true, "user":{"id":"1","name":"some user"}, "random_key":"84"}'
)
})
pairing = api.get_pairing_status('1')
self.assertEqual(api.client.last_called_method, 'GET')
self.assertEqual(pairing.id, '1')
self.assertEqual(pairing.user_name, 'some user')
self.assertEqual(pairing.user_id, '1')
self.assertTrue(pairing.enabled)
self.assertEqual(pairing.random_key, "84")
def test_access_arbitrary_keys_in_authentication_status(self):
api = toopher.ToopherApi('key', 'secret', api_url='http://testonly')
api.client = HttpClientMock({
'http://testonly/authentication_requests/1':(
{'status':'200'},
'{"id":"1", "pending":false, "granted":true, "automated":false, "reason":"its a test", "terminal":{"id":"1", "name":"test terminal"}, "random_key":"84"}'
)
})
auth_request = api.get_authentication_status('1')
self.assertEqual(api.client.last_called_method, 'GET')
self.assertEqual(auth_request.id, '1')
self.assertFalse(auth_request.pending, False)
self.assertTrue(auth_request.granted)
self.assertFalse(auth_request.automated)
self.assertEqual(auth_request.reason, 'its a test')
self.assertEqual(auth_request.terminal_id, '1')
self.assertEqual(auth_request.terminal_name, 'test terminal')
self.assertEqual(auth_request.random_key, "84")
def main():
unittest.main()
if __name__ == '__main__':
main()