From 1f7baeac4fbd69adeb7eda1dea5cdf0ac4ddf7ba Mon Sep 17 00:00:00 2001 From: Diogo Sousa Date: Thu, 2 Feb 2023 23:42:22 +0000 Subject: [PATCH] add user tests --- openbb_terminal/session/user.py | 50 +++++----- tests/openbb_terminal/session/test_user.py | 92 +++++++++++++++++++ .../test_user/test_whoami[False-False].txt | 3 + .../txt/test_user/test_whoami[True-False].txt | 3 + .../txt/test_user/test_whoami[True-True].txt | 2 + 5 files changed, 125 insertions(+), 25 deletions(-) create mode 100644 tests/openbb_terminal/session/test_user.py create mode 100644 tests/openbb_terminal/session/txt/test_user/test_whoami[False-False].txt create mode 100644 tests/openbb_terminal/session/txt/test_user/test_whoami[True-False].txt create mode 100644 tests/openbb_terminal/session/txt/test_user/test_whoami[True-True].txt diff --git a/openbb_terminal/session/user.py b/openbb_terminal/session/user.py index b9dc660729c..401070db547 100644 --- a/openbb_terminal/session/user.py +++ b/openbb_terminal/session/user.py @@ -3,10 +3,10 @@ from openbb_terminal.rich_config import console class User: - token_type: str = "" - token: str = "" - uuid: str = "" - email: str = "" + _token_type: str = "" + _token: str = "" + _uuid: str = "" + _email: str = "" @classmethod def load_user_info(cls, session: dict, email: str): @@ -17,18 +17,18 @@ class User: session : dict The login info. """ - User.token_type = session.get("token_type", "") - User.token = session.get("access_token", "") - User.uuid = session.get("uuid", "") - User.email = email + cls._token_type = session.get("token_type", "") + cls._token = session.get("access_token", "") + cls._uuid = session.get("uuid", "") + cls._email = email @classmethod def get_session(cls): """Get session info.""" return { - "token_type": User.token_type, - "access_token": User.token, - "uuid": User.uuid, + "token_type": cls._token_type, + "access_token": cls._token, + "uuid": cls._uuid, } @staticmethod @@ -36,22 +36,22 @@ class User: """Update flair if user has not changed it.""" if flair is None: MAX_FLAIR_LEN = 20 - username = User.email[: User.email.find("@")] + username = User._email[: User._email.find("@")] username = "[" + username[:MAX_FLAIR_LEN] + "]" setattr(obbff, "USE_FLAIR", username + " 🦋") @classmethod def get_uuid(cls): """Get uuid.""" - return User.uuid + return cls._uuid @classmethod def whoami(cls): """Display user info.""" - if User.uuid: - console.print(f"[info]email:[/info] {User.email}") - console.print(f"[info]uuid:[/info] {User.uuid}") - if obbff.SYNC_ENABLED: + if not User.is_guest(): + console.print(f"[info]email:[/info] {cls._email}") + console.print(f"[info]uuid:[/info] {cls._uuid}") + if obbff.SYNC_ENABLED is True: sync = "ON" else: sync = "OFF" @@ -59,22 +59,22 @@ class User: else: console.print( "[info]You are currently logged as a guest.\n" - "Create an account here https://my.openbb.co/register.[/info]\n" + "Create an account here https://my.openbb.co/register.[/info]" ) @classmethod def clear(cls): """Clear user info.""" - User.token_type = "" - User.token = "" - User.email = "" - User.uuid = "" + cls._token_type = "" + cls._token = "" + cls._email = "" + cls._uuid = "" obbff.USE_FLAIR = ":openbb" @classmethod def is_guest(cls): """Check if user is guest.""" - return not bool(User.token) + return not bool(cls._token) @classmethod def is_sync_enabled(cls): @@ -84,9 +84,9 @@ class User: @classmethod def get_auth_header(cls): """Get token.""" - return f"{User.token_type.title()} {User.token}" + return f"{cls._token_type.title()} {cls._token}" @classmethod def get_token(cls): """Get token.""" - return User.token + return cls._token diff --git a/tests/openbb_terminal/session/test_user.py b/tests/openbb_terminal/session/test_user.py new file mode 100644 index 00000000000..cbe055069a2 --- /dev/null +++ b/tests/openbb_terminal/session/test_user.py @@ -0,0 +1,92 @@ +# IMPORTATION STANDARD +from unittest.mock import patch + +# IMPORTATION THIRDPARTY +import pytest + +# IMPORTATION INTERNAL +from openbb_terminal.session import user + + +# pylint: disable=protected-access, redefined-outer-name + +TEST_SESSION = { + "access_token": "test_token", + "token_type": "bearer", + "uuid": "test_uuid", +} + + +@pytest.fixture +def User(): + """User fixture.""" + user.User.load_user_info(TEST_SESSION, "test@email.com") + yield user.User + + +class obbff: + """Mock obbff.""" + + USE_FLAIR = "" + SYNC_ENABLED = True + + +def test_load_user_info(User): + """Test load user info.""" + assert User._token == "test_token" + assert User._token_type == "bearer" + assert User._uuid == "test_uuid" + assert User._email == "test@email.com" + + +@patch("openbb_terminal.session.user.obbff", obbff) +def test_update_flair(User): + """Test update flair.""" + User.update_flair(flair=None) + assert obbff.USE_FLAIR == "[test] 🦋" + + +def test_get_session(User): + """Test get session.""" + assert User.get_session() == TEST_SESSION + + +def test_get_uuid(User): + """Test get uuid.""" + assert User.get_uuid() == "test_uuid" + + +@pytest.mark.record_stdout +@pytest.mark.parametrize( + "sync, guest", + [ + (False, False), + (True, False), + (True, True), + ], +) +def test_whoami(User, sync, guest): + """Test whoami.""" + path = "openbb_terminal.session.user." + with ( + patch(path + "obbff.SYNC_ENABLED", sync), + patch(path + "User.is_guest", return_value=guest), + ): + User.whoami() + + +def test_clear(User): + """Test clear.""" + User.clear() + assert User._token == "" + assert User._token_type == "" + assert User._uuid == "" + assert User._email == "" + + +def test_get_auth_header(User): + assert User.get_auth_header() == "Bearer test_token" + + +def test_get_token(User): + assert User.get_token() == "test_token" diff --git a/tests/openbb_terminal/session/txt/test_user/test_whoami[False-False].txt b/tests/openbb_terminal/session/txt/test_user/test_whoami[False-False].txt new file mode 100644 index 00000000000..700960c5405 --- /dev/null +++ b/tests/openbb_terminal/session/txt/test_user/test_whoami[False-False].txt @@ -0,0 +1,3 @@ +[info]email:[/info] test@email.com +[info]uuid:[/info] test_uuid +[info]sync:[/info] OFF diff --git a/tests/openbb_terminal/session/txt/test_user/test_whoami[True-False].txt b/tests/openbb_terminal/session/txt/test_user/test_whoami[True-False].txt new file mode 100644 index 00000000000..49e66764b03 --- /dev/null +++ b/tests/openbb_terminal/session/txt/test_user/test_whoami[True-False].txt @@ -0,0 +1,3 @@ +[info]email:[/info] test@email.com +[info]uuid:[/info] test_uuid +[info]sync:[/info] ON diff --git a/tests/openbb_terminal/session/txt/test_user/test_whoami[True-True].txt b/tests/openbb_terminal/session/txt/test_user/test_whoami[True-True].txt new file mode 100644 index 00000000000..f9e689b7db0 --- /dev/null +++ b/tests/openbb_terminal/session/txt/test_user/test_whoami[True-True].txt @@ -0,0 +1,2 @@ +[info]You are currently logged as a guest. +Create an account here https://my.openbb.co/register.[/info]