Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Authentication QA fixes #4472

Merged
merged 11 commits into from
Mar 14, 2023
35 changes: 19 additions & 16 deletions openbb_terminal/account/account_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
from pathlib import Path
from typing import Dict, List, Optional

from prompt_toolkit.completion import NestedCompleter

from openbb_terminal import (
keys_model,
)
Expand All @@ -19,6 +17,7 @@
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
Expand Down Expand Up @@ -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"""
Expand All @@ -88,7 +90,6 @@ def get_routines(self):

def print_help(self):
"""Print help"""

mt = MenuText("account/", 100)
mt.add_info("_info_")
mt.add_cmd("sync")
Expand All @@ -109,6 +110,7 @@ def print_help(self):
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:
Expand Down Expand Up @@ -154,15 +156,16 @@ def call_sync(self, other_args: List[str]):

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="OPENBB_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)
Expand Down Expand Up @@ -250,7 +253,7 @@ def call_list(self, other_args: List[str]):
)
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:
Expand Down
8 changes: 4 additions & 4 deletions openbb_terminal/core/session/hub_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -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}")
Expand All @@ -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


Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
[info]sync:[/info] True
[info]sync:[/info] OFF
['--on']
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
[info]sync:[/info] False
[info]sync:[/info] ON
['--off']
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
[info]sync:[/info] True
[info]sync:[/info] ON
['--on']
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
[info]sync:[/info] False
[info]sync:[/info] OFF
['--off']
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
sync is True, use --on or --off to change.
sync is ON, use --on or --off to change.
[]
8 changes: 4 additions & 4 deletions tests/openbb_terminal/session/test_hub_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -117,23 +117,23 @@ 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


@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


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