test_auth.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. """Test authentication API routes."""
  2. import pytest
  3. from flask_bcrypt import Bcrypt
  4. from app import db
  5. from app.models.admin import Admin
  6. @pytest.fixture
  7. def bcrypt_instance(app):
  8. """Create bcrypt instance for testing."""
  9. bcrypt = Bcrypt(app)
  10. return bcrypt
  11. @pytest.fixture
  12. def test_admin(app, db_session, bcrypt_instance):
  13. """Create a test admin for authentication tests."""
  14. with app.app_context():
  15. password_hash = bcrypt_instance.generate_password_hash('testpassword123').decode('utf-8')
  16. admin = Admin(
  17. username='testadmin',
  18. password_hash=password_hash
  19. )
  20. db.session.add(admin)
  21. db.session.commit()
  22. yield admin
  23. def test_login_success(client, test_admin):
  24. """Test successful login returns JWT token."""
  25. response = client.post('/api/auth/login', json={
  26. 'username': 'testadmin',
  27. 'password': 'testpassword123'
  28. })
  29. assert response.status_code == 200
  30. data = response.get_json()
  31. assert data['success'] is True
  32. assert 'token' in data['data']
  33. assert data['data']['admin']['username'] == 'testadmin'
  34. def test_login_invalid_username(client, test_admin):
  35. """Test login with invalid username returns 401."""
  36. response = client.post('/api/auth/login', json={
  37. 'username': 'wronguser',
  38. 'password': 'testpassword123'
  39. })
  40. assert response.status_code == 401
  41. data = response.get_json()
  42. assert data['success'] is False
  43. assert data['code'] == 'AUTH_ERROR'
  44. def test_login_invalid_password(client, test_admin):
  45. """Test login with invalid password returns 401."""
  46. response = client.post('/api/auth/login', json={
  47. 'username': 'testadmin',
  48. 'password': 'wrongpassword'
  49. })
  50. assert response.status_code == 401
  51. data = response.get_json()
  52. assert data['success'] is False
  53. assert data['code'] == 'AUTH_ERROR'
  54. def test_login_missing_credentials(client, test_admin):
  55. """Test login with missing credentials returns 400."""
  56. response = client.post('/api/auth/login', json={
  57. 'username': '',
  58. 'password': ''
  59. })
  60. assert response.status_code == 400
  61. data = response.get_json()
  62. assert data['success'] is False
  63. assert data['code'] == 'VALIDATION_ERROR'
  64. def test_me_with_valid_token(client, test_admin):
  65. """Test /me endpoint with valid token returns admin info."""
  66. # First login to get token
  67. login_response = client.post('/api/auth/login', json={
  68. 'username': 'testadmin',
  69. 'password': 'testpassword123'
  70. })
  71. token = login_response.get_json()['data']['token']
  72. # Then call /me with token
  73. response = client.get('/api/auth/me', headers={
  74. 'Authorization': f'Bearer {token}'
  75. })
  76. assert response.status_code == 200
  77. data = response.get_json()
  78. assert data['success'] is True
  79. assert data['data']['username'] == 'testadmin'
  80. def test_me_without_token(client, test_admin):
  81. """Test /me endpoint without token returns 401."""
  82. response = client.get('/api/auth/me')
  83. assert response.status_code == 401
  84. data = response.get_json()
  85. assert data['success'] is False
  86. assert data['code'] == 'UNAUTHORIZED'
  87. def test_me_with_invalid_token(client, test_admin):
  88. """Test /me endpoint with invalid token returns 401."""
  89. response = client.get('/api/auth/me', headers={
  90. 'Authorization': 'Bearer invalid_token_here'
  91. })
  92. assert response.status_code == 401
  93. data = response.get_json()
  94. assert data['success'] is False
  95. assert data['code'] == 'INVALID_TOKEN'