""" Tests for routes and form submissions in the Flask job application system. """ import pytest from flask import session from app.models import load_application_data class TestPage1Email: """Tests for Page 1: Email capture.""" def test_page1_loads(self, client): """Test that page 1 loads successfully.""" response = client.get('/apply?job=TestJob') assert response.status_code == 200 assert b'Bewerbung' in response.data assert b'TestJob' in response.data def test_page1_default_job_name(self, client): """Test page 1 with no job parameter.""" response = client.get('/apply') assert response.status_code == 200 assert b'Offene Position' in response.data def test_submit_valid_email(self, client, app): """Test submitting a valid email address.""" with app.app_context(): response = client.post('/apply/submit-email', data={ 'email': 'test@example.com', 'job_name': 'Software Developer' }, follow_redirects=False) assert response.status_code == 302 # Redirect assert b'/apply/' in response.data # Redirects to page 2 def test_submit_invalid_email(self, client): """Test submitting an invalid email address.""" response = client.post('/apply/submit-email', data={ 'email': 'invalid-email', 'job_name': 'Test Job' }, follow_redirects=True) assert b'E-Mail' in response.data or b'email' in response.data class TestPage2Personal: """Tests for Page 2: Personal information.""" def test_page2_loads(self, client, create_test_application): """Test that page 2 loads successfully.""" session_id = 'test-page2-123' create_test_application(session_id=session_id) response = client.get(f'/apply/{session_id}/personal') assert response.status_code == 200 assert b'Pers' in response.data or b'Name' in response.data def test_page2_nonexistent_session(self, client): """Test page 2 with non-existent session.""" response = client.get('/apply/nonexistent/personal', follow_redirects=True) assert response.status_code == 200 # Should redirect to page 1 def test_submit_valid_personal_info(self, client, create_test_application): """Test submitting valid personal information.""" session_id = 'test-personal-valid' create_test_application(session_id=session_id, current_page=2) response = client.post(f'/apply/{session_id}/submit-personal', data={ 'name': 'Müller', 'firstname': 'Anna', 'address': 'Hauptstrasse 123', 'zip_code': '8001', 'city': 'Zürich', 'phone': '+41 79 123 45 67', 'birth_year': '1990', 'civil_status': 'ledig' }, follow_redirects=False) assert response.status_code == 302 # Redirect to page 3 assert b'/motivation' in response.data def test_submit_missing_required_fields(self, client, create_test_application): """Test submitting personal info with missing required fields.""" session_id = 'test-personal-missing' create_test_application(session_id=session_id, current_page=2) response = client.post(f'/apply/{session_id}/submit-personal', data={ 'name': 'Test', # Missing firstname, address, etc. }, follow_redirects=True) assert response.status_code == 200 # Should show error messages def test_submit_invalid_phone(self, client, create_test_application): """Test submitting with invalid phone number.""" session_id = 'test-personal-phone' create_test_application(session_id=session_id, current_page=2) response = client.post(f'/apply/{session_id}/submit-personal', data={ 'name': 'Test', 'firstname': 'User', 'address': 'Street 1', 'zip_code': '12345', 'city': 'City', 'phone': '123456789', # Missing + prefix 'birth_year': '1990' }, follow_redirects=True) assert response.status_code == 200 class TestPage3Motivation: """Tests for Page 3: Motivation questions.""" def test_page3_loads(self, client, create_test_application): """Test that page 3 loads successfully.""" session_id = 'test-page3-123' create_test_application(session_id=session_id, current_page=3) response = client.get(f'/apply/{session_id}/motivation') assert response.status_code == 200 assert b'Motivation' in response.data or b'berufliche' in response.data def test_submit_valid_motivation(self, client, create_test_application): """Test submitting valid motivation answers.""" session_id = 'test-motivation-valid' create_test_application(session_id=session_id, current_page=3) response = client.post(f'/apply/{session_id}/submit-motivation', data={ 'current_job': 'Software Entwickler', 'motivation': 'Ich möchte in einem innovativen Team arbeiten', 'qualifications': 'Python, Flask, 5 Jahre Erfahrung', 'salary': '80000 CHF' }, follow_redirects=False) assert response.status_code == 302 assert b'/upload' in response.data def test_submit_empty_motivation(self, client, create_test_application): """Test submitting empty motivation (all optional).""" session_id = 'test-motivation-empty' create_test_application(session_id=session_id, current_page=3) response = client.post(f'/apply/{session_id}/submit-motivation', data={ 'current_job': '', 'motivation': '', 'qualifications': '', 'salary': '' }, follow_redirects=False) assert response.status_code == 302 # Should still work def test_submit_too_long_text(self, client, create_test_application): """Test submitting text that exceeds the character limit.""" session_id = 'test-motivation-long' create_test_application(session_id=session_id, current_page=3) long_text = 'x' * 3001 # Exceeds 3000 character limit response = client.post(f'/apply/{session_id}/submit-motivation', data={ 'current_job': long_text, 'motivation': '', 'qualifications': '', 'salary': '' }, follow_redirects=True) assert response.status_code == 200 # Should show error about character limit class TestPage4Upload: """Tests for Page 4: File upload.""" def test_page4_loads(self, client, create_test_application): """Test that page 4 loads successfully.""" session_id = 'test-page4-123' create_test_application(session_id=session_id, current_page=4) response = client.get(f'/apply/{session_id}/upload') assert response.status_code == 200 assert b'Dokument' in response.data or b'hochladen' in response.data def test_submit_application(self, client, create_test_application): """Test submitting the final application.""" session_id = 'test-submit-app' create_test_application(session_id=session_id, current_page=4) response = client.post(f'/apply/{session_id}/submit-application', follow_redirects=False) assert response.status_code == 302 assert b'/confirmation' in response.data class TestPage5Confirmation: """Tests for Page 5: Confirmation.""" def test_page5_loads(self, client, create_test_application): """Test that confirmation page loads successfully.""" session_id = 'test-page5-123' create_test_application(session_id=session_id, current_page=5, email='test@example.com') response = client.get(f'/apply/{session_id}/confirmation') assert response.status_code == 200 assert b'Vielen Dank' in response.data or b'erfolgreich' in response.data class TestResumeFunction: """Tests for resume functionality.""" def test_resume_redirects_to_correct_page(self, client, create_test_application): """Test that resume redirects to the correct page based on current_page.""" # Test redirect to page 2 session_id = 'test-resume-page2' create_test_application(session_id=session_id, current_page=2) response = client.get(f'/resume/{session_id}', follow_redirects=False) assert response.status_code == 302 assert b'/personal' in response.data # Test redirect to page 3 session_id = 'test-resume-page3' create_test_application(session_id=session_id, current_page=3) response = client.get(f'/resume/{session_id}', follow_redirects=False) assert response.status_code == 302 assert b'/motivation' in response.data # Test redirect to page 4 session_id = 'test-resume-page4' create_test_application(session_id=session_id, current_page=4) response = client.get(f'/resume/{session_id}', follow_redirects=False) assert response.status_code == 302 assert b'/upload' in response.data def test_resume_nonexistent_session(self, client): """Test resuming with non-existent session ID.""" response = client.get('/resume/nonexistent-session', follow_redirects=True) assert response.status_code == 200 class TestIndexRoute: """Tests for index route.""" def test_index_redirects_to_apply(self, client): """Test that index redirects to the apply page.""" response = client.get('/', follow_redirects=False) assert response.status_code == 302 assert b'/apply' in response.data