-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_api_client.py
151 lines (116 loc) · 4.8 KB
/
test_api_client.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
# -*- coding: utf-8 -*-
import mock
import pytest
from tests.base import TestBase
from tvdbrest import VERSION
from tvdbrest.client import TVDB, Unauthorized, APIError, NotFound
@pytest.fixture
def tvdb():
return TVDB("myusername", "myuserkey", "myapikey")
@pytest.fixture
def empty_positive_response():
m = mock.Mock()
m.status_code = 200
m.json = mock.Mock(return_value={"data": []})
return m
class TestLoginLogout(object):
def test_login_status(self):
tvdb = TVDB("myusername", "myuserkey", "myapikey")
assert not tvdb.logged_in
@mock.patch('tvdbrest.client.requests.request')
def test_login_status_after_login(self, request_mock):
response_mock = mock.MagicMock()
response_mock.status_code = 200
response_mock.json = mock.MagicMock(return_value={
'token': 'jwttoken'
})
request_mock.return_value = response_mock
tvdb = TVDB("myusername", "myuserkey", "myapikey")
tvdb.login()
request_mock.assert_called_with('post', 'https://api.thetvdb.com/login', headers={
'Accept-Language': 'en',
'User-Agent': 'tvdb-rest %s' % VERSION
}, json={
'username': 'myusername',
'userkey': 'myuserkey',
'apikey': 'myapikey'
})
assert tvdb.logged_in
@mock.patch('tvdbrest.client.requests.request')
def test_failed_login(self, request_method_mock):
response_mock = mock.MagicMock()
response_mock.status_code = 401
request_method_mock.return_value = response_mock
tvdb = TVDB("myusername", "myuserkey", "myapikey")
with pytest.raises(Unauthorized):
tvdb.login()
assert not tvdb.logged_in
def test_logout(self, tvdb):
tvdb.jwttoken = "abc"
assert tvdb.logged_in
tvdb.logout()
assert tvdb.jwttoken is None
assert not tvdb.logged_in
@mock.patch('tvdbrest.client.requests.request')
def test_decorator_login_before_api_call(self, request_method_mock, tvdb):
response_mock = mock.MagicMock()
response_mock.status_code = 200
response_mock.json = mock.MagicMock(return_value={"data": []})
request_method_mock.return_value = response_mock
login_mock = mock.MagicMock()
tvdb.login = login_mock
tvdb.languages()
assert login_mock.called
@mock.patch('tvdbrest.client.requests.request')
def test_authorization_for_api_call(self, request_mock, tvdb, empty_positive_response):
request_mock.return_value = empty_positive_response
tvdb.jwttoken = "test"
tvdb.languages()
request_mock.assert_called_with('get', 'https://api.thetvdb.com/languages', headers={
'Authorization': 'Bearer test',
'Accept-Language': 'en',
'User-Agent': 'tvdb-rest %s' % VERSION
})
class TestClientBasics(TestBase):
@mock.patch('tvdbrest.client.requests.request')
def test_accept_language(self, request_mock, empty_positive_response):
tvdb = TVDB("myusername", "myuserkey", "myapikey", 'de')
request_mock.return_value = empty_positive_response
tvdb.jwttoken = "test"
tvdb.languages()
request_mock.assert_called_with('get', 'https://api.thetvdb.com/languages', headers={
'Authorization': 'Bearer test',
'Accept-Language': 'de',
'User-Agent': 'tvdb-rest %s' % VERSION
})
@mock.patch('tvdbrest.client.requests.request')
def test_accept_language_not_set(self, request_mock, tvdb, empty_positive_response):
tvdb.accept_language = None
request_mock.return_value = empty_positive_response
tvdb.jwttoken = "test"
tvdb.languages()
request_mock.assert_called_with('get', 'https://api.thetvdb.com/languages', headers={
'Authorization': 'Bearer test',
'User-Agent': 'tvdb-rest %s' % VERSION
})
@mock.patch('tvdbrest.client.requests.request')
def test_raise_apierror_on_4xx(self, request_mock, tvdb):
m = mock.Mock()
m.status_code = 405
request_mock.return_value = m
with pytest.raises(APIError):
tvdb.login()
@mock.patch('tvdbrest.client.requests.request')
def test_raise_apierror_on_5xx(self, request_mock, tvdb):
m = mock.Mock()
m.status_code = 500
request_mock.return_value = m
with pytest.raises(APIError):
tvdb.login()
@mock.patch('tvdbrest.client.requests.request')
def test_raise_not_found_on_404(self, request_mock, tvdb):
# m = mock.Mock()
# m.status_code = 404
request_mock.return_value = self.api_response_404_mock()
with pytest.raises(NotFound):
tvdb.login()