Beispiel #1
0
 def test_str_for_localized_error_string(self):
     exception = APIException(
             'RATELIMIT', u'実行回数が多すぎます', u'フィールド')
     assert str(exception) == (
             'RATELIMIT: \'\\u5b9f\\u884c\\u56de\\u6570\\u304c\\u591a'
             '\\u3059\\u304e\\u307e\\u3059\' on field '
             '\'\\u30d5\\u30a3\\u30fc\\u30eb\\u30c9\'')
Beispiel #2
0
 def test_str(self):
     exception = APIException(
         "BAD_SOMETHING", "invalid something", "some_field"
     )
     assert str(exception) == (
         "BAD_SOMETHING: 'invalid something' on field 'some_field'"
     )
Beispiel #3
0
 def test_api_exception(self):
     exc = APIException(["test", "testing", "test"])
     with pytest.raises(DeprecationWarning):
         exc.error_type
     with pytest.raises(DeprecationWarning):
         exc.message
     with pytest.raises(DeprecationWarning):
         exc.field
def test_api_exception(client, logged_in_profile, mocker):
    """Make sure APIExceptions which aren't recognized become 500 errors"""
    exception = APIException("bizarre", "A bizarre exception", "bizarre_field")
    mocker.patch(
        "channels.serializers.comments.CommentSerializer.update", side_effect=exception
    )
    url = reverse("comment-detail", kwargs={"comment_id": "e8h"})
    with pytest.raises(APIException) as ex:
        client.patch(url, type="json", data={"text": "updated text"})
    assert ex.value == exception
Beispiel #5
0
        raise Exception("some random error")

    notification.refresh_from_db()
    assert notification.state == EmailNotification.STATE_PENDING
    assert notification.sent_at is None


@pytest.mark.parametrize(
    "error,expected_cls",
    [
        (
            PRAWException("No 'Comment' data returned for thing t1_u5"),
            CancelNotificationError,
        ),
        (PRAWException("NOT TRANSLATED"), PRAWException),
        (APIException("FAKE_ERROR_TYPE", "", ""), APIException),
        (Exception("NOT TRANSLATED"), Exception),
    ]
    + [
        (APIException(error_type, "", ""), CancelNotificationError)
        for error_type in (
            "SUBREDDIT_NOTALLOWED",
            "SUBREDDIT_NOEXIST",
            "DELETED_COMMENT",
        )
    ]
    + [
        (
            error_cls(
                MagicMock(
                    # because of side-affecting constructors
Beispiel #6
0
 def test_inheritance(self):
     assert isinstance(APIException(None, None, None), PRAWException)
Beispiel #7
0
 def test_str(self):
     exception = APIException('BAD_SOMETHING', 'invalid something',
                              'some_field')
     assert str(exception) == ('BAD_SOMETHING: \'invalid something\' on '
                               'field \'some_field\'')
Beispiel #8
0
 def test_str_for_localized_error_string(self):
     exception = APIException("RATELIMIT", u"実行回数が多すぎます", u"フィールド")
     assert str(exception) == (
         "RATELIMIT: '\\u5b9f\\u884c\\u56de\\u6570\\u304c\\u591a"
         "\\u3059\\u304e\\u307e\\u3059' on field "
         "'\\u30d5\\u30a3\\u30fc\\u30eb\\u30c9'")
    posts._next_batch.assert_called_once_with()


@pytest.mark.parametrize(
    "raised_exception,is_anonymous,expected_exception",
    [
        [None, False, None],
        [Forbidden(Mock(status_code=403)), True, NotAuthenticated],
        [Forbidden(Mock(status_code=403)), False, PermissionDenied],
        [PrawNotFound(Mock()), False, NotFound],
        [
            Redirect(Mock(headers={"location": "http://example.com"})), False,
            NotFound
        ],
        [
            APIException("SUBREDDIT_NOTALLOWED", "msg", "field"), False,
            PermissionDenied
        ],
        [APIException("NOT_AUTHOR", "msg", "field"), False, PermissionDenied],
        [APIException("SUBREDDIT_NOEXIST", "msg", "field"), False, NotFound],
        [
            APIException("SUBREDDIT_EXISTS", "msg", "field"), False,
            ConflictException
        ],
        [
            APIException("DELETED_COMMENT", "msg", "field"), False,
            GoneException
        ],
        [KeyError(), False, KeyError],
    ],
)