mirror of
https://github.com/balena-os/balena-supervisor.git
synced 2024-12-23 15:32:24 +00:00
Add POST /v1/regenerate-api-key unit tests
Signed-off-by: Christina Wang <christina@balena.io>
This commit is contained in:
parent
6e5c553c3f
commit
f748c1a8e7
@ -662,5 +662,68 @@ describe('SupervisorAPI [V1 Endpoints]', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('POST /v1/regenerate-api-key', () => {
|
||||
it('returns a valid new API key', async () => {
|
||||
const refreshKeySpy: SinonSpy = spy(apiKeys, 'refreshKey');
|
||||
|
||||
let newKey: string = '';
|
||||
|
||||
await request
|
||||
.post('/v1/regenerate-api-key')
|
||||
.set('Accept', 'application/json')
|
||||
.set('Authorization', `Bearer ${apiKeys.cloudApiKey}`)
|
||||
.expect(sampleResponses.V1.POST['/regenerate-api-key'].statusCode)
|
||||
.then((response) => {
|
||||
expect(response.body).to.deep.equal(
|
||||
sampleResponses.V1.POST['/regenerate-api-key'].body,
|
||||
);
|
||||
expect(response.text).to.equal(apiKeys.cloudApiKey);
|
||||
newKey = response.text;
|
||||
expect(refreshKeySpy.callCount).to.equal(1);
|
||||
});
|
||||
|
||||
// Ensure persistence with future calls
|
||||
await request
|
||||
.post('/v1/blink')
|
||||
.set('Accept', 'application/json')
|
||||
.set('Authorization', `Bearer ${newKey}`)
|
||||
.expect(sampleResponses.V1.POST['/blink'].statusCode);
|
||||
|
||||
refreshKeySpy.restore();
|
||||
});
|
||||
|
||||
it('expires old API key after generating new key', async () => {
|
||||
const oldKey: string = apiKeys.cloudApiKey;
|
||||
|
||||
await request
|
||||
.post('/v1/regenerate-api-key')
|
||||
.set('Accept', 'application/json')
|
||||
.set('Authorization', `Bearer ${oldKey}`)
|
||||
.expect(sampleResponses.V1.POST['/regenerate-api-key'].statusCode);
|
||||
|
||||
await request
|
||||
.post('/v1/restart')
|
||||
.set('Accept', 'application/json')
|
||||
.set('Authorization', `Bearer ${oldKey}`)
|
||||
.expect(401);
|
||||
});
|
||||
|
||||
it('communicates the new API key to balena API', async () => {
|
||||
const reportStateSpy: SinonSpy = spy(deviceState, 'reportCurrentState');
|
||||
|
||||
await request
|
||||
.post('/v1/regenerate-api-key')
|
||||
.set('Accept', 'application/json')
|
||||
.set('Authorization', `Bearer ${apiKeys.cloudApiKey}`)
|
||||
.then(() => {
|
||||
expect(reportStateSpy.callCount).to.equal(1);
|
||||
// Further reportCurrentState tests should be in 05-device-state.spec.ts,
|
||||
// but its test case seems to currently be skipped until interface redesign
|
||||
});
|
||||
|
||||
reportStateSpy.restore();
|
||||
});
|
||||
});
|
||||
|
||||
// TODO: add tests for V1 endpoints
|
||||
});
|
||||
|
@ -69,6 +69,10 @@
|
||||
"statusCode": 200,
|
||||
"body": {},
|
||||
"text": "OK"
|
||||
},
|
||||
"/regenerate-api-key": {
|
||||
"statusCode": 200,
|
||||
"body": {}
|
||||
}
|
||||
}
|
||||
},
|
||||
|
Loading…
Reference in New Issue
Block a user