# retoor <retoor@molodetz.nl>

import base64
import time
import requests
from tests.conftest import BASE_URL
from devplacepy.database import get_table
_counter_api_auth = [0]
def _signup_api_auth(password="secret123"):
    _counter_api_auth[0] += 1
    name = f"apiauth{int(time.time() * 1000)}{_counter_api_auth[0]}"
    email = f"{name}@t.dev"
    session = requests.Session()
    session.post(
        f"{BASE_URL}/auth/signup",
        data={
            "username": name,
            "email": email,
            "password": password,
            "confirm_password": password,
            "birth_date": "1990-01-01",
            "accept_terms": "1",
        },
        allow_redirects=True,
    )
    return session, name, email, password
def _user_api_auth(name):
    return get_table("users").find_one(username=name)
def _key_api_auth(name):
    return _user_api_auth(name)["api_key"]


def test_signup_assigns_api_key(app_server):
    _, name, _, _ = _signup_api_auth()
    key = _key_api_auth(name)
    assert key and len(key) == 36
