Skip to content

Commit

Permalink
Separate routes & actions, add tests for GET /v2/local/device-info
Browse files Browse the repository at this point in the history
Signed-off-by: Christina Ying Wang <[email protected]>
  • Loading branch information
cywang117 committed Dec 12, 2023
1 parent e9535cd commit 8112e51
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 7 deletions.
9 changes: 9 additions & 0 deletions src/device-api/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -518,3 +518,12 @@ export const getContainerIds = async (
throw new Error(`Could not find service with name '${serviceName}'`);
}
};

/**
* Get device type & arch
* Used by:
* - GET /v2/local/device-info
*/
export const getDeviceInfo = async () => {
return await config.getMany(['deviceType', 'deviceArch']);
};
11 changes: 4 additions & 7 deletions src/device-api/v2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -330,10 +330,7 @@ router.post('/v2/local/target-state', async (req, res) => {

router.get('/v2/local/device-info', async (_req, res) => {
try {
const { deviceType, deviceArch } = await config.getMany([
'deviceType',
'deviceArch',
]);
const { deviceType, deviceArch } = await actions.getDeviceInfo();

return res.status(200).json({
status: 'success',
Expand All @@ -342,10 +339,10 @@ router.get('/v2/local/device-info', async (_req, res) => {
deviceType,
},
});
} catch (e: any) {
res.status(500).json({
} catch (e: unknown) {
return res.status(500).json({
status: 'failed',
message: e.message,
message: (e as Error).message ?? e,
});
}
});
Expand Down
22 changes: 22 additions & 0 deletions test/integration/device-api/actions.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -992,3 +992,25 @@ describe('gets service container ids', () => {
}
});
});

describe('gets device type and arch', () => {
let configGetManyStub: SinonStub;
before(() => {
// @ts-expect-error
configGetManyStub = stub(config, 'getMany').resolves({
deviceType: 'test-type',
deviceArch: 'test-arch',
});
});

after(() => {
configGetManyStub.restore();
});

it('returns device type and arch', async () => {
expect(await actions.getDeviceInfo()).to.deep.equal({
deviceType: 'test-type',
deviceArch: 'test-arch',
});
});
});
35 changes: 35 additions & 0 deletions test/integration/device-api/v2.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -896,4 +896,39 @@ describe('device-api/v2', () => {
.expect(503);
});
});

describe('GET /v2/local/device-info', () => {
let getDeviceInfoStub: SinonStub;
beforeEach(() => {
getDeviceInfoStub = stub(actions, 'getDeviceInfo');
});
afterEach(() => {
getDeviceInfoStub.restore();
});

it('responds with 200 and device info', async () => {
getDeviceInfoStub.resolves({
deviceArch: 'aarch64',
deviceType: 'raspberrypi4-64',
});
await request(api)
.get('/v2/local/device-info')
.set('Authorization', `Bearer ${await deviceApi.getGlobalApiKey()}`)
.expect(200, {
status: 'success',
info: {
arch: 'aarch64',
deviceType: 'raspberrypi4-64',
},
});
});

it('responds with 500 if an error occurred', async () => {
getDeviceInfoStub.throws(new Error());
await request(api)
.get('/v2/local/device-info')
.set('Authorization', `Bearer ${await deviceApi.getGlobalApiKey()}`)
.expect(500);
});
});
});

0 comments on commit 8112e51

Please sign in to comment.