feat: Implement CSRF protection and basic form validation, streamline form data, and add comprehensive unit and E2E tests.

This commit is contained in:
Feiko Wielsma 2026-03-25 09:10:34 +01:00
parent 4c435f8e17
commit 197ae8d75b
4 changed files with 120 additions and 19 deletions

45
tests/test_app.py Normal file
View file

@ -0,0 +1,45 @@
import pytest
from app import app
from unittest.mock import patch
@pytest.fixture
def client():
app.config['TESTING'] = True
with app.test_client() as client:
with client.session_transaction() as sess:
sess['_csrf_token'] = 'test-token'
yield client
def test_home_page(client):
response = client.get('/')
assert response.status_code == 200
assert b"Zomeravond" in response.data or b"Papklokken" in response.data
@patch('app.get_google_sheet')
def test_post_missing_fields(mock_get_sheet, client):
response = client.post('/zomeravond', data={'_csrf_token': 'test-token'})
assert b"Oeps! Bepaalde verplichte velden ontbreken" in response.data
mock_get_sheet.return_value.append_row.assert_not_called()
@patch('app.get_google_sheet')
@patch('app.get_public_participants')
def test_post_success(mock_participants, mock_get_sheet, client):
mock_participants.return_value = []
data = {
'_csrf_token': 'test-token',
'klasse': 'Kajuitklasse',
'zeilnummer': '123',
'bootnaam': 'TestBoat',
'naam': 'Test Name',
'telefoonmobiel': '0612345678',
'email': 'test@example.com'
}
response = client.post('/zomeravond', data=data)
assert response.status_code == 302
assert '/zomeravond/success' in response.headers.get('Location', '')
# In full testing, append_row is called unless TESTING_NO_APPEND is set.
# But since it's mocked, we can check it.
assert mock_get_sheet.return_value.append_row.called