File: test_exceptions.py

package info (click to toggle)
python-fitbit 0.3.1-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 276 kB
  • sloc: python: 1,571; makefile: 134
file content (134 lines) | stat: -rw-r--r-- 4,112 bytes parent folder | download | duplicates (4)
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
import unittest
import json
import mock
import requests
import sys
from fitbit import Fitbit
from fitbit import exceptions


class ExceptionTest(unittest.TestCase):
    """
    Tests that certain response codes raise certain exceptions
    """
    client_kwargs = {
        "client_id": "",
        "client_secret": "",
        "access_token": None,
        "refresh_token": 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 = b'{"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
        json_response = {
            "errors": [{
                "errorType": "unauthorized",
                "message": "Unknown auth error"}
            ],
            "normal": "resource"
        }
        r.content = json.dumps(json_response).encode('utf8')

        f = Fitbit(**self.client_kwargs)
        f.client._request = lambda *args, **kwargs: r

        self.assertRaises(exceptions.HTTPUnauthorized, f.user_profile_get)

        r.status_code = 403
        json_response['errors'][0].update({
            "errorType": "forbidden",
            "message": "Forbidden"
        })
        r.content = json.dumps(json_response).encode('utf8')
        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 = b'{"normal": "resource"}'

        self.client_kwargs['oauth2'] = True
        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_too_many_requests(self):
        """
        Tests the 429 response, given in case of exceeding the rate limit
        """
        r = mock.Mock(spec=requests.Response)
        r.content = b"{'normal': 'resource'}"
        r.headers = {'Retry-After': '10'}

        f = Fitbit(**self.client_kwargs)
        f.client._request = lambda *args, **kwargs: r

        r.status_code = 429
        try:
            f.user_profile_get()
            self.assertEqual(True, False)  # Won't run if an exception's raised
        except exceptions.HTTPTooManyRequests:
            e = sys.exc_info()[1]
            self.assertEqual(e.retry_after_secs, 10)

    def test_serialization(self):
        """
        Tests non-json data returned
        """
        r = mock.Mock(spec=requests.Response)
        r.status_code = 200
        r.content = b"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 = b'{"it\'s all": "ok"}'

        f = Fitbit(**self.client_kwargs)
        f.client._request = lambda *args, **kwargs: r
        self.assertRaises(exceptions.DeleteError, f.delete_activities, 12345)