2026-04-26 18:32:32 +02:00
|
|
|
import os
|
|
|
|
|
import base64
|
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
|
|
import app as app_module
|
|
|
|
|
from app import app as flask_app, init_db
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
|
def app(tmp_path):
|
|
|
|
|
db_path = str(tmp_path / "test.db")
|
|
|
|
|
app_module.DATABASE = db_path
|
|
|
|
|
flask_app.config["TESTING"] = True
|
|
|
|
|
with flask_app.app_context():
|
|
|
|
|
init_db()
|
|
|
|
|
yield flask_app
|
|
|
|
|
app_module.DATABASE = os.environ.get("DATABASE", "punktfri.db")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
|
def client(app):
|
|
|
|
|
return app.test_client()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_health(client):
|
|
|
|
|
r = client.get("/health")
|
|
|
|
|
assert r.status_code == 200
|
|
|
|
|
data = r.get_json()
|
|
|
|
|
assert data["status"] == "ok"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_index_get(client):
|
|
|
|
|
r = client.get("/")
|
|
|
|
|
assert r.status_code == 200
|
|
|
|
|
assert b"PunktFri" in r.data
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_tak_get(client):
|
|
|
|
|
r = client.get("/tak")
|
|
|
|
|
assert r.status_code == 200
|
|
|
|
|
assert b"tak" in r.data.lower()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_admin_unauthorized(client):
|
|
|
|
|
r = client.get("/admin")
|
|
|
|
|
assert r.status_code == 401
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_admin_authorized(client):
|
|
|
|
|
creds = base64.b64encode(b"admin:punktfri2024").decode()
|
|
|
|
|
r = client.get("/admin", headers={"Authorization": f"Basic {creds}"})
|
|
|
|
|
assert r.status_code == 200
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_signup_missing_fields(client):
|
|
|
|
|
r = client.post("/", data={})
|
|
|
|
|
assert r.status_code == 200
|
|
|
|
|
assert "felter" in r.data.decode("utf-8")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_signup_invalid_domains(client):
|
|
|
|
|
r = client.post("/", data={
|
|
|
|
|
"navn": "Test", "email": "test@test.dk",
|
|
|
|
|
"domaener": "0", "egne_ns": "ja",
|
|
|
|
|
})
|
|
|
|
|
assert r.status_code == 200
|
|
|
|
|
assert "gyldigt antal" in r.data.decode("utf-8")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_signup_success(client):
|
|
|
|
|
r = client.post("/", data={
|
|
|
|
|
"navn": "Test Bruger",
|
|
|
|
|
"email": "test@test.dk",
|
|
|
|
|
"domaener": "3",
|
|
|
|
|
"egne_ns": "ja",
|
|
|
|
|
"kommentar": "Test kommentar",
|
|
|
|
|
}, follow_redirects=False)
|
|
|
|
|
assert r.status_code == 302
|
|
|
|
|
assert "/tak" in r.location
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_signup_duplicate_email(client):
|
|
|
|
|
data = {
|
|
|
|
|
"navn": "Test Bruger",
|
|
|
|
|
"email": "dup@test.dk",
|
|
|
|
|
"domaener": "2",
|
|
|
|
|
"egne_ns": "nej",
|
|
|
|
|
}
|
|
|
|
|
client.post("/", data=data)
|
|
|
|
|
r = client.post("/", data=data)
|
|
|
|
|
assert r.status_code == 200
|
|
|
|
|
assert "allerede" in r.data.decode("utf-8")
|
2026-09-19 06:52:45 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_signup_sends_notification(client, monkeypatch):
|
|
|
|
|
sent = []
|
|
|
|
|
monkeypatch.setattr(app_module, "SMTP_HOST", "smtp.example")
|
|
|
|
|
monkeypatch.setattr(app_module, "_send", sent.append)
|
|
|
|
|
monkeypatch.setattr(app_module.threading, "Thread",
|
|
|
|
|
lambda target, args, daemon: type("T", (), {"start": lambda self: target(*args)})())
|
|
|
|
|
client.post("/", data={
|
|
|
|
|
"navn": "Notif Test", "email": "notif@test.dk",
|
|
|
|
|
"domaener": "4", "egne_ns": "ja", "kommentar": "hej",
|
|
|
|
|
})
|
|
|
|
|
assert len(sent) == 1
|
|
|
|
|
msg = sent[0]
|
|
|
|
|
assert msg["To"] == "henrik@i80.dk"
|
|
|
|
|
assert msg["Reply-To"] == "notif@test.dk"
|
|
|
|
|
assert "Notif Test" in msg["Subject"]
|
|
|
|
|
assert "hej" in msg.get_content()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_signup_without_smtp_skips_notification(client, monkeypatch):
|
|
|
|
|
monkeypatch.setattr(app_module, "SMTP_HOST", "")
|
|
|
|
|
monkeypatch.setattr(app_module, "_send", lambda m: pytest.fail("should not send"))
|
|
|
|
|
r = client.post("/", data={
|
|
|
|
|
"navn": "X", "email": "nosmtp@test.dk", "domaener": "1", "egne_ns": "nej",
|
|
|
|
|
})
|
|
|
|
|
assert r.status_code == 302
|