Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import pathlib
  2. import random
  3. import secrets
  4. from collections.abc import Generator
  5. import pytest
  6. from flask import Flask
  7. from flask.testing import FlaskClient
  8. from sqlalchemy.orm import Session
  9. from app_factory import create_app
  10. from models import Account, DifySetup, Tenant, TenantAccountJoin, db
  11. from services.account_service import AccountService, RegisterService
  12. # Loading the .env file if it exists
  13. def _load_env() -> None:
  14. current_file_path = pathlib.Path(__file__).absolute()
  15. # Items later in the list have higher precedence.
  16. files_to_load = [".env", "vdb.env"]
  17. env_file_paths = [current_file_path.parent / i for i in files_to_load]
  18. for path in env_file_paths:
  19. if not path.exists():
  20. continue
  21. from dotenv import load_dotenv
  22. # Set `override=True` to ensure values from `vdb.env` take priority over values from `.env`
  23. load_dotenv(str(path), override=True)
  24. _load_env()
  25. _CACHED_APP = create_app()
  26. @pytest.fixture
  27. def flask_app() -> Flask:
  28. return _CACHED_APP
  29. @pytest.fixture(scope="session")
  30. def setup_account(request) -> Generator[Account, None, None]:
  31. """`dify_setup` completes the setup process for the Dify application.
  32. It creates `Account` and `Tenant`, and inserts a `DifySetup` record into the database.
  33. Most tests in the `controllers` package may require dify has been successfully setup.
  34. """
  35. with _CACHED_APP.test_request_context():
  36. rand_suffix = random.randint(int(1e6), int(1e7)) # noqa
  37. name = f"test-user-{rand_suffix}"
  38. email = f"{name}@example.com"
  39. RegisterService.setup(
  40. email=email,
  41. name=name,
  42. password=secrets.token_hex(16),
  43. ip_address="localhost",
  44. )
  45. with _CACHED_APP.test_request_context():
  46. with Session(bind=db.engine, expire_on_commit=False) as session:
  47. account = session.query(Account).filter_by(email=email).one()
  48. yield account
  49. with _CACHED_APP.test_request_context():
  50. db.session.query(DifySetup).delete()
  51. db.session.query(TenantAccountJoin).delete()
  52. db.session.query(Account).delete()
  53. db.session.query(Tenant).delete()
  54. db.session.commit()
  55. @pytest.fixture
  56. def flask_req_ctx():
  57. with _CACHED_APP.test_request_context():
  58. yield
  59. @pytest.fixture
  60. def auth_header(setup_account) -> dict[str, str]:
  61. token = AccountService.get_account_jwt_token(setup_account)
  62. return {"Authorization": f"Bearer {token}"}
  63. @pytest.fixture
  64. def test_client() -> Generator[FlaskClient, None, None]:
  65. with _CACHED_APP.test_client() as client:
  66. yield client