add user tests

This commit is contained in:
Diogo Sousa
2023-02-02 23:42:22 +00:00
parent 7f4e0a701e
commit 1f7baeac4f
5 changed files with 125 additions and 25 deletions

View File

@@ -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

View File

@@ -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"

View File

@@ -0,0 +1,3 @@
[info]email:[/info] test@email.com
[info]uuid:[/info] test_uuid
[info]sync:[/info] OFF

View File

@@ -0,0 +1,3 @@
[info]email:[/info] test@email.com
[info]uuid:[/info] test_uuid
[info]sync:[/info] ON

View File

@@ -0,0 +1,2 @@
[info]You are currently logged as a guest.
Create an account here https://my.openbb.co/register.[/info]