from django.test import TestCase

from oscar.core.loading import get_model

Order = get_model("order", "Order")


class CheckoutThankYouTest(TestCase):
    fixtures = [
        "checkout/auth",
        "checkout/catalogue",
        "checkout/shipping",
        "checkout/basket",
        "checkout/order",
    ]

    def test_thankyou_from_link_view(self):
        order = Order.objects.get(pk="1")
        resp = self.client.get(order.get_thank_you_url(), follow=True)

        self.assertEqual(resp.context.get("object", None), order)
        self.assertEqual(resp.template_name[0], "oscar/checkout/thank_you.html")
        self.assertEqual(
            resp.context["language_neutral_url_path"],
            "/checkout/thank-you/MQ/hoela202100000001:m8SxZrjG1ERKfA-NRgW4tfzP-aqcttY1WMAfNuGW7e4/",
        )

    def test_thankyou_from_link_invalid_token(self):
        resp = self.client.get("/checkout/thank-you/MQ/fail/")
        self.assertEqual(resp.url, "/checkout/order-not-found/")
        self.assertEqual(resp.status_code, 302)

    def test_thankyou_from_link_invalid_oidb64(self):
        resp = self.client.get(
            "/checkout/thank-you/fail/hoela202100000001:m8SxZrjG1ERKfA-NRgW4tfzP-aqcttY1WMAfNuGW7e4/"
        )
        self.assertEqual(resp.url, "/checkout/order-not-found/")
        self.assertEqual(resp.status_code, 302)

    def test_thankyou_from_link_invalid_no_oidb64_or_token(self):
        resp = self.client.get("/checkout/thank-you/", follow=True)

        self.assertEqual(resp.status_code, 200)
