forked from openviess/PyViCare
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_ViCareOAuthManager.py
79 lines (59 loc) · 2.57 KB
/
test_ViCareOAuthManager.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
import unittest
from unittest.mock import Mock
from PyViCare.PyViCareOAuthManager import AbstractViCareOAuthManager
from PyViCare.PyViCareUtils import (PyViCareCommandError,
PyViCareInternalServerError,
PyViCareRateLimitError)
from tests.helper import readJson
class OAuthManagerWithMock(AbstractViCareOAuthManager):
def __init__(self, mock):
super().__init__(mock)
def renewToken(self):
self.oauth_session.renewToken()
class FakeResponse:
def __init__(self, file_name):
self.file_name = file_name
def json(self):
return readJson(self.file_name)
class PyViCareServiceTest(unittest.TestCase):
def setUp(self):
self.oauth_mock = Mock()
self.manager = OAuthManagerWithMock(self.oauth_mock)
def test_get_raiseratelimit_ifthatreponse(self):
self.oauth_mock.get.return_value = FakeResponse(
'response/rate_limit.json')
def func():
return self.manager.get("/")
self.assertRaises(PyViCareRateLimitError, func)
def test_post_raisecommanderror_ifthatreponse(self):
self.oauth_mock.post.return_value = FakeResponse(
'response/error_502.json')
def func():
return self.manager.post("/", {})
self.assertRaises(PyViCareCommandError, func)
def test_get_raiseservererror_ifthatreponse(self):
self.oauth_mock.get.return_value = FakeResponse(
'response/error_500.json')
def func():
return self.manager.get("/")
self.assertRaises(PyViCareInternalServerError, func)
def test_get_renewtoken_ifexpired(self):
self.oauth_mock.get.side_effect = [
FakeResponse('response/expired_token.json'), # first call expired
FakeResponse('response/Vitodens200W.json') # second call success
]
self.manager.get("/")
self.oauth_mock.renewToken.assert_called_once()
def test_post_raiseratelimit_ifthatreponse(self):
self.oauth_mock.post.return_value = FakeResponse(
'response/rate_limit.json')
def func():
return self.manager.post("/", "some")
self.assertRaises(PyViCareRateLimitError, func)
def test_post_renewtoken_ifexpired(self):
self.oauth_mock.post.side_effect = [
FakeResponse('response/expired_token.json'), # first call expired
FakeResponse('response/Vitodens200W.json') # second call success
]
self.manager.post("/", "some")
self.oauth_mock.renewToken.assert_called_once()