1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# https://flask.palletsprojects.com/en/3.0.x/tutorial/tests/
import os
import tempfile

import pytest
from sqlalchemy import create_engine

from go_ji import create_app
from go_ji.db import Base
from go_ji.db import create_session as create_db_session


@pytest.fixture
def authed(app, client):
    with app.test_request_context("/") as context:
        client.get(
            "/",
            headers={
                "Tailscale-User-Login": "foo@example",
                "Tailscale-User-Name": "Foo Bar",
            },
        )

        yield context


@pytest.fixture
def app(db_url):
    app = create_app(
        {
            "TESTING": True,
            "DB_URL": db_url,
            "SENTRY": {},
        }
    )

    engine = create_engine(db_url)
    Base.metadata.create_all(bind=engine)

    yield app


@pytest.fixture
def client(app):
    return app.test_client()


@pytest.fixture
def runner(app):
    return app.test_cli_runner()


@pytest.fixture
def db_session(db_url):
    return create_db_session(db_url)


@pytest.fixture
def db_url():
    db_fd, db_path = tempfile.mkstemp()

    yield f"sqlite:////{db_path}"

    os.close(db_fd)
    os.unlink(db_path)