"""API integration tests (real PostgreSQL): auth, CSRF, recipients, subjects, templates."""
import base64
import io
import uuid

import pytest


def csrf(client) -> str:
    for c in client.cookies.jar:
        if c.name == "mailer_csrf":
            return c.value
    raise AssertionError("no csrf cookie")


def H(client, **extra):
    h = {"X-CSRF-Token": csrf(client)}
    h.update(extra)
    return h


def tpl_id_of(r) -> int:
    j = r.json()
    return j["id"] if "id" in j else j["template"]["id"]


def sub_id_of(r) -> int:
    j = r.json()
    return j["id"] if "id" in j else j["subject"]["id"]

