Files
OpenBB/tests/openbb_terminal/session/test_session_model.py
montezdesousa 5953be0a42 Terminal authentication + keys/settings/routines (#4015)
* import order

* pylint

* no login, go to prompt

* add logout and whoami

* clear before prompt

* black

* these are unecessary

* error

* improve login prompt message

* login does not follow the controller pattern, so will name it differently

* nop

* move logout and change to results

* change return obj

* ruff

* pyupgrade

* remove user from statics

* message

* fetch configs at start

* remove unecessary func

* unecessary dataclass

* change user vars

* change login flow

* remote logout should not be used

* simplify

* fix double exit logout bug

* wrong exception

* apply keys

* change controller

* mypy

* save if no internet

* pylint

* logout everywhere

* breaking changes: move to local/hub models

* unused import

* fix logout bug

* whoami in guest mode

* add todo

* small bug guest allowed

* limit flair length

* fix bug on user pull

* logout working

* handle no connection

* logout user when logout

* remove keys push

* split logger funcs

* commant apply configs for now

* account to session

* rename user

* clear sounds better

* breaking changes: splitting guest from logged users

* pull imports up

* delete session model

* move logout func

* add ability to save and apply keys from remote

* no sensitive keys

* add support for feature flags

* unecessary print

* allow settings from differnt modules

* change setff to static

* change way to set ff

* fix bug in applying configs

* simplify obbff application

* bug on setting configs

* allow text ff

* obbff in settings

* add upload to cfg_plot 1

* data sources + backend

* this solves circular import?

* move create_session to model

* rever logs changes, not needed for now

* sync optionally

* outline account controller interface

* don't sync when guest

* block guest on installer

* or not and

* start routines

* fix bug on ff, settings

* is installer

* implementing chavi suggestions

* fix colors on installer prompt

* Logging login (#4046)

* basic logging for the login

* removing unused import

* hacking the logs custom filter to not filter the email

* black

* cooping with the new changes

* removing redundant method

* ruff

* ruff

* mypy

* SDK login (#4051)

* sdk login

* adding to the trail map

* minor fix

* improve sdk expirience

* adapting to new changes

* add docstrings

* block guest from account

* change guest msg

* change guest messages

* add sync on/off

* display onoff

* missing paragraph

* fix glitch on logout

* add sys clear

* filtering password

* filtering the email from user

* avoid writing to multiple log files if a user logs out

* fix bug, guest allowed in installer without connection

* log user on the sdk

* split login from lauch

* add clear and change messages on account

* docstrings

* start pull cmd

* change account menu description

* Fix login bug and decouple some logic (#4056)

* fix bug, guest allowed in installer without connection

* split login from lauch

* improvements on the sdk login

---------

Co-authored-by: hjoaquim <h.joaquim@campus.fct.unl.pt>

* sync fix

* change account controller description

* ruff

* add help on account cmds

* update clear prompt

* fix email not in jwt

* remove jwt from requiremnts

* pull command output

* remove character

* pull cmd when no data

* add space below whoami

* fix log filenotfound exception

* changed arg name

* delete .hist on logout

* add asteriscs to password

* pylint

* uncomment rich config

* show only diffs on pull

* black#

* warn when no changes#

* pull msg was switched

* apply diff only

* breaking?: strbool change

* reduce some code

* add timeout to hub requests

* black

* add remote logout

* success msg only if success

* remove colors in prompt

* update docstrings

* keep timeout outside funcs

* update clear

* clear and patch

* delete session

* routine upload

* move .his file to .openbb folder

* change apply configs

* refactor logout

* remove mixed portfolio changes

* fix bug in lock file

* fix bug pwidth, pheight are floats

* fix sources command & add choices

* refactor timezone setting

* userdata

* fix flair

* need to cast paths

* cast on diff

* fix flair default

* save session test

* get session

* data

* test remove

* test remove with assert

* add bad test cases

* add exception cases to unittests

* test remove cli hist file

* black

* test apply configs

* test apply with sync flag

* test create session, session model

* login tests, session model

* some tests for the hub_model

* test logout user

* black

* test clear vars, reload

* test create session

* update user-json to user

* update user class

* add user tests

* tests sdk session

* tests for hub_model

* start account controller tsts

* unused import

* tests account controller: clear, pull

* tests for session_controller

* logout success bugfix

* add integration test for account

* black

* move routine related commands to other PR

* test for the user input

* completed tests for session controller

* convert to hist to str)

* fix import errors

* ruff

* ruff tests

* fix qa

* add help to settings cmds

* ruff

* fix pull keys bug

* point to dev

* account model tests

* black

* ruff

* fix for empty feature settings

* ruff

* don't display config msg if wrong token

* general msgs

* remove print

* ruff

* fix sources not updated real time

* missing return doc

* add guest if no email

* Routine remote storage (#4143)

* add upload routine command

* remove prints

* point to local again

* add commands

* add delete

* add download command and upload 409

* list comprehension

* add download & save plus upload fixe

* add user uuid folder

* add list command

* ruff

* en.yml detail

* provide choices to delete & download

* add choices to upload

* change menu text

* starting adding unit tests

* bad english

* Update test_print_help.txt

* genearl msgs

* remove print

* add docstrings

* add unit tests to account controller

* change parser

* fix routine upload test

* add to be implemented tests hub

* add to be implemented tests local

* add total pages to title

* split list into model and view

* add docstring

* print msg if character

* add local model tests

* hub tests routines

* fix test

* point to dev and glob

* fix small bug on upload

* missing return

* log decorators missing

* black

* ruff

* change prompt display

* rewrite test

* rewrite expected

* rewrite call

* try passing git action

* change .yml everywhere

---------

Co-authored-by: Henrique Joaquim <h.joaquim@campus.fct.unl.pt>
Co-authored-by: James Maslek <jmaslek11@gmail.com>
2023-02-09 13:35:44 +00:00

176 lines
5.6 KiB
Python

# IMPORTATION STANDARD
# IMPORTATION THIRDPARTY
import json
import os
from unittest.mock import patch
import pytest
from requests import Response
# IMPORTATION INTERNAL
from openbb_terminal.session import session_model
TEST_SESSION = {
"access_token": "test_token",
"token_type": "bearer",
"uuid": "test_uuid",
}
CONFIGS = {
"features_settings": {
"USE_WATERMARK": "False",
"TIMEZONE": "Europe/London",
"PLOT_DPI": "95",
"PLOT_HEIGHT_PERCENTAGE": "50.5",
"USER_DATA_DIRECTORY": "some/path/to/user/data",
},
"features_keys": {
"API_KEY_ALPHAVANTAGE": "test_av",
"API_FRED_KEY": "test_fred",
},
}
@pytest.mark.parametrize(
"email, password, save",
[
(
"test_email",
"test_pass",
False,
),
(
"test_email",
"test_pass",
True,
),
],
)
def test_create_session(email, password, save):
path = "openbb_terminal.session.session_model."
with (
patch(path + "Hub.get_session") as mock_get_session,
patch(path + "Local.save_session") as mock_save_session,
):
mock_get_session.return_value = TEST_SESSION
session = session_model.create_session(email, password, save)
assert session == TEST_SESSION
mock_get_session.assert_called_once_with(email, password)
if save and session:
mock_save_session.assert_called_once_with(session)
def test_login_no_response():
path = "openbb_terminal.session.session_model."
with (
patch(path + "Hub.fetch_user_configs") as mock_fetch_user_configs,
patch(path + "Local.apply_configs") as mock_apply_configs,
patch(path + "User.load_user_info") as mock_load_user_info,
patch(path + "User.update_flair") as mock_update_flair,
):
mock_fetch_user_configs.return_value = None
assert (
session_model.login(TEST_SESSION) == session_model.LoginStatus.NO_RESPONSE
)
mock_fetch_user_configs.assert_called_once_with(TEST_SESSION)
mock_apply_configs.assert_not_called()
mock_load_user_info.assert_not_called()
mock_update_flair.assert_not_called()
def test_login_fail_response():
path = "openbb_terminal.session.session_model."
with (
patch(path + "Hub.fetch_user_configs") as mock_fetch_user_configs,
patch(path + "Local.apply_configs") as mock_apply_configs,
patch(path + "User.load_user_info") as mock_load_user_info,
patch(path + "User.update_flair") as mock_update_flair,
):
response = Response()
response.status_code = 400
mock_fetch_user_configs.return_value = response
assert session_model.login(TEST_SESSION) == session_model.LoginStatus.FAILED
mock_fetch_user_configs.assert_called_once_with(TEST_SESSION)
mock_apply_configs.assert_not_called()
mock_load_user_info.assert_not_called()
mock_update_flair.assert_not_called()
def test_login_success_response():
path = "openbb_terminal.session.session_model."
with (
patch(path + "Hub.fetch_user_configs") as mock_fetch_user_configs,
patch(path + "Local.apply_configs") as mock_apply_configs,
patch(path + "User.load_user_info") as mock_load_user_info,
patch(path + "User.update_flair") as mock_update_flair,
):
response = Response()
response.status_code = 200
response._content = json.dumps( # pylint: disable=protected-access
CONFIGS
).encode("utf-8")
mock_fetch_user_configs.return_value = response
assert session_model.login(TEST_SESSION) == session_model.LoginStatus.SUCCESS
mock_fetch_user_configs.assert_called_once_with(TEST_SESSION)
mock_apply_configs.assert_called_once()
mock_load_user_info.assert_called_once()
mock_update_flair.assert_called_once()
@pytest.mark.parametrize(
"guest",
[
False,
True,
],
)
def test_logout_user(guest):
path = "openbb_terminal.session.session_model."
with (
patch(path + "Hub.delete_session") as mock_delete_session,
patch(path + "Local.remove_session_file") as mock_remove_session_file,
patch(path + "Local.remove_cli_history_file") as mock_remove_cli_history_file,
patch(path + "reload_openbb_modules") as mock_reload_openbb_modules,
patch(path + "clear_openbb_env_vars") as mock_clear_openbb_env_vars,
patch(path + "User.clear") as mock_clear_user,
patch(path + "plt.close") as mock_plt_close,
):
auth_header = "Bearer test_token"
token = "test_token"
session_model.logout(auth_header, token, guest)
if not guest:
mock_delete_session.assert_called_once_with(auth_header, token)
mock_clear_user.assert_called_once()
mock_clear_openbb_env_vars.assert_called_once()
mock_reload_openbb_modules.assert_called_once()
mock_remove_session_file.assert_called_once()
mock_remove_cli_history_file.assert_called_once()
mock_plt_close.assert_called_once()
def test_clear_openbb_env_vars():
mock_env = {"OPENBB_TEST": "test", "OPENBB_TEST2": "test2", "TEST": "test"}
with patch.dict("openbb_terminal.session.session_model.os.environ", mock_env):
session_model.clear_openbb_env_vars()
assert "OPENBB_TEST" not in os.environ
assert "OPENBB_TEST2" not in os.environ
assert "TEST" in os.environ
def test_reload_openbb_modules():
with patch("openbb_terminal.session.session_model.importlib.reload") as mock_reload:
session_model.reload_openbb_modules()
mock_reload.assert_called()