62 lines
1.7 KiB
Python
62 lines
1.7 KiB
Python
import pytest
|
|
from werkzeug.security import generate_password_hash
|
|
|
|
from hunter import create_app
|
|
from hunter.db import connect, now
|
|
|
|
|
|
@pytest.fixture
|
|
def app(tmp_path, monkeypatch):
|
|
monkeypatch.delenv("AI_API_KEY", raising=False)
|
|
monkeypatch.delenv("SENTRY_DSN", raising=False)
|
|
app = create_app(
|
|
{
|
|
"TESTING": True,
|
|
"SECRET_KEY": "mock-test-secret",
|
|
"DATABASE": str(tmp_path / "test.sqlite3"),
|
|
}
|
|
)
|
|
with connect(app.config["DATABASE"]) as db:
|
|
for role in ("admin", "analyst", "user"):
|
|
db.execute(
|
|
"INSERT INTO users(email,password,role,created) VALUES(?,?,?,?)",
|
|
(role + "@example.com", generate_password_hash("mock-password-123"), role, now()),
|
|
)
|
|
return app
|
|
|
|
|
|
@pytest.fixture
|
|
def client(app):
|
|
return app.test_client()
|
|
|
|
|
|
def sign_in(client, role="admin"):
|
|
with client.session_transaction() as session:
|
|
session["user_id"] = {"admin": 1, "analyst": 2, "user": 3}[role]
|
|
session["csrf"] = "mock-csrf"
|
|
|
|
|
|
@pytest.fixture
|
|
def admin(client):
|
|
sign_in(client)
|
|
return client
|
|
|
|
|
|
@pytest.fixture
|
|
def material(app):
|
|
with connect(app.config["DATABASE"]) as db:
|
|
return db.execute(
|
|
"INSERT INTO signals(source_id,title,summary,url,published,collected,category,country,translated_title) VALUES(?,?,?,?,?,?,?,?,?)",
|
|
(
|
|
1,
|
|
"Smart home research",
|
|
"New smart home technology",
|
|
"https://news.mit.edu/mock-article",
|
|
now(),
|
|
now(),
|
|
"Smart Home",
|
|
"US",
|
|
"Умный дом",
|
|
),
|
|
).lastrowid
|