Skip to content

Commit

Permalink
feat(bb:auth:reset): added configuration reset functionality
Browse files Browse the repository at this point in the history
ability to reset config file and directory in case of corruption/misconfiguration
  • Loading branch information
psadi committed Jul 2, 2024
1 parent 99aefe9 commit 2c084e4
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 27 deletions.
2 changes: 1 addition & 1 deletion bb/__version__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@
the utility and pyproject
"""

__version__ = "0.6.0"
__version__ = "0.6.1"
52 changes: 48 additions & 4 deletions bb/auth/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,13 @@
import typer

from bb.utils.helper import error_handler, validate_config
from bb.utils.ini import BB_CONFIG_FILE, auth_setup, is_config_present, parse
from bb.utils.ini import (
BB_CONFIG_FILE,
XDG_CONFIG_HOME,
auth_setup,
is_config_present,
parse,
)
from bb.utils.richprint import console

_auth: typer.Typer = typer.Typer(add_completion=False, no_args_is_help=True)
Expand Down Expand Up @@ -62,9 +68,8 @@ def _setup() -> None:
typer.prompt("> username"),
typer.prompt("> token", hide_input=True),
)
typer.echo(
f"Configuration written at '{BB_CONFIG_FILE}',"
+ "Please re-run 'bb auth test' to validate"
console.print(
f"\nConfiguration written at [yellow]'{BB_CONFIG_FILE}'[/yellow]\nPlease re-run [yellow]`bb auth test`[/yellow] to validate\n"
)

_setup()
Expand Down Expand Up @@ -126,3 +131,42 @@ def _status(token: bool) -> None:
console.print(f"{hcm} Token: {_token if token else '*' * len(_token)}")

_status(token)


@_auth.command(help="Reset authentication configuration")
def reset() -> None:
"""
The function `reset` contains an inner function `_reset` that resets the configuration file.
Args:
- :param: The `reset` function does not take any parameters
Raises:
- :raises: This function does not raise any exceptions
Returns:
- :rtype: None
"""

@error_handler
def _reset() -> None:
console.print(
f"\nThis will delete,\n\n- File: [yellow]{BB_CONFIG_FILE}[/yellow]\n- Directory: [yellow]{XDG_CONFIG_HOME}[/yellow]\n"
)
if (
typer.prompt(
"Are you sure you want to reset the configuration? (y/n)", default="n"
)
== "y"
):
import os

if os.path.exists(BB_CONFIG_FILE):
os.remove(BB_CONFIG_FILE)

if os.path.exists(XDG_CONFIG_HOME):
os.rmdir(XDG_CONFIG_HOME)

console.print(
"\nConfiguration reset successfully, please run [yellow]`bb auth setup`[/yellow] to configure again\n"
)

_reset()
7 changes: 4 additions & 3 deletions bb/utils/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
"""

import json
import sys

from typer import Exit

from bb.utils.ini import is_config_present, parse

Expand Down Expand Up @@ -469,5 +470,5 @@ def load_bitbucket_api() -> BitbucketAPI:
try:
bitbucket_api = load_bitbucket_api()
except ValueError:
print("Failed to load Bitbucket API", file=sys.stderr)
sys.exit(1)
bitbucket_api = None
Exit(code=1)
4 changes: 2 additions & 2 deletions bb/utils/ini.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def config_path() -> Tuple[str, str]:
return (config_dir, config_file)


_XDG_CONFIG_HOME, BB_CONFIG_FILE = config_path()
XDG_CONFIG_HOME, BB_CONFIG_FILE = config_path()


def is_config_present() -> bool:
Expand All @@ -68,7 +68,7 @@ def auth_setup(bitbucket_host: str, username: str, token: str) -> None:
Returns:
None
"""
Path(_XDG_CONFIG_HOME).mkdir(parents=True, exist_ok=True)
Path(XDG_CONFIG_HOME).mkdir(parents=True, exist_ok=True)
Path(BB_CONFIG_FILE).touch(exist_ok=True)

defaut_config = """[auth]
Expand Down
2 changes: 1 addition & 1 deletion config.ini
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[auth]
; Dummu local hostname
bitbucket_host = http://picolo.box:7990
bitbucket_host = https://bb.picolo.co.in
username = xxxxxxxx
token = xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
1 change: 1 addition & 0 deletions test
Submodule test added at 176bed
10 changes: 5 additions & 5 deletions tests/props.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

@dataclass(frozen=True, order=True)
class Api:
bitbucket_host: str = "http://picolo.box:7990"
bitbucket_host: str = "https://bb.picolo.co.in"
project: str = "test-project"
repository: str = "test-repo"
repo_id: str = "1234"
Expand Down Expand Up @@ -64,12 +64,12 @@ class Ini:
from platform import system
from typing import Dict

_XDG_CONFIG_HOME: str = os.path.expanduser("~")
XDG_CONFIG_HOME: str = os.path.expanduser("~")

platform_config: Dict[str, dict] = {
"Windows": f"{_XDG_CONFIG_HOME}\\.config\\bb\\config.ini",
"Linux": f"{_XDG_CONFIG_HOME}/.config/bb/config.ini",
"Darwin": f"{_XDG_CONFIG_HOME}/.config/bb/config.ini",
"Windows": f"{XDG_CONFIG_HOME}\\.config\\bb\\config.ini",
"Linux": f"{XDG_CONFIG_HOME}/.config/bb/config.ini",
"Darwin": f"{XDG_CONFIG_HOME}/.config/bb/config.ini",
}

BB_CONFIG_FILE: str = platform_config.get(system(), "n/a")
38 changes: 27 additions & 11 deletions tests/test_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

from unittest.mock import patch

from props import Ini
from typer.testing import CliRunner

from bb.auth import _auth, setup
Expand All @@ -30,14 +29,9 @@


@patch("bb.auth.typer.prompt")
@patch("bb.auth.typer.echo")
@patch("bb.auth.console.print")
@patch("bb.auth.is_config_present")
@patch("bb.auth.auth_setup")
def test_mock_setup(
mock_auth_setup, mock_is_config_present, mock_print, mock_echo, mock_prompt
):
property: Ini = Ini()
def test_mock_setup(mock_auth_setup, mock_is_config_present, mock_prompt):
# Arrange
mock_is_config_present.return_value = False
mock_prompt.side_effect = ["host", "username", "token"]
Expand All @@ -51,15 +45,28 @@ def test_mock_setup(
mock_prompt.assert_any_call("> username")
mock_prompt.assert_any_call("> token", hide_input=True)
mock_auth_setup.assert_called_once_with("host", "username", "token")
mock_echo.assert_called_once_with(
f"Configuration written at '{property.BB_CONFIG_FILE}',"
+ "Please re-run 'bb auth test' to validate"
)


def test_invalid_setup():
result = runner.invoke(_auth, ["setup", "--token"])
assert result.exit_code != 0


def test_invalid_test():
result = runner.invoke(_auth, ["test", "--token"])
assert result.exit_code != 0


def test_invalid_reset():
result = runner.invoke(_auth, ["reset", "--token"])
assert result.exit_code != 0


def test_setup():
result = runner.invoke(_auth, ["setup"])
assert result.exit_code == 0
result = runner.invoke(_auth, ["setup", "--token"])
assert result.exit_code != 0


def test_status():
Expand All @@ -72,3 +79,12 @@ def test_status():
def test_test():
result = runner.invoke(_auth, ["test"])
assert result.exit_code == 0
result = runner.invoke(_auth, ["test", "--token"])
assert result.exit_code != 0


def test_reset():
result = runner.invoke(_auth, ["reset"])
assert result.exit_code == 0
result = runner.invoke(_auth, ["reset", "--token"])
assert result.exit_code != 0

0 comments on commit 2c084e4

Please sign in to comment.