diff --git a/src/device-api/v1.js b/src/device-api/v1.js index 92958e96..2cdde800 100644 --- a/src/device-api/v1.js +++ b/src/device-api/v1.js @@ -28,7 +28,7 @@ export const createV1Api = function (router, applications) { return applications .getCurrentApp(appId) .then(function (app) { - let service = app?.app.services?.[0]; + let service = app?.services?.[0]; if (service == null) { return res.status(400).send('App not found'); } diff --git a/test/21-supervisor-api.spec.ts b/test/21-supervisor-api.spec.ts index d5d58c5d..e2a58ee2 100644 --- a/test/21-supervisor-api.spec.ts +++ b/test/21-supervisor-api.spec.ts @@ -95,7 +95,6 @@ describe('SupervisorAPI', () => { await request .get('/v1/healthy') .set('Accept', 'application/json') - .set('Authorization', `Bearer ${VALID_SECRET}`) .expect(sampleResponses.V1.GET['/healthy [2]'].statusCode) .then((response) => { expect(response.body).to.deep.equal( @@ -107,7 +106,40 @@ describe('SupervisorAPI', () => { }); }); }); + // TODO: add tests for V1 endpoints + describe('GET /v1/apps/:appId', () => { + it('returns information about a SPECIFIC application', async () => { + await request + .get('/v1/apps/2') + .set('Accept', 'application/json') + .set('Authorization', `Bearer ${VALID_SECRET}`) + .expect('Content-Type', /json/) + .expect(sampleResponses.V1.GET['/apps/2'].statusCode) + .then((response) => { + expect(response.body).to.deep.equal( + sampleResponses.V1.GET['/apps/2'].body, + ); + }); + }); + }); + + describe('POST /v1/apps/:appId/stop', () => { + it('stops a SPECIFIC application and returns a containerId', async () => { + await request + .post('/v1/apps/2/stop') + .set('Accept', 'application/json') + .set('Authorization', `Bearer ${VALID_SECRET}`) + .expect('Content-Type', /json/) + .expect(sampleResponses.V1.GET['/apps/2/stop'].statusCode) + .then((response) => { + expect(response.body).to.deep.equal( + sampleResponses.V1.GET['/apps/2/stop'].body, + ); + }); + }); + }); + describe('GET /v1/device', () => { it('returns MAC address', async () => { const response = await request diff --git a/test/data/device-api-responses.json b/test/data/device-api-responses.json index ff8ae954..1323e0fa 100644 --- a/test/data/device-api-responses.json +++ b/test/data/device-api-responses.json @@ -10,6 +10,22 @@ "statusCode": 500, "body": {}, "text": "Unhealthy" + }, + "/apps/2": { + "statusCode": 200, + "body": { + "appId": 2, + "containerId": "abc123", + "commit": "7fc9c5bea8e361acd49886fe6cc1e1cd", + "env": {}, + "releaseId": 77777 + } + }, + "/apps/2/stop": { + "statusCode": 200, + "body": { + "containerId": "abc123" + } } } }, diff --git a/test/lib/mocked-device-api.ts b/test/lib/mocked-device-api.ts index 1728d745..9bf75394 100644 --- a/test/lib/mocked-device-api.ts +++ b/test/lib/mocked-device-api.ts @@ -1,3 +1,4 @@ +import * as _ from 'lodash'; import { Router } from 'express'; import { fs } from 'mz'; @@ -40,6 +41,7 @@ const STUBBED_VALUES = { { appId: 2, imageId: 3333, + containerId: 'abc123', status: 'Running', releaseId: 77777, createdAt: new Date('2020-05-15T19:33:06.088Z'), @@ -140,7 +142,9 @@ function buildRoutes(appManager: ApplicationManager): Router { const originalNetGetAll = networkManager.getAllByAppId; const originalVolGetAll = volumeManager.getAllByAppId; +const originalSvcGetAppId = serviceManager.getAllByAppId; const originalSvcGetStatus = serviceManager.getStatus; + function setupStubs() { // @ts-expect-error Assigning to a RO property networkManager.getAllByAppId = async () => STUBBED_VALUES.networks; @@ -148,6 +152,9 @@ function setupStubs() { volumeManager.getAllByAppId = async () => STUBBED_VALUES.volumes; // @ts-expect-error Assigning to a RO property serviceManager.getStatus = async () => STUBBED_VALUES.services; + // @ts-expect-error Assigning to a RO property + serviceManager.getAllByAppId = async (appId) => + _.filter(STUBBED_VALUES.services, (service) => service.appId === appId); } function restoreStubs() { @@ -157,6 +164,8 @@ function restoreStubs() { volumeManager.getAllByAppId = originalVolGetAll; // @ts-expect-error Assigning to a RO property serviceManager.getStatus = originalSvcGetStatus; + // @ts-expect-error Assigning to a RO property + serviceManager.getAllByAppId = originalSvcGetAppId; } interface SupervisorAPIOpts {