test_person.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. """Tests for Person module."""
  2. import pytest
  3. from app.services.person_service import PersonService
  4. class TestPersonAPI:
  5. """Test Person API endpoints."""
  6. def test_create_person_success(self, client, db_session, auth_headers):
  7. """Test creating a person with valid name."""
  8. response = client.post('/api/persons/create', json={'name': 'Test Person'}, headers=auth_headers)
  9. assert response.status_code == 200
  10. data = response.get_json()
  11. assert data['success'] is True
  12. assert data['data']['name'] == 'Test Person'
  13. assert 'id' in data['data']
  14. def test_create_person_empty_name(self, client, db_session, auth_headers):
  15. """Test creating a person with empty name fails."""
  16. response = client.post('/api/persons/create', json={'name': ''}, headers=auth_headers)
  17. assert response.status_code == 400
  18. data = response.get_json()
  19. assert data['success'] is False
  20. assert data['code'] == 'VALIDATION_ERROR'
  21. def test_create_person_whitespace_name(self, client, db_session, auth_headers):
  22. """Test creating a person with whitespace-only name fails."""
  23. response = client.post('/api/persons/create', json={'name': ' '}, headers=auth_headers)
  24. assert response.status_code == 400
  25. data = response.get_json()
  26. assert data['success'] is False
  27. assert data['code'] == 'VALIDATION_ERROR'
  28. def test_get_all_persons(self, client, db_session, auth_headers):
  29. """Test getting all persons."""
  30. # Create some persons first
  31. client.post('/api/persons/create', json={'name': 'Person 1'}, headers=auth_headers)
  32. client.post('/api/persons/create', json={'name': 'Person 2'}, headers=auth_headers)
  33. response = client.get('/api/persons', headers=auth_headers)
  34. assert response.status_code == 200
  35. data = response.get_json()
  36. assert data['success'] is True
  37. assert len(data['data']) == 2
  38. def test_get_person_by_id(self, client, db_session, auth_headers):
  39. """Test getting a person by ID."""
  40. # Create a person first
  41. create_response = client.post('/api/persons/create', json={'name': 'Test Person'}, headers=auth_headers)
  42. person_id = create_response.get_json()['data']['id']
  43. response = client.get(f'/api/persons/{person_id}', headers=auth_headers)
  44. assert response.status_code == 200
  45. data = response.get_json()
  46. assert data['success'] is True
  47. assert data['data']['name'] == 'Test Person'
  48. def test_get_person_not_found(self, client, db_session, auth_headers):
  49. """Test getting a non-existent person."""
  50. response = client.get('/api/persons/9999', headers=auth_headers)
  51. assert response.status_code == 404
  52. data = response.get_json()
  53. assert data['success'] is False
  54. assert data['code'] == 'NOT_FOUND'
  55. def test_update_person_success(self, client, db_session, auth_headers):
  56. """Test updating a person's name."""
  57. # Create a person first
  58. create_response = client.post('/api/persons/create', json={'name': 'Original Name'}, headers=auth_headers)
  59. person_id = create_response.get_json()['data']['id']
  60. response = client.post('/api/persons/update', json={'id': person_id, 'name': 'Updated Name'}, headers=auth_headers)
  61. assert response.status_code == 200
  62. data = response.get_json()
  63. assert data['success'] is True
  64. assert data['data']['name'] == 'Updated Name'
  65. def test_update_person_not_found(self, client, db_session, auth_headers):
  66. """Test updating a non-existent person."""
  67. response = client.post('/api/persons/update', json={'id': 9999, 'name': 'New Name'}, headers=auth_headers)
  68. assert response.status_code == 400
  69. data = response.get_json()
  70. assert data['success'] is False
  71. def test_update_person_empty_name(self, client, db_session, auth_headers):
  72. """Test updating a person with empty name fails."""
  73. # Create a person first
  74. create_response = client.post('/api/persons/create', json={'name': 'Original Name'}, headers=auth_headers)
  75. person_id = create_response.get_json()['data']['id']
  76. response = client.post('/api/persons/update', json={'id': person_id, 'name': ''}, headers=auth_headers)
  77. assert response.status_code == 400
  78. data = response.get_json()
  79. assert data['success'] is False
  80. assert data['code'] == 'VALIDATION_ERROR'
  81. def test_delete_person_success(self, client, db_session, auth_headers):
  82. """Test deleting a person."""
  83. # Create a person first
  84. create_response = client.post('/api/persons/create', json={'name': 'To Delete'}, headers=auth_headers)
  85. person_id = create_response.get_json()['data']['id']
  86. response = client.post('/api/persons/delete', json={'id': person_id}, headers=auth_headers)
  87. assert response.status_code == 200
  88. data = response.get_json()
  89. assert data['success'] is True
  90. # Verify person is deleted
  91. get_response = client.get(f'/api/persons/{person_id}', headers=auth_headers)
  92. assert get_response.status_code == 404
  93. def test_delete_person_not_found(self, client, db_session, auth_headers):
  94. """Test deleting a non-existent person."""
  95. response = client.post('/api/persons/delete', json={'id': 9999}, headers=auth_headers)
  96. assert response.status_code == 404
  97. data = response.get_json()
  98. assert data['success'] is False
  99. def test_unauthorized_access(self, client, db_session):
  100. """Test that endpoints require authentication."""
  101. response = client.get('/api/persons')
  102. assert response.status_code == 401
  103. data = response.get_json()
  104. assert data['success'] is False
  105. assert data['code'] == 'UNAUTHORIZED'