Files
Fatima/tests/test_app.py
T

120 lines
4.2 KiB
Python
Raw Normal View History

import tempfile
from app import create_app
def client():
database = tempfile.NamedTemporaryFile(suffix=".sqlite")
app = create_app({
"TESTING": True,
"SECRET_KEY": "test-secret",
"APP_USERNAME": "user",
"APP_PASSWORD": "pass",
"DATABASE": database.name,
})
test_client = app.test_client()
test_client._database_file = database
return test_client
def test_calendar_requires_login():
response = client().get("/")
assert response.status_code == 302
assert response.headers["Location"].endswith("/login")
def test_login_and_logout():
test_client = client()
response = test_client.post("/login", data={"username": "user", "password": "pass"})
assert response.status_code == 302
assert response.headers["Location"].endswith("/")
assert test_client.get("/").status_code == 200
assert test_client.post("/logout").headers["Location"].endswith("/login")
def test_bad_login_shows_error():
response = client().post("/login", data={"username": "user", "password": "wrong"})
assert response.status_code == 200
assert b"not correct" in response.data
def test_choice_is_saved_and_rendered():
test_client = client()
test_client.post("/login", data={"username": "user", "password": "pass"})
response = test_client.post("/api/choices", json={"date": "2026-07-15", "amount": "27.50"})
assert response.status_code == 200
assert response.json["amount_cents"] == 2750
assert b'"2026-07-15": 2750' in test_client.get("/").data
def test_choice_rejects_invalid_amount():
test_client = client()
test_client.post("/login", data={"username": "user", "password": "pass"})
response = test_client.post("/api/choices", json={"date": "2026-07-15", "amount": "-1"})
assert response.status_code == 400
def test_day_status_is_saved_and_rendered():
test_client = client()
test_client.post("/login", data={"username": "user", "password": "pass"})
response = test_client.post(
"/api/statuses",
json={"date": "2026-07-15", "status": "vacation"},
)
assert response.status_code == 200
assert response.json["status"] == "vacation"
assert b'"2026-07-15": "vacation"' in test_client.get("/").data
def test_day_status_rejects_unknown_value():
test_client = client()
test_client.post("/login", data={"username": "user", "password": "pass"})
response = test_client.post(
"/api/statuses",
json={"date": "2026-07-15", "status": "elsewhere"},
)
assert response.status_code == 400
def test_delete_removes_amount_and_status():
test_client = client()
test_client.post("/login", data={"username": "user", "password": "pass"})
test_client.post("/api/choices", json={"date": "2026-07-15", "amount": "55"})
test_client.post("/api/statuses", json={"date": "2026-07-15", "status": "sick"})
response = test_client.post("/api/delete-date", json={"date": "2026-07-15"})
assert response.status_code == 200
assert response.json["deleted"] is True
page = test_client.get("/").data
assert b'"2026-07-15": 5500' not in page
assert b'"2026-07-15": "sick"' not in page
def test_report_groups_money_and_days_by_status():
from datetime import date
test_client = client()
test_client.post("/login", data={"username": "user", "password": "pass"})
year = date.today().year
worked_date = f"{year}-01-10"
vacation_date = f"{year}-02-10"
test_client.post("/api/choices", json={"date": worked_date, "amount": "55"})
test_client.post("/api/choices", json={"date": vacation_date, "amount": "27.50"})
test_client.post("/api/statuses", json={"date": vacation_date, "status": "vacation"})
response = test_client.get("/report")
assert response.status_code == 200
assert b"\xe2\x82\xac55.00" in response.data
assert b"\xe2\x82\xac27.50" in response.data
assert b"2 recorded days" in response.data
def test_report_can_navigate_to_another_year():
test_client = client()
test_client.post("/login", data={"username": "user", "password": "pass"})
response = test_client.get("/report?year=2025")
assert response.status_code == 200
assert b"2025 report" in response.data
assert b"year=2024" in response.data
assert b"year=2026" in response.data