From eebfcb31193173c19dc67792fb6666ac3a646e85 Mon Sep 17 00:00:00 2001 From: Darryl Morton Date: Fri, 29 Apr 2022 08:52:34 +0100 Subject: [PATCH 01/23] reorganising helper files/functions, helper routes file for clients, skeleton tests for client --- test/helper/appHelper.js | 17 +++ test/helper/clientRouteHelper.js | 84 ++++++++++ test/helper/projectRouteHelper.js | 84 ++++++++++ test/integration/clientRoutes.test.js | 61 ++++++++ test/integration/projectRoutes.test.js | 200 ++++++++++++++++++++++++ test/integration/routes.test.js | 202 ------------------------- 6 files changed, 446 insertions(+), 202 deletions(-) create mode 100644 test/helper/clientRouteHelper.js create mode 100644 test/helper/projectRouteHelper.js create mode 100644 test/integration/clientRoutes.test.js create mode 100644 test/integration/projectRoutes.test.js delete mode 100644 test/integration/routes.test.js diff --git a/test/helper/appHelper.js b/test/helper/appHelper.js index 9b89f69..e79f696 100644 --- a/test/helper/appHelper.js +++ b/test/helper/appHelper.js @@ -20,6 +20,21 @@ const createDefaultProject = ({ clientId }) => { }) } +const createClient = (client) => { + const clientObj = { ...client } + + return clientObj +} + +const createDefaultClient = () => { + return createProject({ + firstName: 'First name 1', + lastName: 'Last name 1', + company: 'Company 1', + role: 'Role 1', + }) +} + const assertUuidV4 = (id) => { expect(uuidValidate(id) && uuidVersion(id) === 4).to.be.true } @@ -49,6 +64,8 @@ const assertGetProjects = (actualResult, expectedResult) => { } module.exports = { + createClient, + createDefaultClient, createProject, createDefaultProject, assertUuidV4, diff --git a/test/helper/clientRouteHelper.js b/test/helper/clientRouteHelper.js new file mode 100644 index 0000000..c04b9dd --- /dev/null +++ b/test/helper/clientRouteHelper.js @@ -0,0 +1,84 @@ +/* eslint no-console: "off" */ +const request = require('supertest') + +const { API_MAJOR_VERSION } = require('../../app/env') + +async function getClientsRoute({ app }) { + return request(app) + .get(`/${API_MAJOR_VERSION}/profiler/client`) + .set('Accept', 'application/json') + .set('Content-Type', 'application/json') + .then((response) => { + return response + }) + .catch((err) => { + console.error(`getClientsErr ${err}`) + return err + }) +} + +async function getClientByIdRoute(id, { app }) { + return request(app) + .get(`/${API_MAJOR_VERSION}/profiler/client/${id}`) + .set('Accept', 'application/json') + .set('Content-Type', 'application/json') + .then((response) => { + return response + }) + .catch((err) => { + console.error(`getClientsErr ${err}`) + return err + }) +} + +async function postClientRoute(client, { app }) { + return request(app) + .post(`/${API_MAJOR_VERSION}/profiler/client`) + .set('Accept', 'application/json') + .set('Content-Type', 'application/json') + .send(client) + .then((response) => { + return response + }) + .catch((err) => { + console.error(`postClientErr ${err}`) + return err + }) +} + +async function putClientRoute(id, client, { app }) { + return request(app) + .put(`/${API_MAJOR_VERSION}/profiler/client/${id}`) + .set('Accept', 'application/json') + .set('Content-Type', 'application/json') + .send(client) + .then((response) => { + return response + }) + .catch((err) => { + console.error(`putClientErr ${err}`) + return err + }) +} + +async function deleteClientByIdRoute(id, { app }) { + return request(app) + .delete(`/${API_MAJOR_VERSION}/profiler/client/${id}`) + .set('Accept', 'application/json') + .set('Content-Type', 'application/json') + .then((response) => { + return response + }) + .catch((err) => { + console.error(`getClientsErr ${err}`) + return err + }) +} + +module.exports = { + getClientsRoute, + postClientRoute, + getClientByIdRoute, + putClientRoute, + deleteClientByIdRoute, +} diff --git a/test/helper/projectRouteHelper.js b/test/helper/projectRouteHelper.js new file mode 100644 index 0000000..a03d7bc --- /dev/null +++ b/test/helper/projectRouteHelper.js @@ -0,0 +1,84 @@ +/* eslint no-console: "off" */ +const request = require('supertest') + +const { API_MAJOR_VERSION } = require('../../app/env') + +async function getProjectsRoute({ app }) { + return request(app) + .get(`/${API_MAJOR_VERSION}/profiler/project`) + .set('Accept', 'application/json') + .set('Content-Type', 'application/json') + .then((response) => { + return response + }) + .catch((err) => { + console.error(`getProjectsErr ${err}`) + return err + }) +} + +async function getProjectByIdRoute(id, { app }) { + return request(app) + .get(`/${API_MAJOR_VERSION}/profiler/project/${id}`) + .set('Accept', 'application/json') + .set('Content-Type', 'application/json') + .then((response) => { + return response + }) + .catch((err) => { + console.error(`getProjectsErr ${err}`) + return err + }) +} + +async function postProjectRoute(project, { app }) { + return request(app) + .post(`/${API_MAJOR_VERSION}/profiler/project`) + .set('Accept', 'application/json') + .set('Content-Type', 'application/json') + .send(project) + .then((response) => { + return response + }) + .catch((err) => { + console.error(`postProjectErr ${err}`) + return err + }) +} + +async function putProjectRoute(id, project, { app }) { + return request(app) + .put(`/${API_MAJOR_VERSION}/profiler/project/${id}`) + .set('Accept', 'application/json') + .set('Content-Type', 'application/json') + .send(project) + .then((response) => { + return response + }) + .catch((err) => { + console.error(`putProjectErr ${err}`) + return err + }) +} + +async function deleteProjectByIdRoute(id, { app }) { + return request(app) + .delete(`/${API_MAJOR_VERSION}/profiler/project/${id}`) + .set('Accept', 'application/json') + .set('Content-Type', 'application/json') + .then((response) => { + return response + }) + .catch((err) => { + console.error(`getProjectsErr ${err}`) + return err + }) +} + +module.exports = { + getProjectsRoute, + postProjectRoute, + getProjectByIdRoute, + putProjectRoute, + deleteProjectByIdRoute, +} diff --git a/test/integration/clientRoutes.test.js b/test/integration/clientRoutes.test.js new file mode 100644 index 0000000..b7d0df0 --- /dev/null +++ b/test/integration/clientRoutes.test.js @@ -0,0 +1,61 @@ +const { describe, test, before } = require('mocha') +const { expect } = require('chai') + +const { assertPostProjectParams, createDefaultClient } = require('../helper/appHelper') +const { createHttpServer } = require('../../app/server') +const { postClientRoute } = require('../helper/clientRouteHelper') +const { cleanupAll } = require('../helper/seeds/project') + +describe.only('Client routes', function () { + let app + let defaultClient + + before(async function () { + app = await createHttpServer() + + defaultClient = createDefaultClient() + }) + + beforeEach(async function () { + await cleanupAll() + }) + + test('POST Client', async function () { + const expectedResult = defaultClient + + const response = await postClientRoute(defaultClient, app) + + expect(response.status).to.equal(201) + assertPostProjectParams(response.body, expectedResult) + }) + + test.skip('POST client missing fields', async function () {}) + + test.skip('POST invalid client', async function () {}) + + test.skip('POST duplicate client', async function () {}) + + test.skip('GET clients', async function () {}) + + test.skip('GET client by id', async function () {}) + + test.skip('GET client by id for non-existing client', async function () {}) + + test.skip('GET client by id with invalid path id parameter', async function () {}) + + test.skip('PUT client', async function () {}) + + test.skip('PUT client with existing name', async function () {}) + + test.skip('PUT client for non-existing client', async function () {}) + + test.skip('PUT client with invalid id path parameter', async function () {}) + + test.skip('PUT client with missing fields', async function () {}) + + test.skip('DELETE client', async function () {}) + + test.skip('DELETE client for non-existing client', async function () {}) + + test.skip('DELETE client with invalid id path parameter', async function () {}) +}) diff --git a/test/integration/projectRoutes.test.js b/test/integration/projectRoutes.test.js new file mode 100644 index 0000000..4ccf572 --- /dev/null +++ b/test/integration/projectRoutes.test.js @@ -0,0 +1,200 @@ +const { describe, test, before } = require('mocha') +const { expect } = require('chai') + +const { + createDefaultProject, + createProject, + assertPostProjectParams, + assertPostProjectRequiredParams, + assertGetProjects, +} = require('../helper/appHelper') +const { createHttpServer } = require('../../app/server') +const { + getProjectByIdRoute, + getProjectsRoute, + postProjectRoute, + putProjectRoute, + deleteProjectByIdRoute, +} = require('../helper/projectRouteHelper') +const { seed, cleanup } = require('../helper/seeds/project') +const moment = require('moment') + +describe('Project routes', function () { + let app + let clientId + let invalidId + let defaultProject + + before(async function () { + await seed() + + app = await createHttpServer() + clientId = 'c7b9e848-e2bb-456d-8eaa-129c1cb3580c' + invalidId = '00000000-0000-0000-0000-000000000000' + defaultProject = createDefaultProject({ clientId }) + }) + + beforeEach(async function () { + await cleanup('projects') + }) + + test('POST Project with all fields', async function () { + const expectedResult = defaultProject + + const response = await postProjectRoute(defaultProject, app) + + expect(response.status).to.equal(201) + assertPostProjectParams(response.body, expectedResult) + }) + + test('POST Project with only required request body parameters', async function () { + const project = createProject({ clientId, name: 'Project 1', description: 'Project 1 description' }) + const expectedResult = { + ...project, + startDate: null, + endDate: null, + budget: null, + documentUrl: null, + } + + const response = await postProjectRoute(project, app) + + expect(response.status).to.equal(201) + assertPostProjectRequiredParams(response.body, expectedResult) + }) + + test('POST Project missing client id', async function () { + const project = createProject({ name: 'Project 1', description: 'Project 1 description' }) + + const response = await postProjectRoute(project, app) + + expect(response.status).to.equal(400) + expect(response.body).deep.equal({}) + }) + + test('POST invalid project', async function () { + const response = await postProjectRoute({}, app) + + expect(response.status).to.equal(400) + expect(response.body).deep.equal({}) + }) + + test('POST duplicate project', async function () { + await postProjectRoute(defaultProject, app) + const response = await postProjectRoute(defaultProject, app) + + expect(response.status).to.equal(409) + expect(response.body).deep.equal({}) + }) + + test('GET projects', async function () { + const expectedResult = [defaultProject] + + await postProjectRoute(defaultProject, app) + const response = await getProjectsRoute(app) + + expect(response.status).to.equal(200) + assertGetProjects(response.body, expectedResult) + }) + + test('GET project by id', async function () { + const expectedResult = defaultProject + + const projectResponse = await postProjectRoute(defaultProject, app) + const response = await getProjectByIdRoute(projectResponse.body.id, app) + + expect(response.status).to.equal(200) + assertPostProjectRequiredParams(response.body, expectedResult) + }) + + test('GET project by id with invalid path id parameter', async function () { + const response = await getProjectByIdRoute(invalidId, app) + + expect(response.status).to.equal(404) + }) + + test('PUT project with only required fields', async function () { + const project = createProject({ clientId, name: 'Project 1', description: 'Project 1 description' }) + const projectUpdate = createProject({ clientId, name: 'Project 2', description: 'Project 2 description' }) + const expectedResult = projectUpdate + + const projectResponse = await postProjectRoute(project, app) + const response = await putProjectRoute(projectResponse.body.id, projectUpdate, app) + + expect(response.status).to.equal(200) + assertPostProjectRequiredParams(response.body, expectedResult) + }) + + test('PUT project with existing name', async function () { + const project = createProject({ clientId, name: 'Project 1', description: 'Project 1 description' }) + const projectUpdate = createProject({ clientId, name: 'Project 1', description: 'Project 2 description' }) + + const projectResponse = await postProjectRoute(project, app) + const response = await putProjectRoute(projectResponse.body.id, projectUpdate, app) + + expect(response.status).to.equal(409) + expect(response.body).to.deep.equal({}) + }) + + test('PUT project with invalid id path parameter', async function () { + const project = createProject({ + clientId, + name: 'Project 2', + description: 'Project 2 description', + startDate: moment().startOf('day').toISOString(), + endDate: moment().endOf('day').toISOString(), + budget: 200000.0, + documentUrl: 'http://digitalcatapult.org.uk/document/url', + }) + + const response = await putProjectRoute(invalidId, project, app) + + expect(response.status).to.equal(404) + }) + + test('PUT project with missing required client id path parameter', async function () { + const project = createProject({ + name: 'Project 2', + description: 'Project 2 description', + startDate: moment().startOf('day').toISOString(), + endDate: moment().endOf('day').toISOString(), + budget: 200000.0, + documentUrl: 'http://digitalcatapult.org.uk/document/url', + }) + + const response = await putProjectRoute(invalidId, project, app) + + expect(response.status).to.equal(400) + expect(response.body).deep.equal({}) + }) + + test('PUT project with all request body parameters', async function () { + const project = createProject({ + name: 'Project 2', + description: 'Project 2 description', + startDate: moment().startOf('day').toISOString(), + endDate: moment().endOf('day').toISOString(), + budget: 200000.0, + documentUrl: 'http://digitalcatapult.org.uk/document/url', + }) + + const response = await putProjectRoute(invalidId, project, app) + + expect(response.status).to.equal(400) + expect(response.body).deep.equal({}) + }) + + test('DELETE project', async function () { + const projectResponse = await postProjectRoute(defaultProject, app) + const response = await deleteProjectByIdRoute(projectResponse.body.id, app) + + expect(response.status).to.equal(204) + expect(response.body).deep.equal({}) + }) + + test('DELETE project with invalid id path parameter', async function () { + const response = await deleteProjectByIdRoute(invalidId, app) + + expect(response.status).to.equal(404) + }) +}) diff --git a/test/integration/routes.test.js b/test/integration/routes.test.js deleted file mode 100644 index 916f847..0000000 --- a/test/integration/routes.test.js +++ /dev/null @@ -1,202 +0,0 @@ -const { describe, test, before } = require('mocha') -const { expect } = require('chai') - -const { - createDefaultProject, - createProject, - assertPostProjectParams, - assertPostProjectRequiredParams, - assertGetProjects, -} = require('../helper/appHelper') -const { createHttpServer } = require('../../app/server') -const { - getProjectByIdRoute, - getProjectsRoute, - postProjectRoute, - putProjectRoute, - deleteProjectByIdRoute, -} = require('../helper/routeHelper') -const { seed, cleanup } = require('../helper/seeds/project') -const moment = require('moment') - -describe('routes', function () { - describe('Project routes', function () { - let app - let clientId - let invalidId - let defaultProject - - before(async function () { - await seed() - - app = await createHttpServer() - clientId = 'c7b9e848-e2bb-456d-8eaa-129c1cb3580c' - invalidId = '00000000-0000-0000-0000-000000000000' - defaultProject = createDefaultProject({ clientId }) - }) - - beforeEach(async function () { - await cleanup('projects') - }) - - test('POST Project with all fields', async function () { - const expectedResult = defaultProject - - const response = await postProjectRoute(defaultProject, app) - - expect(response.status).to.equal(201) - assertPostProjectParams(response.body, expectedResult) - }) - - test('POST Project with only required request body parameters', async function () { - const project = createProject({ clientId, name: 'Project 1', description: 'Project 1 description' }) - const expectedResult = { - ...project, - startDate: null, - endDate: null, - budget: null, - documentUrl: null, - } - - const response = await postProjectRoute(project, app) - - expect(response.status).to.equal(201) - assertPostProjectRequiredParams(response.body, expectedResult) - }) - - test('POST Project missing client id', async function () { - const project = createProject({ name: 'Project 1', description: 'Project 1 description' }) - - const response = await postProjectRoute(project, app) - - expect(response.status).to.equal(400) - expect(response.body).deep.equal({}) - }) - - test('POST invalid project', async function () { - const response = await postProjectRoute({}, app) - - expect(response.status).to.equal(400) - expect(response.body).deep.equal({}) - }) - - test('POST duplicate project', async function () { - await postProjectRoute(defaultProject, app) - const response = await postProjectRoute(defaultProject, app) - - expect(response.status).to.equal(409) - expect(response.body).deep.equal({}) - }) - - test('GET projects', async function () { - const expectedResult = [defaultProject] - - await postProjectRoute(defaultProject, app) - const response = await getProjectsRoute(app) - - expect(response.status).to.equal(200) - assertGetProjects(response.body, expectedResult) - }) - - test('GET project by id', async function () { - const expectedResult = defaultProject - - const projectResponse = await postProjectRoute(defaultProject, app) - const response = await getProjectByIdRoute(projectResponse.body.id, app) - - expect(response.status).to.equal(200) - assertPostProjectRequiredParams(response.body, expectedResult) - }) - - test('GET project by id with invalid path id parameter', async function () { - const response = await getProjectByIdRoute(invalidId, app) - - expect(response.status).to.equal(404) - }) - - test('PUT project with only required fields', async function () { - const project = createProject({ clientId, name: 'Project 1', description: 'Project 1 description' }) - const projectUpdate = createProject({ clientId, name: 'Project 2', description: 'Project 2 description' }) - const expectedResult = projectUpdate - - const projectResponse = await postProjectRoute(project, app) - const response = await putProjectRoute(projectResponse.body.id, projectUpdate, app) - - expect(response.status).to.equal(200) - assertPostProjectRequiredParams(response.body, expectedResult) - }) - - test('PUT project with existing name', async function () { - const project = createProject({ clientId, name: 'Project 1', description: 'Project 1 description' }) - const projectUpdate = createProject({ clientId, name: 'Project 1', description: 'Project 2 description' }) - - const projectResponse = await postProjectRoute(project, app) - const response = await putProjectRoute(projectResponse.body.id, projectUpdate, app) - - expect(response.status).to.equal(409) - expect(response.body).to.deep.equal({}) - }) - - test('PUT project with invalid id path parameter', async function () { - const project = createProject({ - clientId, - name: 'Project 2', - description: 'Project 2 description', - startDate: moment().startOf('day').toISOString(), - endDate: moment().endOf('day').toISOString(), - budget: 200000.0, - documentUrl: 'http://digitalcatapult.org.uk/document/url', - }) - - const response = await putProjectRoute(invalidId, project, app) - - expect(response.status).to.equal(404) - }) - - test('PUT project with missing required client id path parameter', async function () { - const project = createProject({ - name: 'Project 2', - description: 'Project 2 description', - startDate: moment().startOf('day').toISOString(), - endDate: moment().endOf('day').toISOString(), - budget: 200000.0, - documentUrl: 'http://digitalcatapult.org.uk/document/url', - }) - - const response = await putProjectRoute(invalidId, project, app) - - expect(response.status).to.equal(400) - expect(response.body).deep.equal({}) - }) - - test('PUT project with all request body parameters', async function () { - const project = createProject({ - name: 'Project 2', - description: 'Project 2 description', - startDate: moment().startOf('day').toISOString(), - endDate: moment().endOf('day').toISOString(), - budget: 200000.0, - documentUrl: 'http://digitalcatapult.org.uk/document/url', - }) - - const response = await putProjectRoute(invalidId, project, app) - - expect(response.status).to.equal(400) - expect(response.body).deep.equal({}) - }) - - test('DELETE project', async function () { - const projectResponse = await postProjectRoute(defaultProject, app) - const response = await deleteProjectByIdRoute(projectResponse.body.id, app) - - expect(response.status).to.equal(204) - expect(response.body).deep.equal({}) - }) - - test('DELETE project with invalid id path parameter', async function () { - const response = await deleteProjectByIdRoute(invalidId, app) - - expect(response.status).to.equal(404) - }) - }) -}) From a266fd4f44842dc01a6fbb8c69c201ea31792a5c Mon Sep 17 00:00:00 2001 From: Darryl Morton Date: Fri, 29 Apr 2022 09:00:37 +0100 Subject: [PATCH 02/23] project validator directory --- app/api-v1/routes/profiler/project.js | 7 +++++-- app/api-v1/routes/profiler/project/{id}.js | 9 ++++++--- .../{ => project}/deleteProjectResponseValidator.js | 4 ++-- .../{ => project}/getProjectByIdResponseValidator.js | 4 ++-- .../{ => project}/getProjectsResponseValidator.js | 4 ++-- .../{ => project}/postProjectResponseValidator.js | 4 ++-- .../{ => project}/putProjectResponseValidator.js | 4 ++-- test/integration/clientRoutes.test.js | 2 +- 8 files changed, 22 insertions(+), 16 deletions(-) rename app/api-v1/validators/{ => project}/deleteProjectResponseValidator.js (86%) rename app/api-v1/validators/{ => project}/getProjectByIdResponseValidator.js (87%) rename app/api-v1/validators/{ => project}/getProjectsResponseValidator.js (77%) rename app/api-v1/validators/{ => project}/postProjectResponseValidator.js (87%) rename app/api-v1/validators/{ => project}/putProjectResponseValidator.js (87%) diff --git a/app/api-v1/routes/profiler/project.js b/app/api-v1/routes/profiler/project.js index 7b0ef12..4d61ca7 100644 --- a/app/api-v1/routes/profiler/project.js +++ b/app/api-v1/routes/profiler/project.js @@ -1,5 +1,8 @@ -const { GET_PROJECTS_RESPONSES } = require('../../validators/getProjectsResponseValidator') -const { POST_PROJECT_RESPONSES, validatePostProjectResponse } = require('../../validators/postProjectResponseValidator') +const { GET_PROJECTS_RESPONSES } = require('../../validators/project/getProjectsResponseValidator') +const { + POST_PROJECT_RESPONSES, + validatePostProjectResponse, +} = require('../../validators/project/postProjectResponseValidator') module.exports = function (apiService) { const doc = { diff --git a/app/api-v1/routes/profiler/project/{id}.js b/app/api-v1/routes/profiler/project/{id}.js index b476860..e527b4e 100644 --- a/app/api-v1/routes/profiler/project/{id}.js +++ b/app/api-v1/routes/profiler/project/{id}.js @@ -1,12 +1,15 @@ const { validateGetProjectByIdResponse, GET_PROJECT_BY_ID_RESPONSES, -} = require('../../../validators/getProjectByIdResponseValidator') -const { PUT_PROJECT_RESPONSES, validatePutProjectResponse } = require('../../../validators/putProjectResponseValidator') +} = require('../../../validators/project/getProjectByIdResponseValidator') +const { + PUT_PROJECT_RESPONSES, + validatePutProjectResponse, +} = require('../../../validators/project/putProjectResponseValidator') const { DELETE_PROJECT_RESPONSES, validateDeleteProjectResponse, -} = require('../../../validators/deleteProjectResponseValidator') +} = require('../../../validators/project/deleteProjectResponseValidator') module.exports = function (apiService) { const doc = { diff --git a/app/api-v1/validators/deleteProjectResponseValidator.js b/app/api-v1/validators/project/deleteProjectResponseValidator.js similarity index 86% rename from app/api-v1/validators/deleteProjectResponseValidator.js rename to app/api-v1/validators/project/deleteProjectResponseValidator.js index f60dfe1..5dc5cf8 100644 --- a/app/api-v1/validators/deleteProjectResponseValidator.js +++ b/app/api-v1/validators/project/deleteProjectResponseValidator.js @@ -1,7 +1,7 @@ const OpenAPIResponseValidator = require('openapi-response-validator').default -const apiDocResponses = require('../api-doc-responses') -const apiDoc = require('../api-doc') +const apiDocResponses = require('../../api-doc-responses') +const apiDoc = require('../../api-doc') const DELETE_PROJECT_RESPONSES = { 201: { diff --git a/app/api-v1/validators/getProjectByIdResponseValidator.js b/app/api-v1/validators/project/getProjectByIdResponseValidator.js similarity index 87% rename from app/api-v1/validators/getProjectByIdResponseValidator.js rename to app/api-v1/validators/project/getProjectByIdResponseValidator.js index d0dc6d0..50d2dfc 100644 --- a/app/api-v1/validators/getProjectByIdResponseValidator.js +++ b/app/api-v1/validators/project/getProjectByIdResponseValidator.js @@ -1,7 +1,7 @@ const OpenAPIResponseValidator = require('openapi-response-validator').default -const apiDocResponses = require('../api-doc-responses') -const apiDoc = require('../api-doc') +const apiDocResponses = require('../../api-doc-responses') +const apiDoc = require('../../api-doc') const GET_PROJECT_BY_ID_RESPONSES = { 200: { diff --git a/app/api-v1/validators/getProjectsResponseValidator.js b/app/api-v1/validators/project/getProjectsResponseValidator.js similarity index 77% rename from app/api-v1/validators/getProjectsResponseValidator.js rename to app/api-v1/validators/project/getProjectsResponseValidator.js index 46865c3..d60fe1d 100644 --- a/app/api-v1/validators/getProjectsResponseValidator.js +++ b/app/api-v1/validators/project/getProjectsResponseValidator.js @@ -1,5 +1,5 @@ -const apiDocResponses = require('../api-doc-responses') -const apiDoc = require('../api-doc') +const apiDocResponses = require('../../api-doc-responses') +const apiDoc = require('../../api-doc') const GET_PROJECTS_RESPONSES = { 200: { diff --git a/app/api-v1/validators/postProjectResponseValidator.js b/app/api-v1/validators/project/postProjectResponseValidator.js similarity index 87% rename from app/api-v1/validators/postProjectResponseValidator.js rename to app/api-v1/validators/project/postProjectResponseValidator.js index 231daa5..5f49fdf 100644 --- a/app/api-v1/validators/postProjectResponseValidator.js +++ b/app/api-v1/validators/project/postProjectResponseValidator.js @@ -1,7 +1,7 @@ const OpenAPIResponseValidator = require('openapi-response-validator').default -const apiDocResponses = require('../api-doc-responses') -const apiDoc = require('../api-doc') +const apiDocResponses = require('../../api-doc-responses') +const apiDoc = require('../../api-doc') const POST_PROJECT_RESPONSES = { 201: { diff --git a/app/api-v1/validators/putProjectResponseValidator.js b/app/api-v1/validators/project/putProjectResponseValidator.js similarity index 87% rename from app/api-v1/validators/putProjectResponseValidator.js rename to app/api-v1/validators/project/putProjectResponseValidator.js index 2cd12f7..1f612f7 100644 --- a/app/api-v1/validators/putProjectResponseValidator.js +++ b/app/api-v1/validators/project/putProjectResponseValidator.js @@ -1,7 +1,7 @@ const OpenAPIResponseValidator = require('openapi-response-validator').default -const apiDocResponses = require('../api-doc-responses') -const apiDoc = require('../api-doc') +const apiDocResponses = require('../../api-doc-responses') +const apiDoc = require('../../api-doc') const PUT_PROJECT_RESPONSES = { 200: { diff --git a/test/integration/clientRoutes.test.js b/test/integration/clientRoutes.test.js index b7d0df0..be51a10 100644 --- a/test/integration/clientRoutes.test.js +++ b/test/integration/clientRoutes.test.js @@ -6,7 +6,7 @@ const { createHttpServer } = require('../../app/server') const { postClientRoute } = require('../helper/clientRouteHelper') const { cleanupAll } = require('../helper/seeds/project') -describe.only('Client routes', function () { +describe.skip('Client routes', function () { let app let defaultClient From 6b9801d4b2e939978c5e3348e06548c75a2eadff Mon Sep 17 00:00:00 2001 From: Darryl Morton Date: Fri, 29 Apr 2022 11:01:27 +0100 Subject: [PATCH 03/23] client route file with post method for post client, api service post function for logic, request/response openapi validation, request/response openapi schemas, db function for creating client, helper functions for client, integration test for post client --- app/api-v1/api-doc.js | 51 ++++++++++++ app/api-v1/routes/profiler/client.js | 39 ++++++++++ app/api-v1/services/apiService.js | 8 ++ .../client/postClientResponseValidator.js | 28 +++++++ app/db.js | 17 ++++ test/helper/appHelper.js | 19 +++-- test/helper/routeHelper.js | 77 ------------------- test/integration/clientRoutes.test.js | 10 +-- 8 files changed, 158 insertions(+), 91 deletions(-) create mode 100644 app/api-v1/routes/profiler/client.js create mode 100644 app/api-v1/validators/client/postClientResponseValidator.js diff --git a/app/api-v1/api-doc.js b/app/api-v1/api-doc.js index d2d9cb0..cb2c20e 100644 --- a/app/api-v1/api-doc.js +++ b/app/api-v1/api-doc.js @@ -30,6 +30,57 @@ const apiDoc = { }, }, schemas: { + GetClient: { + description: 'GET client', + type: 'object', + properties: { + id: { + description: 'Id of the client', + type: 'string', + format: 'uuid', + }, + firstName: { + description: 'First name of the client', + type: 'string', + }, + lastName: { + description: 'Last name of the client', + type: 'string', + }, + company: { + description: 'Company of the client', + type: 'string', + }, + role: { + description: 'Role of the client', + type: 'string', + }, + }, + required: ['id', 'firstName', 'lastName', 'company', 'role'], + }, + PostAndPutClient: { + description: 'POST/PUT client', + type: 'object', + properties: { + firstName: { + description: 'First name of the client', + type: 'string', + }, + lastName: { + description: 'Last name of the client', + type: 'string', + }, + company: { + description: 'Company of the client', + type: 'string', + }, + role: { + description: 'Role of the client', + type: 'string', + }, + }, + required: ['firstName', 'lastName', 'company', 'role'], + }, GetProject: { description: 'GET project', type: 'object', diff --git a/app/api-v1/routes/profiler/client.js b/app/api-v1/routes/profiler/client.js new file mode 100644 index 0000000..5608028 --- /dev/null +++ b/app/api-v1/routes/profiler/client.js @@ -0,0 +1,39 @@ +const { + POST_CLIENT_RESPONSES, + validatePostClientResponse, +} = require('../../validators/client/postClientResponseValidator') + +module.exports = function (apiService) { + const doc = { + POST: async function (req, res) { + const { statusCode, result } = await apiService.postClient(req.body) + + const validationErrors = validatePostClientResponse(statusCode, result) + + if (validationErrors) { + res.status(statusCode).json(validationErrors) + return + } else { + res.status(statusCode).json(result) + return + } + }, + } + + doc.POST.apiDoc = { + summary: 'POST client', + requestBody: { + content: { + 'application/json': { + schema: { + $ref: '#/components/schemas/PostAndPutClient', + }, + }, + }, + }, + responses: POST_CLIENT_RESPONSES, + tags: ['clients'], + } + + return doc +} diff --git a/app/api-v1/services/apiService.js b/app/api-v1/services/apiService.js index 2b73837..3ccf5c2 100644 --- a/app/api-v1/services/apiService.js +++ b/app/api-v1/services/apiService.js @@ -5,8 +5,15 @@ const { getProjectByIdDb, deleteProjectByIdDb, updateProjectDb, + postClientDb, } = require('../../db') +async function postClient(reqBody) { + const result = await postClientDb(reqBody) + + return { statusCode: 201, result: result[0] } +} + async function getProjects() { const result = await getProjectsDb() @@ -62,6 +69,7 @@ async function deleteProjectById(id) { } module.exports = { + postClient, getProjects, postProject, getProjectById, diff --git a/app/api-v1/validators/client/postClientResponseValidator.js b/app/api-v1/validators/client/postClientResponseValidator.js new file mode 100644 index 0000000..2d55476 --- /dev/null +++ b/app/api-v1/validators/client/postClientResponseValidator.js @@ -0,0 +1,28 @@ +const OpenAPIResponseValidator = require('openapi-response-validator').default + +const apiDocResponses = require('../../api-doc-responses') +const apiDoc = require('../../api-doc') + +const POST_CLIENT_RESPONSES = { + 201: { + description: 'POST client', + content: { + 'application/json': { + schema: apiDoc.components.schemas.GetClient, + }, + }, + }, + 400: apiDocResponses['400'], + default: apiDocResponses.default, +} + +const validatePostClientResponse = (statusCode, result) => { + const responseValidator = new OpenAPIResponseValidator({ responses: POST_CLIENT_RESPONSES }) + + return responseValidator.validateResponse(statusCode, result) +} + +module.exports = { + POST_CLIENT_RESPONSES, + validatePostClientResponse, +} diff --git a/app/db.js b/app/db.js index 08ebf20..8419eec 100644 --- a/app/db.js +++ b/app/db.js @@ -17,6 +17,21 @@ const client = knex({ }, }) +async function getClientsDb() { + return client('clients').select(['id', 'first_name AS firstName', 'last_name AS lastName', 'company', 'role']) +} + +async function postClientDb(reqBody) { + return client('clients') + .insert({ + first_name: reqBody.firstName, + last_name: reqBody.lastName, + company: reqBody.company, + role: reqBody.role, + }) + .returning(['id', 'first_name AS firstName', 'last_name AS lastName', 'company', 'role']) +} + async function postProjectDb(reqBody) { return client('projects') .insert({ @@ -96,6 +111,8 @@ async function updateProjectDb(id, reqBody) { module.exports = { client, + getClientsDb, + postClientDb, getProjectsDb, getProjectByNameDb, postProjectDb, diff --git a/test/helper/appHelper.js b/test/helper/appHelper.js index e79f696..20770fa 100644 --- a/test/helper/appHelper.js +++ b/test/helper/appHelper.js @@ -3,9 +3,7 @@ const { expect } = require('chai') const moment = require('moment') const createProject = (project) => { - const projectObj = { ...project } - - return projectObj + return { ...project } } const createDefaultProject = ({ clientId }) => { @@ -21,13 +19,11 @@ const createDefaultProject = ({ clientId }) => { } const createClient = (client) => { - const clientObj = { ...client } - - return clientObj + return { ...client } } const createDefaultClient = () => { - return createProject({ + return createClient({ firstName: 'First name 1', lastName: 'Last name 1', company: 'Company 1', @@ -39,6 +35,14 @@ const assertUuidV4 = (id) => { expect(uuidValidate(id) && uuidVersion(id) === 4).to.be.true } +const assertClientParams = (actualResult, expectedResult) => { + assertUuidV4(actualResult.id) + expect(actualResult.firstName).to.equal(expectedResult.firstName) + expect(actualResult.lastName).to.equal(expectedResult.lastName) + expect(actualResult.company).to.equal(expectedResult.company) + expect(actualResult.role).to.equal(expectedResult.role) +} + const assertPostProjectRequiredParams = (actualResult, expectedResult) => { assertUuidV4(actualResult.id) expect(actualResult.clientId).to.equal(expectedResult.clientId) @@ -66,6 +70,7 @@ const assertGetProjects = (actualResult, expectedResult) => { module.exports = { createClient, createDefaultClient, + assertClientParams, createProject, createDefaultProject, assertUuidV4, diff --git a/test/helper/routeHelper.js b/test/helper/routeHelper.js index eebc028..9ed3807 100644 --- a/test/helper/routeHelper.js +++ b/test/helper/routeHelper.js @@ -31,84 +31,7 @@ async function healthCheck({ app }) { }) } -async function getProjectsRoute({ app }) { - return request(app) - .get(`/${API_MAJOR_VERSION}/profiler/project`) - .set('Accept', 'application/json') - .set('Content-Type', 'application/json') - .then((response) => { - return response - }) - .catch((err) => { - console.error(`getProjectsErr ${err}`) - return err - }) -} - -async function getProjectByIdRoute(id, { app }) { - return request(app) - .get(`/${API_MAJOR_VERSION}/profiler/project/${id}`) - .set('Accept', 'application/json') - .set('Content-Type', 'application/json') - .then((response) => { - return response - }) - .catch((err) => { - console.error(`getProjectsErr ${err}`) - return err - }) -} - -async function postProjectRoute(project, { app }) { - return request(app) - .post(`/${API_MAJOR_VERSION}/profiler/project`) - .set('Accept', 'application/json') - .set('Content-Type', 'application/json') - .send(project) - .then((response) => { - return response - }) - .catch((err) => { - console.error(`postProjectErr ${err}`) - return err - }) -} - -async function putProjectRoute(id, project, { app }) { - return request(app) - .put(`/${API_MAJOR_VERSION}/profiler/project/${id}`) - .set('Accept', 'application/json') - .set('Content-Type', 'application/json') - .send(project) - .then((response) => { - return response - }) - .catch((err) => { - console.error(`putProjectErr ${err}`) - return err - }) -} - -async function deleteProjectByIdRoute(id, { app }) { - return request(app) - .delete(`/${API_MAJOR_VERSION}/profiler/project/${id}`) - .set('Accept', 'application/json') - .set('Content-Type', 'application/json') - .then((response) => { - return response - }) - .catch((err) => { - console.error(`getProjectsErr ${err}`) - return err - }) -} - module.exports = { apiDocs, healthCheck, - getProjectsRoute, - postProjectRoute, - getProjectByIdRoute, - putProjectRoute, - deleteProjectByIdRoute, } diff --git a/test/integration/clientRoutes.test.js b/test/integration/clientRoutes.test.js index be51a10..b3fa3ff 100644 --- a/test/integration/clientRoutes.test.js +++ b/test/integration/clientRoutes.test.js @@ -1,12 +1,12 @@ const { describe, test, before } = require('mocha') const { expect } = require('chai') -const { assertPostProjectParams, createDefaultClient } = require('../helper/appHelper') +const { createDefaultClient, assertClientParams } = require('../helper/appHelper') const { createHttpServer } = require('../../app/server') const { postClientRoute } = require('../helper/clientRouteHelper') const { cleanupAll } = require('../helper/seeds/project') -describe.skip('Client routes', function () { +describe('Client routes', function () { let app let defaultClient @@ -26,15 +26,13 @@ describe.skip('Client routes', function () { const response = await postClientRoute(defaultClient, app) expect(response.status).to.equal(201) - assertPostProjectParams(response.body, expectedResult) + assertClientParams(response.body, expectedResult) }) test.skip('POST client missing fields', async function () {}) test.skip('POST invalid client', async function () {}) - test.skip('POST duplicate client', async function () {}) - test.skip('GET clients', async function () {}) test.skip('GET client by id', async function () {}) @@ -45,8 +43,6 @@ describe.skip('Client routes', function () { test.skip('PUT client', async function () {}) - test.skip('PUT client with existing name', async function () {}) - test.skip('PUT client for non-existing client', async function () {}) test.skip('PUT client with invalid id path parameter', async function () {}) From 7484c01a8a061b38bd751b6133a51266ea693477 Mon Sep 17 00:00:00 2001 From: Darryl Morton Date: Fri, 29 Apr 2022 11:04:04 +0100 Subject: [PATCH 04/23] version bump --- helm/adcp-profiler-service/Chart.yaml | 4 ++-- helm/adcp-profiler-service/values.yaml | 2 +- package-lock.json | 4 ++-- package.json | 2 +- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/helm/adcp-profiler-service/Chart.yaml b/helm/adcp-profiler-service/Chart.yaml index 2d29935..e241741 100644 --- a/helm/adcp-profiler-service/Chart.yaml +++ b/helm/adcp-profiler-service/Chart.yaml @@ -1,8 +1,8 @@ apiVersion: v2 name: adcp-profiler-service -appVersion: '0.0.2' +appVersion: '0.0.3' description: A Helm chart for adcp-profiler-service -version: '0.0.2' +version: '0.0.3' type: application maintainers: - name: digicatapult diff --git a/helm/adcp-profiler-service/values.yaml b/helm/adcp-profiler-service/values.yaml index 8942988..351ae63 100644 --- a/helm/adcp-profiler-service/values.yaml +++ b/helm/adcp-profiler-service/values.yaml @@ -3,5 +3,5 @@ config: image: repository: ghcr.io/digicatapult/adcp-profiler-service pullPolicy: IfNotPresent - tag: 'v0.0.2' + tag: 'v0.0.3' pullSecrets: ['ghcr-digicatapult'] diff --git a/package-lock.json b/package-lock.json index d27c1fc..32a79bd 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@digicatapult/adcp-profiler-service", - "version": "0.0.2", + "version": "0.0.3", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@digicatapult/adcp-profiler-service", - "version": "0.0.2", + "version": "0.0.3", "license": "Apache-2.0", "dependencies": { "body-parser": "^1.20.0", diff --git a/package.json b/package.json index 729415b..af694b4 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@digicatapult/adcp-profiler-service", - "version": "0.0.2", + "version": "0.0.3", "description": "Insert repo description", "main": "app/index.js", "scripts": { From c3483c912d7a1024b39e28b4fa563e2d6d85ef90 Mon Sep 17 00:00:00 2001 From: Darryl Morton Date: Fri, 29 Apr 2022 11:08:37 +0100 Subject: [PATCH 05/23] client missing fields 400 test --- test/integration/clientRoutes.test.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/test/integration/clientRoutes.test.js b/test/integration/clientRoutes.test.js index b3fa3ff..aad449a 100644 --- a/test/integration/clientRoutes.test.js +++ b/test/integration/clientRoutes.test.js @@ -29,7 +29,12 @@ describe('Client routes', function () { assertClientParams(response.body, expectedResult) }) - test.skip('POST client missing fields', async function () {}) + test('POST client missing fields', async function () { + const response = await postClientRoute({}, app) + + expect(response.status).to.equal(400) + expect(response.body).to.deep.equal({}) + }) test.skip('POST invalid client', async function () {}) From 9d0db7ea0430bccf9576a54b500d091f3c464091 Mon Sep 17 00:00:00 2001 From: Darryl Morton Date: Fri, 29 Apr 2022 12:22:19 +0100 Subject: [PATCH 06/23] get clients route and test --- app/api-v1/routes/profiler/client.js | 13 ++++++++++++ app/api-v1/services/apiService.js | 8 +++++++ .../client/getClientsResponseValidator.js | 21 +++++++++++++++++++ test/helper/appHelper.js | 17 +++++++++++---- test/integration/clientRoutes.test.js | 20 ++++++++++++------ 5 files changed, 69 insertions(+), 10 deletions(-) create mode 100644 app/api-v1/validators/client/getClientsResponseValidator.js diff --git a/app/api-v1/routes/profiler/client.js b/app/api-v1/routes/profiler/client.js index 5608028..b8981c8 100644 --- a/app/api-v1/routes/profiler/client.js +++ b/app/api-v1/routes/profiler/client.js @@ -2,9 +2,16 @@ const { POST_CLIENT_RESPONSES, validatePostClientResponse, } = require('../../validators/client/postClientResponseValidator') +const { GET_CLIENTS_RESPONSES } = require('../../validators/client/getClientsResponseValidator') module.exports = function (apiService) { const doc = { + GET: async function (req, res) { + const { statusCode, result } = await apiService.getClients() + + res.status(statusCode).json(result) + return + }, POST: async function (req, res) { const { statusCode, result } = await apiService.postClient(req.body) @@ -20,6 +27,12 @@ module.exports = function (apiService) { }, } + doc.GET.apiDoc = { + summary: 'GET clients', + responses: GET_CLIENTS_RESPONSES, + tags: ['clients'], + } + doc.POST.apiDoc = { summary: 'POST client', requestBody: { diff --git a/app/api-v1/services/apiService.js b/app/api-v1/services/apiService.js index 3ccf5c2..3e2297b 100644 --- a/app/api-v1/services/apiService.js +++ b/app/api-v1/services/apiService.js @@ -6,8 +6,15 @@ const { deleteProjectByIdDb, updateProjectDb, postClientDb, + getClientsDb, } = require('../../db') +async function getClients() { + const result = await getClientsDb() + + return { statusCode: 200, result } +} + async function postClient(reqBody) { const result = await postClientDb(reqBody) @@ -69,6 +76,7 @@ async function deleteProjectById(id) { } module.exports = { + getClients, postClient, getProjects, postProject, diff --git a/app/api-v1/validators/client/getClientsResponseValidator.js b/app/api-v1/validators/client/getClientsResponseValidator.js new file mode 100644 index 0000000..fc4cead --- /dev/null +++ b/app/api-v1/validators/client/getClientsResponseValidator.js @@ -0,0 +1,21 @@ +const apiDocResponses = require('../../api-doc-responses') +const apiDoc = require('../../api-doc') + +const GET_CLIENTS_RESPONSES = { + 200: { + description: 'GET clients', + content: { + 'application/json': { + schema: { + type: 'array', + items: apiDoc.components.schemas.GetClient, + }, + }, + }, + }, + default: apiDocResponses.default, +} + +module.exports = { + GET_CLIENTS_RESPONSES, +} diff --git a/test/helper/appHelper.js b/test/helper/appHelper.js index 20770fa..2225ff3 100644 --- a/test/helper/appHelper.js +++ b/test/helper/appHelper.js @@ -43,6 +43,14 @@ const assertClientParams = (actualResult, expectedResult) => { expect(actualResult.role).to.equal(expectedResult.role) } +const assertGetClients = (actualResults, expectedResults) => { + expect(actualResults.length).to.equal(expectedResults.length) + + actualResults.forEach((actualResult, index) => { + assertClientParams(actualResult, expectedResults[index]) + }) +} + const assertPostProjectRequiredParams = (actualResult, expectedResult) => { assertUuidV4(actualResult.id) expect(actualResult.clientId).to.equal(expectedResult.clientId) @@ -59,11 +67,11 @@ const assertPostProjectParams = (actualResult, expectedResult) => { expect(actualResult.documentUrl).to.equal(expectedResult.documentUrl) } -const assertGetProjects = (actualResult, expectedResult) => { - expect(actualResult.length).to.equal(expectedResult.length) +const assertGetProjects = (actualResults, expectedResults) => { + expect(actualResults.length).to.equal(expectedResults.length) - actualResult.forEach((item, index) => { - assertPostProjectParams(item, expectedResult[index]) + actualResults.forEach((actualResult, index) => { + assertPostProjectParams(actualResult, expectedResults[index]) }) } @@ -71,6 +79,7 @@ module.exports = { createClient, createDefaultClient, assertClientParams, + assertGetClients, createProject, createDefaultProject, assertUuidV4, diff --git a/test/integration/clientRoutes.test.js b/test/integration/clientRoutes.test.js index aad449a..0e65553 100644 --- a/test/integration/clientRoutes.test.js +++ b/test/integration/clientRoutes.test.js @@ -1,23 +1,25 @@ const { describe, test, before } = require('mocha') const { expect } = require('chai') -const { createDefaultClient, assertClientParams } = require('../helper/appHelper') +const { createDefaultClient, assertClientParams, assertGetClients } = require('../helper/appHelper') const { createHttpServer } = require('../../app/server') -const { postClientRoute } = require('../helper/clientRouteHelper') -const { cleanupAll } = require('../helper/seeds/project') +const { postClientRoute, getClientsRoute } = require('../helper/clientRouteHelper') +const { cleanupAll, cleanup } = require('../helper/seeds/project') describe('Client routes', function () { let app let defaultClient before(async function () { + await cleanupAll() + app = await createHttpServer() defaultClient = createDefaultClient() }) beforeEach(async function () { - await cleanupAll() + await cleanup('clients') }) test('POST Client', async function () { @@ -36,9 +38,15 @@ describe('Client routes', function () { expect(response.body).to.deep.equal({}) }) - test.skip('POST invalid client', async function () {}) + test('GET clients', async function () { + const expectedResult = [defaultClient] - test.skip('GET clients', async function () {}) + await postClientRoute(defaultClient, app) + const response = await getClientsRoute(app) + + expect(response.status).to.equal(200) + assertGetClients(response.body, expectedResult) + }) test.skip('GET client by id', async function () {}) From d7ba28027bbaa8611c1468df907cc072f4dd56e0 Mon Sep 17 00:00:00 2001 From: Darryl Morton Date: Fri, 29 Apr 2022 12:39:24 +0100 Subject: [PATCH 07/23] added get clients route, db query, response validation and test --- app/api-v1/routes/profiler/client.js | 16 +++++++++++++--- .../client/getClientsResponseValidator.js | 8 ++++++++ 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/app/api-v1/routes/profiler/client.js b/app/api-v1/routes/profiler/client.js index b8981c8..0b563b8 100644 --- a/app/api-v1/routes/profiler/client.js +++ b/app/api-v1/routes/profiler/client.js @@ -2,15 +2,25 @@ const { POST_CLIENT_RESPONSES, validatePostClientResponse, } = require('../../validators/client/postClientResponseValidator') -const { GET_CLIENTS_RESPONSES } = require('../../validators/client/getClientsResponseValidator') +const { + GET_CLIENTS_RESPONSES, + validateGetClientsResponse, +} = require('../../validators/client/getClientsResponseValidator') module.exports = function (apiService) { const doc = { GET: async function (req, res) { const { statusCode, result } = await apiService.getClients() - res.status(statusCode).json(result) - return + const validationErrors = validateGetClientsResponse(statusCode, result) + + if (validationErrors) { + res.status(statusCode).json(validationErrors) + return + } else { + res.status(statusCode).json(result) + return + } }, POST: async function (req, res) { const { statusCode, result } = await apiService.postClient(req.body) diff --git a/app/api-v1/validators/client/getClientsResponseValidator.js b/app/api-v1/validators/client/getClientsResponseValidator.js index fc4cead..9544839 100644 --- a/app/api-v1/validators/client/getClientsResponseValidator.js +++ b/app/api-v1/validators/client/getClientsResponseValidator.js @@ -1,5 +1,6 @@ const apiDocResponses = require('../../api-doc-responses') const apiDoc = require('../../api-doc') +const { default: OpenAPIResponseValidator } = require('openapi-response-validator') const GET_CLIENTS_RESPONSES = { 200: { @@ -16,6 +17,13 @@ const GET_CLIENTS_RESPONSES = { default: apiDocResponses.default, } +const validateGetClientsResponse = (statusCode, result) => { + const responseValidator = new OpenAPIResponseValidator({ responses: GET_CLIENTS_RESPONSES }) + + return responseValidator.validateResponse(statusCode, result) +} + module.exports = { GET_CLIENTS_RESPONSES, + validateGetClientsResponse, } From 93ef4cb6491d2b465967d0d92d9b6433aee5498f Mon Sep 17 00:00:00 2001 From: Darryl Morton Date: Fri, 29 Apr 2022 12:58:30 +0100 Subject: [PATCH 08/23] get client by id route, response validator, db query and test --- app/api-v1/routes/profiler/client/{id}.js | 40 +++++++++++++++++++ app/api-v1/services/apiService.js | 12 ++++++ .../client/getClientByIdResponseValidator.js | 27 +++++++++++++ app/db.js | 7 ++++ test/integration/clientRoutes.test.js | 32 ++++++++++----- 5 files changed, 107 insertions(+), 11 deletions(-) create mode 100644 app/api-v1/routes/profiler/client/{id}.js create mode 100644 app/api-v1/validators/client/getClientByIdResponseValidator.js diff --git a/app/api-v1/routes/profiler/client/{id}.js b/app/api-v1/routes/profiler/client/{id}.js new file mode 100644 index 0000000..f65f862 --- /dev/null +++ b/app/api-v1/routes/profiler/client/{id}.js @@ -0,0 +1,40 @@ +const { + validateGetClientByIdResponse, + GET_CLIENT_BY_ID_RESPONSES, +} = require('../../../validators/client/getClientByIdResponseValidator') + +module.exports = function (apiService) { + const doc = { + GET: async function (req, res) { + const { id } = req.params + const { statusCode, result } = await apiService.getClientById(id) + + const validationErrors = validateGetClientByIdResponse(statusCode, result) + + if (validationErrors) { + res.status(statusCode).json(validationErrors) + return + } else { + res.status(statusCode).json(result) + return + } + }, + } + + doc.GET.apiDoc = { + summary: 'Get client by id', + parameters: [ + { + description: 'Client id', + in: 'path', + required: true, + name: 'id', + allowEmptyValue: false, + }, + ], + responses: GET_CLIENT_BY_ID_RESPONSES, + tags: ['clients'], + } + + return doc +} diff --git a/app/api-v1/services/apiService.js b/app/api-v1/services/apiService.js index 3e2297b..463259e 100644 --- a/app/api-v1/services/apiService.js +++ b/app/api-v1/services/apiService.js @@ -7,6 +7,7 @@ const { updateProjectDb, postClientDb, getClientsDb, + getClientByIdDb, } = require('../../db') async function getClients() { @@ -15,6 +16,16 @@ async function getClients() { return { statusCode: 200, result } } +async function getClientById(id) { + const result = await getClientByIdDb(id) + + if (result.length === 1) { + return { statusCode: 200, result: result[0] } + } else { + return { statusCode: 404, result: {} } + } +} + async function postClient(reqBody) { const result = await postClientDb(reqBody) @@ -77,6 +88,7 @@ async function deleteProjectById(id) { module.exports = { getClients, + getClientById, postClient, getProjects, postProject, diff --git a/app/api-v1/validators/client/getClientByIdResponseValidator.js b/app/api-v1/validators/client/getClientByIdResponseValidator.js new file mode 100644 index 0000000..989e332 --- /dev/null +++ b/app/api-v1/validators/client/getClientByIdResponseValidator.js @@ -0,0 +1,27 @@ +const apiDocResponses = require('../../api-doc-responses') +const apiDoc = require('../../api-doc') +const { default: OpenAPIResponseValidator } = require('openapi-response-validator') + +const GET_CLIENT_BY_ID_RESPONSES = { + 200: { + description: 'GET client by id', + content: { + 'application/json': { + schema: apiDoc.components.schemas.GetClient, + }, + }, + }, + 404: apiDocResponses['404'], + default: apiDocResponses.default, +} + +const validateGetClientByIdResponse = (statusCode, result) => { + const responseValidator = new OpenAPIResponseValidator({ responses: GET_CLIENT_BY_ID_RESPONSES }) + + return responseValidator.validateResponse(statusCode, result) +} + +module.exports = { + GET_CLIENT_BY_ID_RESPONSES, + validateGetClientByIdResponse, +} diff --git a/app/db.js b/app/db.js index 8419eec..25357fc 100644 --- a/app/db.js +++ b/app/db.js @@ -21,6 +21,12 @@ async function getClientsDb() { return client('clients').select(['id', 'first_name AS firstName', 'last_name AS lastName', 'company', 'role']) } +async function getClientByIdDb(id) { + return client('clients') + .select(['id', 'first_name AS firstName', 'last_name AS lastName', 'company', 'role']) + .where({ id }) +} + async function postClientDb(reqBody) { return client('clients') .insert({ @@ -112,6 +118,7 @@ async function updateProjectDb(id, reqBody) { module.exports = { client, getClientsDb, + getClientByIdDb, postClientDb, getProjectsDb, getProjectByNameDb, diff --git a/test/integration/clientRoutes.test.js b/test/integration/clientRoutes.test.js index 0e65553..7623e5a 100644 --- a/test/integration/clientRoutes.test.js +++ b/test/integration/clientRoutes.test.js @@ -3,7 +3,7 @@ const { expect } = require('chai') const { createDefaultClient, assertClientParams, assertGetClients } = require('../helper/appHelper') const { createHttpServer } = require('../../app/server') -const { postClientRoute, getClientsRoute } = require('../helper/clientRouteHelper') +const { postClientRoute, getClientsRoute, getClientByIdRoute } = require('../helper/clientRouteHelper') const { cleanupAll, cleanup } = require('../helper/seeds/project') describe('Client routes', function () { @@ -25,30 +25,40 @@ describe('Client routes', function () { test('POST Client', async function () { const expectedResult = defaultClient - const response = await postClientRoute(defaultClient, app) + const actualResponse = await postClientRoute(defaultClient, app) - expect(response.status).to.equal(201) - assertClientParams(response.body, expectedResult) + expect(actualResponse.status).to.equal(201) + assertClientParams(actualResponse.body, expectedResult) }) test('POST client missing fields', async function () { - const response = await postClientRoute({}, app) + const actualResponse = await postClientRoute({}, app) - expect(response.status).to.equal(400) - expect(response.body).to.deep.equal({}) + expect(actualResponse.status).to.equal(400) + expect(actualResponse.body).to.deep.equal({}) }) test('GET clients', async function () { const expectedResult = [defaultClient] await postClientRoute(defaultClient, app) - const response = await getClientsRoute(app) + const actualResponse = await getClientsRoute(app) - expect(response.status).to.equal(200) - assertGetClients(response.body, expectedResult) + expect(actualResponse.status).to.equal(200) + assertGetClients(actualResponse.body, expectedResult) }) - test.skip('GET client by id', async function () {}) + test('GET client by id', async function () { + const expectedResult = defaultClient + + const response = await postClientRoute(defaultClient, app) + + const actualResponse = await getClientByIdRoute(response.body.id, app) + console.log(actualResponse.body) + + expect(actualResponse.status).to.equal(200) + assertClientParams(actualResponse.body, expectedResult) + }) test.skip('GET client by id for non-existing client', async function () {}) From f0ab95e4cdb9918fc4681a990a4a4140d29bdc7d Mon Sep 17 00:00:00 2001 From: Darryl Morton Date: Fri, 29 Apr 2022 13:01:26 +0100 Subject: [PATCH 09/23] cleanup, get client by id 404 test --- test/integration/clientRoutes.test.js | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/test/integration/clientRoutes.test.js b/test/integration/clientRoutes.test.js index 7623e5a..aede980 100644 --- a/test/integration/clientRoutes.test.js +++ b/test/integration/clientRoutes.test.js @@ -9,6 +9,7 @@ const { cleanupAll, cleanup } = require('../helper/seeds/project') describe('Client routes', function () { let app let defaultClient + let invalidClientId before(async function () { await cleanupAll() @@ -16,6 +17,7 @@ describe('Client routes', function () { app = await createHttpServer() defaultClient = createDefaultClient() + invalidClientId = '00000000-0000-0000-0000-000000000000' }) beforeEach(async function () { @@ -52,15 +54,18 @@ describe('Client routes', function () { const expectedResult = defaultClient const response = await postClientRoute(defaultClient, app) - const actualResponse = await getClientByIdRoute(response.body.id, app) - console.log(actualResponse.body) expect(actualResponse.status).to.equal(200) assertClientParams(actualResponse.body, expectedResult) }) - test.skip('GET client by id for non-existing client', async function () {}) + test('GET client by id for non-existing client', async function () { + const actualResponse = await getClientByIdRoute(invalidClientId, app) + + expect(actualResponse.status).to.equal(404) + expect(actualResponse.body).to.deep.equal({}) + }) test.skip('GET client by id with invalid path id parameter', async function () {}) From 50c68a27eddf1bd4068a3faa99e0d600c68ed177 Mon Sep 17 00:00:00 2001 From: Darryl Morton Date: Fri, 29 Apr 2022 13:53:40 +0100 Subject: [PATCH 10/23] get client by id, 400 path param uuid format request validation with test --- app/api-v1/routes/profiler/client/{id}.js | 5 ++++- test/integration/clientRoutes.test.js | 7 ++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/app/api-v1/routes/profiler/client/{id}.js b/app/api-v1/routes/profiler/client/{id}.js index f65f862..96a0b2e 100644 --- a/app/api-v1/routes/profiler/client/{id}.js +++ b/app/api-v1/routes/profiler/client/{id}.js @@ -29,7 +29,10 @@ module.exports = function (apiService) { in: 'path', required: true, name: 'id', - allowEmptyValue: false, + schema: { + type: 'string', + format: 'uuid', + }, }, ], responses: GET_CLIENT_BY_ID_RESPONSES, diff --git a/test/integration/clientRoutes.test.js b/test/integration/clientRoutes.test.js index aede980..69ecf9a 100644 --- a/test/integration/clientRoutes.test.js +++ b/test/integration/clientRoutes.test.js @@ -67,7 +67,12 @@ describe('Client routes', function () { expect(actualResponse.body).to.deep.equal({}) }) - test.skip('GET client by id with invalid path id parameter', async function () {}) + test('GET client by id with invalid path id parameter', async function () { + const actualResponse = await getClientByIdRoute(1, app) + + expect(actualResponse.status).to.equal(400) + expect(actualResponse.body).to.deep.equal({}) + }) test.skip('PUT client', async function () {}) From 95695838ac02e78cb7026503df62b10bde257eee Mon Sep 17 00:00:00 2001 From: Darryl Morton Date: Fri, 29 Apr 2022 14:24:42 +0100 Subject: [PATCH 11/23] put client, response validator, db query, test --- app/api-v1/routes/profiler/client/{id}.js | 36 +++++++++++++++++++ app/api-v1/services/apiService.js | 14 ++++++++ .../client/putClientResponseValidator.js | 27 ++++++++++++++ app/db.js | 13 +++++++ test/integration/clientRoutes.test.js | 24 ++++++++++--- 5 files changed, 109 insertions(+), 5 deletions(-) create mode 100644 app/api-v1/validators/client/putClientResponseValidator.js diff --git a/app/api-v1/routes/profiler/client/{id}.js b/app/api-v1/routes/profiler/client/{id}.js index 96a0b2e..53c1195 100644 --- a/app/api-v1/routes/profiler/client/{id}.js +++ b/app/api-v1/routes/profiler/client/{id}.js @@ -2,6 +2,10 @@ const { validateGetClientByIdResponse, GET_CLIENT_BY_ID_RESPONSES, } = require('../../../validators/client/getClientByIdResponseValidator') +const { + PUT_CLIENT_RESPONSES, + validatePutClientResponse, +} = require('../../../validators/client/putClientResponseValidator') module.exports = function (apiService) { const doc = { @@ -11,6 +15,20 @@ module.exports = function (apiService) { const validationErrors = validateGetClientByIdResponse(statusCode, result) + if (validationErrors) { + res.status(statusCode).json(validationErrors) + return + } else { + res.status(statusCode).json(result) + return + } + }, + PUT: async function (req, res) { + const { id } = req.params + const { statusCode, result } = await apiService.putClient(id, req.body) + + const validationErrors = validatePutClientResponse(statusCode, result) + if (validationErrors) { res.status(statusCode).json(validationErrors) return @@ -39,5 +57,23 @@ module.exports = function (apiService) { tags: ['clients'], } + doc.PUT.apiDoc = { + summary: 'Put client', + parameters: [ + { + description: 'Client id', + in: 'path', + required: true, + name: 'id', + schema: { + type: 'string', + format: 'uuid', + }, + }, + ], + responses: PUT_CLIENT_RESPONSES, + tags: ['clients'], + } + return doc } diff --git a/app/api-v1/services/apiService.js b/app/api-v1/services/apiService.js index 463259e..f111ffc 100644 --- a/app/api-v1/services/apiService.js +++ b/app/api-v1/services/apiService.js @@ -8,6 +8,7 @@ const { postClientDb, getClientsDb, getClientByIdDb, + putClientDb, } = require('../../db') async function getClients() { @@ -32,6 +33,18 @@ async function postClient(reqBody) { return { statusCode: 201, result: result[0] } } +async function putClient(id, reqBody) { + const getClientByIdResult = await getClientByIdDb(id) + + if (getClientByIdResult.length === 1) { + const result = await putClientDb(id, reqBody) + + return { statusCode: 200, result: result[0] } + } else { + return { statusCode: 404, result: {} } + } +} + async function getProjects() { const result = await getProjectsDb() @@ -90,6 +103,7 @@ module.exports = { getClients, getClientById, postClient, + putClient, getProjects, postProject, getProjectById, diff --git a/app/api-v1/validators/client/putClientResponseValidator.js b/app/api-v1/validators/client/putClientResponseValidator.js new file mode 100644 index 0000000..2d30c73 --- /dev/null +++ b/app/api-v1/validators/client/putClientResponseValidator.js @@ -0,0 +1,27 @@ +const apiDocResponses = require('../../api-doc-responses') +const apiDoc = require('../../api-doc') +const { default: OpenAPIResponseValidator } = require('openapi-response-validator') + +const PUT_CLIENT_RESPONSES = { + 200: { + description: 'Update client', + content: { + 'application/json': { + schema: apiDoc.components.schemas.GetClient, + }, + }, + }, + 404: apiDocResponses['404'], + default: apiDocResponses.default, +} + +const validatePutClientResponse = (statusCode, result) => { + const responseValidator = new OpenAPIResponseValidator({ responses: PUT_CLIENT_RESPONSES }) + + return responseValidator.validateResponse(statusCode, result) +} + +module.exports = { + PUT_CLIENT_RESPONSES, + validatePutClientResponse, +} diff --git a/app/db.js b/app/db.js index 25357fc..efd33e0 100644 --- a/app/db.js +++ b/app/db.js @@ -38,6 +38,18 @@ async function postClientDb(reqBody) { .returning(['id', 'first_name AS firstName', 'last_name AS lastName', 'company', 'role']) } +async function putClientDb(id, reqBody) { + return client('clients') + .update({ + first_name: reqBody.firstName, + last_name: reqBody.lastName, + company: reqBody.company, + role: reqBody.role, + }) + .where({ id }) + .returning(['id', 'first_name AS firstName', 'last_name AS lastName', 'company', 'role']) +} + async function postProjectDb(reqBody) { return client('projects') .insert({ @@ -120,6 +132,7 @@ module.exports = { getClientsDb, getClientByIdDb, postClientDb, + putClientDb, getProjectsDb, getProjectByNameDb, postProjectDb, diff --git a/test/integration/clientRoutes.test.js b/test/integration/clientRoutes.test.js index 69ecf9a..c67934d 100644 --- a/test/integration/clientRoutes.test.js +++ b/test/integration/clientRoutes.test.js @@ -1,9 +1,9 @@ const { describe, test, before } = require('mocha') const { expect } = require('chai') -const { createDefaultClient, assertClientParams, assertGetClients } = require('../helper/appHelper') +const { createDefaultClient, assertClientParams, assertGetClients, createClient } = require('../helper/appHelper') const { createHttpServer } = require('../../app/server') -const { postClientRoute, getClientsRoute, getClientByIdRoute } = require('../helper/clientRouteHelper') +const { postClientRoute, getClientsRoute, getClientByIdRoute, putClientRoute } = require('../helper/clientRouteHelper') const { cleanupAll, cleanup } = require('../helper/seeds/project') describe('Client routes', function () { @@ -74,14 +74,28 @@ describe('Client routes', function () { expect(actualResponse.body).to.deep.equal({}) }) - test.skip('PUT client', async function () {}) + test('PUT client', async function () { + const client = createClient({ + firstName: 'First name 2', + lastName: 'Last name 2', + company: 'Company 2', + role: 'Role 2', + }) + const expectedResult = client - test.skip('PUT client for non-existing client', async function () {}) + const response = await postClientRoute(defaultClient, app) + const actualResponse = await putClientRoute(response.body.id, client, app) - test.skip('PUT client with invalid id path parameter', async function () {}) + expect(actualResponse.status).to.equal(200) + assertClientParams(actualResponse.body, expectedResult) + }) test.skip('PUT client with missing fields', async function () {}) + test.skip('PUT client for non-existing client', async function () {}) + + test.skip('PUT client with invalid id path parameter', async function () {}) + test.skip('DELETE client', async function () {}) test.skip('DELETE client for non-existing client', async function () {}) From f4b0651038bb0aad495c1b27f99e7c83892d3d58 Mon Sep 17 00:00:00 2001 From: Darryl Morton Date: Fri, 29 Apr 2022 14:35:48 +0100 Subject: [PATCH 12/23] put client request body validaiton, test for missing request body parameters --- app/api-v1/routes/profiler/client/{id}.js | 9 +++++++++ test/integration/clientRoutes.test.js | 8 +++++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/app/api-v1/routes/profiler/client/{id}.js b/app/api-v1/routes/profiler/client/{id}.js index 53c1195..036e1c0 100644 --- a/app/api-v1/routes/profiler/client/{id}.js +++ b/app/api-v1/routes/profiler/client/{id}.js @@ -71,6 +71,15 @@ module.exports = function (apiService) { }, }, ], + requestBody: { + content: { + 'application/json': { + schema: { + $ref: '#/components/schemas/PostAndPutClient', + }, + }, + }, + }, responses: PUT_CLIENT_RESPONSES, tags: ['clients'], } diff --git a/test/integration/clientRoutes.test.js b/test/integration/clientRoutes.test.js index c67934d..f9407bf 100644 --- a/test/integration/clientRoutes.test.js +++ b/test/integration/clientRoutes.test.js @@ -90,7 +90,13 @@ describe('Client routes', function () { assertClientParams(actualResponse.body, expectedResult) }) - test.skip('PUT client with missing fields', async function () {}) + test('PUT client with missing fields', async function () { + const response = await postClientRoute(defaultClient, app) + const actualResponse = await putClientRoute(response.body.id, {}, app) + + expect(actualResponse.status).to.equal(400) + expect(actualResponse.body).to.deep.equal({}) + }) test.skip('PUT client for non-existing client', async function () {}) From e9898fe0b3f8cb683057bdd31956df4a42f82eaf Mon Sep 17 00:00:00 2001 From: Darryl Morton Date: Fri, 29 Apr 2022 15:36:19 +0100 Subject: [PATCH 13/23] put test for non-existing client 404 --- test/integration/clientRoutes.test.js | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/test/integration/clientRoutes.test.js b/test/integration/clientRoutes.test.js index f9407bf..ad7928a 100644 --- a/test/integration/clientRoutes.test.js +++ b/test/integration/clientRoutes.test.js @@ -91,14 +91,18 @@ describe('Client routes', function () { }) test('PUT client with missing fields', async function () { - const response = await postClientRoute(defaultClient, app) - const actualResponse = await putClientRoute(response.body.id, {}, app) + const actualResponse = await putClientRoute(invalidClientId, {}, app) expect(actualResponse.status).to.equal(400) expect(actualResponse.body).to.deep.equal({}) }) - test.skip('PUT client for non-existing client', async function () {}) + test('PUT client for non-existing client', async function () { + const actualResponse = await putClientRoute(invalidClientId, defaultClient, app) + + expect(actualResponse.status).to.equal(404) + expect(actualResponse.body).to.deep.equal({}) + }) test.skip('PUT client with invalid id path parameter', async function () {}) From d50b0b3e5ac9639c0b0fa51798939f1f3a5fa67c Mon Sep 17 00:00:00 2001 From: Darryl Morton Date: Fri, 29 Apr 2022 15:37:35 +0100 Subject: [PATCH 14/23] put with invalid uuid path parameter 400 --- test/integration/clientRoutes.test.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/test/integration/clientRoutes.test.js b/test/integration/clientRoutes.test.js index ad7928a..b220d15 100644 --- a/test/integration/clientRoutes.test.js +++ b/test/integration/clientRoutes.test.js @@ -104,7 +104,12 @@ describe('Client routes', function () { expect(actualResponse.body).to.deep.equal({}) }) - test.skip('PUT client with invalid id path parameter', async function () {}) + test('PUT client with invalid id path parameter', async function () { + const actualResponse = await putClientRoute('123', defaultClient, app) + + expect(actualResponse.status).to.equal(400) + expect(actualResponse.body).to.deep.equal({}) + }) test.skip('DELETE client', async function () {}) From 7d304db1a4e79e3844502a9065b55fa872103356 Mon Sep 17 00:00:00 2001 From: Darryl Morton Date: Fri, 29 Apr 2022 16:01:27 +0100 Subject: [PATCH 15/23] delete client route, db query, request/response validation, test for 204 --- app/api-v1/routes/profiler/client/{id}.js | 35 +++++++++++++++++++ app/api-v1/services/apiService.js | 14 ++++++++ .../client/deleteClientResponseValidator.js | 21 +++++++++++ app/db.js | 5 +++ test/helper/clientRouteHelper.js | 4 +-- test/integration/clientRoutes.test.js | 16 +++++++-- 6 files changed, 91 insertions(+), 4 deletions(-) create mode 100644 app/api-v1/validators/client/deleteClientResponseValidator.js diff --git a/app/api-v1/routes/profiler/client/{id}.js b/app/api-v1/routes/profiler/client/{id}.js index 036e1c0..bd78be5 100644 --- a/app/api-v1/routes/profiler/client/{id}.js +++ b/app/api-v1/routes/profiler/client/{id}.js @@ -6,6 +6,10 @@ const { PUT_CLIENT_RESPONSES, validatePutClientResponse, } = require('../../../validators/client/putClientResponseValidator') +const { + validateDeleteClientResponse, + DELETE_CLIENT_RESPONSES, +} = require('../../../validators/client/deleteClientResponseValidator') module.exports = function (apiService) { const doc = { @@ -37,6 +41,19 @@ module.exports = function (apiService) { return } }, + DELETE: async function (req, res) { + const { id } = req.params + const { statusCode, result } = await apiService.deleteClient(id) + + const validationErrors = validateDeleteClientResponse(statusCode, result) + + if (validationErrors) { + res.status(statusCode).json(result) + return + } else { + res.status(statusCode).send() + } + }, } doc.GET.apiDoc = { @@ -84,5 +101,23 @@ module.exports = function (apiService) { tags: ['clients'], } + doc.DELETE.apiDoc = { + summary: 'Delete client', + parameters: [ + { + description: 'Client id', + in: 'path', + required: true, + name: 'id', + schema: { + type: 'string', + format: 'uuid', + }, + }, + ], + responses: DELETE_CLIENT_RESPONSES, + tags: ['clients'], + } + return doc } diff --git a/app/api-v1/services/apiService.js b/app/api-v1/services/apiService.js index f111ffc..c403733 100644 --- a/app/api-v1/services/apiService.js +++ b/app/api-v1/services/apiService.js @@ -9,6 +9,7 @@ const { getClientsDb, getClientByIdDb, putClientDb, + deleteClientDb, } = require('../../db') async function getClients() { @@ -45,6 +46,18 @@ async function putClient(id, reqBody) { } } +async function deleteClient(id) { + const getClientByIdResult = await getClientByIdDb(id) + + if (getClientByIdResult.length === 1) { + await deleteClientDb(id) + + return { statusCode: 204 } + } else { + return { statusCode: 404, result: {} } + } +} + async function getProjects() { const result = await getProjectsDb() @@ -104,6 +117,7 @@ module.exports = { getClientById, postClient, putClient, + deleteClient, getProjects, postProject, getProjectById, diff --git a/app/api-v1/validators/client/deleteClientResponseValidator.js b/app/api-v1/validators/client/deleteClientResponseValidator.js new file mode 100644 index 0000000..f8f0ace --- /dev/null +++ b/app/api-v1/validators/client/deleteClientResponseValidator.js @@ -0,0 +1,21 @@ +const apiDocResponses = require('../../api-doc-responses') +const { default: OpenAPIResponseValidator } = require('openapi-response-validator') + +const DELETE_CLIENT_RESPONSES = { + 204: { + description: 'Delete client', + }, + 404: apiDocResponses['404'], + default: apiDocResponses.default, +} + +const validateDeleteClientResponse = (statusCode, result) => { + const responseValidator = new OpenAPIResponseValidator({ responses: DELETE_CLIENT_RESPONSES }) + + return responseValidator.validateResponse(statusCode, result) +} + +module.exports = { + DELETE_CLIENT_RESPONSES, + validateDeleteClientResponse, +} diff --git a/app/db.js b/app/db.js index efd33e0..8447e56 100644 --- a/app/db.js +++ b/app/db.js @@ -50,6 +50,10 @@ async function putClientDb(id, reqBody) { .returning(['id', 'first_name AS firstName', 'last_name AS lastName', 'company', 'role']) } +async function deleteClientDb(id) { + return client('clients').del().where({ id }) +} + async function postProjectDb(reqBody) { return client('projects') .insert({ @@ -133,6 +137,7 @@ module.exports = { getClientByIdDb, postClientDb, putClientDb, + deleteClientDb, getProjectsDb, getProjectByNameDb, postProjectDb, diff --git a/test/helper/clientRouteHelper.js b/test/helper/clientRouteHelper.js index c04b9dd..bfdea33 100644 --- a/test/helper/clientRouteHelper.js +++ b/test/helper/clientRouteHelper.js @@ -61,7 +61,7 @@ async function putClientRoute(id, client, { app }) { }) } -async function deleteClientByIdRoute(id, { app }) { +async function deleteClientRoute(id, { app }) { return request(app) .delete(`/${API_MAJOR_VERSION}/profiler/client/${id}`) .set('Accept', 'application/json') @@ -80,5 +80,5 @@ module.exports = { postClientRoute, getClientByIdRoute, putClientRoute, - deleteClientByIdRoute, + deleteClientRoute, } diff --git a/test/integration/clientRoutes.test.js b/test/integration/clientRoutes.test.js index b220d15..6b7d43d 100644 --- a/test/integration/clientRoutes.test.js +++ b/test/integration/clientRoutes.test.js @@ -3,7 +3,13 @@ const { expect } = require('chai') const { createDefaultClient, assertClientParams, assertGetClients, createClient } = require('../helper/appHelper') const { createHttpServer } = require('../../app/server') -const { postClientRoute, getClientsRoute, getClientByIdRoute, putClientRoute } = require('../helper/clientRouteHelper') +const { + postClientRoute, + getClientsRoute, + getClientByIdRoute, + putClientRoute, + deleteClientRoute, +} = require('../helper/clientRouteHelper') const { cleanupAll, cleanup } = require('../helper/seeds/project') describe('Client routes', function () { @@ -111,7 +117,13 @@ describe('Client routes', function () { expect(actualResponse.body).to.deep.equal({}) }) - test.skip('DELETE client', async function () {}) + test('DELETE client', async function () { + const response = await postClientRoute(defaultClient, app) + const actualResponse = await deleteClientRoute(response.body.id, app) + + expect(actualResponse.status).to.equal(204) + expect(actualResponse.body).to.deep.equal({}) + }) test.skip('DELETE client for non-existing client', async function () {}) From 029439e0d02d10eeacdf3bf98a8f313d757275f4 Mon Sep 17 00:00:00 2001 From: Darryl Morton Date: Fri, 29 Apr 2022 16:03:07 +0100 Subject: [PATCH 16/23] delete client test for 404 --- test/integration/clientRoutes.test.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/test/integration/clientRoutes.test.js b/test/integration/clientRoutes.test.js index 6b7d43d..70cf556 100644 --- a/test/integration/clientRoutes.test.js +++ b/test/integration/clientRoutes.test.js @@ -125,7 +125,12 @@ describe('Client routes', function () { expect(actualResponse.body).to.deep.equal({}) }) - test.skip('DELETE client for non-existing client', async function () {}) + test('DELETE client for non-existing client', async function () { + const actualResponse = await deleteClientRoute(invalidClientId, app) + + expect(actualResponse.status).to.equal(404) + expect(actualResponse.body).to.deep.equal({}) + }) test.skip('DELETE client with invalid id path parameter', async function () {}) }) From aadbd50e5539cb9c13ee39ebf53a6c44e01a54af Mon Sep 17 00:00:00 2001 From: Darryl Morton Date: Fri, 29 Apr 2022 16:05:02 +0100 Subject: [PATCH 17/23] delete client with invalid uuid path parameter 400 --- test/integration/clientRoutes.test.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/test/integration/clientRoutes.test.js b/test/integration/clientRoutes.test.js index 70cf556..3ce40bc 100644 --- a/test/integration/clientRoutes.test.js +++ b/test/integration/clientRoutes.test.js @@ -132,5 +132,10 @@ describe('Client routes', function () { expect(actualResponse.body).to.deep.equal({}) }) - test.skip('DELETE client with invalid id path parameter', async function () {}) + test('DELETE client with invalid id path parameter', async function () { + const actualResponse = await deleteClientRoute('123', app) + + expect(actualResponse.status).to.equal(400) + expect(actualResponse.body).to.deep.equal({}) + }) }) From bad79e772411027517392c7011e232c39593483c Mon Sep 17 00:00:00 2001 From: Darryl Morton Date: Fri, 29 Apr 2022 16:21:58 +0100 Subject: [PATCH 18/23] renamed client db function names, openapi client metadata updates --- app/api-v1/routes/profiler/client.js | 4 +-- app/api-v1/routes/profiler/client/{id}.js | 6 ++-- app/api-v1/services/apiService.js | 28 +++++++++---------- .../client/deleteClientResponseValidator.js | 2 +- .../client/getClientByIdResponseValidator.js | 2 +- .../client/getClientsResponseValidator.js | 2 +- .../client/postClientResponseValidator.js | 2 +- .../client/putClientResponseValidator.js | 2 +- app/db.js | 20 ++++++------- 9 files changed, 34 insertions(+), 34 deletions(-) diff --git a/app/api-v1/routes/profiler/client.js b/app/api-v1/routes/profiler/client.js index 0b563b8..4b5247f 100644 --- a/app/api-v1/routes/profiler/client.js +++ b/app/api-v1/routes/profiler/client.js @@ -38,13 +38,13 @@ module.exports = function (apiService) { } doc.GET.apiDoc = { - summary: 'GET clients', + summary: 'Retrieve clients', responses: GET_CLIENTS_RESPONSES, tags: ['clients'], } doc.POST.apiDoc = { - summary: 'POST client', + summary: 'Create client', requestBody: { content: { 'application/json': { diff --git a/app/api-v1/routes/profiler/client/{id}.js b/app/api-v1/routes/profiler/client/{id}.js index bd78be5..61865c8 100644 --- a/app/api-v1/routes/profiler/client/{id}.js +++ b/app/api-v1/routes/profiler/client/{id}.js @@ -57,7 +57,7 @@ module.exports = function (apiService) { } doc.GET.apiDoc = { - summary: 'Get client by id', + summary: 'Retrieve client by id', parameters: [ { description: 'Client id', @@ -75,7 +75,7 @@ module.exports = function (apiService) { } doc.PUT.apiDoc = { - summary: 'Put client', + summary: 'Update client', parameters: [ { description: 'Client id', @@ -102,7 +102,7 @@ module.exports = function (apiService) { } doc.DELETE.apiDoc = { - summary: 'Delete client', + summary: 'Remove client', parameters: [ { description: 'Client id', diff --git a/app/api-v1/services/apiService.js b/app/api-v1/services/apiService.js index c403733..43c6664 100644 --- a/app/api-v1/services/apiService.js +++ b/app/api-v1/services/apiService.js @@ -5,21 +5,21 @@ const { getProjectByIdDb, deleteProjectByIdDb, updateProjectDb, - postClientDb, - getClientsDb, - getClientByIdDb, - putClientDb, - deleteClientDb, + addClientDb, + findClientsDb, + findClientByIdDb, + updateClientDb, + removeClientDb, } = require('../../db') async function getClients() { - const result = await getClientsDb() + const result = await findClientsDb() return { statusCode: 200, result } } async function getClientById(id) { - const result = await getClientByIdDb(id) + const result = await findClientByIdDb(id) if (result.length === 1) { return { statusCode: 200, result: result[0] } @@ -29,16 +29,16 @@ async function getClientById(id) { } async function postClient(reqBody) { - const result = await postClientDb(reqBody) + const result = await addClientDb(reqBody) return { statusCode: 201, result: result[0] } } async function putClient(id, reqBody) { - const getClientByIdResult = await getClientByIdDb(id) + const findResult = await findClientByIdDb(id) - if (getClientByIdResult.length === 1) { - const result = await putClientDb(id, reqBody) + if (findResult.length === 1) { + const result = await updateClientDb(id, reqBody) return { statusCode: 200, result: result[0] } } else { @@ -47,10 +47,10 @@ async function putClient(id, reqBody) { } async function deleteClient(id) { - const getClientByIdResult = await getClientByIdDb(id) + const findResult = await findClientByIdDb(id) - if (getClientByIdResult.length === 1) { - await deleteClientDb(id) + if (findResult.length === 1) { + await removeClientDb(id) return { statusCode: 204 } } else { diff --git a/app/api-v1/validators/client/deleteClientResponseValidator.js b/app/api-v1/validators/client/deleteClientResponseValidator.js index f8f0ace..4c5aa16 100644 --- a/app/api-v1/validators/client/deleteClientResponseValidator.js +++ b/app/api-v1/validators/client/deleteClientResponseValidator.js @@ -3,7 +3,7 @@ const { default: OpenAPIResponseValidator } = require('openapi-response-validato const DELETE_CLIENT_RESPONSES = { 204: { - description: 'Delete client', + description: 'Deleted client', }, 404: apiDocResponses['404'], default: apiDocResponses.default, diff --git a/app/api-v1/validators/client/getClientByIdResponseValidator.js b/app/api-v1/validators/client/getClientByIdResponseValidator.js index 989e332..9503d83 100644 --- a/app/api-v1/validators/client/getClientByIdResponseValidator.js +++ b/app/api-v1/validators/client/getClientByIdResponseValidator.js @@ -4,7 +4,7 @@ const { default: OpenAPIResponseValidator } = require('openapi-response-validato const GET_CLIENT_BY_ID_RESPONSES = { 200: { - description: 'GET client by id', + description: 'Retrieved client by id', content: { 'application/json': { schema: apiDoc.components.schemas.GetClient, diff --git a/app/api-v1/validators/client/getClientsResponseValidator.js b/app/api-v1/validators/client/getClientsResponseValidator.js index 9544839..cd35bb1 100644 --- a/app/api-v1/validators/client/getClientsResponseValidator.js +++ b/app/api-v1/validators/client/getClientsResponseValidator.js @@ -4,7 +4,7 @@ const { default: OpenAPIResponseValidator } = require('openapi-response-validato const GET_CLIENTS_RESPONSES = { 200: { - description: 'GET clients', + description: 'Retrieved clients', content: { 'application/json': { schema: { diff --git a/app/api-v1/validators/client/postClientResponseValidator.js b/app/api-v1/validators/client/postClientResponseValidator.js index 2d55476..2da7b11 100644 --- a/app/api-v1/validators/client/postClientResponseValidator.js +++ b/app/api-v1/validators/client/postClientResponseValidator.js @@ -5,7 +5,7 @@ const apiDoc = require('../../api-doc') const POST_CLIENT_RESPONSES = { 201: { - description: 'POST client', + description: 'Created client', content: { 'application/json': { schema: apiDoc.components.schemas.GetClient, diff --git a/app/api-v1/validators/client/putClientResponseValidator.js b/app/api-v1/validators/client/putClientResponseValidator.js index 2d30c73..6f74f9d 100644 --- a/app/api-v1/validators/client/putClientResponseValidator.js +++ b/app/api-v1/validators/client/putClientResponseValidator.js @@ -4,7 +4,7 @@ const { default: OpenAPIResponseValidator } = require('openapi-response-validato const PUT_CLIENT_RESPONSES = { 200: { - description: 'Update client', + description: 'Updated client', content: { 'application/json': { schema: apiDoc.components.schemas.GetClient, diff --git a/app/db.js b/app/db.js index 8447e56..cc58156 100644 --- a/app/db.js +++ b/app/db.js @@ -17,17 +17,17 @@ const client = knex({ }, }) -async function getClientsDb() { +async function findClientsDb() { return client('clients').select(['id', 'first_name AS firstName', 'last_name AS lastName', 'company', 'role']) } -async function getClientByIdDb(id) { +async function findClientByIdDb(id) { return client('clients') .select(['id', 'first_name AS firstName', 'last_name AS lastName', 'company', 'role']) .where({ id }) } -async function postClientDb(reqBody) { +async function addClientDb(reqBody) { return client('clients') .insert({ first_name: reqBody.firstName, @@ -38,7 +38,7 @@ async function postClientDb(reqBody) { .returning(['id', 'first_name AS firstName', 'last_name AS lastName', 'company', 'role']) } -async function putClientDb(id, reqBody) { +async function updateClientDb(id, reqBody) { return client('clients') .update({ first_name: reqBody.firstName, @@ -50,7 +50,7 @@ async function putClientDb(id, reqBody) { .returning(['id', 'first_name AS firstName', 'last_name AS lastName', 'company', 'role']) } -async function deleteClientDb(id) { +async function removeClientDb(id) { return client('clients').del().where({ id }) } @@ -133,11 +133,11 @@ async function updateProjectDb(id, reqBody) { module.exports = { client, - getClientsDb, - getClientByIdDb, - postClientDb, - putClientDb, - deleteClientDb, + findClientsDb, + findClientByIdDb, + addClientDb, + updateClientDb, + removeClientDb, getProjectsDb, getProjectByNameDb, postProjectDb, From 8be28e6870948b9018149b46d133da0ecbd394e5 Mon Sep 17 00:00:00 2001 From: Darryl Morton Date: Fri, 29 Apr 2022 16:29:33 +0100 Subject: [PATCH 19/23] client openapi metdata updates --- app/api-v1/api-doc.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/api-v1/api-doc.js b/app/api-v1/api-doc.js index cb2c20e..b4e0e5d 100644 --- a/app/api-v1/api-doc.js +++ b/app/api-v1/api-doc.js @@ -31,7 +31,7 @@ const apiDoc = { }, schemas: { GetClient: { - description: 'GET client', + description: 'Schema for returning a client', type: 'object', properties: { id: { @@ -59,7 +59,7 @@ const apiDoc = { required: ['id', 'firstName', 'lastName', 'company', 'role'], }, PostAndPutClient: { - description: 'POST/PUT client', + description: 'Schema for creating/updating a client', type: 'object', properties: { firstName: { From 7d3117d8d8944692f631545aa9e5a0c2fc544bd2 Mon Sep 17 00:00:00 2001 From: Darryl Morton Date: Fri, 29 Apr 2022 16:33:23 +0100 Subject: [PATCH 20/23] delete client returns validation errors --- app/api-v1/routes/profiler/client/{id}.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/api-v1/routes/profiler/client/{id}.js b/app/api-v1/routes/profiler/client/{id}.js index 61865c8..21e8a12 100644 --- a/app/api-v1/routes/profiler/client/{id}.js +++ b/app/api-v1/routes/profiler/client/{id}.js @@ -48,7 +48,7 @@ module.exports = function (apiService) { const validationErrors = validateDeleteClientResponse(statusCode, result) if (validationErrors) { - res.status(statusCode).json(result) + res.status(statusCode).json(validationErrors) return } else { res.status(statusCode).send() From a61dc9c941103d33f1517d18f9358d3f5b3171ae Mon Sep 17 00:00:00 2001 From: Darryl Morton Date: Fri, 29 Apr 2022 16:35:53 +0100 Subject: [PATCH 21/23] client openapi metadata updates --- app/api-v1/routes/profiler/client/{id}.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app/api-v1/routes/profiler/client/{id}.js b/app/api-v1/routes/profiler/client/{id}.js index 21e8a12..27a2aac 100644 --- a/app/api-v1/routes/profiler/client/{id}.js +++ b/app/api-v1/routes/profiler/client/{id}.js @@ -60,7 +60,7 @@ module.exports = function (apiService) { summary: 'Retrieve client by id', parameters: [ { - description: 'Client id', + description: 'Id of the client', in: 'path', required: true, name: 'id', @@ -78,7 +78,7 @@ module.exports = function (apiService) { summary: 'Update client', parameters: [ { - description: 'Client id', + description: 'Id of the client', in: 'path', required: true, name: 'id', @@ -105,7 +105,7 @@ module.exports = function (apiService) { summary: 'Remove client', parameters: [ { - description: 'Client id', + description: 'Id of the client', in: 'path', required: true, name: 'id', From da2c59490c332496ba58d2375e4ad618a937ef6b Mon Sep 17 00:00:00 2001 From: Darryl Morton Date: Fri, 29 Apr 2022 16:55:57 +0100 Subject: [PATCH 22/23] PR large enough, project changes will be in a separate PR --- app/api-v1/routes/profiler/project.js | 7 ++----- app/api-v1/routes/profiler/project/{id}.js | 9 +++------ .../{project => }/deleteProjectResponseValidator.js | 4 ++-- .../{project => }/getProjectByIdResponseValidator.js | 4 ++-- .../{project => }/getProjectsResponseValidator.js | 4 ++-- .../{project => }/postProjectResponseValidator.js | 4 ++-- .../{project => }/putProjectResponseValidator.js | 4 ++-- 7 files changed, 15 insertions(+), 21 deletions(-) rename app/api-v1/validators/{project => }/deleteProjectResponseValidator.js (86%) rename app/api-v1/validators/{project => }/getProjectByIdResponseValidator.js (87%) rename app/api-v1/validators/{project => }/getProjectsResponseValidator.js (77%) rename app/api-v1/validators/{project => }/postProjectResponseValidator.js (87%) rename app/api-v1/validators/{project => }/putProjectResponseValidator.js (87%) diff --git a/app/api-v1/routes/profiler/project.js b/app/api-v1/routes/profiler/project.js index 4d61ca7..7b0ef12 100644 --- a/app/api-v1/routes/profiler/project.js +++ b/app/api-v1/routes/profiler/project.js @@ -1,8 +1,5 @@ -const { GET_PROJECTS_RESPONSES } = require('../../validators/project/getProjectsResponseValidator') -const { - POST_PROJECT_RESPONSES, - validatePostProjectResponse, -} = require('../../validators/project/postProjectResponseValidator') +const { GET_PROJECTS_RESPONSES } = require('../../validators/getProjectsResponseValidator') +const { POST_PROJECT_RESPONSES, validatePostProjectResponse } = require('../../validators/postProjectResponseValidator') module.exports = function (apiService) { const doc = { diff --git a/app/api-v1/routes/profiler/project/{id}.js b/app/api-v1/routes/profiler/project/{id}.js index e527b4e..b476860 100644 --- a/app/api-v1/routes/profiler/project/{id}.js +++ b/app/api-v1/routes/profiler/project/{id}.js @@ -1,15 +1,12 @@ const { validateGetProjectByIdResponse, GET_PROJECT_BY_ID_RESPONSES, -} = require('../../../validators/project/getProjectByIdResponseValidator') -const { - PUT_PROJECT_RESPONSES, - validatePutProjectResponse, -} = require('../../../validators/project/putProjectResponseValidator') +} = require('../../../validators/getProjectByIdResponseValidator') +const { PUT_PROJECT_RESPONSES, validatePutProjectResponse } = require('../../../validators/putProjectResponseValidator') const { DELETE_PROJECT_RESPONSES, validateDeleteProjectResponse, -} = require('../../../validators/project/deleteProjectResponseValidator') +} = require('../../../validators/deleteProjectResponseValidator') module.exports = function (apiService) { const doc = { diff --git a/app/api-v1/validators/project/deleteProjectResponseValidator.js b/app/api-v1/validators/deleteProjectResponseValidator.js similarity index 86% rename from app/api-v1/validators/project/deleteProjectResponseValidator.js rename to app/api-v1/validators/deleteProjectResponseValidator.js index 5dc5cf8..f60dfe1 100644 --- a/app/api-v1/validators/project/deleteProjectResponseValidator.js +++ b/app/api-v1/validators/deleteProjectResponseValidator.js @@ -1,7 +1,7 @@ const OpenAPIResponseValidator = require('openapi-response-validator').default -const apiDocResponses = require('../../api-doc-responses') -const apiDoc = require('../../api-doc') +const apiDocResponses = require('../api-doc-responses') +const apiDoc = require('../api-doc') const DELETE_PROJECT_RESPONSES = { 201: { diff --git a/app/api-v1/validators/project/getProjectByIdResponseValidator.js b/app/api-v1/validators/getProjectByIdResponseValidator.js similarity index 87% rename from app/api-v1/validators/project/getProjectByIdResponseValidator.js rename to app/api-v1/validators/getProjectByIdResponseValidator.js index 50d2dfc..d0dc6d0 100644 --- a/app/api-v1/validators/project/getProjectByIdResponseValidator.js +++ b/app/api-v1/validators/getProjectByIdResponseValidator.js @@ -1,7 +1,7 @@ const OpenAPIResponseValidator = require('openapi-response-validator').default -const apiDocResponses = require('../../api-doc-responses') -const apiDoc = require('../../api-doc') +const apiDocResponses = require('../api-doc-responses') +const apiDoc = require('../api-doc') const GET_PROJECT_BY_ID_RESPONSES = { 200: { diff --git a/app/api-v1/validators/project/getProjectsResponseValidator.js b/app/api-v1/validators/getProjectsResponseValidator.js similarity index 77% rename from app/api-v1/validators/project/getProjectsResponseValidator.js rename to app/api-v1/validators/getProjectsResponseValidator.js index d60fe1d..46865c3 100644 --- a/app/api-v1/validators/project/getProjectsResponseValidator.js +++ b/app/api-v1/validators/getProjectsResponseValidator.js @@ -1,5 +1,5 @@ -const apiDocResponses = require('../../api-doc-responses') -const apiDoc = require('../../api-doc') +const apiDocResponses = require('../api-doc-responses') +const apiDoc = require('../api-doc') const GET_PROJECTS_RESPONSES = { 200: { diff --git a/app/api-v1/validators/project/postProjectResponseValidator.js b/app/api-v1/validators/postProjectResponseValidator.js similarity index 87% rename from app/api-v1/validators/project/postProjectResponseValidator.js rename to app/api-v1/validators/postProjectResponseValidator.js index 5f49fdf..231daa5 100644 --- a/app/api-v1/validators/project/postProjectResponseValidator.js +++ b/app/api-v1/validators/postProjectResponseValidator.js @@ -1,7 +1,7 @@ const OpenAPIResponseValidator = require('openapi-response-validator').default -const apiDocResponses = require('../../api-doc-responses') -const apiDoc = require('../../api-doc') +const apiDocResponses = require('../api-doc-responses') +const apiDoc = require('../api-doc') const POST_PROJECT_RESPONSES = { 201: { diff --git a/app/api-v1/validators/project/putProjectResponseValidator.js b/app/api-v1/validators/putProjectResponseValidator.js similarity index 87% rename from app/api-v1/validators/project/putProjectResponseValidator.js rename to app/api-v1/validators/putProjectResponseValidator.js index 1f612f7..2cd12f7 100644 --- a/app/api-v1/validators/project/putProjectResponseValidator.js +++ b/app/api-v1/validators/putProjectResponseValidator.js @@ -1,7 +1,7 @@ const OpenAPIResponseValidator = require('openapi-response-validator').default -const apiDocResponses = require('../../api-doc-responses') -const apiDoc = require('../../api-doc') +const apiDocResponses = require('../api-doc-responses') +const apiDoc = require('../api-doc') const PUT_PROJECT_RESPONSES = { 200: { From 4ab7ed5ce34fac9f810f124f2eee486aefaa20f0 Mon Sep 17 00:00:00 2001 From: Darryl Morton Date: Fri, 29 Apr 2022 18:10:00 +0100 Subject: [PATCH 23/23] client openapi metadata change --- app/api-v1/api-doc.js | 2 +- app/api-v1/validators/client/deleteClientResponseValidator.js | 3 ++- app/api-v1/validators/client/getClientByIdResponseValidator.js | 3 ++- app/api-v1/validators/client/getClientsResponseValidator.js | 3 ++- app/api-v1/validators/client/postClientResponseValidator.js | 2 +- app/api-v1/validators/client/putClientResponseValidator.js | 3 ++- 6 files changed, 10 insertions(+), 6 deletions(-) diff --git a/app/api-v1/api-doc.js b/app/api-v1/api-doc.js index b4e0e5d..c3faed1 100644 --- a/app/api-v1/api-doc.js +++ b/app/api-v1/api-doc.js @@ -31,7 +31,7 @@ const apiDoc = { }, schemas: { GetClient: { - description: 'Schema for returning a client', + description: 'Schema for retrieving a client', type: 'object', properties: { id: { diff --git a/app/api-v1/validators/client/deleteClientResponseValidator.js b/app/api-v1/validators/client/deleteClientResponseValidator.js index 4c5aa16..3cda9a4 100644 --- a/app/api-v1/validators/client/deleteClientResponseValidator.js +++ b/app/api-v1/validators/client/deleteClientResponseValidator.js @@ -1,6 +1,7 @@ -const apiDocResponses = require('../../api-doc-responses') const { default: OpenAPIResponseValidator } = require('openapi-response-validator') +const apiDocResponses = require('../../api-doc-responses') + const DELETE_CLIENT_RESPONSES = { 204: { description: 'Deleted client', diff --git a/app/api-v1/validators/client/getClientByIdResponseValidator.js b/app/api-v1/validators/client/getClientByIdResponseValidator.js index 9503d83..b617c66 100644 --- a/app/api-v1/validators/client/getClientByIdResponseValidator.js +++ b/app/api-v1/validators/client/getClientByIdResponseValidator.js @@ -1,6 +1,7 @@ +const { default: OpenAPIResponseValidator } = require('openapi-response-validator') + const apiDocResponses = require('../../api-doc-responses') const apiDoc = require('../../api-doc') -const { default: OpenAPIResponseValidator } = require('openapi-response-validator') const GET_CLIENT_BY_ID_RESPONSES = { 200: { diff --git a/app/api-v1/validators/client/getClientsResponseValidator.js b/app/api-v1/validators/client/getClientsResponseValidator.js index cd35bb1..8f272a5 100644 --- a/app/api-v1/validators/client/getClientsResponseValidator.js +++ b/app/api-v1/validators/client/getClientsResponseValidator.js @@ -1,6 +1,7 @@ +const { default: OpenAPIResponseValidator } = require('openapi-response-validator') + const apiDocResponses = require('../../api-doc-responses') const apiDoc = require('../../api-doc') -const { default: OpenAPIResponseValidator } = require('openapi-response-validator') const GET_CLIENTS_RESPONSES = { 200: { diff --git a/app/api-v1/validators/client/postClientResponseValidator.js b/app/api-v1/validators/client/postClientResponseValidator.js index 2da7b11..d8463bb 100644 --- a/app/api-v1/validators/client/postClientResponseValidator.js +++ b/app/api-v1/validators/client/postClientResponseValidator.js @@ -1,4 +1,4 @@ -const OpenAPIResponseValidator = require('openapi-response-validator').default +const { default: OpenAPIResponseValidator } = require('openapi-response-validator') const apiDocResponses = require('../../api-doc-responses') const apiDoc = require('../../api-doc') diff --git a/app/api-v1/validators/client/putClientResponseValidator.js b/app/api-v1/validators/client/putClientResponseValidator.js index 6f74f9d..3ffb8a4 100644 --- a/app/api-v1/validators/client/putClientResponseValidator.js +++ b/app/api-v1/validators/client/putClientResponseValidator.js @@ -1,6 +1,7 @@ +const { default: OpenAPIResponseValidator } = require('openapi-response-validator') + const apiDocResponses = require('../../api-doc-responses') const apiDoc = require('../../api-doc') -const { default: OpenAPIResponseValidator } = require('openapi-response-validator') const PUT_CLIENT_RESPONSES = { 200: {