/
test_gist_api.py
229 lines (183 loc) · 7.81 KB
/
test_gist_api.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
import unittest
import requests
import json
import os
from sync_settings.libs import gist, path
from unittest import mock
def get_output(f):
return path.join(os.path.abspath(os.path.dirname(__file__)), 'outputs', f)
class GistTest(unittest.TestCase):
def setUp(self):
self.api = gist.Gist()
self.mock_response = mock.Mock()
class TestDecorators(unittest.TestCase):
token = None
def test_auth(self):
def to_test(*args):
return 'yay'
with self.assertRaises(gist.AuthenticationError):
gist.auth(to_test)(self)
self.token = 'valid token'
self.assertEqual(gist.auth(to_test)(self), 'yay')
def test_with_gid(self):
def to_test(*args):
return 'yay'
with self.assertRaises(ValueError):
gist.with_gid(to_test)(self, '')
with self.assertRaises(ValueError):
gist.with_gid(to_test)(self, None)
self.assertEqual(gist.with_gid(to_test)(self, '123123123'), 'yay')
class GetGistTest(GistTest):
def test_raise_error_without_gist_id(self):
with self.assertRaises(ValueError):
self.api.get('')
@mock.patch('requests.get')
def test_raise_gist_not_found_error(self, mock_get):
self.mock_response.status_code = 404
mock_get.return_value = self.mock_response
with self.assertRaises(gist.NotFoundError):
self.api.get('not found')
@mock.patch('requests.get')
def test_raise_network_error(self, mock_get):
mock_get.side_effect = requests.exceptions.ConnectionError()
with self.assertRaises(gist.NetworkError):
self.api.get('123123123')
@mock.patch('requests.get')
def test_unexpected_error_with_invalid_data(self, mock_get):
self.mock_response.status_code = 408
self.mock_response.json.return_value = {
'message': 'an error',
}
mock_get.return_value = self.mock_response
with self.assertRaises(gist.UnexpectedError):
self.api.get('123123123')
@mock.patch('requests.get')
def test_valid_response(self, mock_get):
self.mock_response.status_code = 200
with open(get_output('gist.json'), 'r') as f:
content = json.load(f)
self.mock_response.json.return_value = content
mock_get.return_value = self.mock_response
self.assertEqual(self.api.get('aa5a315d61ae9438b18d'), content)
@mock.patch('requests.get')
def test_get_commits(self, mock_get):
self.mock_response.status_code = 200
with open(get_output('gist.json'), 'r') as f:
content = json.load(f)
self.mock_response.json.return_value = content['history']
mock_get.return_value = self.mock_response
commits = self.api.commits('123123123')
self.assertEqual(1, len(commits))
self.assertEqual('57a7f021a713b1c5a6a199b54cc514735d2d462f', commits[0]['version'])
class CreateGistTest(GistTest):
def test_raise_authentication_error_without_token(self):
with self.assertRaises(gist.AuthenticationError):
self.api.create({'files': {}})
def test_argument_exception_without_data(self):
self.api = gist.Gist('some_access_token')
with self.assertRaises(ValueError):
self.api.create({})
@mock.patch('requests.patch')
def test_unprocessable_data_error(self, mock_patch):
self.mock_response.status_code = 422
mock_patch.return_value = self.mock_response
self.api = gist.Gist('some_access_token')
with self.assertRaises(gist.UnprocessableDataError):
self.api.update('123123123', {'description': 'some description'})
def test_raise_argument_exception_with_no_dict(self):
self.api = gist.Gist('some_access_token')
with self.assertRaises(ValueError):
self.api.create('')
@mock.patch('requests.post')
def test_valid_response(self, mock_post):
self.api = gist.Gist('123123123')
self.mock_response.status_code = 201
with open(get_output('gist.json'), 'r') as f:
content = json.load(f)
self.mock_response.json.return_value = content
mock_post.return_value = self.mock_response
self.assertEqual(self.api.create({
'files': {
'file.txt': {
'content': 'file with content'
}
},
'description': 'gist description'
}), content)
class DeleteGistTest(GistTest):
def test_raise_authentication_error_without_token(self):
with self.assertRaises(gist.AuthenticationError):
self.api.delete('....')
def test_argument_exception_without_id(self):
self.api = gist.Gist('123123')
with self.assertRaises(ValueError):
self.api.delete('')
@mock.patch('requests.delete')
def test_failed_delete(self, mock_delete):
self.api = gist.Gist('123123')
self.mock_response.status_code = 205
mock_delete.return_value = self.mock_response
self.assertFalse(self.api.delete('123123'))
@mock.patch('requests.delete')
def test_success_delete(self, mock_delete):
self.api = gist.Gist('123123')
self.mock_response.status_code = 204
mock_delete.return_value = self.mock_response
self.assertTrue(self.api.delete('123123'))
class UpdateGistTest(GistTest):
def setUp(self):
self.api = gist.Gist('access token')
self.mock_response = mock.Mock()
def test_raise_argument_exception_without_data(self):
with self.assertRaises(ValueError):
self.api.update('asdfasdf', {})
def test_raise_argument_exception_with_no_dict(self):
with self.assertRaises(ValueError):
self.api.update('123123', '')
def test_raise_argument_exception_without_id(self):
with self.assertRaises(ValueError):
self.api.update('', {'files': {}})
def test_raise_authentication_error_without_token(self):
self.api = gist.Gist()
with self.assertRaises(gist.AuthenticationError):
self.api.update('123', {})
@mock.patch('requests.patch')
def test_raise_authentication_with_gist_of_someone_else(self, mock_patch):
self.mock_response.status_code = 403
mock_patch.return_value = self.mock_response
with self.assertRaises(gist.AuthenticationError):
self.api.update('123123123', {
'files': {
'file.txt': None
}
})
class ProxiesTest(unittest.TestCase):
def test_proxies_property(self):
tests = [
{'proxies': {'http_proxy': '123.123.123.123'}, 'expected': {}},
{'proxies': {'http_proxy': None}, 'expected': {}},
{'proxies': {'https_proxy': None, 'http_proxy': 'http://12.1'}, 'expected': {}},
{'proxies': {'http_proxy': 'localhost:9090'}, 'expected': {}},
{'proxies': {'http_proxy': 'http://localhost:9090'}, 'expected': {'http': 'http://localhost:9090'}},
{
'proxies': {'http_proxy': 'http://localhost:9090', 'https_proxy': None},
'expected': {'http': 'http://localhost:9090'}
},
{
'proxies': {'https_proxy': 'https://localhost:9090', 'http_proxy': None},
'expected': {'https': 'https://localhost:9090'}
},
{
'proxies': {
'https_proxy': 'https://localhost:9090',
'http_proxy': 'http://localhost:9090',
},
'expected': {
'https': 'https://localhost:9090',
'http': 'http://localhost:9090',
}
},
]
for test in tests:
g = gist.Gist(**test['proxies'])
self.assertDictEqual(test['expected'], g.proxies)