from io import StringIO

from django.contrib.auth import get_user_model
from django.core.management import call_command
from django.test import TestCase
from django.urls import reverse

User = get_user_model()


class DjangoPluginRuntimeTests(TestCase):
    def setUp(self):
        self.client.raise_request_exception = False
        self.user = User.objects.create_superuser(
            username="runtime-test-superuser",
            email="superuser@example.com",
            password="StrongTestPass123!",
        )
        self.client.force_login(self.user)

    def test_django_checks_pass(self):
        call_command("check", stdout=StringIO())

    def test_error_views_render_expected_statuses(self):
        self.assertEqual(self.client.get(reverse("error403")).status_code, 403)
        self.assertEqual(self.client.get(reverse("error404")).status_code, 404)
        self.assertEqual(self.client.get(reverse("error500")).status_code, 500)
        self.assertEqual(self.client.get(reverse("errorcsrf")).status_code, 403)
