mirror of
https://github.com/OpenBB-finance/OpenBB.git
synced 2026-10-27 12:40:35 +00:00
[BugFix] Fix Hub Session Not Being Set After Login (#7197)
* fix hub session not being set after login * try test another way, ci didn't like that one * 3.9 doesn't like staticmethod
This commit is contained in:
@@ -139,6 +139,10 @@ class Account: # noqa: D205, D400
|
||||
self._hub_service.session.model_dump(mode="json"), f, indent=4
|
||||
)
|
||||
|
||||
self._base_app._command_runner.user_settings.profile.hub_session = getattr(
|
||||
self._base_app._account._hub_service, "_session", None
|
||||
)
|
||||
|
||||
if return_settings:
|
||||
return self._base_app._command_runner.user_settings
|
||||
return None
|
||||
@@ -189,6 +193,7 @@ class Account: # noqa: D205, D400
|
||||
self._base_app.user.defaults.update(incoming.defaults)
|
||||
if return_settings:
|
||||
return self._base_app._command_runner.user_settings
|
||||
|
||||
return None
|
||||
|
||||
@_log_account_command # type: ignore
|
||||
|
||||
@@ -2,12 +2,16 @@
|
||||
|
||||
# pylint: disable=redefined-outer-name
|
||||
|
||||
from unittest.mock import PropertyMock, patch
|
||||
|
||||
import pytest
|
||||
from openbb_core.app.model.hub.hub_session import HubSession
|
||||
from openbb_core.app.model.system_settings import SystemSettings
|
||||
from openbb_core.app.model.user_settings import UserSettings
|
||||
from openbb_core.app.static.account import Account
|
||||
from openbb_core.app.static.app_factory import create_app
|
||||
from openbb_core.app.static.coverage import Coverage
|
||||
from pydantic import SecretStr
|
||||
|
||||
|
||||
@pytest.fixture(scope="module")
|
||||
@@ -42,6 +46,55 @@ def test_app_account(app_factory):
|
||||
assert isinstance(account, Account)
|
||||
|
||||
|
||||
# flake8: noqa: S106 # pylint: disable=W0212
|
||||
@patch("openbb_core.app.static.account.HubService", autospec=True)
|
||||
def test_app_account_login(mock_hub_service, app_factory):
|
||||
"""Test app account login."""
|
||||
mock_hub_service.return_value.pull.return_value = UserSettings()
|
||||
mock_hub_service.return_value.connect.return_value = None
|
||||
|
||||
# Mock the session object
|
||||
mock_session = HubSession(
|
||||
access_token=SecretStr("mock_token"),
|
||||
token_type="bearer",
|
||||
user_uuid="mock_user_uuid",
|
||||
email="test@example.com",
|
||||
primary_usage="mock_usage",
|
||||
)
|
||||
# We need to mock the _session attribute and session property on the HubService instance
|
||||
mock_hub_service.return_value._session = mock_session
|
||||
type(mock_hub_service.return_value).session = PropertyMock(
|
||||
return_value=mock_session
|
||||
)
|
||||
|
||||
# Get the account object from the app factory
|
||||
account = app_factory.account
|
||||
assert account
|
||||
assert hasattr(account, "login")
|
||||
assert callable(account.login)
|
||||
|
||||
# Call the login method
|
||||
account.login(email="test@example.com", password="password", remember_me=True)
|
||||
|
||||
# Assert that the hub_session is set in the user settings
|
||||
assert app_factory.user.profile.hub_session is not None
|
||||
assert (
|
||||
app_factory.user.profile.hub_session.access_token.get_secret_value()
|
||||
== "mock_token"
|
||||
)
|
||||
|
||||
# Assert that the HubService was called correctly
|
||||
mock_hub_service.return_value.connect.assert_called_once_with(
|
||||
"test@example.com", "password", None
|
||||
)
|
||||
mock_hub_service.return_value.pull.assert_called_once()
|
||||
|
||||
# Test logout
|
||||
with patch("openbb_core.app.static.account.Path.unlink") as mock_unlink:
|
||||
account.logout()
|
||||
mock_unlink.assert_called_once()
|
||||
|
||||
|
||||
def test_app_coverage(app_factory):
|
||||
"""Test app coverage."""
|
||||
coverage = app_factory.coverage
|
||||
|
||||
Reference in New Issue
Block a user