#!/usr/bin/env python3

from tentacles.store import Store


def test_store_initializes(store: Store):
    assert isinstance(store, Store)


def test_mkuser(store: Store, uid_testy, username_testy):
    assert store.list_users() == [(uid_testy, username_testy)]


def test_mksession(store: Store, uid_testy, username_testy, password_testy, login_ttl):
    sid = store.try_login(username_testy, password_testy, login_ttl)
    assert sid is not None
    assert store.list_keys() == [(sid, uid_testy)]
    assert store.try_key(sid) == uid_testy


def test_refresh_key(store: Store, sid_testy, login_ttl):
    before = store.fetch_key(sid_testy)
    store.refresh_key(sid_testy, login_ttl * 2)
    after = store.fetch_key(sid_testy)
    assert before != after


def tets_mkkey(store: Store, sid_testy, uid_testy):
    assert store.try_key(sid_testy) == uid_testy
    new_key = store.create_key(sid_testy, None)
    assert new_key is not None
    assert store.try_key(new_key) == uid_testy


def test_logout(store: Store, sid_testy):
    assert store.try_key(sid_testy)
    store.delete_key(sid_testy)
    assert not store.try_key(sid_testy)
