# retoor <retoor@molodetz.nl>

import pytest

from devplacepy.database import set_setting
from devplacepy.services.dbapi import policy
from devplacepy.services.dbapi.policy import DbApiBadTable


def test_assert_table_accepts_known_table(local_db):
    assert policy.assert_table("users") == "users"


def test_assert_table_rejects_bad_name(local_db):
    with pytest.raises(DbApiBadTable):
        policy.assert_table("users; drop")


def test_assert_table_rejects_denied(local_db):
    with pytest.raises(DbApiBadTable):
        policy.assert_table("sessions")


def test_assert_table_rejects_unknown(local_db):
    with pytest.raises(DbApiBadTable):
        policy.assert_table("table_that_does_not_exist_zzz")


def test_allowed_tables_excludes_deny(local_db):
    tables = policy.allowed_tables()
    assert "sessions" not in tables
    assert "password_resets" not in tables
    assert "users" in tables


def test_soft_delete_aware():
    assert policy.soft_delete_aware("posts") is True
    assert policy.soft_delete_aware("users") is False


def test_deny_tables_includes_setting(local_db):
    set_setting("dbapi_deny_tables", "users, news")
    try:
        deny = policy.deny_tables()
        assert "users" in deny and "news" in deny
        assert "sessions" in deny
    finally:
        set_setting("dbapi_deny_tables", "")
