test_setup.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import os
  2. import pytest
  3. from models.model import Account, Tenant, TenantAccountJoin
  4. def test_setup_api_get(test_client,db_session):
  5. response = test_client.get("/setup")
  6. assert response.status_code == 200
  7. assert response.json == {"step": "not_start"}
  8. # create a tenant and check again
  9. tenant = Tenant(name="Test Tenant", status="normal")
  10. db_session.add(tenant)
  11. db_session.commit()
  12. response = test_client.get("/setup")
  13. assert response.status_code == 200
  14. assert response.json == {"step": "step2"}
  15. # create setup file and check again
  16. response = test_client.get("/setup")
  17. assert response.status_code == 200
  18. assert response.json == {"step": "finished"}
  19. def test_setup_api_post(test_client):
  20. response = test_client.post("/setup", json={
  21. "email": "test@test.com",
  22. "name": "Test User",
  23. "password": "Abc123456"
  24. })
  25. assert response.status_code == 200
  26. assert response.json == {"result": "success", "next_step": "step2"}
  27. # check if the tenant, account, and tenant account join records were created
  28. tenant = Tenant.query.first()
  29. assert tenant.name == "Test User's LLM Factory"
  30. assert tenant.status == "normal"
  31. assert tenant.encrypt_public_key
  32. account = Account.query.first()
  33. assert account.email == "test@test.com"
  34. assert account.name == "Test User"
  35. assert account.password_salt
  36. assert account.password
  37. assert TenantAccountJoin.query.filter_by(account_id=account.id, is_tenant_owner=True).count() == 1
  38. # check if password is encrypted correctly
  39. salt = account.password_salt.encode()
  40. password_hashed = account.password.encode()
  41. assert account.password == base64.b64encode(hash_password("Abc123456", salt)).decode()
  42. def test_setup_step2_api_post(test_client,db_session):
  43. # create a tenant, account, and setup file
  44. tenant = Tenant(name="Test Tenant", status="normal")
  45. account = Account(email="test@test.com", name="Test User")
  46. db_session.add_all([tenant, account])
  47. db_session.commit()
  48. # try to set up with incorrect language
  49. response = test_client.post("/setup/step2", json={
  50. "interface_language": "invalid_language",
  51. "timezone": "Asia/Shanghai"
  52. })
  53. assert response.status_code == 400
  54. # set up successfully
  55. response = test_client.post("/setup/step2", json={
  56. "interface_language": "en",
  57. "timezone": "Asia/Shanghai"
  58. })
  59. assert response.status_code == 200
  60. assert response.json == {"result": "success", "next_step": "finished"}
  61. # check if account was updated correctly
  62. account = Account.query.first()
  63. assert account.interface_language == "en"
  64. assert account.timezone == "Asia/Shanghai"
  65. assert account.interface_theme == "light"
  66. assert account.last_login_ip == "127.0.0.1"