5
0
Fork 0
mirror of https://github.com/hashicorp/vault-action.git synced 2025-11-14 18:13:45 +00:00

Set default role, stop mocks conflicting across tests, cache generated test JWT

This commit is contained in:
Tom Proctor 2022-04-07 15:29:19 +01:00
parent 4bd533450e
commit e3eff1b249
No known key found for this signature in database
GPG key ID: 9AA1838744D16345

View file

@ -51,6 +51,9 @@ function mockGithubOIDCResponse(aud= "https://github.com/hashicorp/vault-action"
return rsasign.KJUR.jws.JWS.sign(alg, JSON.stringify(header), JSON.stringify(payload), decryptedKey); return rsasign.KJUR.jws.JWS.sign(alg, JSON.stringify(header), JSON.stringify(payload), decryptedKey);
} }
// The sign call inside this function takes a while to run, so cache the default JWT in a constant.
const defaultGithubJwt = mockGithubOIDCResponse();
describe('jwt auth', () => { describe('jwt auth', () => {
beforeAll(async () => { beforeAll(async () => {
// Verify Connection // Verify Connection
@ -99,7 +102,8 @@ describe('jwt auth', () => {
'X-Vault-Token': 'testtoken', 'X-Vault-Token': 'testtoken',
}, },
json: { json: {
jwt_validation_pubkeys: publicRsaKey jwt_validation_pubkeys: publicRsaKey,
default_role: "default"
} }
}); });
@ -198,20 +202,20 @@ describe('jwt auth', () => {
.calledWith('jwtPrivateKey') .calledWith('jwtPrivateKey')
.mockReturnValueOnce(''); .mockReturnValueOnce('');
when(core.getInput)
.calledWith('role')
.mockReturnValueOnce('default');
when(core.getInput) when(core.getInput)
.calledWith('secrets') .calledWith('secrets')
.mockReturnValueOnce('secret/data/test secret'); .mockReturnValueOnce('secret/data/test secret');
when(core.getIDToken)
.calledWith()
.mockReturnValueOnce(mockGithubOIDCResponse());
}); });
it('successfully authenticates', async () => { it('successfully authenticates', async () => {
when(core.getInput)
.calledWith('role')
.mockReturnValueOnce('default');
when(core.getIDToken)
.calledWith()
.mockReturnValueOnce(defaultGithubJwt);
await exportSecrets(); await exportSecrets();
expect(core.exportVariable).toBeCalledWith('SECRET', 'SUPERSECRET'); expect(core.exportVariable).toBeCalledWith('SECRET', 'SUPERSECRET');
}); });
@ -238,6 +242,10 @@ describe('jwt auth', () => {
.calledWith('role') .calledWith('role')
.mockReturnValueOnce(null); .mockReturnValueOnce(null);
when(core.getIDToken)
.calledWith()
.mockReturnValueOnce(defaultGithubJwt);
await exportSecrets(); await exportSecrets();
expect(core.exportVariable).toBeCalledWith('SECRET', 'SUPERSECRET'); expect(core.exportVariable).toBeCalledWith('SECRET', 'SUPERSECRET');
}) })