|
7 | 7 | from threading import Thread |
8 | 8 | import uuid |
9 | 9 |
|
10 | | -from mock import Mock |
| 10 | +from mock import Mock, patch |
11 | 11 | import pytest |
12 | 12 | from six.moves import range # pylint:disable=redefined-builtin |
13 | 13 | from six.moves.urllib import parse as urlparse # pylint:disable=import-error,no-name-in-module,wrong-import-order |
|
18 | 18 | from boxsdk.config import API |
19 | 19 |
|
20 | 20 |
|
| 21 | +class MyError(Exception): |
| 22 | + pass |
| 23 | + |
| 24 | + |
| 25 | +class MyBaseException(BaseException): |
| 26 | + pass |
| 27 | + |
| 28 | + |
21 | 29 | @pytest.fixture(params=('https://url.com/foo?bar=baz', 'https://ȕŕľ.com/ƒőő?Ƅȁŕ=Ƅȁż', None)) |
22 | 30 | def redirect_url(request): |
23 | 31 | """A value for the `redirect_uri` query string parameter for OAuth2.""" |
@@ -354,3 +362,94 @@ def _get_tokens(self): |
354 | 362 |
|
355 | 363 | assert oauth.refresh(access_token) == new_tokens |
356 | 364 | assert oauth.access_token == new_access_token |
| 365 | + |
| 366 | + |
| 367 | +def test_closed_is_false_after_init(client_id, client_secret, mock_network_layer): |
| 368 | + auth = OAuth2(client_id=client_id, client_secret=client_secret, network_layer=mock_network_layer) |
| 369 | + assert auth.closed is False |
| 370 | + |
| 371 | + |
| 372 | +def test_closed_is_true_after_close(client_id, client_secret, mock_network_layer): |
| 373 | + auth = OAuth2(client_id=client_id, client_secret=client_secret, network_layer=mock_network_layer) |
| 374 | + auth.close() |
| 375 | + assert auth.closed is True |
| 376 | + |
| 377 | + |
| 378 | +def test_token_requests_fail_after_close(client_id, client_secret, mock_network_layer): |
| 379 | + auth = OAuth2(client_id=client_id, client_secret=client_secret, network_layer=mock_network_layer) |
| 380 | + auth.close() |
| 381 | + with pytest.raises(ValueError): |
| 382 | + auth.refresh(auth.access_token) |
| 383 | + |
| 384 | + |
| 385 | +@pytest.mark.parametrize('raise_exception', [False, True]) |
| 386 | +def test_context_manager_closes_auth_object(client_id, client_secret, mock_network_layer, raise_exception): |
| 387 | + auth = OAuth2(client_id=client_id, client_secret=client_secret, network_layer=mock_network_layer) |
| 388 | + try: |
| 389 | + with auth.closing(): |
| 390 | + if raise_exception: |
| 391 | + raise MyError |
| 392 | + except MyError: |
| 393 | + pass |
| 394 | + assert auth.closed is True |
| 395 | + |
| 396 | + |
| 397 | +def test_context_manager_fails_after_close(client_id, client_secret, mock_network_layer): |
| 398 | + auth = OAuth2(client_id=client_id, client_secret=client_secret, network_layer=mock_network_layer) |
| 399 | + with auth.closing(): |
| 400 | + pass |
| 401 | + with pytest.raises(ValueError): |
| 402 | + with auth.closing(): |
| 403 | + assert False |
| 404 | + |
| 405 | + |
| 406 | +@pytest.mark.parametrize(('close_args', 'close_kwargs'), [((), {}), ((True,), {}), ((), dict(revoke=True))]) |
| 407 | +def test_revoke_on_close(client_id, client_secret, access_token, mock_network_layer, close_args, close_kwargs): |
| 408 | + auth = OAuth2(client_id=client_id, client_secret=client_secret, access_token=access_token, network_layer=mock_network_layer) |
| 409 | + with patch.object(auth, 'revoke') as mock_revoke: |
| 410 | + auth.close(*close_args, **close_kwargs) |
| 411 | + mock_revoke.assert_called_once_with() |
| 412 | + |
| 413 | + |
| 414 | +def test_auth_object_is_closed_even_if_revoke_fails(client_id, client_secret, access_token, mock_network_layer): |
| 415 | + auth = OAuth2(client_id=client_id, client_secret=client_secret, access_token=access_token, network_layer=mock_network_layer) |
| 416 | + with patch.object(auth, 'revoke', side_effect=BoxOAuthException(status=500)): |
| 417 | + with pytest.raises(BoxOAuthException): |
| 418 | + auth.close(revoke=True) |
| 419 | + assert auth.closed is True |
| 420 | + |
| 421 | + |
| 422 | +@pytest.mark.parametrize(('close_args', 'close_kwargs'), [((False,), {}), ((), dict(revoke=False))]) |
| 423 | +def test_revoke_on_close_can_be_skipped(client_id, client_secret, access_token, mock_network_layer, close_args, close_kwargs): |
| 424 | + auth = OAuth2(client_id=client_id, client_secret=client_secret, access_token=access_token, network_layer=mock_network_layer) |
| 425 | + with patch.object(auth, 'revoke') as mock_revoke: |
| 426 | + auth.close(*close_args, **close_kwargs) |
| 427 | + mock_revoke.assert_not_called() |
| 428 | + |
| 429 | + |
| 430 | +@pytest.mark.parametrize(('raise_from_block', 'raise_from_close', 'expected_exception'), [ |
| 431 | + (MyError, None, MyError), |
| 432 | + (None, BoxOAuthException(status=500), BoxOAuthException), |
| 433 | + (MyError, BoxOAuthException(status=500), MyError), |
| 434 | +]) |
| 435 | +@pytest.mark.parametrize('close_kwargs', [{}, dict(revoke=False), dict(revoke=True)]) |
| 436 | +def test_context_manager_reraises_first_exception_after_close( |
| 437 | + client_id, client_secret, mock_network_layer, close_kwargs, raise_from_block, raise_from_close, expected_exception, |
| 438 | +): |
| 439 | + auth = OAuth2(client_id=client_id, client_secret=client_secret, network_layer=mock_network_layer) |
| 440 | + with patch.object(auth, 'close', side_effect=raise_from_close) as mock_close: |
| 441 | + with pytest.raises(expected_exception): |
| 442 | + with auth.closing(**close_kwargs): |
| 443 | + if raise_from_block: |
| 444 | + raise raise_from_block |
| 445 | + mock_close.assert_called_once_with(**close_kwargs) |
| 446 | + |
| 447 | + |
| 448 | +@pytest.mark.parametrize('close_kwargs', [{}, dict(revoke=False), dict(revoke=True)]) |
| 449 | +def test_context_manager_skips_revoke_on_base_exception(client_id, client_secret, mock_network_layer, close_kwargs): |
| 450 | + auth = OAuth2(client_id=client_id, client_secret=client_secret, network_layer=mock_network_layer) |
| 451 | + with patch.object(auth, 'close') as mock_close: |
| 452 | + with pytest.raises(MyBaseException): |
| 453 | + with auth.closing(**close_kwargs): |
| 454 | + raise MyBaseException |
| 455 | + mock_close.assert_called_once_with(revoke=False) |
0 commit comments