import pytest
from nuc.envelope import (
    InvalidSignatureException,
    NucTokenEnvelope,
    urlsafe_base64_decode,
    urlsafe_base64_encode,
)
from nuc.token import Did

_VALID_TOKEN: str = "eyJhbGciOiJFUzI1NksifQ.eyJpc3MiOiJkaWQ6bmlsOjAyMjZhNGQ0YTRhNWZhZGUxMmM1ZmYwZWM5YzQ3MjQ5ZjIxY2Y3N2EyMDI3NTFmOTU5ZDVjNzc4ZjBiNjUyYjcxNiIsImF1ZCI6ImRpZDpuaWw6YmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiIiwic3ViIjoiZGlkOm5pbDpjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2MiLCJjbWQiOiIvbmlsL2RiL3JlYWQiLCJhcmdzIjp7ImZvbyI6NDJ9LCJub25jZSI6IjAxMDIwMyIsInByZiI6WyJjOTA0YzVhMWFiMzY5YWVhMWI0ZDlkMTkwMmE0NmU2ZWY5NGFhYjk2OTY0YmI1MWQ2MWE2MWIwM2UyM2Q1ZGZmIl19.ufDYxqoSVNVETrVKReu0h_Piul5c6RoC_VnGGLw04mkyn2OMrtQjK92sGXNHCjlp7T9prIwxX14ZB_N3gx7hPg"


class TestEnvelope:
    def test_specific_token(self):
        raw_token = "eyJhbGciOiJFUzI1NksifQ.eyJpc3MiOiJkaWQ6bmlsOjAyMjZhNGQ0YTRhNWZhZGUxMmM1ZmYwZWM5YzQ3MjQ5ZjIxY2Y3N2EyMDI3NTFmOTU5ZDVjNzc4ZjBiNjUyYjcxNiIsImF1ZCI6ImRpZDpuaWw6YmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiIiwic3ViIjoiZGlkOm5pbDpjY2NjY2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jQ5YzljZjc0M2Y4YTZlZDQyMDNlNzgyZThlZTA5YWZhNTNkMWI1NzY0OTg0NjEyMzQyNSIsImF1ZCI6ImRpZDpuaWw6YmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiIiwic3ViIjoiZGlkOm5pbDpjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2MiLCJjbWQiOiIvbmlsL2RiL3JlYWQiLCJwb2wiOltbIj09IiwiLmZvbyIsNDJdXSwibm9uY2UiOiIwMTAyMDMiLCJwcmYiOltdfQ.o3lnQxCjDCW10UuRABrHp8FpB_C6q1xgEGvfuXTb7Epp63ry8R2h0wHjToDKDFmkmUmO2jcBkrttuy8kftV6og"
        token = NucTokenEnvelope.parse(raw_token)
        token.validate_signatures()

        proofs = token.proofs
        assert len(proofs) == 2
        assert proofs[0].token.issuer == Did.nil(
            b"\x03op\x7f\xbe\xf0\xb7R\x13\x80\x8b\xbb\xf5K\x18!v\xf52\x0f\xa5 \xcb\x81\x9f5\xb5\xba\xf228\xa0\x15"
        )
        assert proofs[1].token.issuer == Did.nil(
            b"\x03\x95\x90\xcc\xaf\x10$\xf4\x9c\x9c\xf7C\xf8\xa6\xedB\x03\xe7\x82\xe8\xee\t\xaf\xa5=\x1bWd\x98F\x124%"
        )

    def test_different_signature(self):
        (header, payload, signature) = _VALID_TOKEN.split(".")
        invalid_signature = bytearray(urlsafe_base64_decode(signature))
        invalid_signature[0] ^= 1
        with pytest.raises(InvalidSignatureException):
            NucTokenEnvelope.parse(
                f"{header}.{payload}.{urlsafe_base64_encode(bytes(invalid_signature))}"
            ).validate_signatures()

    def test_different_header(self):
        (_, payload, signature) = _VALID_TOKEN.split(".")
        header = urlsafe_base64_encode('{"alg":"ES256K"     }'.encode("utf8"))
        with pytest.raises(InvalidSignatureException):
            NucTokenEnvelope.parse(
                f"{header}.{payload}.{signature}"
            ).validate_signatures()

    def test_different_payload(self):
        (header, payload, signature) = _VALID_TOKEN.split(".")
        payload = urlsafe_base64_decode(payload)
        payload = urlsafe_base64_encode(payload + b"    ")
        with pytest.raises(InvalidSignatureException):
            NucTokenEnvelope.parse(
                f"{header}.{payload}.{signature}"
            ).validate_signatures()
