from unittest import TestCase

from vbv_lernwelt.shop.services import is_signature_valid


class DatatransSigningTestCase(TestCase):
    """
    Test based on the example from the docs.
    Key is from their example, not ours!
    """

    HMAC_KEY_FROM_THE_DOCS_NOT_HAZARDOUS = (
        "861bbfc01e089259091927d6ad7f71c8"
        "b46b7ee13499574e83c633b74cdc29e3"
        "b7e262e41318c8425c520f146986675f"
        "dd58a4531a01c99f06da378fdab0414a"
    )

    def test_signature_happy_ala_docs(self):
        # GIVEN
        payload = b"HELLO"
        signature = "t=1605697463367,s0=82ef9a8178dcb4df0b71540fa06d7da826ecb26e1977e230bdc8c9d6f9f1af84"

        # WHEN / THEN
        self.assertTrue(
            is_signature_valid(
                hmac_key=self.HMAC_KEY_FROM_THE_DOCS_NOT_HAZARDOUS,
                payload=payload,
                signature=signature,
            )
        )

    def test_signature_not_happy(self):
        # GIVEN
        tampered_payload = b"HELLO=I=TAMPERED=WITH=PAYLOAD=HIHI=I=AM=EVIL"
        signature = "t=1605697463367,s0=82ef9a8178dcb4df0b71540fa06d7da826ecb26e1977e230bdc8c9d6f9f1af84"

        # THEN
        self.assertFalse(
            is_signature_valid(
                hmac_key=self.HMAC_KEY_FROM_THE_DOCS_NOT_HAZARDOUS,
                payload=tampered_payload,
                signature=signature,
            )
        )
