Write POST /v1/blink unit test

Signed-off-by: Christina Wang <christina@balena.io>
This commit is contained in:
Christina Wang 2021-02-02 19:02:06 +09:00
parent c96d732131
commit 6e5c553c3f
No known key found for this signature in database
GPG Key ID: 7C3ED0230F440835
2 changed files with 53 additions and 1 deletions

View File

@ -1,7 +1,14 @@
import * as _ from 'lodash';
import * as Bluebird from 'bluebird';
import { expect } from 'chai';
import { stub, spy, SinonStub, SinonSpy } from 'sinon';
import {
stub,
spy,
useFakeTimers,
SinonStub,
SinonSpy,
SinonFakeTimers,
} from 'sinon';
import * as supertest from 'supertest';
import * as appMock from './lib/application-state-mock';
@ -18,6 +25,7 @@ import * as dbus from '../src//lib/dbus';
import * as updateLock from '../src/lib/update-lock';
import * as TargetState from '../src/device-state/target-state';
import * as targetStateCache from '../src/device-state/target-state-cache';
import blink = require('../src/lib/blink');
import { UpdatesLockedError } from '../src/lib/errors';
@ -615,5 +623,44 @@ describe('SupervisorAPI [V1 Endpoints]', () => {
});
});
describe('POST /v1/blink', () => {
// Further blink function-specific testing located in 07-blink.spec.ts
it('responds with code 200 and empty body', async () => {
await request
.post('/v1/blink')
.set('Accept', 'application/json')
.set('Authorization', `Bearer ${apiKeys.cloudApiKey}`)
.expect(sampleResponses.V1.POST['/blink'].statusCode)
.then((response) => {
expect(response.body).to.deep.equal(
sampleResponses.V1.POST['/blink'].body,
);
expect(response.text).to.deep.equal(
sampleResponses.V1.POST['/blink'].text,
);
});
});
it('directs device to blink for 15000ms (hardcoded length)', async () => {
const blinkStartSpy: SinonSpy = spy(blink.pattern, 'start');
const blinkStopSpy: SinonSpy = spy(blink.pattern, 'stop');
const clock: SinonFakeTimers = useFakeTimers();
await request
.post('/v1/blink')
.set('Accept', 'application/json')
.set('Authorization', `Bearer ${apiKeys.cloudApiKey}`)
.then(() => {
expect(blinkStartSpy.callCount).to.equal(1);
clock.tick(15000);
expect(blinkStopSpy.callCount).to.equal(1);
});
blinkStartSpy.restore();
blinkStopSpy.restore();
clock.restore();
});
});
// TODO: add tests for V1 endpoints
});

View File

@ -64,6 +64,11 @@
"statusCode": 202,
"body": {},
"text": "OK"
},
"/blink": {
"statusCode": 200,
"body": {},
"text": "OK"
}
}
},