Skip to content

Commit

Permalink
Added Tests for Exceptions & Pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
maganuk committed Feb 19, 2024
1 parent 3624453 commit f55b62d
Showing 1 changed file with 116 additions and 80 deletions.
196 changes: 116 additions & 80 deletions tests/sdk.spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
/* eslint-disable @typescript-eslint/no-non-null-assertion */
/* eslint-disable import/no-extraneous-dependencies */
import nock from 'nock';
import {
MonoCloudAdminClient,
ModelStateException,
ServerErrorException,
BadRequestException,
MonoCloudBadRequestException,
MonoCloudErrorCodeValidationException,
MonoCloudKeyValidationException,
MonoCloudServerException,
CreateClientRequest,
RefreshTokenExpirationTypes,
} from '../src';
Expand Down Expand Up @@ -93,140 +95,174 @@ describe('MonoCloud Admin SDK Tests', () => {

const result = await client.clients.getAllClients();

const pageData = JSON.parse(result.headers['x-pagination']);
expect(result.result.length).toBe(2);
expect(result.pageData.total_count).toBe(20);
expect(result.pageData.page_size).toBe(2);
expect(result.pageData.current_page).toBe(3);
expect(result.pageData.has_next).toBe(true);
expect(result.pageData.has_previous).toBe(true);
});

test('Get with paging should handle no pagination header', async () => {
nockInst.get('/api/clients').reply(200, [{}, {}]);

const result = await client.clients.getAllClients();
expect(result.result.length).toBe(2);
expect(pageData.total_count).toBe(20);
expect(pageData.page_size).toBe(2);
expect(pageData.current_page).toBe(3);
expect(pageData.has_next).toBe(true);
expect(pageData.has_previous).toBe(true);
expect(result.pageData.total_count).toBe(0);
expect(result.pageData.page_size).toBe(0);
expect(result.pageData.current_page).toBe(0);
expect(result.pageData.has_next).toBe(false);
expect(result.pageData.has_previous).toBe(false);
});

test('Identity error should handle correctly', async () => {
nockInst.post('/api/clients').reply(422, {
type: 'https://httpstatuses.io/422#identity-validation-error',
title: 'Unprocessable Entity',
status: 422,
errors: [
{
code: 'PasswordTooShort',
description: 'Passwords must be at least 8 characters.',
},
{
code: 'PasswordRequiresNonAlphanumeric',
description:
'Passwords must have at least one non alphanumeric character.',
},
{
code: 'PasswordRequiresUpper',
description: "Passwords must have at least one uppercase ('A'-'Z').",
},
],
traceId: '00-cd3f24e893675e2dae242875e99e7c85-296286fe1c04c085-01',
});
nockInst.post('/api/clients').reply(
422,
{
type: 'https://httpstatuses.io/422#identity-validation-error',
title: 'Unprocessable Entity',
status: 422,
errors: [
{
code: 'PasswordTooShort',
description: 'Passwords must be at least 8 characters.',
},
{
code: 'PasswordRequiresNonAlphanumeric',
description:
'Passwords must have at least one non alphanumeric character.',
},
{
code: 'PasswordRequiresUpper',
description:
"Passwords must have at least one uppercase ('A'-'Z').",
},
],
traceId: '00-cd3f24e893675e2dae242875e99e7c85-296286fe1c04c085-01',
},
{ 'Content-Type': 'application/problem+json' }
);

try {
await client.clients.createClient({} as any);
throw new Error('Invalid');
} catch (error: unknown) {
expect(error).toBeInstanceOf(ModelStateException);
const err = error as ModelStateException;
expect(err.message).toBe('Unprocessable Entity');
expect(error).toBeInstanceOf(MonoCloudErrorCodeValidationException);
const err = error as MonoCloudErrorCodeValidationException;
expect(err.message).toContain('Unprocessable Entity');
expect(err.errors.length).toBe(3);
expect(err.errors[0].code).toBe('PasswordTooShort');
expect(err.errors[1].code).toBe('PasswordRequiresNonAlphanumeric');
expect(err.errors[2].code).toBe('PasswordRequiresUpper');
expect(err.raw).not.toBeFalsy();
expect(err.raw.data.type).toBe(
expect(err.response).not.toBeFalsy();
expect(err.response!.type).toBe(
'https://httpstatuses.io/422#identity-validation-error'
);
expect(err.raw.data.title).toBe('Unprocessable Entity');
expect(err.raw.data.status).toBe(422);
expect(err.raw.data.traceId).toBe(
expect(err.response!.title).toBe('Unprocessable Entity');
expect(err.response!.status).toBe(422);
expect(err.response!.traceId).toBe(
'00-cd3f24e893675e2dae242875e99e7c85-296286fe1c04c085-01'
);
}
});

test('Key validation error should handle correctly', async () => {
nockInst.post('/api/clients').reply(422, {
type: 'https://httpstatuses.io/422#validation-error',
title: 'Unprocessable Entity',
status: 422,
errors: {
client_name: ['should not be empty'],
description: ['should not be more than 200 characters'],
nockInst.post('/api/clients').reply(
422,
{
type: 'https://httpstatuses.io/422#validation-error',
title: 'Unprocessable Entity',
status: 422,
errors: {
client_name: ['should not be empty'],
description: ['should not be more than 200 characters'],
},
traceId: '00-cd3f24e893675e2dae242875e99e7c85-296286fe1c04c085-01',
},
traceId: '00-cd3f24e893675e2dae242875e99e7c85-296286fe1c04c085-01',
});
{ 'Content-Type': 'application/problem+json' }
);

try {
await client.clients.createClient({} as CreateClientRequest);
throw new Error('Invalid');
} catch (error: unknown) {
expect(error).toBeInstanceOf(ModelStateException);
const err = error as ModelStateException<CreateClientRequest>;
expect(err.message).toBe('Unprocessable Entity');
expect(error).toBeInstanceOf(MonoCloudKeyValidationException);
const err = error as MonoCloudKeyValidationException;
expect(err.message).toContain('Unprocessable Entity');
expect(Object.keys(err.errors).length).toBe(2);
expect(err.errors.client_name?.[0]).toBe('should not be empty');
expect(err.errors.description?.[0]).toBe(
'should not be more than 200 characters'
);
expect(err.raw).not.toBeFalsy();
expect(err.raw.data.type).toBe(
expect(err.response).not.toBeFalsy();
expect(err.response!.type).toBe(
'https://httpstatuses.io/422#validation-error'
);
expect(err.raw.data.status).toBe(422);
expect(err.raw.data.title).toBe('Unprocessable Entity');
expect(err.raw.data.traceId).toBe(
expect(err.response!.status).toBe(422);
expect(err.response!.title).toBe('Unprocessable Entity');
expect(err.response!.traceId).toBe(
'00-cd3f24e893675e2dae242875e99e7c85-296286fe1c04c085-01'
);
}
});

test('Internal server error should handle correctly', async () => {
nockInst.post('/api/clients').reply(500, {
type: 'https://httpstatuses.io/500',
title: 'Internal Server Error',
status: 500,
detail: 'Internal Server Error Detail',
traceId: '00-b2ceddefca0cf958ed678f144872e3c7-d0b2da5c8fe32598-01',
});
nockInst.post('/api/clients').reply(
500,
{
type: 'https://httpstatuses.io/500',
title: 'Internal Server Error',
status: 500,
detail: 'Internal Server Error Detail',
traceId: '00-b2ceddefca0cf958ed678f144872e3c7-d0b2da5c8fe32598-01',
},
{ 'Content-Type': 'application/problem+json' }
);

try {
await client.clients.createClient({} as CreateClientRequest);
throw new Error('Invalid');
} catch (error: unknown) {
expect(error).toBeInstanceOf(ServerErrorException);
const err = error as ServerErrorException;
expect(error).toBeInstanceOf(MonoCloudServerException);
const err = error as MonoCloudServerException;
expect(err.message).toBe('Internal Server Error');
expect(err.status).toBe(500);
expect(err.response).not.toBeFalsy();
expect(err.response!.type).toBe('https://httpstatuses.io/500');
expect(err.response!.title).toBe('Internal Server Error');
expect(err.response!.status).toBe(500);
expect(err.response!.detail).toBe('Internal Server Error Detail');
expect(err.response!.traceId).toBe(
'00-b2ceddefca0cf958ed678f144872e3c7-d0b2da5c8fe32598-01'
);
}
});

test('Bad request error should handle correctly', async () => {
nockInst.post('/api/clients').reply(400, {
type: 'https://httpstatuses.io/400',
title: 'Bad Request',
status: 400,
detail: 'Bad Request Detail',
traceId: '00-2e0cd141be28223b233dd3907cbe58b4-2ba9f9375b4b78e0-01',
});
nockInst.post('/api/clients').reply(
400,
{
type: 'https://httpstatuses.io/400',
title: 'Bad Request',
status: 400,
detail: 'Bad Request Detail',
traceId: '00-2e0cd141be28223b233dd3907cbe58b4-2ba9f9375b4b78e0-01',
},
{ 'Content-Type': 'application/problem+json' }
);

try {
await client.clients.createClient({} as CreateClientRequest);
throw new Error('Invalid');
} catch (error: unknown) {
expect(error).toBeInstanceOf(BadRequestException);
const err = error as BadRequestException;
expect(error).toBeInstanceOf(MonoCloudBadRequestException);
const err = error as MonoCloudBadRequestException;
expect(err.message).toBe('Bad Request');
expect(err.raw).not.toBeFalsy();
expect(err.raw.data.type).toBe('https://httpstatuses.io/400');
expect(err.raw.data.title).toBe('Bad Request');
expect(err.raw.data.status).toBe(400);
expect(err.raw.data.detail).toBe('Bad Request Detail');
expect(err.raw.data.traceId).toBe(
expect(err.response).not.toBeFalsy();
expect(err.response!.type).toBe('https://httpstatuses.io/400');
expect(err.response!.title).toBe('Bad Request');
expect(err.response!.status).toBe(400);
expect(err.response!.detail).toBe('Bad Request Detail');
expect(err.response!.traceId).toBe(
'00-2e0cd141be28223b233dd3907cbe58b4-2ba9f9375b4b78e0-01'
);
}
Expand Down

0 comments on commit f55b62d

Please sign in to comment.