"""Tests for config module."""

import os

import pytest

from ect import default_config, load_config_from_env
from ect.config import ENV_IAT_MAX_AGE_MINUTES, ENV_JTI_REPLAY_CACHE_SIZE


def test_default_config():
    c = default_config()
    assert c.iat_max_age_sec == 900
    assert c.jti_replay_size == 0


def test_load_config_from_env():
    os.environ[ENV_IAT_MAX_AGE_MINUTES] = "20"
    os.environ[ENV_JTI_REPLAY_CACHE_SIZE] = "500"
    try:
        c = load_config_from_env()
        assert c.iat_max_age_sec == 20 * 60
        assert c.jti_replay_size == 500
    finally:
        os.environ.pop(ENV_IAT_MAX_AGE_MINUTES, None)
        os.environ.pop(ENV_JTI_REPLAY_CACHE_SIZE, None)


def test_config_create_options():
    c = default_config()
    opts = c.create_options("my-kid")
    assert opts.key_id == "my-kid"
    assert opts.default_expiry_sec == c.default_expiry_sec


def test_config_verify_options():
    c = default_config()
    opts = c.verify_options()
    assert opts.iat_max_age_sec == c.iat_max_age_sec
    assert opts.dag is not None


def test_load_config_invalid_int():
    os.environ[ENV_IAT_MAX_AGE_MINUTES] = "bad"
    try:
        c = load_config_from_env()
        assert c.iat_max_age_sec == 900
    finally:
        os.environ.pop(ENV_IAT_MAX_AGE_MINUTES, None)
