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

Rbac summary #11

Merged
merged 2 commits into from
Oct 19, 2023
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
11 changes: 10 additions & 1 deletion rctab_cli/sub_apps/sub.py
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,9 @@ def allocations(
@subscription_app.command()
def summary(
subscription_id: Optional[UUID] = typer.Option(None, help="Subscription id"),
show_rbac: bool = typer.Option(
False, "--show-rbac", help="Include the role assignments"
),
) -> None:
"""Get a summary of approvals, allocations and costs for one or all subscriptions."""
path = "accounting/subscription"
Expand All @@ -346,7 +349,13 @@ def summary(
)

raise_for_status(resp)
typer.echo(json.dumps(resp.json(), indent=4, sort_keys=True))

summaries = resp.json()
if not show_rbac:
for item in summaries:
item.pop("role_assignments")

typer.echo(json.dumps(summaries, indent=4, sort_keys=True))


@finance_app.command("create")
Expand Down
70 changes: 65 additions & 5 deletions tests/sub_apps/test_sub.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from datetime import date
from unittest.mock import patch
from unittest.mock import MagicMock, patch
from uuid import UUID

import requests
from typer.testing import CliRunner

from rctab_cli import cli
Expand All @@ -11,8 +12,8 @@
runner = CliRunner()


def test_create() -> None:
"""Test create command with all commandline options."""
def test_add() -> None:
"""Test add command with all commandline options."""

with patch("rctab_cli.sub_apps.sub.add_subscription") as mock_sub, patch(
"rctab_cli.sub_apps.sub.set_the_persistence"
Expand Down Expand Up @@ -42,8 +43,8 @@ def test_create() -> None:
)


def test_create_defaults() -> None:
"""Test create command with minimal commandline options."""
def test_add_defaults() -> None:
"""Test add command with minimal commandline options."""

with patch("rctab_cli.sub_apps.sub.add_subscription") as mock_sub, patch(
"rctab_cli.sub_apps.sub.set_the_persistence"
Expand Down Expand Up @@ -136,3 +137,62 @@ def test_approve_defaults() -> None:
"2020-02-01",
False,
)


def test_summary() -> None:
"""Test summary command with all commandline options."""
with patch("requests.get", autospec=True) as mock_get, patch(
"rctab_cli.sub_apps.sub.create_url", autospec=True
), patch("rctab_cli.sub_apps.sub.state", autospec=True), patch(
"rctab_cli.sub_apps.sub.BearerAuth", autospec=True
), patch(
"typer.echo", autospec=True
) as mock_echo, patch(
"json.dumps", autospec=True
) as mock_dumps:
mock_response = MagicMock(spec=requests.Response)
mock_response.status_code = 200
mock_response.json.return_value = [{"role_assignments": []}]
mock_get.return_value = mock_response

sub.summary(subscription_id=UUID(int=1), show_rbac=True)

# Expect role assignments to be included.
mock_dumps.assert_called_once_with(
[{"role_assignments": []}], indent=4, sort_keys=True
)
mock_echo.assert_called_once_with(mock_dumps.return_value)


def test_summary_defaults() -> None:
"""Test summary command with minimal commandline options."""
with patch("requests.get", autospec=True) as mock_get, patch(
"rctab_cli.sub_apps.sub.create_url", autospec=True
), patch("rctab_cli.sub_apps.sub.state", autospec=True), patch(
"rctab_cli.sub_apps.sub.BearerAuth", autospec=True
), patch(
"typer.echo", autospec=True
) as mock_echo, patch(
"json.dumps", autospec=True
) as mock_dumps:
mock_response = MagicMock(spec=requests.Response)
mock_response.status_code = 200
mock_response.json.return_value = [{"role_assignments": []}]
mock_get.return_value = mock_response

# More complicated invocation needed to test default params.
result = runner.invoke(
cli.app,
[
"sub",
"summary",
"--subscription-id",
str(UUID(int=1)),
],
)
if result.exit_code != 0:
raise ExitCodeException(result)

# Expect role assignments to have been removed.
mock_dumps.assert_called_once_with([{}], indent=4, sort_keys=True)
mock_echo.assert_called_once_with(mock_dumps.return_value)