Files
PunktFri/tests/test_app.py
Henrik Jess Nielsen 347bbb748b
All checks were successful
Build and Deploy PunktFri / build-and-deploy (push) Successful in 1m20s
Update copy to post-July-2026 reality and notify on signups
- Rewrite copy from "this will happen" to "this has happened": direct
  registration closed 1 July 2026, transition ends 30 June 2028
- Fix timeline (30 June 2028, not 1 July), update FAQ + JSON-LD, add FAQ
  on registering new domains now
- Send signup notification mail to henrik@i80.dk via smtp.i80.dk, with
  credentials from Consul KV
- Fix CI health check URL (punktfri.dk)

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-09-19 06:52:45 +02:00

120 lines
3.2 KiB
Python

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")
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