forked from orcasgit/python-fitbit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_exceptions.py
More file actions
104 lines (80 loc) · 2.91 KB
/
test_exceptions.py
File metadata and controls
104 lines (80 loc) · 2.91 KB
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
import unittest
import mock
import requests
from fitbit import Fitbit
from fitbit import exceptions
class ExceptionTest(unittest.TestCase):
"""
Tests that certain response codes raise certain exceptions
"""
client_kwargs = {
"consumer_key": "",
"consumer_secret": "",
"user_key": None,
"user_secret": None,
}
def test_response_ok(self):
"""
This mocks a pretty normal resource, that the request was authenticated,
and data was returned. This test should just run and not raise any
exceptions
"""
r = mock.Mock(spec=requests.Response)
r.status_code = 200
r.content = '{"normal": "resource"}'
f = Fitbit(**self.client_kwargs)
f.client._request = lambda *args, **kwargs: r
f.user_profile_get()
r.status_code = 202
f.user_profile_get()
r.status_code = 204
f.user_profile_get()
def test_response_auth(self):
"""
This test checks how the client handles different auth responses, and
the exceptions raised by the client.
"""
r = mock.Mock(spec=requests.Response)
r.status_code = 401
r.content = "{'normal': 'resource'}"
f = Fitbit(**self.client_kwargs)
f.client._request = lambda *args, **kwargs: r
self.assertRaises(exceptions.HTTPUnauthorized, f.user_profile_get)
r.status_code = 403
self.assertRaises(exceptions.HTTPForbidden, f.user_profile_get)
def test_response_error(self):
"""
Tests other HTTP errors
"""
r = mock.Mock(spec=requests.Response)
r.content = "{'normal': 'resource'}"
f = Fitbit(**self.client_kwargs)
f.client._request = lambda *args, **kwargs: r
r.status_code = 404
self.assertRaises(exceptions.HTTPNotFound, f.user_profile_get)
r.status_code = 409
self.assertRaises(exceptions.HTTPConflict, f.user_profile_get)
r.status_code = 500
self.assertRaises(exceptions.HTTPServerError, f.user_profile_get)
r.status_code = 499
self.assertRaises(exceptions.HTTPBadRequest, f.user_profile_get)
def test_serialization(self):
"""
Tests non-json data returned
"""
r = mock.Mock(spec=requests.Response)
r.status_code = 200
r.content = "iyam not jason"
f = Fitbit(**self.client_kwargs)
f.client._request = lambda *args, **kwargs: r
self.assertRaises(exceptions.BadResponse, f.user_profile_get)
def test_delete_error(self):
"""
Delete requests should return 204
"""
r = mock.Mock(spec=requests.Response)
r.status_code = 201
r.content = '{"it\'s all": "ok"}'
f = Fitbit(**self.client_kwargs)
f.client._request = lambda *args, **kwargs: r
self.assertRaises(exceptions.DeleteError, f.delete_activities, 12345)