from rest_framework.test import APITestCase

from vbv_lernwelt.core.create_default_users import create_default_users
from vbv_lernwelt.course.creators.test_course import create_test_course
from vbv_lernwelt.learnpath.models import LearningContentEdoniqTest


class RedirectTestCase(APITestCase):
    def setUp(self) -> None:
        create_default_users()
        create_test_course(with_sessions=True)

    def test_redirects_normal_test(self):
        test = LearningContentEdoniqTest.objects.get(
            title="Wissens- und Verständnisfragen"
        )
        data = {"learning_content_id": test.id, "extended_time_test": False}
        self.client.login(username="admin", password="test")
        self._test_redirect(
            data,
            "https://exam.vbv-afa.ch/e-tutor/v4/user/course/pre_course_object?aid=1689096897473,2147466097",
        )

    def test_redirects_extended_test(self):
        test = LearningContentEdoniqTest.objects.get(
            title="Wissens- und Verständnisfragen"
        )
        data = {"learning_content_id": test.id, "extended_time_test": True}
        self.client.login(username="admin", password="test")
        self._test_redirect(
            data,
            "https://exam2.vbv-afa.ch/e-tutor/v4/user/course/pre_course_object?aid=1689096897473,2147466097",
        )

    def _test_redirect(self, data, expected_url):
        response = self.client.post(
            "/api/core/edoniq-test/redirect/", data, format="json"
        )
        self.assertEqual(200, response.status_code)
        content = response.json()
        self.assertTrue(content["redirect_url"].startswith(expected_url))
