Files
mostovik-backend/tests/apps/core/test_exception_handler.py
Aleksandr Meshchriakov 3d298ce352
Some checks failed
CI/CD Pipeline / Run Tests (pull_request) Successful in 1m53s
CI/CD Pipeline / Telegram Notify Success (push) Has been cancelled
CI/CD Pipeline / Run Tests (push) Has been cancelled
CI/CD Pipeline / Code Quality Checks (push) Has been cancelled
CI/CD Pipeline / Code Quality Checks (pull_request) Failing after 2m54s
CI/CD Pipeline / Telegram Notify Success (pull_request) Has been skipped
feat: expand platform APIs, sources, and test coverage
2026-03-17 12:56:48 +01:00

64 lines
2.8 KiB
Python

"""Tests for custom exception handler."""
from apps.core.exception_handler import custom_exception_handler
from apps.core.exceptions import BaseAPIException
from django.core.exceptions import PermissionDenied
from django.http import Http404
from django.test import SimpleTestCase
from rest_framework.exceptions import APIException, ValidationError
class CustomExceptionHandlerTest(SimpleTestCase):
def _context(self):
return {"request": None, "view": None}
def test_base_api_exception(self):
class CustomAPIException(BaseAPIException):
status_code = 418
exc = CustomAPIException(message="oops", code="oops_code")
response = custom_exception_handler(exc, self._context())
self.assertEqual(response.status_code, 418)
self.assertFalse(response.data["success"])
self.assertEqual(response.data["errors"][0]["code"], "oops_code")
def test_http404_exception(self):
response = custom_exception_handler(Http404("missing"), self._context())
self.assertEqual(response.status_code, 404)
self.assertEqual(response.data["errors"][0]["code"], "not_found")
def test_permission_denied_exception(self):
response = custom_exception_handler(PermissionDenied("no"), self._context())
self.assertEqual(response.status_code, 403)
self.assertEqual(response.data["errors"][0]["code"], "permission_denied")
def test_api_exception_with_detail(self):
exc = APIException("bad")
response = custom_exception_handler(exc, self._context())
self.assertIsNotNone(response)
self.assertEqual(response.status_code, 500)
self.assertFalse(response.data["success"])
def test_validation_error_fields(self):
exc = ValidationError({"field": ["invalid"]})
response = custom_exception_handler(exc, self._context())
self.assertEqual(response.status_code, 400)
self.assertEqual(response.data["errors"][0]["code"], "validation_error")
def test_validation_error_list(self):
exc = ValidationError(["item1", "item2"])
response = custom_exception_handler(exc, self._context())
self.assertEqual(response.status_code, 400)
self.assertEqual(len(response.data["errors"]), 2)
def test_validation_error_scalar_message(self):
exc = ValidationError("plain error")
response = custom_exception_handler(exc, self._context())
self.assertEqual(response.status_code, 400)
self.assertEqual(response.data["errors"][0]["message"], "plain error")
def test_unhandled_exception(self):
response = custom_exception_handler(RuntimeError("boom"), self._context())
self.assertEqual(response.status_code, 500)
self.assertEqual(response.data["errors"][0]["code"], "internal_error")