feat: added saveRecord method

This commit is contained in:
StevanFreeborn
2023-02-13 20:21:28 -06:00
parent f07cd3043a
commit 757e773f9f
7 changed files with 385 additions and 3 deletions
+262
View File
@@ -32,6 +32,7 @@ import fs from 'fs';
import path from 'path';
import * as sinon from 'sinon';
import { QueryRecordsRequest } from '../src/models/QueryRecordsRequest';
import { SaveRecordRequest } from '../src/models/SaveRecordRequest';
describe('OnspringClient', function () {
const baseUrl = 'https://api.onspring.dev';
@@ -3757,4 +3758,265 @@ describe('OnspringClient', function () {
expect(result).to.have.property('data', null);
});
});
describe('saveRecord', function () {
it('should be defined', function () {
expect(OnspringClient.prototype.saveRecord).to.not.be.undefined;
});
it('should be a function', function () {
expect(OnspringClient.prototype.saveRecord).to.be.a('function');
});
it('should return a promise', function () {
expect(
new OnspringClient(baseUrl, apiKey).saveRecord(
new SaveRecordRequest(1, 1)
)
).to.be.instanceOf(Promise);
});
it('should return a promise that resolves to an api response when request is successful at creating a record using a SaveRecordRequest', 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, 'put').returns(
Promise.resolve({
status: 201,
statusText: 'Created',
data: { id: 1, warnings: ['warning'] },
headers: {},
config: {} as InternalAxiosRequestConfig,
} as AxiosResponse)
);
sinon.stub(client, '_client' as any).value(mockAxiosClient);
const result = await client.saveRecord(new SaveRecordRequest(1, null));
expect(result).to.be.instanceOf(ApiResponse);
expect(result).to.have.property('statusCode', 201);
expect(result).to.have.property('isSuccessful', true);
expect(result).to.have.property('message', '');
expect(result).to.have.property('data');
expect(result.data).to.have.property('id', 1);
expect(result.data)
.to.have.property('warnings')
.that.is.an('array')
.that.has.lengthOf(1);
});
it('should return a promise that resolves to an api response when request is successful at updating a record using a SaveRecordRequest', 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, 'put').returns(
Promise.resolve({
status: 200,
statusText: 'OK',
data: { id: 1, warnings: ['warning'] },
headers: {},
config: {} as InternalAxiosRequestConfig,
} as AxiosResponse)
);
sinon.stub(client, '_client' as any).value(mockAxiosClient);
const result = await client.saveRecord(new SaveRecordRequest(1, 1));
expect(result).to.be.instanceOf(ApiResponse);
expect(result).to.have.property('statusCode', 200);
expect(result).to.have.property('isSuccessful', true);
expect(result).to.have.property('message', '');
expect(result).to.have.property('data');
expect(result.data).to.have.property('id', 1);
expect(result.data)
.to.have.property('warnings')
.that.is.an('array')
.that.has.lengthOf(1);
});
it('should return a promise that resolves to an api response when request is successful at creating a record using a Record', 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, 'put').returns(
Promise.resolve({
status: 201,
statusText: 'Created',
data: { id: 1, warnings: ['warning'] },
headers: {},
config: {} as InternalAxiosRequestConfig,
} as AxiosResponse)
);
sinon.stub(client, '_client' as any).value(mockAxiosClient);
const result = await client.saveRecord(new Record(1, null));
expect(result).to.be.instanceOf(ApiResponse);
expect(result).to.have.property('statusCode', 201);
expect(result).to.have.property('isSuccessful', true);
expect(result).to.have.property('message', '');
expect(result).to.have.property('data');
expect(result.data).to.have.property('id', 1);
expect(result.data)
.to.have.property('warnings')
.that.is.an('array')
.that.has.lengthOf(1);
});
it('should return a promise that resolves to an api response when request is successful at updating a record using a Record', 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, 'put').returns(
Promise.resolve({
status: 200,
statusText: 'OK',
data: { id: 1, warnings: ['warning'] },
headers: {},
config: {} as InternalAxiosRequestConfig,
} as AxiosResponse)
);
sinon.stub(client, '_client' as any).value(mockAxiosClient);
const result = await client.saveRecord(new Record(1, 1));
expect(result).to.be.instanceOf(ApiResponse);
expect(result).to.have.property('statusCode', 200);
expect(result).to.have.property('isSuccessful', true);
expect(result).to.have.property('message', '');
expect(result).to.have.property('data');
expect(result.data).to.have.property('id', 1);
expect(result.data)
.to.have.property('warnings')
.that.is.an('array')
.that.has.lengthOf(1);
});
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, 'put').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.saveRecord(new SaveRecordRequest(1, 1));
expect(result).to.be.instanceOf(ApiResponse);
expect(result).to.have.property('statusCode', 401);
expect(result).to.have.property('isSuccessful', false);
expect(result).to.have.property('message', undefined);
expect(result).to.have.property('data', 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, 'put').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.saveRecord(new SaveRecordRequest(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).to.have.property('data', 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, 'put').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.saveRecord(new SaveRecordRequest(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).to.have.property('data', null);
});
});
});