Skip to content

Commit

Permalink
BE members: Fix not being able to modify members
Browse files Browse the repository at this point in the history
  • Loading branch information
ccremer committed May 8, 2023
1 parent 41409c4 commit 60c87ba
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 268 deletions.
99 changes: 1 addition & 98 deletions cypress/e2e/billingentity-members.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@ import { BillingEntityPermissions } from '../../src/app/types/billing-entity';
import { ClusterRoleBinding, ClusterRoleBindingPermissions } from '../../src/app/types/clusterrole-binding';
import { billingEntityNxt } from '../fixtures/billingentities';
import { createClusterRoleBinding } from '../fixtures/clusterrole-binding';
import { createClusterRole } from '../fixtures/clusterrole';
import { ClusterRolePermissions } from '../../src/app/types/clusterRole';

describe('billing entity edit members with existing roles', () => {
describe('billing entity edit members', () => {
beforeEach(() => {
cy.setupAuth();
window.localStorage.setItem('hideFirstTimeLoginDialog', 'true');
Expand All @@ -20,17 +18,9 @@ describe('billing entity edit members with existing roles', () => {
cy.setPermission(
{ verb: 'list', ...BillingEntityPermissions },
{ verb: 'get', ...BillingEntityPermissions, name: 'be-2345' },
{ verb: 'get', ...ClusterRoleBindingPermissions, name: 'billingentities-be-2345-viewer' },
{ verb: 'get', ...ClusterRoleBindingPermissions, name: 'billingentities-be-2345-admin' },
{ verb: 'update', ...ClusterRoleBindingPermissions, name: 'billingentities-be-2345-viewer' },
{ verb: 'update', ...ClusterRoleBindingPermissions, name: 'billingentities-be-2345-admin' }
);
cy.intercept('GET', 'appuio-api/apis/rbac.authorization.k8s.io/v1/clusterroles/billingentities-be-2345-admin', {
body: createClusterRole('be-2345', true),
});
cy.intercept('GET', 'appuio-api/apis/rbac.authorization.k8s.io/v1/clusterroles/billingentities-be-2345-viewer', {
body: createClusterRole('be-2345', false),
});
});

it('add member', () => {
Expand Down Expand Up @@ -290,90 +280,3 @@ describe('billing entity edit members with existing roles', () => {
cy.get('p-message').contains('You are about to remove yourself as admin!');
});
});

describe('billing entity edit members without initial roles', () => {
beforeEach(() => {
cy.setupAuth();
window.localStorage.setItem('hideFirstTimeLoginDialog', 'true');
cy.disableCookieBanner();
});
beforeEach(() => {
// needed for initial getUser request
cy.intercept('GET', 'appuio-api/apis/appuio.io/v1/users/mig', {
body: createUser({ username: 'mig', defaultOrganizationRef: 'nxt' }),
});
cy.setPermission(
{ verb: 'list', ...BillingEntityPermissions },
{ verb: 'get', ...BillingEntityPermissions, name: 'be-2345' },
{ verb: 'get', ...ClusterRoleBindingPermissions, name: 'billingentities-be-2345-viewer' },
{ verb: 'update', ...ClusterRoleBindingPermissions, name: 'billingentities-be-2345-admin' },
{ verb: 'update', ...ClusterRoleBindingPermissions, name: 'billingentities-be-2345-viewer' },
{ verb: 'update', ...ClusterRolePermissions, name: 'billingentities-be-2345-admin' }
);
cy.intercept('GET', 'appuio-api/apis/rbac.authorization.k8s.io/v1/clusterroles/billingentities-be-2345-admin', {
statusCode: 404,
});
cy.intercept('GET', 'appuio-api/apis/rbac.authorization.k8s.io/v1/clusterroles/billingentities-be-2345-viewer', {
statusCode: 404,
});
cy.intercept(
'GET',
'appuio-api/apis/rbac.authorization.k8s.io/v1/clusterrolebindings/billingentities-be-2345-viewer',
{
statusCode: 404,
}
);
cy.intercept(
'GET',
'appuio-api/apis/rbac.authorization.k8s.io/v1/clusterrolebindings/billingentities-be-2345-admin',
{
statusCode: 404,
}
);
});

it('add member', () => {
cy.intercept('GET', 'appuio-api/apis/billing.appuio.io/v1/billingentities/be-2345', {
body: billingEntityNxt,
});
cy.intercept('POST', 'appuio-api/apis/rbac.authorization.k8s.io/v1/clusterroles', (req) => {
if (req.body.metadata.name.includes('admin')) {
expect(req.body.rules).to.have.length(2);
const rule = req.body.rules && req.body.rules[0];
expect(rule && rule.resourceNames).to.include('billingentities-be-2345-admin');
expect(rule && rule.verbs).to.eql(['*']);

req.reply(createClusterRole('be-2345', true));
return;
}
if (req.body.metadata.name.includes('viewer')) {
const rule = req.body.rules && req.body.rules[0];
expect(rule && rule.resourceNames).to.include('billingentities-be-2345-viewer');
expect(rule && rule.verbs).to.eql(['get', 'watch']);

req.reply(createClusterRole('be-2345', false));
return;
}
}).as('createRole');

cy.intercept('POST', 'appuio-api/apis/rbac.authorization.k8s.io/v1/clusterrolebindings', (req) => {
expect(req.body.subjects).to.have.length(1);
const subject = req.body.subjects && req.body.subjects[0];
expect(subject && subject.name).to.eq('appuio#crc');

if (req.body.metadata.name.includes('admin')) {
req.reply(createClusterRoleBinding({ name: 'billingentities-be-2345-admin', users: ['appuio#crc'] }));
}
if (req.body.metadata.name.includes('viewer')) {
req.reply(createClusterRoleBinding({ name: 'billingentities-be-2345-viewer', users: ['appuio#crc'] }));
}
}).as('createRoleBinding');

cy.visit('/billingentities/be-2345/members');
cy.get('.text-3xl').should('contain.text', 'be-2345 Members');
cy.get('[data-cy="name-input-0"]').type('crc');
cy.get('p-multiselect').eq(0).click().contains('billingentities-be-2345-admin').click();
cy.get('button[type=submit]').click();
cy.wait(['@createRole', '@createRoleBinding']);
});
});
25 changes: 0 additions & 25 deletions cypress/fixtures/clusterrole.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/app/billingentity/billing-entity.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ <h1 class="pt-0 mt-0 mb-0" i18n id="billingentities-title">Billing</h1>
{{ p.billingEntity.metadata.name }}
</div>
<div>
<a *ngIf="p.canViewMembers" [routerLink]="[p.billingEntity.metadata.name, 'members']"
<a *ngIf="p.canEditMembers" [routerLink]="[p.billingEntity.metadata.name, 'members']"
class="text-blue-500 hover:text-primary cursor-pointer ml-3" i18n-title title="Edit members">
<fa-icon [icon]="faUserGroup" />
</a>
Expand Down
6 changes: 3 additions & 3 deletions src/app/billingentity/billing-entity.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@ export class BillingEntityComponent implements OnInit {
this.billingEntityService.canEditMembers(`billingentities-${be.metadata.name}-admin`),
this.billingEntityService.canEditBilling(be.metadata.name),
]).pipe(
map(([billingEntity, canViewMembers, canEdit]) => {
map(([billingEntity, canEditMembers, canEdit]) => {
return {
billingEntity,
canViewMembers,
canEditMembers,
canEdit,
} satisfies BillingModel;
})
Expand Down Expand Up @@ -91,5 +91,5 @@ interface ViewModel {
interface BillingModel {
billingEntity: BillingEntity;
canEdit: boolean;
canViewMembers: boolean;
canEditMembers: boolean;
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<ng-container *ngrxLet="payload$ as payload; suspenseTpl: loading; error as err">
<ng-container *ngIf="payload as p">
<ng-container *ngrxLet="viewModel$ as vm; suspenseTpl: loading; error as err">
<ng-container *ngIf="vm as p">
<div class="surface-card p-4 shadow-2 border-round mb-4">
<div class="flex flex-row justify-content-between">
<div class="text-3xl font-medium text-900 mb-3">
{{ payload.billingEntity.metadata.name }}
{{ vm.billingEntity.metadata.name }}
<span i18n>Members</span>
</div>
<a appBackLink="../.." class="text-blue-500 hover:text-primary text-2xl cursor-pointer">
Expand All @@ -12,7 +12,7 @@
</div>

<div class="border-top-1 surface-border" *ngIf="form">
<form (submit)="save(payload)" [formGroup]="form" class="flex flex-column" novalidate>
<form (submit)="save(vm)" [formGroup]="form" class="flex flex-column" novalidate>
<div class="max-w-1200px">
<div class="grid mb-2 mt-2">
<p-message
Expand Down Expand Up @@ -44,7 +44,7 @@
<label i18n for="selectedRoles" class="md:hidden">Roles</label>
<p-multiSelect
formControlName="selectedRoles"
[options]="[payload.viewerBinding.metadata.name, payload.adminBinding.metadata.name]"
[options]="[vm.viewerBinding.metadata.name, vm.adminBinding.metadata.name]"
defaultLabel="Select Roles"
[maxSelectedLabels]="1"
[styleClass]="'w-full'"
Expand Down Expand Up @@ -80,7 +80,7 @@
severity="warn"
text="You are about to remove yourself as admin!"></p-message>
</div>
<button [disabled]="form.invalid" [loading]="(rolebindingService.loading$ | ngrxPush) ?? false" class="w-auto mt-3" pButton pRipple type="submit">
<button [disabled]="form.invalid" [loading]="(roleBindingService.loading$ | ngrxPush) ?? false" class="w-auto mt-3" pButton pRipple type="submit">
<fa-icon [icon]="faSave" class="pr-2"></fa-icon>
<span class="pr-2" i18n>Save</span>
</button>
Expand Down
Loading

0 comments on commit 60c87ba

Please sign in to comment.