-
Notifications
You must be signed in to change notification settings - Fork 0
/
conftest.py
62 lines (44 loc) · 1.22 KB
/
conftest.py
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
import os
import tempfile
import pytest
import json
from myapp import create_app
from myapp.db import get_db, init_db
with open(os.path.join(os.path.dirname(__file__), "tests/data.sql"), "rb") as f:
_data_sql = f.read().decode("utf8")
@pytest.fixture
def app():
db_fd, db_path = tempfile.mkstemp()
app = create_app(
{
"TESTING": True,
"DATABASE": db_path,
}
)
with app.app_context():
init_db()
get_db().executescript(_data_sql)
yield app
os.close(db_fd)
os.unlink(db_path)
@pytest.fixture
def client(app):
return app.test_client()
@pytest.fixture
def runner(app):
return app.test_cli_runner()
class AuthActions(object):
def __init__(self, client):
self._client = client
def login(self, username="test", password="test"):
return self._client.post(
"/api/v1/auth/login", content_type="application/json", json={"username": username, "password": password}
)
@pytest.fixture
def auth(client):
return AuthActions(client)
@pytest.fixture
def headers(auth):
response = auth.login()
token = json.loads(response.data)
return {"Authorization": "Bearer {}".format(token["access_token"])}