| 1 |
from django.test import RequestFactory, TestCase |
| 2 |
from django.utils import translation |
| 3 |
from django.utils.translation import gettext_lazy as _ |
| 4 |
|
| 5 |
from rest_framework.exceptions import ( |
| 6 |
APIException, ErrorDetail, Throttled, _get_error_details, bad_request, |
| 7 |
server_error |
| 8 |
) |
| 9 |
|
| 10 |
|
| 11 |
class ExceptionTestCase(TestCase): |
| 12 |
|
| 13 |
def test_get_error_details(self): |
| 14 |
|
| 15 |
example = "string" |
| 16 |
lazy_example = _(example) |
| 17 |
|
| 18 |
assert _get_error_details(lazy_example) == example |
| 19 |
|
| 20 |
assert isinstance( |
| 21 |
_get_error_details(lazy_example), |
| 22 |
ErrorDetail |
| 23 |
) |
| 24 |
|
| 25 |
assert _get_error_details({'nested': lazy_example})['nested'] == example |
| 26 |
|
| 27 |
assert isinstance( |
| 28 |
_get_error_details({'nested': lazy_example})['nested'], |
| 29 |
ErrorDetail |
| 30 |
) |
| 31 |
|
| 32 |
assert _get_error_details([[lazy_example]])[0][0] == example |
| 33 |
|
| 34 |
assert isinstance( |
| 35 |
_get_error_details([[lazy_example]])[0][0], |
| 36 |
ErrorDetail |
| 37 |
) |
| 38 |
|
| 39 |
def test_get_full_details_with_throttling(self): |
| 40 |
exception = Throttled() |
| 41 |
assert exception.get_full_details() == { |
| 42 |
'message': 'Request was throttled.', 'code': 'throttled'} |
| 43 |
|
| 44 |
exception = Throttled(wait=2) |
| 45 |
assert exception.get_full_details() == { |
| 46 |
'message': f'Request was throttled. Expected available in {2} seconds.', |
| 47 |
'code': 'throttled'} |
| 48 |
|
| 49 |
exception = Throttled(wait=2, detail='Slow down!') |
| 50 |
assert exception.get_full_details() == { |
| 51 |
'message': f'Slow down! Expected available in {2} seconds.', |
| 52 |
'code': 'throttled'} |
| 53 |
|
| 54 |
|
| 55 |
class ErrorDetailTests(TestCase): |
| 56 |
|
| 57 |
def test_eq(self): |
| 58 |
assert ErrorDetail('msg') == ErrorDetail('msg') |
| 59 |
assert ErrorDetail('msg', 'code') == ErrorDetail('msg', code='code') |
| 60 |
|
| 61 |
assert ErrorDetail('msg') == 'msg' |
| 62 |
assert ErrorDetail('msg', 'code') == 'msg' |
| 63 |
|
| 64 |
def test_ne(self): |
| 65 |
assert ErrorDetail('msg1') != ErrorDetail('msg2') |
| 66 |
assert ErrorDetail('msg') != ErrorDetail('msg', code='invalid') |
| 67 |
|
| 68 |
assert ErrorDetail('msg1') != 'msg2' |
| 69 |
assert ErrorDetail('msg1', 'code') != 'msg2' |
| 70 |
|
| 71 |
def test_repr(self): |
| 72 |
assert repr(ErrorDetail('msg1')) == \ |
| 73 |
'ErrorDetail(string={!r}, code=None)'.format('msg1') |
| 74 |
assert repr(ErrorDetail('msg1', 'code')) == \ |
| 75 |
'ErrorDetail(string={!r}, code={!r})'.format('msg1', 'code') |
| 76 |
|
| 77 |
def test_str(self): |
| 78 |
assert str(ErrorDetail('msg1')) == 'msg1' |
| 79 |
assert str(ErrorDetail('msg1', 'code')) == 'msg1' |
| 80 |
|
| 81 |
def test_hash(self): |
| 82 |
assert hash(ErrorDetail('msg')) == hash('msg') |
| 83 |
assert hash(ErrorDetail('msg', 'code')) == hash('msg') |
| 84 |
|
| 85 |
|
| 86 |
class TranslationTests(TestCase): |
| 87 |
|
| 88 |
@translation.override('fr') |
| 89 |
def test_message(self): |
| 90 |
# this test largely acts as a sanity test to ensure the translation files are present. |
| 91 |
self.assertEqual(_('A server error occurred.'), 'Une erreur du serveur est survenue.') |
| 92 |
self.assertEqual(str(APIException()), 'Une erreur du serveur est survenue.') |
| 93 |
|
| 94 |
|
| 95 |
def test_server_error(): |
| 96 |
request = RequestFactory().get('/') |
| 97 |
response = server_error(request) |
| 98 |
assert response.status_code == 500 |
| 99 |
assert response["content-type"] == 'application/json' |
| 100 |
|
| 101 |
|
| 102 |
def test_bad_request(): |
| 103 |
request = RequestFactory().get('/') |
| 104 |
exception = Exception('Something went wrong — Not used') |
| 105 |
response = bad_request(request, exception) |
| 106 |
assert response.status_code == 400 |
| 107 |
assert response["content-type"] == 'application/json' |