From a68ea4f63e29110e2012d7ea0bd91c2339dd3c69 Mon Sep 17 00:00:00 2001 From: StevanFreeborn Date: Mon, 6 Feb 2023 22:16:43 -0600 Subject: [PATCH] feat: implement deleteFileById method --- src/models/OnspringClient.ts | 25 +++++ tests/OnspringClient.spec.ts | 208 +++++++++++++++++++++++++++++++++++ 2 files changed, 233 insertions(+) diff --git a/src/models/OnspringClient.ts b/src/models/OnspringClient.ts index e824900..193d792 100644 --- a/src/models/OnspringClient.ts +++ b/src/models/OnspringClient.ts @@ -253,6 +253,22 @@ export class OnspringClient { return apiResponse.asCreatedWithIdResponseType(); } + public async deleteFileById( + recordId: number, + fieldId: number, + fileId: number + ): Promise> { + const endpoint = EndpointFactory.getDeleteFileByIdEndpoint( + recordId, + fieldId, + fileId + ); + + const apiResponse = await this.delete(endpoint); + + return apiResponse; + } + /** * @method get - Makes a GET request to the specified endpoint. * @param {string} endpoint - The endpoint that will be used to make the request. @@ -284,4 +300,13 @@ export class OnspringClient { const apiResponse = ApiResponseFactory.getApiResponse(response); return apiResponse; } + + private async delete( + endpoint: string, + config: AxiosRequestConfig = {} + ): Promise> { + const response = await this._client.delete(endpoint, config); + const apiResponse = ApiResponseFactory.getApiResponse(response); + return apiResponse; + } } diff --git a/tests/OnspringClient.spec.ts b/tests/OnspringClient.spec.ts index be21578..2486ee6 100644 --- a/tests/OnspringClient.spec.ts +++ b/tests/OnspringClient.spec.ts @@ -1900,4 +1900,212 @@ describe('OnspringClient', function () { expect(result.data).to.be.null; }); }); + + describe('deleteFileById', function () { + it('should be defined', function () { + expect(OnspringClient.prototype.deleteFileById).to.not.be.undefined; + }); + + it('should be a function', function () { + expect(OnspringClient.prototype.deleteFileById).to.be.a('function'); + }); + + it('should return a promise', function () { + expect( + new OnspringClient(baseUrl, apiKey).deleteFileById(1, 1, 1) + ).to.be.instanceOf(Promise); + }); + + it('should return a promise that resolves to an api response when request is successful', async function () { + const client = new OnspringClient(baseUrl, apiKey); + + const mockAxiosClient = axios.create({ + baseURL: baseUrl, + headers: { + 'x-apikey': apiKey, + 'x-api-version': '2', + }, + }); + + sinon.stub(mockAxiosClient, 'delete').returns( + Promise.resolve({ + status: 204, + statusText: 'No Content', + headers: {}, + config: {} as InternalAxiosRequestConfig, + } as AxiosResponse) + ); + + sinon.stub(client, '_client' as any).value(mockAxiosClient); + + const result = await client.deleteFileById(1, 1, 1); + + expect(result).to.be.instanceOf(ApiResponse); + expect(result).to.have.property('statusCode', 204); + expect(result).to.have.property('isSuccessful', true); + expect(result).to.have.property('message', ''); + expect(result).to.have.property('data', undefined); + }); + + it('should return a promise that resolves to an api response when request receives a 400 response', async function () { + const client = new OnspringClient(baseUrl, apiKey); + + const mockAxiosClient = axios.create({ + baseURL: baseUrl, + headers: { + 'x-apikey': apiKey, + 'x-api-version': '2', + }, + }); + + sinon.stub(mockAxiosClient, 'delete').returns( + Promise.resolve({ + status: 400, + statusText: 'Bad Request', + data: { field: ['Field requested is not a file type field.'] }, + headers: {}, + config: {} as InternalAxiosRequestConfig, + } as AxiosResponse) + ); + + sinon.stub(client, '_client' as any).value(mockAxiosClient); + + const result = await client.deleteFileById(1, 1, 1); + + expect(result).to.be.instanceOf(ApiResponse); + expect(result).to.have.property('statusCode', 400); + expect(result).to.have.property('isSuccessful', false); + expect(result).to.have.property( + 'message', + '{"field":["Field requested is not a file type field."]}' + ); + expect(result.data).to.be.null; + }); + + it('should return a promise that resolves to an api response when request receives a 401 response', async function () { + const client = new OnspringClient(baseUrl, apiKey); + + const mockAxiosClient = axios.create({ + baseURL: baseUrl, + headers: { + 'x-apikey': apiKey, + 'x-api-version': '2', + }, + }); + + sinon.stub(mockAxiosClient, 'delete').returns( + Promise.resolve({ + status: 401, + statusText: 'Unauthorized', + headers: {}, + config: {} as InternalAxiosRequestConfig, + } as AxiosResponse) + ); + + sinon.stub(client, '_client' as any).value(mockAxiosClient); + + const result = await client.deleteFileById(1, 1, 1); + + expect(result).to.be.instanceOf(ApiResponse); + expect(result).to.have.property('statusCode', 401); + expect(result).to.have.property('isSuccessful', false); + expect(result.message).to.be.undefined; + expect(result.data).to.be.null; + }); + + it('should return a promise that resolves to an api response when request receives a 403 response', async function () { + const client = new OnspringClient(baseUrl, apiKey); + + const mockAxiosClient = axios.create({ + baseURL: baseUrl, + headers: { + 'x-apikey': apiKey, + 'x-api-version': '2', + }, + }); + + sinon.stub(mockAxiosClient, 'delete').returns( + Promise.resolve({ + status: 403, + statusText: 'Forbidden', + data: { message: 'Forbidden' }, + headers: {}, + config: {} as InternalAxiosRequestConfig, + } as AxiosResponse) + ); + + sinon.stub(client, '_client' as any).value(mockAxiosClient); + + const result = await client.deleteFileById(1, 1, 1); + + expect(result).to.be.instanceOf(ApiResponse); + expect(result).to.have.property('statusCode', 403); + expect(result).to.have.property('isSuccessful', false); + expect(result).to.have.property('message', 'Forbidden'); + expect(result.data).to.be.null; + }); + + it('should return a promise that resolves to an api response when request receives a 404 response', async function () { + const client = new OnspringClient(baseUrl, apiKey); + + const mockAxiosClient = axios.create({ + baseURL: baseUrl, + headers: { + 'x-apikey': apiKey, + 'x-api-version': '2', + }, + }); + + sinon.stub(mockAxiosClient, 'delete').returns( + Promise.resolve({ + status: 404, + statusText: 'Not Found', + data: { message: 'Not Found' }, + headers: {}, + config: {} as InternalAxiosRequestConfig, + } as AxiosResponse) + ); + + sinon.stub(client, '_client' as any).value(mockAxiosClient); + + const result = await client.deleteFileById(1, 1, 1); + + expect(result).to.be.instanceOf(ApiResponse); + expect(result).to.have.property('statusCode', 404); + expect(result).to.have.property('isSuccessful', false); + expect(result).to.have.property('message', 'Not Found'); + expect(result.data).to.be.null; + }); + + it('should return a promise that resolves to an api response when request receives a 500 response', async function () { + const client = new OnspringClient(baseUrl, apiKey); + + const mockAxiosClient = axios.create({ + baseURL: baseUrl, + headers: { + 'x-apikey': apiKey, + 'x-api-version': '2', + }, + }); + + sinon.stub(mockAxiosClient, 'delete').returns( + Promise.resolve({ + status: 500, + statusText: 'Internal Server Error', + headers: {}, + config: {} as InternalAxiosRequestConfig, + } as AxiosResponse) + ); + + sinon.stub(client, '_client' as any).value(mockAxiosClient); + + const result = await client.deleteFileById(1, 1, 1); + + expect(result).to.be.instanceOf(ApiResponse); + expect(result).to.have.property('statusCode', 500); + expect(result).to.have.property('isSuccessful', false); + expect(result.message).to.be.undefined; + expect(result.data).to.be.null; + }); + }); });