Added tests to plugin vault & vault-backend

Signed-off-by: ivgo <ivgo@spreadgroup.com>
This commit is contained in:
ivgo
2022-05-11 12:45:52 +02:00
parent 7c310a5bc2
commit 477061a096
14 changed files with 1086 additions and 189 deletions
+1
View File
@@ -34,6 +34,7 @@
"postpack": "backstage-cli package postpack"
},
"dependencies": {
"@backstage/backend-test-utils": "^0.1.23",
"@backstage/catalog-model": "^1.0.1",
"@backstage/core-components": "^0.9.3",
"@backstage/core-plugin-api": "^1.0.1",
+77
View File
@@ -0,0 +1,77 @@
/*
* Copyright 2020 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 { setupRequestMockHandlers } from '@backstage/backend-test-utils';
import { rest } from 'msw';
import { setupServer } from 'msw/node';
import { Secret, VaultClient } from './api';
import { UrlPatternDiscovery } from '@backstage/core-app-api';
describe('api', () => {
const server = setupServer();
setupRequestMockHandlers(server);
const mockBaseUrl = 'https://api-vault.com/api/vault';
const discoveryApi = UrlPatternDiscovery.compile(mockBaseUrl);
const mockSecretsResult: Secret[] = [
{
name: 'secret::one',
editUrl: `${mockBaseUrl}/ui/vault/secrets/secrets/edit/test/success/secret::one`,
showUrl: `${mockBaseUrl}/ui/vault/secrets/secrets/show/test/success/secret::one`,
},
{
name: 'secret::two',
editUrl: `${mockBaseUrl}/ui/vault/secrets/secrets/edit/test/success/secret::two`,
showUrl: `${mockBaseUrl}/ui/vault/secrets/secrets/show/test/success/secret::two`,
},
];
const setupHandlers = () => {
server.use(
rest.get(`${mockBaseUrl}/v1/secrets`, (req, res, ctx) => {
const path = req.url.searchParams.get('path');
if (path === 'test/success') {
return res(ctx.json(mockSecretsResult));
} else if (path === 'test/error') {
return res(ctx.json([]));
}
return res(ctx.status(400));
}),
);
};
it('should return secrets', async () => {
setupHandlers();
const api = new VaultClient({ discoveryApi });
const secrets = await api.listSecrets('test/success');
expect(secrets).toEqual(mockSecretsResult);
});
it('should return empty secret list', async () => {
setupHandlers();
const api = new VaultClient({ discoveryApi });
expect(await api.listSecrets('test/error')).toEqual([]);
expect(await api.listSecrets('')).toEqual([]);
});
it('should return no secrets', async () => {
setupHandlers();
const api = new VaultClient({ discoveryApi });
const secrets = await api.listSecrets('');
expect(secrets).toEqual([]);
});
});
@@ -0,0 +1,52 @@
/*
* Copyright 2020 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 React from 'react';
import { setupServer } from 'msw/node';
import { setupRequestMockHandlers } from '@backstage/test-utils';
import { ComponentEntity } from '@backstage/catalog-model';
import { render } from '@testing-library/react';
import { EntityVaultCard } from './EntityVaultCard';
import { EntityProvider } from '@backstage/plugin-catalog-react';
describe('EntityVautCard', () => {
const server = setupServer();
setupRequestMockHandlers(server);
const entityAnnotationMissing: ComponentEntity = {
apiVersion: 'backstage.io/v1alpha1',
kind: 'Component',
metadata: {
name: 'test',
description: 'This is the description',
},
spec: {
lifecycle: 'production',
owner: 'owner',
type: 'service',
},
};
it('should render missing entity annotation', async () => {
const rendered = render(
<EntityProvider entity={entityAnnotationMissing}>
<EntityVaultCard />
</EntityProvider>,
);
expect(
rendered.getByText(/Add the annotation to your component YAML/),
).toBeInTheDocument();
});
});
@@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import React from 'react';
import { useEntity } from '@backstage/plugin-catalog-react';
import { makeStyles, Typography } from '@material-ui/core';
@@ -0,0 +1,127 @@
/*
* Copyright 2020 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 React from 'react';
import { setupServer } from 'msw/node';
import {
setupRequestMockHandlers,
TestApiRegistry,
} from '@backstage/test-utils';
import { ComponentEntity } from '@backstage/catalog-model';
import { renderInTestApp } from '@backstage/test-utils';
import { EntityVaultTable } from './EntityVaultTable';
import { ApiProvider, UrlPatternDiscovery } from '@backstage/core-app-api';
import { Secret, vaultApiRef, VaultClient } from '../../api';
import { rest } from 'msw';
describe('EntityVautTable', () => {
const server = setupServer();
setupRequestMockHandlers(server);
let apis: TestApiRegistry;
const mockBaseUrl = 'https://api-vault.com/api/vault';
const discoveryApi = UrlPatternDiscovery.compile(mockBaseUrl);
const entityOk: ComponentEntity = {
apiVersion: 'backstage.io/v1alpha1',
kind: 'Component',
metadata: {
name: 'test',
description: 'This is the description',
annotations: {
'vault.io/secrets-path': 'test/success',
},
},
spec: {
lifecycle: 'production',
owner: 'owner',
type: 'service',
},
};
const entityNotOk: ComponentEntity = {
apiVersion: 'backstage.io/v1alpha1',
kind: 'Component',
metadata: {
name: 'test',
description: 'This is the description',
annotations: {
'vault.io/secrets-path': 'test/error',
},
},
spec: {
lifecycle: 'production',
owner: 'owner',
type: 'service',
},
};
const mockSecretsResult: Secret[] = [
{
name: 'secret::one',
editUrl: `${mockBaseUrl}/ui/vault/secrets/secrets/edit/test/success/secret::one`,
showUrl: `${mockBaseUrl}/ui/vault/secrets/secrets/show/test/success/secret::one`,
},
{
name: 'secret::two',
editUrl: `${mockBaseUrl}/ui/vault/secrets/secrets/edit/test/success/secret::two`,
showUrl: `${mockBaseUrl}/ui/vault/secrets/secrets/show/test/success/secret::two`,
},
];
const setupHandlers = () => {
server.use(
rest.get(`${mockBaseUrl}/v1/secrets`, (req, res, ctx) => {
const path = req.url.searchParams.get('path');
if (path === 'test/success') {
return res(ctx.json(mockSecretsResult));
} else if (path === 'test/error') {
return res(ctx.json([]));
}
return res(ctx.status(400));
}),
);
};
beforeEach(() => {
apis = TestApiRegistry.from([
vaultApiRef,
new VaultClient({ discoveryApi }),
]);
});
it('should render secrets', async () => {
setupHandlers();
const rendered = await renderInTestApp(
<ApiProvider apis={apis}>
<EntityVaultTable entity={entityOk} />
</ApiProvider>,
);
expect(await rendered.findAllByText(/secret::one/)).toBeDefined();
expect(await rendered.findAllByText(/secret::two/)).toBeDefined();
});
it('should render no secrets found', async () => {
setupHandlers();
const rendered = await renderInTestApp(
<ApiProvider apis={apis}>
<EntityVaultTable entity={entityNotOk} />
</ApiProvider>,
);
expect(rendered.getByText(/No secrets found/)).toBeInTheDocument();
});
});