Some checks failed
Build and Deploy PunktFri / build-and-deploy (push) Has been cancelled
- Dockerfile: python:3.12-slim + gunicorn, dynamic port, BUILD_VERSION args - punktfri.nomad: Traefik routing for punktfri.i80.dk, host volume for SQLite - .gitea/workflows/deploy.yml: build/push to Harbor, deploy to Nomad - Makefile: install/run/test/build targets - tests/test_app.py: 9 pytest tests covering all routes and validation - requirements.txt: add gunicorn - requirements-dev.txt: pytest - app.py: health endpoint returns version/commit info
93 lines
2.2 KiB
Python
93 lines
2.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")
|