Skip to content

Commit

Permalink
fix: testing EndpointsController
Browse files Browse the repository at this point in the history
  • Loading branch information
raronpxcsw committed Nov 11, 2024
1 parent c1dab60 commit 76c6a10
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 13 deletions.
10 changes: 5 additions & 5 deletions projects/aas-server/src/app/controller/endpoints-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)) };
Expand Down Expand Up @@ -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'])
Expand All @@ -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<void> {
try {
this.logger.start('startEndpointScan');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,13 @@ describe('EndpointsController', function () {

aasProvider = createSpyObj<AASProvider>([
'getEndpoints',
'getEndpointCount',
'addEndpointAsync',
'updateEndpointAsync',
'removeEndpointAsync',
'getCountAsync',
'resetAsync',
'startEndpointScan',
]);

authentication = createSpyObj<Authentication>(['checkAsync']);
Expand All @@ -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',
Expand All @@ -79,37 +83,74 @@ 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);

expect(response.statusCode).toBe(204);
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<void>(resolve => resolve()));
auth.hasUserAsync.mockReturnValue(new Promise<boolean>(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<boolean>(resolve => resolve(true)));
aasProvider.resetAsync.mockReturnValue(new Promise<void>(resolve => resolve()));
const response = await request(app)
Expand All @@ -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<boolean>(resolve => resolve(true)));
aasProvider.startEndpointScan.mockReturnValue(new Promise<void>(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');
});
});

0 comments on commit 76c6a10

Please sign in to comment.