Skip to content

Commit

Permalink
Guard against empty RBAC lists
Browse files Browse the repository at this point in the history
  • Loading branch information
Iain-S committed Dec 7, 2023
1 parent bf57100 commit 3422acd
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 1 deletion.
2 changes: 1 addition & 1 deletion rctab/routers/frontend.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ async def subscription_details(
# ...then sort the assignments by role
sorted_all_rbac_assignments = []
for role in role_order:
sorted_all_rbac_assignments.extend(assignments_dict[role])
sorted_all_rbac_assignments.extend(assignments_dict.get(role, []))

# pylint: disable=line-too-long
views = {
Expand Down
63 changes: 63 additions & 0 deletions tests/test_routes/test_frontend.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from rctab.crud.accounting_models import subscription, subscription_details
from rctab.crud.schema import RoleAssignment, SubscriptionState, UserRBAC
from rctab.routers.frontend import check_user_on_subscription, home
from rctab.routers.frontend import subscription_details as subscription_details_page
from tests.test_routes import constants
from tests.test_routes.test_routes import test_db # pylint: disable=unused-import

Expand Down Expand Up @@ -181,3 +182,65 @@ async def test_render_home_page(mocker: MockerFixture, test_db: Database) -> Non
mocker.patch("rctab.routers.frontend.check_user_access", mock_check_access)

await home(mock_request, mock_user)


@pytest.mark.asyncio
async def test_render_details_page(mocker: MockerFixture, test_db: Database) -> None:
"""Check that we can pick up on undefined variable template issues."""
# Use StrictUndefined while testing
mocker.patch(
"rctab.routers.frontend.templates",
Jinja2Templates(
(
Path(rctab.routers.frontend.__file__).parent.parent / "templates"
).absolute(),
undefined=StrictUndefined,
),
)
subscription_id = UUID(int=random.randint(0, (2**32) - 1))

await test_db.execute(
subscription.insert().values(),
dict(
admin=str(constants.ADMIN_UUID),
subscription_id=str(subscription_id),
),
)

await test_db.execute(
subscription_details.insert().values(),
dict(
subscription_id=str(subscription_id),
state=SubscriptionState("Enabled"),
display_name="a subscription",
role_assignments=[
RoleAssignment(
role_definition_id="123",
role_name="Sous chef",
principal_id="456",
display_name="Max Mustermann",
# Note the missing email address, which does sometimes happen
mail=None,
scope="some/scope/string",
).dict()
],
),
)

mock_request = mocker.Mock()

mock_user = mocker.Mock()
mock_user.token = {
"access_token": jwt.encode(
{"unique_name": "[email protected]", "name": "My Name"}, "my key"
)
}
mock_user.oid = str(UUID(int=434))

mock_check_access = AsyncMock()
mock_check_access.return_value = UserRBAC(
oid=UUID(int=111), has_access=True, is_admin=True
)
mocker.patch("rctab.routers.frontend.check_user_access", mock_check_access)

await subscription_details_page(subscription_id, mock_request, mock_user)

0 comments on commit 3422acd

Please sign in to comment.