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.

refactor(tests): use caplog in web login tests (#37889)

linhongkuan4964359961e6
diff --git a/api/tests/unit_tests/controllers/web/test_web_login.py b/api/tests/unit_tests/controllers/web/test_web_login.py
index 839939367c4..bfffd5cbb2c 100644
--- a/api/tests/unit_tests/controllers/web/test_web_login.py
+++ b/api/tests/unit_tests/controllers/web/test_web_login.py
@@ -1,4 +1,5 @@
import base64
+import logging
from types import SimpleNamespace
from unittest.mock import MagicMock, patch
@@ -16,6 +17,13 @@ def encode_code(code: str) -> str:
return base64.b64encode(code.encode("utf-8")).decode()
+def assert_login_failure_logged(caplog: pytest.LogCaptureFixture, email: str, reason: LoginFailureReason) -> None:
+ records = [record for record in caplog.records if record.name == "controllers.web.login"]
+ assert len(records) == 1
+ assert records[0].args[0] == email
+ assert records[0].args[1] == reason
+
+
@pytest.fixture
def app():
flask_app = Flask(__name__)
@@ -114,10 +122,10 @@ class TestLoginApi:
"controllers.web.login.WebAppAuthService.authenticate",
side_effect=services.errors.account.AccountLoginError(),
)
- def test_login_banned_account(self, mock_auth: MagicMock, app: Flask) -> None:
+ def test_login_banned_account(self, mock_auth: MagicMock, app: Flask, caplog: pytest.LogCaptureFixture) -> None:
from controllers.console.error import AccountBannedError
- with patch("controllers.web.login.logger.warning") as mock_log_warning:
+ with caplog.at_level(logging.WARNING, logger="controllers.web.login"):
with app.test_request_context(
"/web/login",
method="POST",
@@ -126,18 +134,16 @@ class TestLoginApi:
with pytest.raises(AccountBannedError):
LoginApi().post()
- assert mock_log_warning.call_count == 1
- assert mock_log_warning.call_args.args[1] == "[email protected]"
- assert mock_log_warning.call_args.args[2] == LoginFailureReason.ACCOUNT_BANNED
+ assert_login_failure_logged(caplog, "[email protected]", LoginFailureReason.ACCOUNT_BANNED)
@patch(
"controllers.web.login.WebAppAuthService.authenticate",
side_effect=services.errors.account.AccountPasswordError(),
)
- def test_login_wrong_password(self, mock_auth: MagicMock, app: Flask) -> None:
+ def test_login_wrong_password(self, mock_auth: MagicMock, app: Flask, caplog: pytest.LogCaptureFixture) -> None:
from controllers.console.auth.error import AuthenticationFailedError
- with patch("controllers.web.login.logger.warning") as mock_log_warning:
+ with caplog.at_level(logging.WARNING, logger="controllers.web.login"):
with app.test_request_context(
"/web/login",
method="POST",
@@ -146,18 +152,16 @@ class TestLoginApi:
with pytest.raises(AuthenticationFailedError):
LoginApi().post()
- assert mock_log_warning.call_count == 1
- assert mock_log_warning.call_args.args[1] == "[email protected]"
- assert mock_log_warning.call_args.args[2] == LoginFailureReason.INVALID_CREDENTIALS
+ assert_login_failure_logged(caplog, "[email protected]", LoginFailureReason.INVALID_CREDENTIALS)
@patch(
"controllers.web.login.WebAppAuthService.authenticate",
side_effect=services.errors.account.AccountNotFoundError(),
)
- def test_login_account_not_found(self, mock_auth: MagicMock, app: Flask) -> None:
+ def test_login_account_not_found(self, mock_auth: MagicMock, app: Flask, caplog: pytest.LogCaptureFixture) -> None:
from controllers.console.auth.error import AuthenticationFailedError
- with patch("controllers.web.login.logger.warning") as mock_log_warning:
+ with caplog.at_level(logging.WARNING, logger="controllers.web.login"):
with app.test_request_context(
"/web/login",
method="POST",
@@ -166,13 +170,13 @@ class TestLoginApi:
with pytest.raises(AuthenticationFailedError):
LoginApi().post()
- assert mock_log_warning.call_count == 1
- assert mock_log_warning.call_args.args[1] == "[email protected]"
- assert mock_log_warning.call_args.args[2] == LoginFailureReason.ACCOUNT_NOT_FOUND
+ assert_login_failure_logged(caplog, "[email protected]", LoginFailureReason.ACCOUNT_NOT_FOUND)
@patch("controllers.web.login.WebAppAuthService.get_email_code_login_data", return_value=None)
- def test_email_code_login_logs_invalid_token(self, mock_get_token_data: MagicMock, app: Flask) -> None:
- with patch("controllers.web.login.logger.warning") as mock_log_warning:
+ def test_email_code_login_logs_invalid_token(
+ self, mock_get_token_data: MagicMock, app: Flask, caplog: pytest.LogCaptureFixture
+ ) -> None:
+ with caplog.at_level(logging.WARNING, logger="controllers.web.login"):
with app.test_request_context(
"/web/email-code-login/validity",
method="POST",
@@ -182,9 +186,7 @@ class TestLoginApi:
EmailCodeLoginApi().post()
mock_get_token_data.assert_called_once_with("token-123")
- assert mock_log_warning.call_count == 1
- assert mock_log_warning.call_args.args[1] == "[email protected]"
- assert mock_log_warning.call_args.args[2] == LoginFailureReason.INVALID_EMAIL_CODE_TOKEN
+ assert_login_failure_logged(caplog, "[email protected]", LoginFailureReason.INVALID_EMAIL_CODE_TOKEN)
@patch("controllers.web.login.WebAppAuthService.revoke_email_code_login_token")
@patch(
@@ -201,10 +203,11 @@ class TestLoginApi:
mock_get_user: MagicMock,
mock_revoke_token: MagicMock,
app: Flask,
+ caplog: pytest.LogCaptureFixture,
) -> None:
from controllers.console.error import AccountBannedError
- with patch("controllers.web.login.logger.warning") as mock_log_warning:
+ with caplog.at_level(logging.WARNING, logger="controllers.web.login"):
with app.test_request_context(
"/web/email-code-login/validity",
method="POST",
@@ -215,9 +218,7 @@ class TestLoginApi:
mock_get_token_data.assert_called_once_with("token-123")
mock_revoke_token.assert_called_once_with("token-123")
- assert mock_log_warning.call_count == 1
- assert mock_log_warning.call_args.args[1] == "[email protected]"
- assert mock_log_warning.call_args.args[2] == LoginFailureReason.ACCOUNT_BANNED
+ assert_login_failure_logged(caplog, "[email protected]", LoginFailureReason.ACCOUNT_BANNED)
class TestLoginStatusApi: