Merge pull request #8972 from backstage/catalog-permission-integration/unregister-entity

Disable unregister entity button in catalog if unauthorized
This commit is contained in:
Fredrik Adelöw
2022-01-20 10:23:15 +01:00
committed by GitHub
20 changed files with 340 additions and 6 deletions
+1
View File
@@ -69,6 +69,7 @@
"zen-observable": "^0.8.15"
},
"devDependencies": {
"@backstage/plugin-permission-react": "^0.3.0-next.0",
"@backstage/test-utils": "^0.2.3-next.0",
"@rjsf/core": "^3.2.1",
"@testing-library/cypress": "^8.0.2",
@@ -21,7 +21,9 @@ import {
starredEntitiesApiRef,
} from '@backstage/plugin-catalog-react';
import { githubActionsApiRef } from '@backstage/plugin-github-actions';
import { permissionApiRef } from '@backstage/plugin-permission-react';
import {
MockPermissionApi,
MockStorageApi,
renderInTestApp,
TestApiProvider,
@@ -49,6 +51,7 @@ describe('EntityPage Test', () => {
const mockedApi = {
listWorkflowRuns: jest.fn().mockResolvedValue([]),
};
const mockPermissionApi = new MockPermissionApi();
describe('cicdContent', () => {
it('Should render GitHub Actions View', async () => {
@@ -62,6 +65,7 @@ describe('EntityPage Test', () => {
storageApi: MockStorageApi.create(),
}),
],
[permissionApiRef, mockPermissionApi],
]}
>
<EntityProvider entity={entity}>
+15
View File
@@ -7,6 +7,9 @@ import { AnalyticsApi } from '@backstage/core-plugin-api';
import { AnalyticsEvent } from '@backstage/core-plugin-api';
import { ApiHolder } from '@backstage/core-plugin-api';
import { ApiRef } from '@backstage/core-plugin-api';
import { AuthorizeDecision } from '@backstage/plugin-permission-common';
import { AuthorizeQuery } from '@backstage/plugin-permission-common';
import { AuthorizeResult } from '@backstage/plugin-permission-common';
import { ComponentType } from 'react';
import { Config } from '@backstage/config';
import { ConfigApi } from '@backstage/core-plugin-api';
@@ -17,6 +20,7 @@ import { ExternalRouteRef } from '@backstage/core-plugin-api';
import { JsonObject } from '@backstage/types';
import { JsonValue } from '@backstage/types';
import { Observable } from '@backstage/types';
import { PermissionApi } from '@backstage/plugin-permission-react';
import { ReactElement } from 'react';
import { ReactNode } from 'react';
import { RenderResult } from '@testing-library/react';
@@ -113,6 +117,17 @@ export type MockErrorApiOptions = {
collect?: boolean;
};
// @public
export class MockPermissionApi implements PermissionApi {
constructor(
requestHandler?: (
request: AuthorizeQuery,
) => AuthorizeResult.ALLOW | AuthorizeResult.DENY,
);
// (undocumented)
authorize(request: AuthorizeQuery): Promise<AuthorizeDecision>;
}
// @public
export class MockStorageApi implements StorageApi {
// (undocumented)
+2
View File
@@ -32,6 +32,8 @@
"@backstage/config": "^0.1.13-next.0",
"@backstage/core-app-api": "^0.5.0-next.0",
"@backstage/core-plugin-api": "^0.6.0-next.0",
"@backstage/plugin-permission-common": "^0.4.0-next.0",
"@backstage/plugin-permission-react": "^0.3.0-next.0",
"@backstage/theme": "^0.2.14",
"@backstage/types": "^0.1.1",
"@material-ui/core": "^4.12.2",
@@ -0,0 +1,43 @@
/*
* Copyright 2021 The Backstage Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import {
AuthorizeResult,
Permission,
} from '@backstage/plugin-permission-common';
import { MockPermissionApi } from './MockPermissionApi';
describe('MockPermissionApi', () => {
it('returns ALLOW by default', async () => {
const api = new MockPermissionApi();
await expect(
api.authorize({ permission: { name: 'permission.1' } as Permission }),
).resolves.toEqual({ result: AuthorizeResult.ALLOW });
});
it('allows passing a handler to customize the result', async () => {
const api = new MockPermissionApi(request =>
request.permission.name === 'permission.2'
? AuthorizeResult.DENY
: AuthorizeResult.ALLOW,
);
await expect(
api.authorize({ permission: { name: 'permission.2' } as Permission }),
).resolves.toEqual({ result: AuthorizeResult.DENY });
});
});
@@ -0,0 +1,41 @@
/*
* Copyright 2022 The Backstage Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { PermissionApi } from '@backstage/plugin-permission-react';
import {
AuthorizeDecision,
AuthorizeQuery,
AuthorizeResult,
} from '@backstage/plugin-permission-common';
/**
* Mock implementation of {@link core-plugin-api#PermissionApi}. Supply a
* requestHandler function to override the mock result returned for a given
* request.
* @public
*/
export class MockPermissionApi implements PermissionApi {
constructor(
private readonly requestHandler: (
request: AuthorizeQuery,
) => AuthorizeResult.ALLOW | AuthorizeResult.DENY = () =>
AuthorizeResult.ALLOW,
) {}
async authorize(request: AuthorizeQuery): Promise<AuthorizeDecision> {
return { result: this.requestHandler(request) };
}
}
@@ -0,0 +1,17 @@
/*
* Copyright 2022 The Backstage Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
export { MockPermissionApi } from './MockPermissionApi';
@@ -17,4 +17,5 @@
export * from './AnalyticsApi';
export * from './ConfigApi';
export * from './ErrorApi';
export * from './PermissionApi';
export * from './StorageApi';