150 lines
4.6 KiB
Python
150 lines
4.6 KiB
Python
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 = '<script>alert(1)</script>'
|
|
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'<script>alert(1)</script>' 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 |