import re
from types import SimpleNamespace

import pytest

from security import Security


def test_bt_hsh_01_deterministic_hw_hash():
    h1 = Security.get_hw_hash("test-hardware-info")
    h2 = Security.get_hw_hash("test-hardware-info")
    assert h1 == h2


def test_bt_hsh_02_different_hardware_different_hash():
    assert Security.get_hw_hash("hw-a") != Security.get_hw_hash("hw-b")


def test_bt_hsh_03_output_valid_base64():
    h = Security.get_hw_hash("test-hardware-info")
    assert re.match(r"^[A-Za-z0-9+/]+=*$", h)


@pytest.mark.security
def test_st_hsh_01_hardware_hash_deterministic():
    h1 = Security.get_hw_hash("test-hardware-info")
    h2 = Security.get_hw_hash("test-hardware-info")
    assert h1 == h2


@pytest.mark.security
def test_st_hsh_02_different_hardware_different_hash():
    assert Security.get_hw_hash("hw-a") != Security.get_hw_hash("hw-b")


@pytest.mark.security
def test_st_hsh_03_api_key_depends_on_hardware():
    creds = SimpleNamespace(email="a@b.com", password="pass1")
    hw1 = Security.get_hw_hash("hw-1")
    hw2 = Security.get_hw_hash("hw-2")
    k1 = Security.get_api_encryption_key(creds, hw1)
    k2 = Security.get_api_encryption_key(creds, hw2)
    assert k1 != k2


@pytest.mark.security
def test_st_hsh_04_api_key_depends_on_credentials():
    hw = Security.get_hw_hash("fixed-hw")
    c1 = SimpleNamespace(email="a@b.com", password="pass1")
    c2 = SimpleNamespace(email="x@y.com", password="pass2")
    k1 = Security.get_api_encryption_key(c1, hw)
    k2 = Security.get_api_encryption_key(c2, hw)
    assert k1 != k2
