|
15 | 15 | ) |
16 | 16 | from dropbox.content_hash import content_hash |
17 | 17 | from dropbox.common import PathRoot |
18 | | -from dropbox.exceptions import AuthError, BadInputError |
| 18 | +from dropbox.exceptions import ApiError, AuthError, BadInputError |
19 | 19 | from dropbox.oauth import OAuth2FlowNoRedirectResult, DropboxOAuth2FlowNoRedirect |
20 | 20 | from datetime import datetime, timedelta |
21 | 21 |
|
@@ -509,6 +509,62 @@ def test_refresh_raises_after_exhausting_retries(self, server_error_session_inst |
509 | 509 | # Initial attempt + 2 retries. |
510 | 510 | assert server_error_session_instance.post.call_count == 3 |
511 | 511 |
|
| 512 | + def test_upload_decodes_global_access_error(self, mocker): |
| 513 | + payload = { |
| 514 | + 'error': { |
| 515 | + '.tag': 'invalid_account_type', |
| 516 | + 'invalid_account_type': {'.tag': 'feature'}, |
| 517 | + }, |
| 518 | + 'error_summary': 'invalid_account_type/feature/', |
| 519 | + 'user_message': { |
| 520 | + 'text': 'Uploads are unavailable for this account.', |
| 521 | + 'locale': 'en', |
| 522 | + }, |
| 523 | + } |
| 524 | + response = mock.MagicMock(status_code=403) |
| 525 | + response.headers = { |
| 526 | + 'content-type': 'application/json', |
| 527 | + 'x-dropbox-request-id': 'request-id', |
| 528 | + } |
| 529 | + response.json.return_value = payload |
| 530 | + response.content = json.dumps(payload).encode('utf-8') |
| 531 | + response.text = json.dumps(payload) |
| 532 | + session_obj = create_session() |
| 533 | + mocker.patch.object(session_obj, 'post', return_value=response) |
| 534 | + dbx = Dropbox(oauth2_access_token=ACCESS_TOKEN, session=session_obj) |
| 535 | + |
| 536 | + with pytest.raises(ApiError) as exc_info: |
| 537 | + dbx.files_upload(b'test', '/test.txt') |
| 538 | + |
| 539 | + assert exc_info.value.request_id == 'request-id' |
| 540 | + assert exc_info.value.error.is_invalid_account_type() |
| 541 | + assert exc_info.value.error.get_invalid_account_type().is_feature() |
| 542 | + assert exc_info.value.user_message_text == \ |
| 543 | + 'Uploads are unavailable for this account.' |
| 544 | + assert exc_info.value.user_message_locale == 'en' |
| 545 | + |
| 546 | + def test_upload_preserves_route_specific_403_error(self, mocker): |
| 547 | + payload = { |
| 548 | + 'error': {'.tag': 'payload_too_large'}, |
| 549 | + 'error_summary': 'payload_too_large/', |
| 550 | + } |
| 551 | + response = mock.MagicMock(status_code=403) |
| 552 | + response.headers = { |
| 553 | + 'content-type': 'application/json', |
| 554 | + 'x-dropbox-request-id': 'request-id', |
| 555 | + } |
| 556 | + response.json.return_value = payload |
| 557 | + response.content = json.dumps(payload).encode('utf-8') |
| 558 | + response.text = json.dumps(payload) |
| 559 | + session_obj = create_session() |
| 560 | + mocker.patch.object(session_obj, 'post', return_value=response) |
| 561 | + dbx = Dropbox(oauth2_access_token=ACCESS_TOKEN, session=session_obj) |
| 562 | + |
| 563 | + with pytest.raises(ApiError) as exc_info: |
| 564 | + dbx.files_upload(b'test', '/test.txt') |
| 565 | + |
| 566 | + assert exc_info.value.error.is_payload_too_large() |
| 567 | + |
512 | 568 |
|
513 | 569 | class TestAutoContentHash: |
514 | 570 |
|
|
0 commit comments