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
This commit is contained in:
montezdesousa
2023-03-14 17:52:56 +00:00
committed by GitHub
parent 1cdee7da70
commit 7ec63f827b
8 changed files with 32 additions and 29 deletions

View File

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

View File

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

View File

@@ -1,2 +1,2 @@
[info]sync:[/info] True
[info]sync:[/info] OFF
['--on']

View File

@@ -1,2 +1,2 @@
[info]sync:[/info] False
[info]sync:[/info] ON
['--off']

View File

@@ -1,2 +1,2 @@
[info]sync:[/info] True
[info]sync:[/info] ON
['--on']

View File

@@ -1,2 +1,2 @@
[info]sync:[/info] False
[info]sync:[/info] OFF
['--off']

View File

@@ -1,2 +1,2 @@
sync is True, use --on or --off to change.
sync is ON, use --on or --off to change.
[]

View File

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