"""Additional tests for types module."""

import pytest

from ect import Payload


def test_payload_contains_audience():
    p = Payload(iss="", aud=["a", "b"], iat=0, exp=0, jti="", exec_act="", pred=[])
    assert p.contains_audience("a") is True
    assert p.contains_audience("c") is False


def test_payload_compensation_required():
    p = Payload(iss="", aud=[], iat=0, exp=0, jti="", exec_act="", pred=[])
    assert p.compensation_required() is False
    p.ext = {"compensation_required": True}
    assert p.compensation_required() is True


def test_payload_has_policy_claims():
    p = Payload(iss="", aud=[], iat=0, exp=0, jti="", exec_act="", pred=[],
                ext={"pol": "p", "pol_decision": "approved"})
    assert p.has_policy_claims() is True
    p.ext = {"pol_decision": "approved"}
    assert p.has_policy_claims() is False
    p.ext = None
    assert p.has_policy_claims() is False


def test_payload_pol_decision():
    p = Payload(iss="", aud=[], iat=0, exp=0, jti="", exec_act="", pred=[],
                ext={"pol_decision": "rejected"})
    assert p.pol_decision() == "rejected"
    p.ext = None
    assert p.pol_decision() == ""


def test_payload_to_claims_optional():
    p = Payload(iss="i", aud=["a"], iat=1, exp=2, jti="j", exec_act="e", pred=[], wid="wf")
    claims = p.to_claims()
    assert claims["wid"] == "wf"
    assert "ect_ext" not in claims or not claims.get("ect_ext")


def test_payload_from_claims_aud_string():
    claims = {"iss": "i", "aud": "single", "iat": 1, "exp": 2, "jti": "j", "exec_act": "e", "pred": []}
    p = Payload.from_claims(claims)
    assert p.aud == ["single"]


def test_payload_to_claims_all_optional():
    p = Payload(
        iss="i", aud=["a"], iat=1, exp=2, jti="j", exec_act="e", pred=[],
        wid="w", inp_hash="h", out_hash="o", inp_classification="c",
        ext={"pol": "p", "pol_decision": "approved"},
    )
    claims = p.to_claims()
    assert claims["wid"] == "w"
    assert claims["inp_hash"] == "h"
    assert claims["out_hash"] == "o"
    assert claims["inp_classification"] == "c"
    assert claims["ect_ext"]["pol"] == "p"
    assert claims["ect_ext"]["pol_decision"] == "approved"
