-
Notifications
You must be signed in to change notification settings - Fork 125
/
Copy pathtest_auth.py
310 lines (262 loc) · 11.3 KB
/
test_auth.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
"""Test login handler."""
from unittest import mock
from unittest import IsolatedAsyncioTestCase
from aiohttp import ClientConnectionError
from blinkpy.auth import (
Auth,
TokenRefreshFailed,
BlinkBadResponse,
UnauthorizedError,
)
import blinkpy.helpers.constants as const
import tests.mock_responses as mresp
USERNAME = "foobar"
PASSWORD = "deadbeef"
class TestAuth(IsolatedAsyncioTestCase):
"""Test the Auth class in blinkpy."""
def setUp(self):
"""Set up Login Handler."""
self.auth = Auth()
def tearDown(self):
"""Clean up after test."""
self.auth = None
@mock.patch("blinkpy.helpers.util.gen_uid")
@mock.patch("blinkpy.auth.util.getpass")
def test_empty_init(self, getpwd, genuid):
"""Test initialization with no params."""
auth = Auth()
self.assertDictEqual(auth.data, {})
getpwd.return_value = "bar"
genuid.return_value = 1234
with mock.patch("builtins.input", return_value="foo"):
auth.validate_login()
expected_data = {
"username": "foo",
"password": "bar",
"uid": 1234,
"device_id": const.DEVICE_ID,
}
self.assertDictEqual(auth.data, expected_data)
@mock.patch("blinkpy.helpers.util.gen_uid")
@mock.patch("blinkpy.auth.util.getpass")
def test_barebones_init(self, getpwd, genuid):
"""Test basebones initialization."""
login_data = {"username": "foo", "password": "bar"}
auth = Auth(login_data)
self.assertDictEqual(auth.data, login_data)
getpwd.return_value = "bar"
genuid.return_value = 1234
with mock.patch("builtins.input", return_value="foo"):
auth.validate_login()
expected_data = {
"username": "foo",
"password": "bar",
"uid": 1234,
"device_id": const.DEVICE_ID,
}
self.assertDictEqual(auth.data, expected_data)
def test_full_init(self):
"""Test full initialization."""
login_data = {
"username": "foo",
"password": "bar",
"token": "token",
"host": "host",
"region_id": "region_id",
"client_id": "client_id",
"account_id": "account_id",
"uid": 1234,
"notification_key": 4321,
"device_id": "device_id",
}
auth = Auth(login_data)
self.assertEqual(auth.token, "token")
self.assertEqual(auth.host, "host")
self.assertEqual(auth.region_id, "region_id")
self.assertEqual(auth.client_id, "client_id")
self.assertEqual(auth.account_id, "account_id")
auth.validate_login()
self.assertDictEqual(auth.login_attributes, login_data)
async def test_bad_response_code(self):
"""Check bad response code from server."""
self.auth.is_errored = False
fake_resp = mresp.MockResponse({"code": 404}, 404)
with self.assertRaises(ClientConnectionError):
await self.auth.validate_response(fake_resp, True)
self.assertTrue(self.auth.is_errored)
self.auth.is_errored = False
fake_resp = mresp.MockResponse({"code": 101}, 401)
with self.assertRaises(UnauthorizedError):
await self.auth.validate_response(fake_resp, True)
self.assertTrue(self.auth.is_errored)
async def test_good_response_code(self):
"""Check good response code from server."""
fake_resp = mresp.MockResponse({"foo": "bar"}, 200)
self.auth.is_errored = True
self.assertEqual(
await self.auth.validate_response(fake_resp, True), {"foo": "bar"}
)
self.assertFalse(self.auth.is_errored)
async def test_response_not_json(self):
"""Check response when not json."""
fake_resp = "foobar"
self.auth.is_errored = True
self.assertEqual(await self.auth.validate_response(fake_resp, False), "foobar")
self.assertFalse(self.auth.is_errored)
async def test_response_bad_json(self):
"""Check response when not json but expecting json."""
self.auth.is_errored = False
with self.assertRaises(BlinkBadResponse):
await self.auth.validate_response(None, True)
self.assertTrue(self.auth.is_errored)
def test_header(self):
"""Test header data."""
self.auth.token = "bar"
expected_header = {
"TOKEN_AUTH": "bar",
"user-agent": const.DEFAULT_USER_AGENT,
"content-type": "application/json",
}
self.assertDictEqual(self.auth.header, expected_header)
def test_header_no_token(self):
"""Test header without token."""
self.auth.token = None
self.assertEqual(self.auth.header, None)
@mock.patch("blinkpy.auth.Auth.validate_login")
@mock.patch("blinkpy.auth.Auth.refresh_token")
async def test_auth_startup(self, mock_validate, mock_refresh):
"""Test auth startup."""
await self.auth.startup()
@mock.patch("blinkpy.auth.Auth.query")
async def test_refresh_token(self, mock_resp):
"""Test refresh token method."""
mock_resp.return_value.json = mock.AsyncMock(
return_value={
"account": {"account_id": 5678, "client_id": 1234, "tier": "test"},
"auth": {"token": "foobar"},
}
)
mock_resp.return_value.status = 200
self.auth.no_prompt = True
self.assertTrue(await self.auth.refresh_token())
self.assertEqual(self.auth.region_id, "test")
self.assertEqual(self.auth.token, "foobar")
self.assertEqual(self.auth.client_id, 1234)
self.assertEqual(self.auth.account_id, 5678)
mock_resp.return_value.status = 400
with self.assertRaises(TokenRefreshFailed):
await self.auth.refresh_token()
mock_resp.return_value.status = 200
mock_resp.return_value.json = mock.AsyncMock(side_effect=AttributeError)
with self.assertRaises(TokenRefreshFailed):
await self.auth.refresh_token()
@mock.patch("blinkpy.auth.Auth.login")
async def test_refresh_token_failed(self, mock_login):
"""Test refresh token failed."""
mock_login.return_value = {}
self.auth.is_errored = False
with self.assertRaises(TokenRefreshFailed):
await self.auth.refresh_token()
self.assertTrue(self.auth.is_errored)
def test_check_key_required(self):
"""Check key required method."""
self.auth.login_response = {}
self.assertFalse(self.auth.check_key_required())
self.auth.login_response = {"account": {"client_verification_required": False}}
self.assertFalse(self.auth.check_key_required())
self.auth.login_response = {"account": {"client_verification_required": True}}
self.assertTrue(self.auth.check_key_required())
@mock.patch("blinkpy.auth.api.request_logout")
async def test_logout(self, mock_req):
"""Test logout method."""
mock_blink = MockBlink(None)
mock_req.return_value = True
self.assertTrue(await self.auth.logout(mock_blink))
@mock.patch("blinkpy.auth.api.request_verify")
async def test_send_auth_key(self, mock_req):
"""Check sending of auth key."""
mock_blink = MockBlink(None)
mock_req.return_value = mresp.MockResponse({"valid": True}, 200)
self.assertTrue(await self.auth.send_auth_key(mock_blink, 1234))
self.assertTrue(mock_blink.available)
mock_req.return_value = mresp.MockResponse(None, 200)
self.assertFalse(await self.auth.send_auth_key(mock_blink, 1234))
mock_req.return_value = mresp.MockResponse({}, 200)
self.assertFalse(await self.auth.send_auth_key(mock_blink, 1234))
self.assertTrue(await self.auth.send_auth_key(mock_blink, None))
@mock.patch("blinkpy.auth.api.request_verify")
async def test_send_auth_key_fail(self, mock_req):
"""Check handling of auth key failure."""
mock_blink = MockBlink(None)
mock_req.return_value = mresp.MockResponse(None, 200)
self.assertFalse(await self.auth.send_auth_key(mock_blink, 1234))
mock_req.return_value = mresp.MockResponse({}, 200)
self.assertFalse(await self.auth.send_auth_key(mock_blink, 1234))
mock_req.return_value = mresp.MockResponse(
{"valid": False, "message": "Not good"}, 200
)
self.assertFalse(await self.auth.send_auth_key(mock_blink, 1234))
@mock.patch(
"blinkpy.auth.Auth.validate_response",
mock.AsyncMock(side_effect=[UnauthorizedError, "foobar"]),
)
@mock.patch("blinkpy.auth.Auth.refresh_token", mock.AsyncMock(return_value=True))
@mock.patch("blinkpy.auth.Auth.query", mock.AsyncMock(return_value="foobar"))
async def test_query_retry(self): # , mock_refresh, mock_validate):
"""Check handling of request retry."""
self.auth.session = MockSession()
self.assertEqual(await self.auth.query(url="http://example.com"), "foobar")
@mock.patch("blinkpy.auth.Auth.validate_response")
@mock.patch("blinkpy.auth.Auth.refresh_token")
async def test_query_retry_failed(self, mock_refresh, mock_validate):
"""Check handling of failed retry request."""
self.auth.session = MockSession()
mock_validate.side_effect = [
BlinkBadResponse,
UnauthorizedError,
TokenRefreshFailed,
]
mock_refresh.return_value = True
self.assertEqual(await self.auth.query(url="http://example.com"), None)
self.assertEqual(await self.auth.query(url="http://example.com"), None)
@mock.patch("blinkpy.auth.Auth.validate_response")
async def test_query(self, mock_validate):
"""Test query functions."""
self.auth.session = MockSession_with_data()
await self.auth.query("URL", "data", "headers", "get")
await self.auth.query("URL", "data", "headers", "post")
mock_validate.side_effect = ClientConnectionError
self.assertIsNone(await self.auth.query("URL", "data", "headers", "get"))
mock_validate.side_effect = BlinkBadResponse
self.assertIsNone(await self.auth.query("URL", "data", "headers", "post"))
mock_validate.side_effect = UnauthorizedError
self.auth.refresh_token = mock.AsyncMock()
self.assertIsNone(await self.auth.query("URL", "data", "headers", "post"))
class MockSession:
"""Object to mock a session."""
async def get(self, *args, **kwargs):
"""Mock send function."""
return None
async def post(self, *args, **kwargs):
"""Mock send function."""
return None
class MockSession_with_data:
"""Object to mock a session."""
async def get(self, *args, **kwargs):
"""Mock send function."""
response = mock.AsyncMock
response.status = 400
response.reason = "Some Reason"
return response
async def post(self, *args, **kwargs):
"""Mock send function."""
response = mock.AsyncMock
response.status = 400
response.reason = "Some Reason"
return response
class MockBlink:
"""Object to mock basic blink class."""
def __init__(self, login_response):
"""Initialize mock blink class."""
self.available = False
self.login_response = login_response