""" Tests for application view and file download functionality. """ import os import pytest from pathlib import Path class TestApplicationView: """Test application view page for HR.""" def test_view_application_page_renders(self, client, create_test_application): """Test that the application view page renders correctly.""" session_id = 'test-view-123' create_test_application( session_id=session_id, email='test@example.com', job_name='Software Engineer', personal_info={ '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' }, motivation_answers={ 'current_job': 'Software Developer', 'motivation': 'Great opportunity', 'qualifications': 'Python, Flask', 'salary': '100000 CHF' } ) response = client.get(f'/application/{session_id}/') assert response.status_code == 200 assert b'Software Engineer' in response.data assert b'test@example.com' in response.data assert b'Anna' in response.data assert b'Great opportunity' in response.data def test_view_application_404_for_invalid_session(self, client): """Test that viewing a non-existent application returns 404.""" response = client.get('/application/invalid-session-id/') assert response.status_code == 404 def test_view_application_displays_all_personal_info(self, client, create_test_application): """Test that all personal information fields are displayed.""" session_id = 'test-personal-display' create_test_application( session_id=session_id, email='applicant@example.com', job_name='Data Analyst', personal_info={ 'name': 'Schmidt', 'firstname': 'Max', 'address': 'Bahnhofstrasse 1', 'zip_code': '8000', 'city': 'Zürich', 'phone': '+41 79 999 88 77', 'birth_year': '1985', 'civil_status': 'verheiratet' } ) response = client.get(f'/application/{session_id}/') assert response.status_code == 200 assert b'Schmidt' in response.data assert b'Max' in response.data assert b'Bahnhofstrasse 1' in response.data assert b'8000' in response.data assert b'verheiratet' in response.data def test_view_application_displays_motivation_answers(self, client, create_test_application): """Test that motivation answers are properly displayed.""" session_id = 'test-motivation-display' create_test_application( session_id=session_id, email='applicant@example.com', job_name='Project Manager', motivation_answers={ 'current_job': 'Senior Developer at ABC Corp', 'motivation': 'I want to work on challenging projects', 'qualifications': '10 years of experience in software development', 'salary': '120000 CHF per year' } ) response = client.get(f'/application/{session_id}/') assert response.status_code == 200 assert b'Senior Developer at ABC Corp' in response.data assert b'challenging projects' in response.data assert b'10 years of experience' in response.data assert b'120000 CHF' in response.data def test_view_application_with_uploaded_files( self, client, create_test_application, temp_applications_dir ): """Test that uploaded files are displayed in the application view.""" session_id = 'test-files-display' # Create the application with uploaded files create_test_application( session_id=session_id, email='applicant@example.com', job_name='UI Designer', uploaded_files=[ { 'original_name': 'cv.pdf', 'stored_name': '20250101_120000_cv.pdf', 'size': 102400, 'uploaded_at': '2025-01-01T12:00:00' }, { 'original_name': 'portfolio.pdf', 'stored_name': '20250101_120100_portfolio.pdf', 'size': 204800, 'uploaded_at': '2025-01-01T12:01:00' } ] ) response = client.get(f'/application/{session_id}/') assert response.status_code == 200 assert b'cv.pdf' in response.data assert b'portfolio.pdf' in response.data # Check that file sizes are displayed (in MB) assert b'0.10 MB' in response.data assert b'0.20 MB' in response.data def test_view_application_without_uploaded_files(self, client, create_test_application): """Test that application view works when no files are uploaded.""" session_id = 'test-no-files' create_test_application( session_id=session_id, email='applicant@example.com', job_name='Sales Representative', uploaded_files=[] ) response = client.get(f'/application/{session_id}/') assert response.status_code == 200 assert b'Keine Dokumente hochgeladen' in response.data def test_view_application_shows_timestamps(self, client, create_test_application): """Test that creation and update timestamps are displayed.""" session_id = 'test-timestamps' create_test_application( session_id=session_id, email='applicant@example.com', job_name='Marketing Manager' ) response = client.get(f'/application/{session_id}/') assert response.status_code == 200 # Check for timestamp labels in German assert b'Erstellt am:' in response.data assert b'Zuletzt aktualisiert:' in response.data class TestFileDownload: """Test file download functionality.""" def test_download_file_success(self, client, create_test_application, temp_applications_dir): """Test that files can be successfully downloaded.""" session_id = 'test-download-success' # Create application directory and attachment from app import get_attachments_path attachments_path = get_attachments_path(session_id) Path(attachments_path).mkdir(parents=True, exist_ok=True) # Create a test file test_filename = '20250101_120000_test.pdf' test_file_path = os.path.join(attachments_path, test_filename) test_content = b'Test PDF content' with open(test_file_path, 'wb') as f: f.write(test_content) # Create application with the uploaded file create_test_application( session_id=session_id, email='applicant@example.com', job_name='Test Job', uploaded_files=[ { 'original_name': 'test.pdf', 'stored_name': test_filename, 'size': len(test_content), 'uploaded_at': '2025-01-01T12:00:00' } ] ) # Download the file response = client.get(f'/application/{session_id}/download/{test_filename}') assert response.status_code == 200 assert response.data == test_content assert 'attachment' in response.headers.get('Content-Disposition', '') def test_download_file_not_in_uploaded_list( self, client, create_test_application, temp_applications_dir ): """Test that downloading a file not in the uploaded list returns 404.""" session_id = 'test-download-unauthorized' # Create application directory and attachment from app import get_attachments_path attachments_path = get_attachments_path(session_id) Path(attachments_path).mkdir(parents=True, exist_ok=True) # Create a file that exists physically but is NOT in the uploaded_files list unauthorized_filename = '20250101_120000_unauthorized.pdf' unauthorized_path = os.path.join(attachments_path, unauthorized_filename) with open(unauthorized_path, 'wb') as f: f.write(b'Unauthorized content') # Create application with NO uploaded files create_test_application( session_id=session_id, email='applicant@example.com', job_name='Test Job', uploaded_files=[] ) # Try to download the unauthorized file response = client.get(f'/application/{session_id}/download/{unauthorized_filename}') assert response.status_code == 404 def test_download_file_nonexistent_file(self, client, create_test_application): """Test that downloading a non-existent file returns 404.""" session_id = 'test-download-nonexistent' # Create application with a file in the list, but the file doesn't exist physically create_test_application( session_id=session_id, email='applicant@example.com', job_name='Test Job', uploaded_files=[ { 'original_name': 'missing.pdf', 'stored_name': 'nonexistent_file.pdf', 'size': 1024, 'uploaded_at': '2025-01-01T12:00:00' } ] ) # Try to download the missing file response = client.get(f'/application/{session_id}/download/nonexistent_file.pdf') assert response.status_code == 404 def test_download_file_invalid_session(self, client): """Test that downloading with an invalid session ID returns 404.""" response = client.get('/application/invalid-session/download/somefile.pdf') assert response.status_code == 404 def test_download_multiple_files(self, client, create_test_application, temp_applications_dir): """Test downloading multiple files from the same application.""" session_id = 'test-download-multiple' # Create application directory from app import get_attachments_path attachments_path = get_attachments_path(session_id) Path(attachments_path).mkdir(parents=True, exist_ok=True) # Create multiple test files files = [ ('20250101_120000_cv.pdf', b'CV content'), ('20250101_120100_cover_letter.pdf', b'Cover letter content'), ('20250101_120200_certificate.pdf', b'Certificate content') ] uploaded_files = [] for filename, content in files: file_path = os.path.join(attachments_path, filename) with open(file_path, 'wb') as f: f.write(content) uploaded_files.append({ 'original_name': filename.split('_', 2)[2], 'stored_name': filename, 'size': len(content), 'uploaded_at': '2025-01-01T12:00:00' }) # Create application with multiple files create_test_application( session_id=session_id, email='applicant@example.com', job_name='Test Job', uploaded_files=uploaded_files ) # Download each file and verify for filename, expected_content in files: response = client.get(f'/application/{session_id}/download/{filename}') assert response.status_code == 200 assert response.data == expected_content def test_download_file_with_special_characters( self, client, create_test_application, temp_applications_dir ): """Test downloading files with special characters in the name.""" session_id = 'test-download-special-chars' # Create application directory from app import get_attachments_path attachments_path = get_attachments_path(session_id) Path(attachments_path).mkdir(parents=True, exist_ok=True) # Create a file with a secure filename (as it would be stored) from werkzeug.utils import secure_filename original_name = 'Lebenslauf Müller.pdf' stored_name = f'20250101_120000_{secure_filename(original_name)}' file_path = os.path.join(attachments_path, stored_name) content = b'CV with special chars' with open(file_path, 'wb') as f: f.write(content) # Create application create_test_application( session_id=session_id, email='applicant@example.com', job_name='Test Job', uploaded_files=[ { 'original_name': original_name, 'stored_name': stored_name, 'size': len(content), 'uploaded_at': '2025-01-01T12:00:00' } ] ) # Download the file response = client.get(f'/application/{session_id}/download/{stored_name}') assert response.status_code == 200 assert response.data == content