Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

test: persist account password and profile fixtures (#43183)

Asuka Minato636e0f0ace00
diff --git a/api/tests/unit_tests/services/test_account_password_service.py b/api/tests/unit_tests/services/test_account_password_service.py
index 19ccad4125e..e754ac0deb6 100644
--- a/api/tests/unit_tests/services/test_account_password_service.py
+++ b/api/tests/unit_tests/services/test_account_password_service.py
@@ -1,87 +1,110 @@
from __future__ import annotations
-from unittest.mock import Mock
-
import pytest
+from pytest_mock import MockerFixture
+from sqlalchemy.orm import Session, sessionmaker
from machinery.context import RequestContext
+from repositories.account.repository import SQLAlchemyAccountRepository
from services.account_errors import AccountNotFoundError, CurrentAccountPasswordIncorrectError
+from services.account_password_hasher import DefaultAccountPasswordHasher
from services.account_password_service import AccountPasswordService
-from services.account_ports import AccountPasswordHasher, AccountRepository
-from services.entities.account_entities import AccountCredentials, AccountPasswordDigest, AccountSnapshot
-from tests.unit_tests.model_factories import make_account_snapshot
+from services.entities.account_entities import AccountPasswordDigest
+from tests.unit_tests.model_factories import make_account
def _context() -> RequestContext:
return RequestContext(
- request_id="request-1",
- trace_id="trace-1",
- account_id="account-1",
- active_workspace_id="workspace-1",
+ request_id="request-1", trace_id="trace-1", account_id="account-1", active_workspace_id="workspace-1"
)
-def _account() -> AccountSnapshot:
- return make_account_snapshot(is_password_set=True)
-
-
-def test_change_verifies_current_password_and_updates_digest() -> None:
- accounts = Mock(spec=AccountRepository)
- accounts.get_credentials.return_value = AccountCredentials(password_hash="old-hash", password_salt="old-salt")
- accounts.update_password.return_value = _account()
- passwords = Mock(spec=AccountPasswordHasher)
- passwords.verify.return_value = True
- digest = AccountPasswordDigest(password_hash="new-hash", password_salt="new-salt")
- passwords.hash.return_value = digest
+def _accounts(
+ session_factory: sessionmaker[Session], password: AccountPasswordDigest | None
+) -> SQLAlchemyAccountRepository:
+ """Persist an account; None means the account has not set a password."""
+ account = make_account()
+ if password is not None:
+ account.password = password.password_hash
+ account.password_salt = password.password_salt
+ with session_factory.begin() as session:
+ session.add(account)
+ return SQLAlchemyAccountRepository(session_factory)
+
+
+def test_change_verifies_current_password_and_updates_digest(
+ sqlite_session_factory: sessionmaker[Session], mocker: MockerFixture
+) -> None:
+ passwords = DefaultAccountPasswordHasher()
+ old_digest = passwords.hash("old-password1")
+ accounts = _accounts(sqlite_session_factory, old_digest)
+ verify = mocker.spy(passwords, "verify")
+ hash_password = mocker.spy(passwords, "hash")
+ update = mocker.spy(accounts, "update_password")
service = AccountPasswordService(accounts=accounts, passwords=passwords)
- result = service.change(_context(), current_password="old-password", new_password="new-password1")
+ result = service.change(_context(), current_password="old-password1", new_password="new-password1")
- assert result == _account()
- passwords.verify.assert_called_once_with(
- "old-password",
- password_hash="old-hash",
- password_salt="old-salt",
+ assert result.id == "account-1"
+ assert result.is_password_set
+ verify.assert_called_once_with(
+ "old-password1", password_hash=old_digest.password_hash, password_salt=old_digest.password_salt
+ )
+ hash_password.assert_called_once_with("new-password1")
+ update.assert_called_once_with("account-1", hash_password.spy_return)
+ credentials = accounts.get_credentials("account-1")
+ assert credentials is not None
+ assert credentials.password_hash is not None
+ assert credentials.password_salt is not None
+ assert passwords.verify(
+ "new-password1", password_hash=credentials.password_hash, password_salt=credentials.password_salt
)
- passwords.hash.assert_called_once_with("new-password1")
- accounts.update_password.assert_called_once_with("account-1", digest)
+ assert credentials.password_hash != old_digest.password_hash
-def test_change_rejects_incorrect_current_password_without_hashing_or_update() -> None:
- accounts = Mock(spec=AccountRepository)
- accounts.get_credentials.return_value = AccountCredentials(password_hash="old-hash", password_salt="old-salt")
- passwords = Mock(spec=AccountPasswordHasher)
- passwords.verify.return_value = False
+def test_change_rejects_incorrect_current_password_without_hashing_or_update(
+ sqlite_session_factory: sessionmaker[Session], mocker: MockerFixture
+) -> None:
+ passwords = DefaultAccountPasswordHasher()
+ accounts = _accounts(sqlite_session_factory, passwords.hash("old-password1"))
+ previous = accounts.get_credentials("account-1")
+ hash_password = mocker.spy(passwords, "hash")
+ update = mocker.spy(accounts, "update_password")
service = AccountPasswordService(accounts=accounts, passwords=passwords)
with pytest.raises(CurrentAccountPasswordIncorrectError):
service.change(_context(), current_password="wrong", new_password="new-password1")
- passwords.hash.assert_not_called()
- accounts.update_password.assert_not_called()
+ hash_password.assert_not_called()
+ update.assert_not_called()
+ assert accounts.get_credentials("account-1") == previous
-def test_change_does_not_verify_current_password_when_account_has_no_password() -> None:
- accounts = Mock(spec=AccountRepository)
- accounts.get_credentials.return_value = AccountCredentials(password_hash=None, password_salt=None)
- accounts.update_password.return_value = _account()
- passwords = Mock(spec=AccountPasswordHasher)
- passwords.hash.return_value = AccountPasswordDigest(password_hash="new-hash", password_salt="new-salt")
+def test_change_does_not_verify_current_password_when_account_has_no_password(
+ sqlite_session_factory: sessionmaker[Session], mocker: MockerFixture
+) -> None:
+ accounts = _accounts(sqlite_session_factory, None)
+ passwords = DefaultAccountPasswordHasher()
+ verify = mocker.spy(passwords, "verify")
+ update = mocker.spy(accounts, "update_password")
service = AccountPasswordService(accounts=accounts, passwords=passwords)
service.change(_context(), current_password="", new_password="new-password1")
- passwords.verify.assert_not_called()
- accounts.update_password.assert_called_once()
+ verify.assert_not_called()
+ update.assert_called_once()
+ account = accounts.get("account-1")
+ assert account is not None
+ assert account.is_password_set
-def test_change_reports_missing_account() -> None:
- accounts = Mock(spec=AccountRepository)
- accounts.get_credentials.return_value = None
- passwords = Mock(spec=AccountPasswordHasher)
+def test_change_reports_missing_account(sqlite_session_factory: sessionmaker[Session], mocker: MockerFixture) -> None:
+ accounts = SQLAlchemyAccountRepository(sqlite_session_factory)
+ passwords = DefaultAccountPasswordHasher()
+ hash_password = mocker.spy(passwords, "hash")
service = AccountPasswordService(accounts=accounts, passwords=passwords)
with pytest.raises(AccountNotFoundError):
service.change(_context(), current_password="old", new_password="new-password1")
- passwords.hash.assert_not_called()
+ hash_password.assert_not_called()
diff --git a/api/tests/unit_tests/services/test_account_profile_service.py b/api/tests/unit_tests/services/test_account_profile_service.py
index 76837a88a3c..007d97dcae4 100644
--- a/api/tests/unit_tests/services/test_account_profile_service.py
+++ b/api/tests/unit_tests/services/test_account_profile_service.py
@@ -1,72 +1,76 @@
from __future__ import annotations
-from unittest.mock import Mock
-
import pytest
+from pytest_mock import MockerFixture
+from sqlalchemy.orm import Session, sessionmaker
from machinery.context import RequestContext
+from repositories.account.repository import SQLAlchemyAccountRepository
from services.account_errors import AccountNotFoundError
-from services.account_ports import AccountRepository
from services.account_profile_service import AccountProfileService
-from services.entities.account_entities import AccountProfileChanges, AccountSnapshot
-from tests.unit_tests.model_factories import make_account_snapshot
+from services.entities.account_entities import AccountProfileChanges
+from tests.unit_tests.model_factories import make_account
def _context() -> RequestContext:
return RequestContext(
- request_id="request-1",
- trace_id="trace-1",
- account_id="account-1",
- active_workspace_id="workspace-1",
+ request_id="request-1", trace_id="trace-1", account_id="account-1", active_workspace_id="workspace-1"
)
-def _account() -> AccountSnapshot:
- return make_account_snapshot()
+def accounts(sqlite_session_factory: sessionmaker[Session]) -> SQLAlchemyAccountRepository:
+ with sqlite_session_factory.begin() as session:
+ session.add(make_account())
+ return SQLAlchemyAccountRepository(sqlite_session_factory)
-def test_get_returns_framework_neutral_account_snapshot() -> None:
- accounts = Mock(spec=AccountRepository)
- accounts.get.return_value = _account()
+def test_get_returns_framework_neutral_account_snapshot(
+ accounts: SQLAlchemyAccountRepository, mocker: MockerFixture
+) -> None:
+ expected = accounts.get("account-1")
+ lookup = mocker.spy(accounts, "get")
service = AccountProfileService(accounts=accounts)
result = service.get(_context())
- assert result == _account()
- accounts.get.assert_called_once_with("account-1")
+ assert result == expected
+ lookup.assert_called_once_with("account-1")
-def test_update_applies_profile_changes() -> None:
- accounts = Mock(spec=AccountRepository)
- accounts.update_profile.return_value = _account()
+def test_update_applies_profile_changes(accounts: SQLAlchemyAccountRepository, mocker: MockerFixture) -> None:
+ update = mocker.spy(accounts, "update_profile")
service = AccountProfileService(accounts=accounts)
changes = AccountProfileChanges(name="Updated", timezone="Asia/Singapore")
result = service.update(_context(), changes)
- assert result == _account()
- accounts.update_profile.assert_called_once_with("account-1", changes)
+ assert result.name == "Updated"
+ assert result.timezone == "Asia/Singapore"
+ assert accounts.get("account-1") == result
+ update.assert_called_once_with("account-1", changes)
-def test_update_treats_empty_changes_as_noop() -> None:
- accounts = Mock(spec=AccountRepository)
- accounts.get.return_value = _account()
+def test_update_treats_empty_changes_as_noop(accounts: SQLAlchemyAccountRepository, mocker: MockerFixture) -> None:
+ expected = accounts.get("account-1")
+ lookup = mocker.spy(accounts, "get")
+ update = mocker.spy(accounts, "update_profile")
service = AccountProfileService(accounts=accounts)
result = service.update(_context(), AccountProfileChanges())
- assert result == _account()
- accounts.get.assert_called_once_with("account-1")
- accounts.update_profile.assert_not_called()
+ assert result == expected
+ lookup.assert_called_once_with("account-1")
+ update.assert_not_called()
-def test_update_rejects_missing_account() -> None:
- accounts = Mock(spec=AccountRepository)
- accounts.update_profile.return_value = None
+def test_update_rejects_missing_account(sqlite_session_factory: sessionmaker[Session], mocker: MockerFixture) -> None:
+ accounts = SQLAlchemyAccountRepository(sqlite_session_factory)
+ update = mocker.spy(accounts, "update_profile")
service = AccountProfileService(accounts=accounts)
changes = AccountProfileChanges(name="Updated")
with pytest.raises(AccountNotFoundError):
service.update(_context(), changes)
- accounts.update_profile.assert_called_once_with("account-1", changes)
+ update.assert_called_once_with("account-1", changes)