Files
state-corp-backend/tests/apps/core/test_views.py
Aleksandr Meshchriakov 74d6181a63
All checks were successful
State Corp Backend CI/CD / Quality gate (pull_request) Successful in 4m15s
State Corp Backend CI/CD / Build linux/amd64 images once (pull_request) Has been skipped
State Corp Backend CI/CD / Release dev (pull_request) Has been skipped
State Corp Backend CI/CD / Refresh and release internal main (pull_request) Has been skipped
State Corp Backend CI/CD / Release customer main (pull_request) Has been skipped
fix: keep readiness probes unthrottled
2026-08-27 15:00:23 +03:00

123 lines
4.3 KiB
Python

"""Tests for core views (health checks)"""
from unittest.mock import patch
from django.urls import reverse
from rest_framework import status
from rest_framework.test import APITestCase
from rest_framework.throttling import BaseThrottle
from rest_framework.views import APIView
class RejectAllThrottle(BaseThrottle):
"""Test throttle proving readiness ignores inherited API throttles."""
def allow_request(self, request, view):
return False
class HealthCheckViewTest(APITestCase):
"""Tests for HealthCheckView"""
def test_health_check_url_reverse(self):
"""Test reverse URL resolution for health check"""
url = reverse("core:health")
self.assertEqual(url, "/health/")
def test_health_check_success(self):
"""Test health check returns healthy status"""
url = reverse("core:health")
response = self.client.get(url)
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertIn("status", response.data)
self.assertIn("version", response.data)
self.assertIn("checks", response.data)
self.assertIn("database", response.data["checks"])
def test_health_check_database_up(self):
"""Test health check reports database as up"""
url = reverse("core:health")
response = self.client.get(url)
self.assertEqual(response.data["checks"]["database"]["status"], "up")
self.assertIn("latency_ms", response.data["checks"]["database"])
class LivenessViewTest(APITestCase):
"""Tests for LivenessView"""
def test_liveness_url_reverse(self):
"""Test reverse URL resolution for liveness"""
url = reverse("core:liveness")
self.assertEqual(url, "/health/live/")
def test_liveness_returns_alive(self):
"""Test liveness probe returns alive status"""
url = reverse("core:liveness")
response = self.client.get(url)
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.data["status"], "alive")
class ReadinessViewTest(APITestCase):
"""Tests for ReadinessView"""
def test_readiness_url_reverse(self):
"""Test reverse URL resolution for readiness"""
url = reverse("core:readiness")
self.assertEqual(url, "/health/ready/")
def test_readiness_returns_ready(self):
"""Test readiness probe returns ready when DB is available"""
url = reverse("core:readiness")
response = self.client.get(url)
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.data["status"], "ready")
def test_readiness_bypasses_inherited_api_throttles(self):
"""Readiness probes remain available when global throttles reject traffic."""
url = reverse("core:readiness")
with patch.object(APIView, "throttle_classes", [RejectAllThrottle]):
response = self.client.get(url)
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.data["status"], "ready")
class APIVersioningURLTest(APITestCase):
"""Tests for API versioning URL structure"""
def test_api_v1_user_register_reverse(self):
"""Test reverse URL for user registration"""
url = reverse("api_v1:user:register")
self.assertEqual(url, "/api/v1/users/register/")
def test_api_v1_user_login_reverse(self):
"""Test reverse URL for user login"""
url = reverse("api_v1:user:login")
self.assertEqual(url, "/api/v1/users/login/")
def test_api_v1_user_logout_reverse(self):
"""Test reverse URL for user logout"""
url = reverse("api_v1:user:logout")
self.assertEqual(url, "/api/v1/users/logout/")
def test_api_v1_user_current_user_reverse(self):
"""Test reverse URL for current user"""
url = reverse("api_v1:user:current_user")
self.assertEqual(url, "/api/v1/users/me/")
def test_api_v1_user_token_refresh_reverse(self):
"""Test reverse URL for token refresh"""
url = reverse("api_v1:user:token_refresh")
self.assertEqual(url, "/api/v1/users/token/refresh/")
def test_api_v1_user_password_change_reverse(self):
"""Test reverse URL for password change"""
url = reverse("api_v1:user:password_change")
self.assertEqual(url, "/api/v1/users/password/change/")