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

feat: allow passing a JWT to superset #82

Merged
merged 1 commit into from
Sep 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/preset_cli/cli/superset/main.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
"""
Main entry point for Superset commands.
"""
from typing import Any
from typing import Any, Optional

import click
from yarl import URL

from preset_cli.auth.jwt import JWTAuth
from preset_cli.auth.main import UsernamePasswordAuth
from preset_cli.cli.superset.export import (
export_assets,
Expand All @@ -22,6 +23,7 @@

@click.group()
@click.argument("instance")
@click.option("--jwt-token", default=None, help="JWT token")
@click.option("-u", "--username", default="admin", help="Username")
@click.option(
"-p",
Expand All @@ -36,6 +38,7 @@
def superset_cli(
ctx: click.core.Context,
instance: str,
jwt_token: Optional[str],
username: str = "admin",
password: str = "admin",
):
Expand All @@ -48,7 +51,10 @@ def superset_cli(

# allow a custom authenticator to be passed via the context
if "AUTH" not in ctx.obj:
ctx.obj["AUTH"] = UsernamePasswordAuth(URL(instance), username, password)
if jwt_token:
ctx.obj["AUTH"] = JWTAuth(jwt_token)
else:
ctx.obj["AUTH"] = UsernamePasswordAuth(URL(instance), username, password)


superset_cli.add_command(sql)
Expand Down
20 changes: 19 additions & 1 deletion tests/cli/superset/main_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@

import click
from click.testing import CliRunner
from pytest_mock import MockerFixture

from preset_cli.cli.superset.main import mutate_commands, superset
from preset_cli.cli.superset.main import mutate_commands, superset, superset_cli


def test_mutate_commands() -> None:
Expand Down Expand Up @@ -138,3 +139,20 @@ def test_superset() -> None:
--help Show this message and exit.
"""
)


def test_superset_jwt_auth(mocker: MockerFixture) -> None:
"""
Test passing a JWT to authenticate with Superset.
"""
# pylint: disable=invalid-name
JWTAuth = mocker.patch("preset_cli.cli.superset.main.JWTAuth")

runner = CliRunner()
runner.invoke(
superset_cli,
["--jwt-token=SECRET", "http://localhost:8088/", "export"],
catch_exceptions=False,
)

JWTAuth.assert_called_with("SECRET")