clean up AwsIamStrategy tests
Make it easier to stub and check the presign mock. Signed-off-by: Jamie Klassen <jamie.klassen@broadcom.com>
This commit is contained in:
@@ -20,12 +20,6 @@ import {
|
||||
} from '@backstage/plugin-kubernetes-common';
|
||||
import { AwsIamStrategy } from './AwsIamStrategy';
|
||||
|
||||
let presign = jest.fn(async () => ({
|
||||
hostname: 'https://example.com',
|
||||
query: {},
|
||||
path: '/asdf',
|
||||
}));
|
||||
|
||||
const credsManager = {
|
||||
getCredentialProvider: async () => ({
|
||||
sdkCredentialProvider: {
|
||||
@@ -40,12 +34,16 @@ jest.mock('@backstage/integration-aws-node', () => ({
|
||||
},
|
||||
}));
|
||||
|
||||
const config = new ConfigReader({});
|
||||
const signer = {
|
||||
presign: jest.fn().mockResolvedValue({
|
||||
hostname: 'https://example.com',
|
||||
query: {},
|
||||
path: '/asdf',
|
||||
}),
|
||||
};
|
||||
|
||||
jest.mock('@aws-sdk/signature-v4', () => ({
|
||||
SignatureV4: jest.fn().mockImplementation(() => ({
|
||||
presign,
|
||||
})),
|
||||
SignatureV4: jest.fn().mockImplementation(() => signer),
|
||||
}));
|
||||
|
||||
const fromTemporaryCredentials = jest.fn();
|
||||
@@ -55,9 +53,10 @@ jest.mock('@aws-sdk/credential-providers', () => ({
|
||||
},
|
||||
}));
|
||||
|
||||
describe('AwsIamStrategy tests', () => {
|
||||
beforeEach(() => {});
|
||||
it('returns a signed url for AWS credentials without assume role', async () => {
|
||||
describe('AwsIamStrategy#getCredential', () => {
|
||||
const config = new ConfigReader({});
|
||||
|
||||
it('returns a presigned url for cluster name', async () => {
|
||||
const strategy = new AwsIamStrategy({ config });
|
||||
|
||||
const credential = await strategy.getCredential({
|
||||
@@ -65,10 +64,17 @@ describe('AwsIamStrategy tests', () => {
|
||||
url: '',
|
||||
authMetadata: {},
|
||||
});
|
||||
|
||||
expect(credential).toEqual({
|
||||
type: 'bearer token',
|
||||
token: 'k8s-aws-v1.aHR0cHM6Ly9odHRwczovL2V4YW1wbGUuY29tL2FzZGY_',
|
||||
});
|
||||
expect(signer.presign).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
headers: expect.objectContaining({ 'x-k8s-aws-id': 'test-cluster' }),
|
||||
}),
|
||||
expect.anything(),
|
||||
);
|
||||
});
|
||||
|
||||
it('returns a signed url for AWS credentials with assume role', async () => {
|
||||
@@ -129,21 +135,17 @@ describe('AwsIamStrategy tests', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('When the credentials is failing', () => {
|
||||
beforeEach(() => {
|
||||
presign = jest.fn(async () => {
|
||||
throw new Error('no way');
|
||||
});
|
||||
});
|
||||
it('throws the right error', async () => {
|
||||
const strategy = new AwsIamStrategy({ config });
|
||||
await expect(
|
||||
strategy.getCredential({
|
||||
name: 'test-cluster',
|
||||
url: '',
|
||||
authMetadata: {},
|
||||
}),
|
||||
).rejects.toThrow('no way');
|
||||
});
|
||||
it('fails on signer error', () => {
|
||||
signer.presign.mockRejectedValue(new Error('no way'));
|
||||
|
||||
const strategy = new AwsIamStrategy({ config });
|
||||
|
||||
return expect(
|
||||
strategy.getCredential({
|
||||
name: 'test-cluster',
|
||||
url: '',
|
||||
authMetadata: {},
|
||||
}),
|
||||
).rejects.toThrow('no way');
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user