From 927fcd42b33f231a90161b7e786a900dd4368fd1 Mon Sep 17 00:00:00 2001 From: StevanFreeborn <65925598+StevanFreeborn@users.noreply.github.com> Date: Tue, 14 Feb 2023 21:21:47 -0600 Subject: [PATCH] feat: add getAppById, getAppsByIds, and getFieldById integration tests --- integrationTests/getAppById.spec.ts | 75 +++++++++++++++++++++++++ integrationTests/getApps.spec.ts | 6 +- integrationTests/getAppsByIds.spec.ts | 74 +++++++++++++++++++++++++ integrationTests/getFieldById.spec.ts | 79 +++++++++++++++++++++++++++ 4 files changed, 231 insertions(+), 3 deletions(-) create mode 100644 integrationTests/getAppById.spec.ts create mode 100644 integrationTests/getAppsByIds.spec.ts create mode 100644 integrationTests/getFieldById.spec.ts diff --git a/integrationTests/getAppById.spec.ts b/integrationTests/getAppById.spec.ts new file mode 100644 index 0000000..2127ce8 --- /dev/null +++ b/integrationTests/getAppById.spec.ts @@ -0,0 +1,75 @@ +import { OnspringClient } from '../src/index'; +import { expect } from 'chai'; +import * as dotenv from 'dotenv'; +import path from 'path'; +const envPath = path.resolve(__dirname, '.env'); +dotenv.config({ path: envPath }); + +describe('getAppById', function () { + this.timeout('30s'); + let baseURL; + let apiKey; + + before(function () { + baseURL = process.env.API_BASE_URL; + apiKey = process.env.SANDBOX_API_KEY; + }); + + it('should return an app', async function () { + const client = new OnspringClient(baseURL, apiKey); + + if (process.env.TEST_APP_ID === undefined) { + return expect.fail('TEST_APP_ID is not defined'); + } + + const appId = parseInt(process.env.TEST_APP_ID); + const response = await client.getAppById(appId); + + expect(response.statusCode).to.equal(200); + expect(response.isSuccessful).to.be.true; + expect(response.message).to.equal(''); + expect(response.data).to.not.be.null; + + if (response.data != null) { + expect(response.data.id).to.equal(appId); + expect(response.data.name).to.not.be.null; + expect(response.data.href).to.not.be.null; + } + }); + + it('should return a 401 error when an invalid api key is used', async function () { + const client = new OnspringClient(baseURL, 'invalid'); + const response = await client.getAppById(1); + + expect(response.statusCode).to.equal(401); + expect(response.isSuccessful).to.be.false; + expect(response.message).to.be.undefined; + expect(response.data).to.be.null; + }); + + it('should return a 403 error when api key does not have access to the requested app', async function () { + const client = new OnspringClient(baseURL, apiKey); + + if (process.env.TEST_APP_ID_NO_ACCESS === undefined) { + return expect.fail('TEST_APP_ID_NO_ACCESS is not defined'); + } + + const appId = parseInt(process.env.TEST_APP_ID_NO_ACCESS); + const response = await client.getAppById(appId); + + expect(response.statusCode).to.equal(403); + expect(response.isSuccessful).to.be.false; + expect(response.message).to.not.be.null; + expect(response.data).to.be.null; + }); + + it('should return a 404 error when an app id cannot be found', async function () { + const client = new OnspringClient(baseURL, apiKey); + const response = await client.getAppById(0); + + expect(response.statusCode).to.equal(404); + expect(response.isSuccessful).to.be.false; + expect(response.message).to.not.be.null; + expect(response.data).to.be.null; + }); +}); diff --git a/integrationTests/getApps.spec.ts b/integrationTests/getApps.spec.ts index 8e535b3..5210ca7 100644 --- a/integrationTests/getApps.spec.ts +++ b/integrationTests/getApps.spec.ts @@ -6,7 +6,7 @@ const envPath = path.resolve(__dirname, '.env'); dotenv.config({ path: envPath }); describe('getApps', function () { - this.timeout('5s'); + this.timeout('30s'); let baseURL; let apiKey; @@ -15,7 +15,7 @@ describe('getApps', function () { apiKey = process.env.SANDBOX_API_KEY; }); - it('should return a list of apps', async function () { + it('should return a paged list of apps', async function () { const client = new OnspringClient(baseURL, apiKey); const response = await client.getApps(); @@ -42,7 +42,7 @@ describe('getApps', function () { } }); - it('should return a list of apps with with correct page size and number when passed paging request', async function () { + it('should return a paged list of apps with with correct page size and number when passed paging request', async function () { const client = new OnspringClient(baseURL, apiKey); const response = await client.getApps(new PagingRequest(1, 1)); diff --git a/integrationTests/getAppsByIds.spec.ts b/integrationTests/getAppsByIds.spec.ts new file mode 100644 index 0000000..5806ef3 --- /dev/null +++ b/integrationTests/getAppsByIds.spec.ts @@ -0,0 +1,74 @@ +import { OnspringClient } from '../src/index'; +import { expect } from 'chai'; +import * as dotenv from 'dotenv'; +import path from 'path'; +const envPath = path.resolve(__dirname, '.env'); +dotenv.config({ path: envPath }); + +describe('getAppsByIds', function () { + this.timeout('30s'); + let baseURL; + let apiKey; + + before(function () { + baseURL = process.env.API_BASE_URL; + apiKey = process.env.SANDBOX_API_KEY; + }); + + it('should return a collection of apps', async function () { + const client = new OnspringClient(baseURL, apiKey); + const appIds = process.env.TEST_APP_IDS; + + if (appIds === undefined) { + return expect.fail('TEST_APP_IDS is not defined'); + } + + const appIdsAsNumbers = appIds.split(',').map((id) => parseInt(id)); + const response = await client.getAppsByIds(appIdsAsNumbers); + + expect(response.statusCode).to.equal(200); + expect(response.isSuccessful).to.be.true; + expect(response.message).to.equal(''); + expect(response.data).to.not.be.null; + + if (response.data != null) { + expect(response.data.items).to.not.be.null; + expect(response.data.items) + .to.be.an('array') + .that.has.lengthOf(appIdsAsNumbers.length); + + response.data.items.forEach((item) => { + expect(item.id).to.not.be.null; + expect(item.name).to.not.be.null; + expect(item.href).to.not.be.null; + }); + } + }); + + it('should return a 401 error when an invalid api key is used', async function () { + const client = new OnspringClient(baseURL, 'invalid'); + const response = await client.getAppsByIds([1]); + + expect(response.statusCode).to.equal(401); + expect(response.isSuccessful).to.be.false; + expect(response.message).to.be.undefined; + expect(response.data).to.be.null; + }); + + it('should return a 403 error when api key does not have access to any of the requested app', async function () { + const client = new OnspringClient(baseURL, apiKey); + const appIds = process.env.TEST_APP_IDS_NO_ACCESS; + + if (appIds === undefined) { + return expect.fail('TEST_APP_IDS_NO_ACCESS is not defined'); + } + + const appIdsAsNumbers = appIds.split(',').map((id) => parseInt(id)); + const response = await client.getAppsByIds(appIdsAsNumbers); + + expect(response.statusCode).to.equal(403); + expect(response.isSuccessful).to.be.false; + expect(response.message).to.not.be.null; + expect(response.data).to.be.null; + }); +}); diff --git a/integrationTests/getFieldById.spec.ts b/integrationTests/getFieldById.spec.ts new file mode 100644 index 0000000..21d8d3c --- /dev/null +++ b/integrationTests/getFieldById.spec.ts @@ -0,0 +1,79 @@ +import { OnspringClient } from '../src/index'; +import { expect } from 'chai'; +import * as dotenv from 'dotenv'; +import path from 'path'; +const envPath = path.resolve(__dirname, '.env'); +dotenv.config({ path: envPath }); + +describe('getAppById', function () { + this.timeout('30s'); + let baseURL; + let apiKey; + + before(function () { + baseURL = process.env.API_BASE_URL; + apiKey = process.env.SANDBOX_API_KEY; + }); + + it('should return a field', async function () { + const client = new OnspringClient(baseURL, apiKey); + + if (process.env.TEST_FIELD_ID === undefined) { + return expect.fail('TEST_FIELD_ID is not defined'); + } + + const fieldId = parseInt(process.env.TEST_FIELD_ID); + const response = await client.getFieldById(fieldId); + + expect(response.statusCode).to.equal(200); + expect(response.isSuccessful).to.be.true; + expect(response.message).to.equal(''); + expect(response.data).to.not.be.null; + + if (response.data != null) { + expect(response.data.id).to.equal(fieldId); + expect(response.data.name).to.not.be.null; + expect(response.data.appId).to.not.be.null; + expect(response.data.type).to.not.be.null; + expect(response.data.status).to.not.be.null; + expect(response.data.isRequired).to.not.be.null; + expect(response.data.isUnique).to.not.be.null; + } + }); + + it('should return a 401 error when an invalid api key is used', async function () { + const client = new OnspringClient(baseURL, 'invalid'); + const response = await client.getFieldById(1); + + expect(response.statusCode).to.equal(401); + expect(response.isSuccessful).to.be.false; + expect(response.message).to.be.undefined; + expect(response.data).to.be.null; + }); + + it('should return a 403 error when api key does not have access to the requested field', async function () { + const client = new OnspringClient(baseURL, apiKey); + + if (process.env.TEST_FIELD_ID_NO_ACCESS === undefined) { + return expect.fail('TEST_FIELD_ID_NO_ACCESS is not defined'); + } + + const fieldId = parseInt(process.env.TEST_FIELD_ID_NO_ACCESS); + const response = await client.getFieldById(fieldId); + + expect(response.statusCode).to.equal(403); + expect(response.isSuccessful).to.be.false; + expect(response.message).to.not.be.null; + expect(response.data).to.be.null; + }); + + it('should return a 404 error when the requested field does not exist', async function () { + const client = new OnspringClient(baseURL, apiKey); + const response = await client.getFieldById(0); + + expect(response.statusCode).to.equal(404); + expect(response.isSuccessful).to.be.false; + expect(response.message).to.not.be.null; + expect(response.data).to.be.null; + }); +});