test_login.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import pytest
  2. from app import create_app, db
  3. from flask_login import current_user
  4. from models.model import Account, TenantAccountJoin, Tenant
  5. @pytest.fixture
  6. def client(test_client, db_session):
  7. app = create_app()
  8. app.config["TESTING"] = True
  9. with app.app_context():
  10. db.create_all()
  11. yield test_client
  12. db.drop_all()
  13. def test_login_api_post(client, db_session):
  14. # create a tenant, account, and tenant account join
  15. tenant = Tenant(name="Test Tenant", status="normal")
  16. account = Account(email="test@test.com", name="Test User")
  17. account.password_salt = "uQ7K0/0wUJ7VPhf3qBzwNQ=="
  18. account.password = "A9YpfzjK7c/tOwzamrvpJg=="
  19. db.session.add_all([tenant, account])
  20. db.session.flush()
  21. tenant_account_join = TenantAccountJoin(tenant_id=tenant.id, account_id=account.id, is_tenant_owner=True)
  22. db.session.add(tenant_account_join)
  23. db.session.commit()
  24. # login with correct credentials
  25. response = client.post("/login", json={
  26. "email": "test@test.com",
  27. "password": "Abc123456",
  28. "remember_me": True
  29. })
  30. assert response.status_code == 200
  31. assert response.json == {"result": "success"}
  32. assert current_user == account
  33. assert 'tenant_id' in client.session
  34. assert client.session['tenant_id'] == tenant.id
  35. # login with incorrect password
  36. response = client.post("/login", json={
  37. "email": "test@test.com",
  38. "password": "wrong_password",
  39. "remember_me": True
  40. })
  41. assert response.status_code == 401
  42. # login with non-existent account
  43. response = client.post("/login", json={
  44. "email": "non_existent_account@test.com",
  45. "password": "Abc123456",
  46. "remember_me": True
  47. })
  48. assert response.status_code == 401
  49. def test_logout_api_get(client, db_session):
  50. # create a tenant, account, and tenant account join
  51. tenant = Tenant(name="Test Tenant", status="normal")
  52. account = Account(email="test@test.com", name="Test User")
  53. db.session.add_all([tenant, account])
  54. db.session.flush()
  55. tenant_account_join = TenantAccountJoin(tenant_id=tenant.id, account_id=account.id, is_tenant_owner=True)
  56. db.session.add(tenant_account_join)
  57. db.session.commit()
  58. # login and check if session variable and current_user are set
  59. with client.session_transaction() as session:
  60. session['tenant_id'] = tenant.id
  61. client.post("/login", json={
  62. "email": "test@test.com",
  63. "password": "Abc123456",
  64. "remember_me": True
  65. })
  66. assert current_user == account
  67. assert 'tenant_id' in client.session
  68. assert client.session['tenant_id'] == tenant.id
  69. # logout and check if session variable and current_user are unset
  70. response = client.get("/logout")
  71. assert response.status_code == 200
  72. assert current_user.is_authenticated is False
  73. assert 'tenant_id' not in client.session
  74. def test_reset_password_api_get(client, db_session):
  75. # create a tenant, account, and tenant account join
  76. tenant = Tenant(name="Test Tenant", status="normal")
  77. account = Account(email="test@test.com", name="Test User")
  78. db.session.add_all([tenant, account])
  79. db.session.flush()
  80. tenant_account_join = TenantAccountJoin(tenant_id=tenant.id, account_id=account.id, is_tenant_owner=True)
  81. db.session.add(tenant_account_join)
  82. db.session.commit()
  83. # reset password in cloud edition
  84. app = client.application
  85. app.config["CLOUD_EDITION"] = True
  86. response = client.get("/reset_password")
  87. assert response.status_code == 200
  88. assert response.json == {"result": "success"}
  89. # reset password in non-cloud edition
  90. app.config["CLOUD_EDITION"] = False
  91. response = client.get("/reset_password")
  92. assert response.status_code == 200
  93. assert response.json == {"result": "success"}