diff options
author | montezdesousa <79287829+montezdesousa@users.noreply.github.com> | 2023-03-14 17:52:56 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-03-14 17:52:56 +0000 |
commit | 7ec63f827b2fa0fa48ffeb36f0e4f318d2439308 (patch) | |
tree | 6102958c14486bb3548e2c17db8c38909e4bb22b | |
parent | 1cdee7da702633726648dfa7eb4cf6125256b2ec (diff) |
Authentication QA fixes (#4472)
* fix logout cmd
* fix sync flag
* change msg
* update request test for delete session
* fix autocompletion for routines upload
* append to list of choices
* fix autocompletioin
* black
* mypy
* rewrite tests
8 files changed, 32 insertions, 29 deletions
diff --git a/openbb_terminal/account/account_controller.py b/openbb_terminal/account/account_controller.py index e704060e4f3..e834e62bd5d 100644 --- a/openbb_terminal/account/account_controller.py +++ b/openbb_terminal/account/account_controller.py @@ -5,8 +5,6 @@ import os from pathlib import Path from typing import Dict, List, Optional -from prompt_toolkit.completion import NestedCompleter - from openbb_terminal import ( keys_model, ) @@ -19,6 +17,7 @@ from openbb_terminal.core.session import ( from openbb_terminal.core.session.current_user import get_current_user, is_local from openbb_terminal.core.session.preferences_handler import set_preference from openbb_terminal.core.session.session_model import logout +from openbb_terminal.custom_prompt_toolkit import NestedCompleter from openbb_terminal.decorators import log_start_end from openbb_terminal.helper_funcs import check_positive from openbb_terminal.menu import session @@ -46,26 +45,29 @@ class AccountController(BaseController): ] PATH = "/account/" + CHOICES_GENERATION = True def __init__(self, queue: Optional[List[str]] = None): + """Constructor""" super().__init__(queue) self.ROUTINE_FILES: Dict[str, Path] = {} self.REMOTE_CHOICES: List[str] = [] - self.update_runtime_choices() + if session and get_current_user().preferences.USE_PROMPT_TOOLKIT: + self.choices: dict = self.choices_default + self.completer = NestedCompleter.from_nested_dict(self.choices) def update_runtime_choices(self): """Update runtime choices""" self.ROUTINE_FILES = self.get_routines() if session and get_current_user().preferences.USE_PROMPT_TOOLKIT: - choices: dict = {c: {} for c in self.controller_choices} # type: ignore - choices["sync"] = {"--on": {}, "--off": {}} - choices["upload"]["--file"] = {c: {} for c in self.ROUTINE_FILES} - choices["upload"]["-f"] = choices["upload"]["--file"] - choices["download"]["--name"] = {c: {} for c in self.REMOTE_CHOICES} - choices["download"]["-n"] = choices["download"]["--name"] - choices["delete"]["--name"] = {c: {} for c in self.REMOTE_CHOICES} - choices["delete"]["-n"] = choices["delete"]["--name"] - self.completer = NestedCompleter.from_nested_dict(choices) + self.choices["upload"]["--file"].update({c: {} for c in self.ROUTINE_FILES}) + self.choices["download"]["--name"].update( + {c: {} for c in self.REMOTE_CHOICES} + ) + self.choices["delete"]["--name"].update( + {c: {} for c in self.REMOTE_CHOICES} + ) + self.completer = NestedCompleter.from_nested_dict(self.choices) def get_routines(self): """Get routines""" @@ -88,7 +90,6 @@ class AccountController(BaseController): def print_help(self): """Print help""" - mt = MenuText("account/", 100) mt.add_info("_info_") mt.add_cmd("sync") @@ -109,6 +110,7 @@ class AccountController(BaseController): mt.add_info("_authentication_") mt.add_cmd("logout") console.print(text=mt.menu_text, menu="Account") + self.update_runtime_choices() @log_start_end(log=logger) def call_logout(self, other_args: List[str]) -> None: @@ -154,15 +156,16 @@ class AccountController(BaseController): ns_parser = self.parse_known_args_and_warn(parser, other_args) if ns_parser: + current_user = get_current_user() if ns_parser.sync is None: - sync = get_current_user().preferences.SYNC_ENABLED + sync = "ON" if current_user.preferences.SYNC_ENABLED is True else "OFF" console.print(f"sync is {sync}, use --on or --off to change.") else: set_preference( name="SYNC_ENABLED", value=ns_parser.sync, ) - sync = get_current_user().preferences.SYNC_ENABLED + sync = "ON" if current_user.preferences.SYNC_ENABLED is True else "OFF" console.print(f"[info]sync:[/info] {sync}") @log_start_end(log=logger) @@ -250,7 +253,7 @@ class AccountController(BaseController): ) df, page, pages = get_routines_info(response) if not df.empty: - self.REMOTE_CHOICES = list(df["name"]) + self.REMOTE_CHOICES += list(df["name"]) self.update_runtime_choices() display_routines_list(df, page, pages) else: diff --git a/openbb_terminal/core/session/hub_model.py b/openbb_terminal/core/session/hub_model.py index 572820f2d6e..e1fc818e57d 100644 --- a/openbb_terminal/core/session/hub_model.py +++ b/openbb_terminal/core/session/hub_model.py @@ -104,7 +104,7 @@ def delete_session( The response from the logout request. """ try: - response = requests.post( + response = requests.get( url=base_url + "logout", headers={"Authorization": auth_header}, json={"token": token}, @@ -320,9 +320,9 @@ def clear_user_configs( timeout=timeout, ) if response.status_code == 200: - console.print("[green]Cleared configurations.[/green]") + console.print("[green]Cleared data.[/green]") else: - console.print("[red]Failed to clear configurations.[/red]") + console.print("[red]Failed to clear data.[/red]") return response except requests.exceptions.ConnectionError: console.print(f"\n{CONNECTION_ERROR_MSG}") @@ -331,7 +331,7 @@ def clear_user_configs( console.print(f"\n{CONNECTION_TIMEOUT_MSG}") return None except Exception: - console.print("[red]Failed to clear configurations.[/red]") + console.print("[red]Failed to clear data.[/red]") return None diff --git a/tests/openbb_terminal/account/txt/test_account_controller/test_call_sync[other_args0-False].txt b/tests/openbb_terminal/account/txt/test_account_controller/test_call_sync[other_args0-False].txt index d2624cdd039..3992f63030e 100644 --- a/tests/openbb_terminal/account/txt/test_account_controller/test_call_sync[other_args0-False].txt +++ b/tests/openbb_terminal/account/txt/test_account_controller/test_call_sync[other_args0-False].txt @@ -1,2 +1,2 @@ -[info]sync:[/info] True +[info]sync:[/info] OFF ['--on'] diff --git a/tests/openbb_terminal/account/txt/test_account_controller/test_call_sync[other_args1-True].txt b/tests/openbb_terminal/account/txt/test_account_controller/test_call_sync[other_args1-True].txt index a02421955aa..e2401bc93e0 100644 --- a/tests/openbb_terminal/account/txt/test_account_controller/test_call_sync[other_args1-True].txt +++ b/tests/openbb_terminal/account/txt/test_account_controller/test_call_sync[other_args1-True].txt @@ -1,2 +1,2 @@ -[info]sync:[/info] False +[info]sync:[/info] ON ['--off'] diff --git a/tests/openbb_terminal/account/txt/test_account_controller/test_call_sync[other_args2-True].txt b/tests/openbb_terminal/account/txt/test_account_controller/test_call_sync[other_args2-True].txt index d2624cdd039..939d5a1cd39 100644 --- a/tests/openbb_terminal/account/txt/test_account_controller/test_call_sync[other_args2-True].txt +++ b/tests/openbb_terminal/account/txt/test_account_controller/test_call_sync[other_args2-True].txt @@ -1,2 +1,2 @@ -[info]sync:[/info] True +[info]sync:[/info] ON ['--on'] diff --git a/tests/openbb_terminal/account/txt/test_account_controller/test_call_sync[other_args3-False].txt b/tests/openbb_terminal/account/txt/test_account_controller/test_call_sync[other_args3-False].txt index a02421955aa..72602083de8 100644 --- a/tests/openbb_terminal/account/txt/test_account_controller/test_call_sync[other_args3-False].txt +++ b/tests/openbb_terminal/account/txt/test_account_controller/test_call_sync[other_args3-False].txt @@ -1,2 +1,2 @@ -[info]sync:[/info] False +[info]sync:[/info] OFF ['--off'] diff --git a/tests/openbb_terminal/account/txt/test_account_controller/test_call_sync[other_args4-True].txt b/tests/openbb_terminal/account/txt/test_account_controller/test_call_sync[other_args4-True].txt index f4f1eef4b7d..82f1693fb92 100644 --- a/tests/openbb_terminal/account/txt/test_account_controller/test_call_sync[other_args4-True].txt +++ b/tests/openbb_terminal/account/txt/test_account_controller/test_call_sync[other_args4-True].txt @@ -1,2 +1,2 @@ -sync is True, use --on or --off to change. +sync is ON, use --on or --off to change. [] diff --git a/tests/openbb_terminal/session/test_hub_model.py b/tests/openbb_terminal/session/test_hub_model.py index 7b9c9d15d9f..3f94c22c347 100644 --- a/tests/openbb_terminal/session/test_hub_model.py +++ b/tests/openbb_terminal/session/test_hub_model.py @@ -102,7 +102,7 @@ def test_create_session_from_token_exception(token): @pytest.mark.parametrize("auth_header, token", TEST_HEADER_TOKEN) def test_delete_session_success(auth_header, token): - with patch("requests.post") as mock_post: + with patch("requests.get") as mock_post: mock_post.return_value.status_code = 200 response = hub_model.delete_session(auth_header, token) assert response.status_code == 200 @@ -117,7 +117,7 @@ def test_delete_session_success(auth_header, token): @pytest.mark.parametrize("auth_header, token", TEST_HEADER_TOKEN) def test_delete_session_connection_error(auth_header, token): - with patch("requests.post") as mock_post: + with patch("requests.get") as mock_post: mock_post.side_effect = requests.exceptions.ConnectionError response = hub_model.delete_session(auth_header, token) assert response is None @@ -125,7 +125,7 @@ def test_delete_session_connection_error(auth_header, token): @pytest.mark.parametrize("auth_header, token", TEST_HEADER_TOKEN) def test_delete_session_timeout(auth_header, token): - with patch("requests.post") as mock_post: + with patch("requests.get") as mock_post: mock_post.side_effect = requests.exceptions.Timeout response = hub_model.delete_session(auth_header, token) assert response is None @@ -133,7 +133,7 @@ def test_delete_session_timeout(auth_header, token): @pytest.mark.parametrize("auth_header, token", TEST_HEADER_TOKEN) def test_delete_session_exception(auth_header, token): - with patch("requests.post") as mock_post: + with patch("requests.get") as mock_post: mock_post.side_effect = Exception response = hub_model.delete_session(auth_header, token) assert response is None |