2018-04-03 10:16:36 +03:00
|
|
|
|
from django.test import RequestFactory, TestCase
|
2019-04-30 18:53:44 +03:00
|
|
|
|
from django.utils import translation
|
Replace all usage ugettext functions with the non-u versions (#6634)
On Python 3, the ugettext functions are a simple aliases of their non-u
counterparts (the 'u' represents Python 2 unicode type). Starting with
Django 3.0, the u versions will be deprecated.
https://docs.djangoproject.com/en/dev/releases/3.0/#id2
> django.utils.translation.ugettext(), ugettext_lazy(), ugettext_noop(),
> ungettext(), and ungettext_lazy() are deprecated in favor of the
> functions that they’re aliases for:
> django.utils.translation.gettext(), gettext_lazy(), gettext_noop(),
> ngettext(), and ngettext_lazy().
2019-05-01 08:49:54 +03:00
|
|
|
|
from django.utils.translation import gettext_lazy as _
|
2016-02-07 19:26:37 +03:00
|
|
|
|
|
2016-11-01 13:38:56 +03:00
|
|
|
|
from rest_framework.exceptions import (
|
2018-04-03 10:16:36 +03:00
|
|
|
|
APIException, ErrorDetail, Throttled, _get_error_details, bad_request,
|
|
|
|
|
server_error
|
2016-11-01 13:38:56 +03:00
|
|
|
|
)
|
2016-02-07 19:26:37 +03:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ExceptionTestCase(TestCase):
|
|
|
|
|
|
2016-10-11 12:25:21 +03:00
|
|
|
|
def test_get_error_details(self):
|
|
|
|
|
|
|
|
|
|
example = "string"
|
|
|
|
|
lazy_example = _(example)
|
|
|
|
|
|
2016-11-30 14:12:01 +03:00
|
|
|
|
assert _get_error_details(lazy_example) == example
|
|
|
|
|
|
2016-10-11 12:25:21 +03:00
|
|
|
|
assert isinstance(
|
|
|
|
|
_get_error_details(lazy_example),
|
|
|
|
|
ErrorDetail
|
|
|
|
|
)
|
|
|
|
|
|
2016-11-30 14:12:01 +03:00
|
|
|
|
assert _get_error_details({'nested': lazy_example})['nested'] == example
|
|
|
|
|
|
2016-10-11 12:25:21 +03:00
|
|
|
|
assert isinstance(
|
|
|
|
|
_get_error_details({'nested': lazy_example})['nested'],
|
|
|
|
|
ErrorDetail
|
|
|
|
|
)
|
|
|
|
|
|
2016-11-30 14:12:01 +03:00
|
|
|
|
assert _get_error_details([[lazy_example]])[0][0] == example
|
|
|
|
|
|
2016-10-11 12:25:21 +03:00
|
|
|
|
assert isinstance(
|
|
|
|
|
_get_error_details([[lazy_example]])[0][0],
|
|
|
|
|
ErrorDetail
|
|
|
|
|
)
|
2016-11-01 13:38:56 +03:00
|
|
|
|
|
|
|
|
|
def test_get_full_details_with_throttling(self):
|
|
|
|
|
exception = Throttled()
|
|
|
|
|
assert exception.get_full_details() == {
|
|
|
|
|
'message': 'Request was throttled.', 'code': 'throttled'}
|
|
|
|
|
|
|
|
|
|
exception = Throttled(wait=2)
|
|
|
|
|
assert exception.get_full_details() == {
|
2019-04-30 18:53:44 +03:00
|
|
|
|
'message': 'Request was throttled. Expected available in {} seconds.'.format(2),
|
2016-11-01 13:38:56 +03:00
|
|
|
|
'code': 'throttled'}
|
|
|
|
|
|
|
|
|
|
exception = Throttled(wait=2, detail='Slow down!')
|
|
|
|
|
assert exception.get_full_details() == {
|
2019-04-30 18:53:44 +03:00
|
|
|
|
'message': 'Slow down! Expected available in {} seconds.'.format(2),
|
2016-11-01 13:38:56 +03:00
|
|
|
|
'code': 'throttled'}
|
2018-01-02 13:28:45 +03:00
|
|
|
|
|
|
|
|
|
|
2018-01-30 10:45:09 +03:00
|
|
|
|
class ErrorDetailTests(TestCase):
|
|
|
|
|
|
|
|
|
|
def test_eq(self):
|
|
|
|
|
assert ErrorDetail('msg') == ErrorDetail('msg')
|
|
|
|
|
assert ErrorDetail('msg', 'code') == ErrorDetail('msg', code='code')
|
|
|
|
|
|
|
|
|
|
assert ErrorDetail('msg') == 'msg'
|
|
|
|
|
assert ErrorDetail('msg', 'code') == 'msg'
|
|
|
|
|
|
|
|
|
|
def test_ne(self):
|
|
|
|
|
assert ErrorDetail('msg1') != ErrorDetail('msg2')
|
|
|
|
|
assert ErrorDetail('msg') != ErrorDetail('msg', code='invalid')
|
|
|
|
|
|
|
|
|
|
assert ErrorDetail('msg1') != 'msg2'
|
|
|
|
|
assert ErrorDetail('msg1', 'code') != 'msg2'
|
|
|
|
|
|
|
|
|
|
def test_repr(self):
|
|
|
|
|
assert repr(ErrorDetail('msg1')) == \
|
|
|
|
|
'ErrorDetail(string={!r}, code=None)'.format('msg1')
|
|
|
|
|
assert repr(ErrorDetail('msg1', 'code')) == \
|
|
|
|
|
'ErrorDetail(string={!r}, code={!r})'.format('msg1', 'code')
|
|
|
|
|
|
|
|
|
|
def test_str(self):
|
|
|
|
|
assert str(ErrorDetail('msg1')) == 'msg1'
|
|
|
|
|
assert str(ErrorDetail('msg1', 'code')) == 'msg1'
|
|
|
|
|
|
2018-04-20 16:32:37 +03:00
|
|
|
|
def test_hash(self):
|
|
|
|
|
assert hash(ErrorDetail('msg')) == hash('msg')
|
|
|
|
|
assert hash(ErrorDetail('msg', 'code')) == hash('msg')
|
|
|
|
|
|
2018-01-30 10:45:09 +03:00
|
|
|
|
|
2018-01-02 13:28:45 +03:00
|
|
|
|
class TranslationTests(TestCase):
|
|
|
|
|
|
|
|
|
|
@translation.override('fr')
|
|
|
|
|
def test_message(self):
|
|
|
|
|
# this test largely acts as a sanity test to ensure the translation files are present.
|
|
|
|
|
self.assertEqual(_('A server error occurred.'), 'Une erreur du serveur est survenue.')
|
2019-04-30 18:53:44 +03:00
|
|
|
|
self.assertEqual(str(APIException()), 'Une erreur du serveur est survenue.')
|
2018-04-03 10:16:36 +03:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_server_error():
|
|
|
|
|
request = RequestFactory().get('/')
|
|
|
|
|
response = server_error(request)
|
|
|
|
|
assert response.status_code == 500
|
|
|
|
|
assert response["content-type"] == 'application/json'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_bad_request():
|
|
|
|
|
request = RequestFactory().get('/')
|
|
|
|
|
exception = Exception('Something went wrong — Not used')
|
|
|
|
|
response = bad_request(request, exception)
|
|
|
|
|
assert response.status_code == 400
|
|
|
|
|
assert response["content-type"] == 'application/json'
|