| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- """Tests for Person module."""
- import pytest
- from app.services.person_service import PersonService
- class TestPersonAPI:
- """Test Person API endpoints."""
-
- def test_create_person_success(self, client, db_session, auth_headers):
- """Test creating a person with valid name."""
- response = client.post('/api/persons/create', json={'name': 'Test Person'}, headers=auth_headers)
- assert response.status_code == 200
- data = response.get_json()
- assert data['success'] is True
- assert data['data']['name'] == 'Test Person'
- assert 'id' in data['data']
-
- def test_create_person_empty_name(self, client, db_session, auth_headers):
- """Test creating a person with empty name fails."""
- response = client.post('/api/persons/create', json={'name': ''}, headers=auth_headers)
- assert response.status_code == 400
- data = response.get_json()
- assert data['success'] is False
- assert data['code'] == 'VALIDATION_ERROR'
-
- def test_create_person_whitespace_name(self, client, db_session, auth_headers):
- """Test creating a person with whitespace-only name fails."""
- response = client.post('/api/persons/create', json={'name': ' '}, headers=auth_headers)
- assert response.status_code == 400
- data = response.get_json()
- assert data['success'] is False
- assert data['code'] == 'VALIDATION_ERROR'
-
- def test_get_all_persons(self, client, db_session, auth_headers):
- """Test getting all persons."""
- # Create some persons first
- client.post('/api/persons/create', json={'name': 'Person 1'}, headers=auth_headers)
- client.post('/api/persons/create', json={'name': 'Person 2'}, headers=auth_headers)
-
- response = client.get('/api/persons', headers=auth_headers)
- assert response.status_code == 200
- data = response.get_json()
- assert data['success'] is True
- assert len(data['data']) == 2
-
- def test_get_person_by_id(self, client, db_session, auth_headers):
- """Test getting a person by ID."""
- # Create a person first
- create_response = client.post('/api/persons/create', json={'name': 'Test Person'}, headers=auth_headers)
- person_id = create_response.get_json()['data']['id']
-
- response = client.get(f'/api/persons/{person_id}', headers=auth_headers)
- assert response.status_code == 200
- data = response.get_json()
- assert data['success'] is True
- assert data['data']['name'] == 'Test Person'
-
- def test_get_person_not_found(self, client, db_session, auth_headers):
- """Test getting a non-existent person."""
- response = client.get('/api/persons/9999', headers=auth_headers)
- assert response.status_code == 404
- data = response.get_json()
- assert data['success'] is False
- assert data['code'] == 'NOT_FOUND'
-
- def test_update_person_success(self, client, db_session, auth_headers):
- """Test updating a person's name."""
- # Create a person first
- create_response = client.post('/api/persons/create', json={'name': 'Original Name'}, headers=auth_headers)
- person_id = create_response.get_json()['data']['id']
-
- response = client.post('/api/persons/update', json={'id': person_id, 'name': 'Updated Name'}, headers=auth_headers)
- assert response.status_code == 200
- data = response.get_json()
- assert data['success'] is True
- assert data['data']['name'] == 'Updated Name'
-
- def test_update_person_not_found(self, client, db_session, auth_headers):
- """Test updating a non-existent person."""
- response = client.post('/api/persons/update', json={'id': 9999, 'name': 'New Name'}, headers=auth_headers)
- assert response.status_code == 400
- data = response.get_json()
- assert data['success'] is False
-
- def test_update_person_empty_name(self, client, db_session, auth_headers):
- """Test updating a person with empty name fails."""
- # Create a person first
- create_response = client.post('/api/persons/create', json={'name': 'Original Name'}, headers=auth_headers)
- person_id = create_response.get_json()['data']['id']
-
- response = client.post('/api/persons/update', json={'id': person_id, 'name': ''}, headers=auth_headers)
- assert response.status_code == 400
- data = response.get_json()
- assert data['success'] is False
- assert data['code'] == 'VALIDATION_ERROR'
-
- def test_delete_person_success(self, client, db_session, auth_headers):
- """Test deleting a person."""
- # Create a person first
- create_response = client.post('/api/persons/create', json={'name': 'To Delete'}, headers=auth_headers)
- person_id = create_response.get_json()['data']['id']
-
- response = client.post('/api/persons/delete', json={'id': person_id}, headers=auth_headers)
- assert response.status_code == 200
- data = response.get_json()
- assert data['success'] is True
-
- # Verify person is deleted
- get_response = client.get(f'/api/persons/{person_id}', headers=auth_headers)
- assert get_response.status_code == 404
-
- def test_delete_person_not_found(self, client, db_session, auth_headers):
- """Test deleting a non-existent person."""
- response = client.post('/api/persons/delete', json={'id': 9999}, headers=auth_headers)
- assert response.status_code == 404
- data = response.get_json()
- assert data['success'] is False
-
- def test_unauthorized_access(self, client, db_session):
- """Test that endpoints require authentication."""
- response = client.get('/api/persons')
- assert response.status_code == 401
- data = response.get_json()
- assert data['success'] is False
- assert data['code'] == 'UNAUTHORIZED'
|