From 76c6a104a95905efd858f941707930b98352d8f1 Mon Sep 17 00:00:00 2001 From: Ralf Aron Date: Mon, 11 Nov 2024 14:51:48 +0100 Subject: [PATCH] fix: testing EndpointsController --- .../app/controller/endpoints-controller.ts | 10 +-- .../controller/endpoints-controller.spec.ts | 68 ++++++++++++++++--- 2 files changed, 65 insertions(+), 13 deletions(-) diff --git a/projects/aas-server/src/app/controller/endpoints-controller.ts b/projects/aas-server/src/app/controller/endpoints-controller.ts index e55a31f7..e0e1d3fa 100644 --- a/projects/aas-server/src/app/controller/endpoints-controller.ts +++ b/projects/aas-server/src/app/controller/endpoints-controller.ts @@ -69,8 +69,8 @@ export class EndpointsController extends AASController { */ @Get('{name}/documents/count') @Security('bearerAuth', ['guest']) - @OperationId('getEndpointCount') - public async getEndpointCount(@Path() name: string): Promise<{ count: number }> { + @OperationId('getDocumentCount') + public async getDocumentCount(@Path() name: string): Promise<{ count: number }> { try { this.logger.start('getCount'); return { count: await this.aasProvider.getCountAsync(decodeBase64Url(name)) }; @@ -130,7 +130,7 @@ export class EndpointsController extends AASController { } /** - * @summary Resets the AASServer container configuration. + * @summary Resets the AAS endpoint configuration. */ @Delete('') @Security('bearerAuth', ['editor']) @@ -148,9 +148,9 @@ export class EndpointsController extends AASController { * @summary Starts a scan of the AAS endpoint with the specified name. * @param name The endpoint name. */ - @Post('{name}/scan') + @Put('{name}/scan') @Security('bearerAuth', ['editor']) - @OperationId('reset') + @OperationId('startEndpointScan') public async startEndpointScan(@Path() name: string): Promise { try { this.logger.start('startEndpointScan'); diff --git a/projects/aas-server/src/test/controller/endpoints-controller.spec.ts b/projects/aas-server/src/test/controller/endpoints-controller.spec.ts index 74539cc7..a4d45df6 100644 --- a/projects/aas-server/src/test/controller/endpoints-controller.spec.ts +++ b/projects/aas-server/src/test/controller/endpoints-controller.spec.ts @@ -46,9 +46,13 @@ describe('EndpointsController', function () { aasProvider = createSpyObj([ 'getEndpoints', + 'getEndpointCount', 'addEndpointAsync', + 'updateEndpointAsync', 'removeEndpointAsync', + 'getCountAsync', 'resetAsync', + 'startEndpointScan', ]); authentication = createSpyObj(['checkAsync']); @@ -70,7 +74,7 @@ describe('EndpointsController', function () { app.use(errorHandler); }); - it('getEndpoints: /api/v1/endpoints', async function () { + it('GET: /api/v1/endpoints', async () => { const endpoints: AASEndpoint = { name: 'Test', url: 'http://localhost:1234', @@ -79,18 +83,36 @@ describe('EndpointsController', function () { aasProvider.getEndpoints.mockResolvedValue([endpoints]); const response = await request(app).get('/api/v1/endpoints').set('Authorization', `Bearer ${getToken()}`); - expect(response.statusCode).toBe(200); expect(response.body).toEqual([endpoints]); expect(aasProvider.getEndpoints).toHaveBeenCalled(); }); - it('POST: /api/v1/endpoints/:name', async function () { + it('GET: /api/v1/endpoints/count', async () => { + aasProvider.getEndpointCount.mockResolvedValue(42); + const response = await request(app).get('/api/v1/endpoints/count').set('Authorization', `Bearer ${getToken()}`); + expect(response.statusCode).toBe(200); + expect(response.body).toEqual({ count: 42 }); + expect(aasProvider.getEndpointCount).toHaveBeenCalled(); + }); + + it('GET: /api/v1/endpoints/{name}/documents/count', async () => { + aasProvider.getCountAsync.mockResolvedValue(42); + const response = await request(app) + .get('/api/v1/endpoints/U2FtcGxlcw/documents/count') + .set('Authorization', `Bearer ${getToken()}`); + + expect(response.statusCode).toBe(200); + expect(response.body).toEqual({ count: 42 }); + expect(aasProvider.getCountAsync).toHaveBeenCalledWith('Samples'); + }); + + it('POST: /api/v1/endpoints/{name}', async () => { const endpoint: AASEndpoint = { name: 'Samples', url: 'file:///assets/samples', type: 'FileSystem' }; aasProvider.addEndpointAsync.mockResolvedValue(); auth.hasUserAsync.mockResolvedValue(true); const response = await request(app) - .post('/api/v1/endpoints/samples') + .post('/api/v1/endpoints/U2FtcGxlcw') .set('Authorization', `Bearer ${getToken('John')}`) .send(endpoint); @@ -98,18 +120,37 @@ describe('EndpointsController', function () { expect(aasProvider.addEndpointAsync).toHaveBeenCalled(); }); - it('DELETE: /api/v1/endpoints/:name', async function () { + it('PUT: /api/v1/endpoints/{name}', async () => { + const endpoint: AASEndpoint = { + name: 'Samples', + url: 'file:///assets/samples', + type: 'FileSystem', + schedule: { type: 'manual' }, + }; + + aasProvider.updateEndpointAsync.mockResolvedValue(); + auth.hasUserAsync.mockResolvedValue(true); + const response = await request(app) + .put('/api/v1/endpoints/U2FtcGxlcw') + .set('Authorization', `Bearer ${getToken('John')}`) + .send(endpoint); + + expect(response.statusCode).toBe(204); + expect(aasProvider.updateEndpointAsync).toHaveBeenCalledWith('Samples', endpoint); + }); + + it('DELETE: /api/v1/endpoints/{name}', async () => { aasProvider.removeEndpointAsync.mockReturnValue(new Promise(resolve => resolve())); auth.hasUserAsync.mockReturnValue(new Promise(resolve => resolve(true))); const response = await request(app) - .delete('/api/v1/endpoints/samples') + .delete('/api/v1/endpoints/U2FtcGxlcw') .set('Authorization', `Bearer ${getToken('John')}`); expect(response.statusCode).toBe(204); - expect(aasProvider.removeEndpointAsync).toHaveBeenCalled(); + expect(aasProvider.removeEndpointAsync).toHaveBeenCalledWith('Samples'); }); - it('reset: /api/v1/endpoints', async function () { + it('DELETE: /api/v1/endpoints', async () => { auth.hasUserAsync.mockReturnValue(new Promise(resolve => resolve(true))); aasProvider.resetAsync.mockReturnValue(new Promise(resolve => resolve())); const response = await request(app) @@ -119,4 +160,15 @@ describe('EndpointsController', function () { expect(response.statusCode).toBe(204); expect(aasProvider.resetAsync).toHaveBeenCalled(); }); + + it('PUT: /api/v1/endpoints/{name}/scan', async () => { + auth.hasUserAsync.mockReturnValue(new Promise(resolve => resolve(true))); + aasProvider.startEndpointScan.mockReturnValue(new Promise(resolve => resolve())); + const response = await request(app) + .put('/api/v1/endpoints/U2FtcGxlcw/scan') + .set('Authorization', `Bearer ${getToken('John')}`); + + expect(response.statusCode).toBe(204); + expect(aasProvider.startEndpointScan).toHaveBeenCalledWith('Samples'); + }); });