import os import pytest from app import app as flask_app @pytest.fixture def client(): flask_app.config['TESTING'] = True with flask_app.test_client() as client: yield client def test_homepage(client): resp = client.get('/') assert resp.status_code == 200 assert b'Elpatrons Datumsrechner' in resp.data def test_plusminus_tage(client): resp = client.post('/', data={ 'action': 'plusminus', 'datum_pm': '2024-01-10', 'anzahl_pm': '5', 'einheit_pm': 'tage', 'richtung_pm': 'add' }) assert resp.status_code == 200 assert b'plus 5 Tage' in resp.data assert b'15.01.2024' in resp.data # Subtraktion resp = client.post('/', data={ 'action': 'plusminus', 'datum_pm': '2024-01-10', 'anzahl_pm': '5', 'einheit_pm': 'tage', 'richtung_pm': 'sub' }) assert b'minus 5 Tage' in resp.data assert b'05.01.2024' in resp.data def test_plusminus_werktage(client): from numpy import busday_offset from datetime import datetime start = '2024-01-10' anzahl = 5 # Addition result = busday_offset(datetime.strptime(start, '%Y-%m-%d').date(), anzahl, roll='forward') result_str = datetime.strptime(str(result), '%Y-%m-%d').strftime('%d.%m.%Y') resp = client.post('/', data={ 'action': 'plusminus', 'datum_pm': start, 'anzahl_pm': str(anzahl), 'einheit_pm': 'tage', 'richtung_pm': 'add', 'werktage_pm': 'on' }) assert resp.status_code == 200 assert b'plus 5 Werktage' in resp.data assert result_str.encode() in resp.data # Subtraktion result = busday_offset(datetime.strptime(start, '%Y-%m-%d').date(), -anzahl, roll='forward') result_str = datetime.strptime(str(result), '%Y-%m-%d').strftime('%d.%m.%Y') resp = client.post('/', data={ 'action': 'plusminus', 'datum_pm': start, 'anzahl_pm': str(anzahl), 'einheit_pm': 'tage', 'richtung_pm': 'sub', 'werktage_pm': 'on' }) assert b'minus 5 Werktage' in resp.data assert result_str.encode() in resp.data def test_plusminus_wochen_monate(client): from datetime import datetime, timedelta from dateutil.relativedelta import relativedelta start = '2024-01-10' # Wochen addieren resp = client.post('/', data={ 'action': 'plusminus', 'datum_pm': start, 'anzahl_pm': '2', 'einheit_pm': 'wochen', 'richtung_pm': 'add' }) assert b'plus 2 Wochen' in resp.data assert b'24.01.2024' in resp.data # Wochen subtrahieren resp = client.post('/', data={ 'action': 'plusminus', 'datum_pm': start, 'anzahl_pm': '2', 'einheit_pm': 'wochen', 'richtung_pm': 'sub' }) assert b'minus 2 Wochen' in resp.data assert b'27.12.2023' in resp.data # Monate addieren resp = client.post('/', data={ 'action': 'plusminus', 'datum_pm': start, 'anzahl_pm': '2', 'einheit_pm': 'monate', 'richtung_pm': 'add' }) assert b'plus 2 Monate' in resp.data assert b'10.03.2024' in resp.data # Monate subtrahieren resp = client.post('/', data={ 'action': 'plusminus', 'datum_pm': start, 'anzahl_pm': '2', 'einheit_pm': 'monate', 'richtung_pm': 'sub' }) assert b'minus 2 Monate' in resp.data assert b'10.11.2023' in resp.data def test_xss_protection(client): # Versuche ein Skript einzuschleusen xss = '' resp = client.post('/', data={ 'action': 'tage_werktage', 'start1': xss, 'end1': '2024-01-10' }) assert resp.status_code == 200 # Das Skript darf nicht im HTML erscheinen assert b'' not in resp.data # Es sollte eine Fehlermeldung erscheinen html = resp.data.decode('utf-8') assert 'Ungültige Eingabe' in html def test_stats_login_required(client): resp = client.get('/stats') assert resp.status_code == 200 assert b'Dashboard Login' in resp.data def test_werktage_berechnung(client): from numpy import busday_count from datetime import date, timedelta start = '2024-01-01' end = '2024-03-01' expected = busday_count(date.fromisoformat(start), date.fromisoformat(end) + timedelta(days=1)) resp = client.post('/', data={ 'action': 'tage_werktage', 'start1': start, 'end1': end, 'werktage': 'on' }) assert resp.status_code == 200 assert b'Anzahl der Werktage' in resp.data assert f': {expected}'.encode() in resp.data def test_api_tage_werktage(client): # Erfolgsfall: Werktage resp = client.post('/api/tage_werktage', json={ 'start': '2024-06-01', 'end': '2024-06-10', 'werktage': True }) assert resp.status_code == 200 data = resp.get_json() assert 'result' in data # Fehlerfall: Ungültiges Datum resp = client.post('/api/tage_werktage', json={ 'start': 'foo', 'end': 'bar', 'werktage': True }) assert resp.status_code == 400 data = resp.get_json() assert 'error' in data def test_api_wochentag(client): resp = client.post('/api/wochentag', json={'datum': '2024-06-10'}) assert resp.status_code == 200 data = resp.get_json() assert data['result'] == 'Montag' # Fehlerfall resp = client.post('/api/wochentag', json={'datum': 'foo'}) assert resp.status_code == 400 assert 'error' in resp.get_json() def test_api_kw_berechnen(client): resp = client.post('/api/kw_berechnen', json={'datum': '2024-06-10'}) assert resp.status_code == 200 data = resp.get_json() assert 'KW' in data['result'] # Fehlerfall resp = client.post('/api/kw_berechnen', json={'datum': 'foo'}) assert resp.status_code == 400 assert 'error' in resp.get_json() def test_api_kw_datum(client): resp = client.post('/api/kw_datum', json={'jahr': 2024, 'kw': 24}) assert resp.status_code == 200 data = resp.get_json() assert 'result' in data and 'start' in data and 'end' in data # Fehlerfall resp = client.post('/api/kw_datum', json={'jahr': 'foo', 'kw': 'bar'}) assert resp.status_code == 400 assert 'error' in resp.get_json() def test_api_plusminus(client): # Tage addieren resp = client.post('/api/plusminus', json={ 'datum': '2024-06-10', 'anzahl': 5, 'einheit': 'tage', 'richtung': 'add', 'werktage': False }) assert resp.status_code == 200 data = resp.get_json() assert data['result'] == '2024-06-15' # Werktage subtrahieren resp = client.post('/api/plusminus', json={ 'datum': '2024-06-10', 'anzahl': 5, 'einheit': 'tage', 'richtung': 'sub', 'werktage': True }) assert resp.status_code == 200 data = resp.get_json() assert 'result' in data # Fehlerfall: ungültige Einheit resp = client.post('/api/plusminus', json={ 'datum': '2024-06-10', 'anzahl': 5, 'einheit': 'foo' }) assert resp.status_code == 400 assert 'error' in resp.get_json() def test_api_stats(client): resp = client.get('/api/stats') assert resp.status_code == 200 data = resp.get_json() assert 'pageviews' in data and 'func_counts' in data and 'impressions_per_day' in data def test_api_monitor(client): resp = client.get('/api/monitor') assert resp.status_code == 200 data = resp.get_json() assert data['status'] == 'ok' assert 'uptime_seconds' in data