70 lines
2.0 KiB
Python
70 lines
2.0 KiB
Python
import logging
|
|
import pytest
|
|
import json
|
|
|
|
from django.test.client import RequestFactory
|
|
from django.http import Http404
|
|
from django.core.exceptions import ObjectDoesNotExist, PermissionDenied
|
|
|
|
|
|
from passerelle.utils import to_json
|
|
|
|
class WrappedException(Exception):
|
|
pass
|
|
|
|
|
|
@to_json('api')
|
|
def wrapped_exception(req, *args, **kwargs):
|
|
raise WrappedException
|
|
|
|
@to_json('api')
|
|
def http404_exception(req, *args, **kwargs):
|
|
raise Http404
|
|
|
|
@to_json('api')
|
|
def doesnotexist_exception(req, *args, **kwargs):
|
|
raise ObjectDoesNotExist
|
|
|
|
|
|
@to_json('api')
|
|
def permissiondenied_exception(req, *args, **kwargs):
|
|
raise PermissionDenied
|
|
|
|
|
|
def test_jsonresponselog_get(caplog):
|
|
request = RequestFactory()
|
|
wrapped_exception(request.get('/'))
|
|
post_payload = {'data': 'plop'}
|
|
with pytest.raises(WrappedException):
|
|
wrapped_exception(request.post('/?raise=1', post_payload))
|
|
|
|
for record in caplog.records():
|
|
assert record.name == 'passerelle.jsonresponse'
|
|
assert record.levelno == logging.ERROR
|
|
assert hasattr(record, 'method')
|
|
if record.method == 'POST':
|
|
assert hasattr(record, 'body')
|
|
assert "Error occurred while processing request" in record.message
|
|
|
|
def test_jsonresponselog_http404(caplog):
|
|
request = RequestFactory()
|
|
http404_exception(request.get('/'))
|
|
assert caplog.records() == []
|
|
|
|
def test_jsonresponselog_doesnotexist(caplog):
|
|
request = RequestFactory()
|
|
doesnotexist_exception(request.get('/'))
|
|
for record in caplog.records():
|
|
assert record.name == 'passerelle.jsonresponse'
|
|
assert record.levelno == logging.WARNING
|
|
assert 'object not found' in record.message
|
|
|
|
|
|
def test_jsonresponselog_permissiondenied(caplog):
|
|
request = RequestFactory()
|
|
permissiondenied_exception(request.get('/'))
|
|
for record in caplog.records():
|
|
assert record.name == 'passerelle.jsonresponse'
|
|
assert record.levelno == logging.WARNING
|
|
assert 'Permission denied' in record.message
|