# (c) cavaliba.com - tests / helper_apikey

from app_data import crypto
from app_data.data import Instance

_AAA = {"username": "unittest:helper"}


def add_apikey_admin():

    secret = crypto.hash_create("mysecret")

    instance = Instance(classname="_apikey", keyname="test")
    data = {
        "secret": secret,
        "ip_filter": "*",
        "is_readonly": False,
        "is_enabled": True,
        "acl": "\n".join(
            [
                "p_data_admin",
                "p_info",
                "p_schema_read",
                "p_schema_write",
                "p_group_read",
                "p_role_read",
                "p_user_read",
                "p_data_access",
                "p_data_create",
                "p_data_import",
                "p_permission_read",
                "p_pipeline_run",
                "p_task_manage",
            ]
        ),
    }
    instance.merge_import(data)
    instance.save(aaa=_AAA, action="create")

    return {"X-Cavaliba-Key": "test mysecret"}


def add_apikey(keyname="testkey", acl=""):

    secret = crypto.hash_create("testpassword")

    instance = Instance(classname="_apikey", keyname=keyname)
    data = {
        "secret": secret,
        "ip_filter": "*",
        "is_readonly": False,
        "is_enabled": True,
        "acl": acl,
    }
    instance.merge_import(data)
    instance.save(aaa=_AAA, action="create")

    return {"X-Cavaliba-Key": f"{keyname} testpassword"}
