Skip to content

Commit

Permalink
feat: integrate legacy API to capture creation
Browse files Browse the repository at this point in the history
  • Loading branch information
Kpoke committed Sep 12, 2022
1 parent 29ff4b3 commit 2a5774f
Show file tree
Hide file tree
Showing 27 changed files with 301 additions and 1,235 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
const request = require('supertest');
require('dotenv').config();
const { expect } = require('chai');
require('../../setup');
const request = require('supertest');
const sinon = require('sinon');
const { expect } = require('chai');
const uuid = require('uuid');
const LegacyAPI = require('../../../server/services/LegacyAPIService');
const app = require('../../../server/app');
const capture2 = require('../../mock/capture2.json');
const capture1 = require('../../mock/capture1.json');
Expand Down Expand Up @@ -52,6 +54,23 @@ describe('/captures', () => {
});

describe('POST', () => {
const legacyExtraObjects = {
capture_approval_tag: 'approved',
species_id_int: 12,
organization_id: 11,
};
let legacyAPIApproveTreeStub;

beforeEach(async () => {
legacyAPIApproveTreeStub = sinon
.stub(LegacyAPI, 'approveLegacyTree')
.resolves();
});

afterEach(async () => {
legacyAPIApproveTreeStub.restore();
});

before(async () => {
await addCapture({
...capture1,
Expand All @@ -69,10 +88,26 @@ describe('/captures', () => {
it('should create a capture', async () => {
const res = await request(app)
.post(`/captures`)
.send(capture2)
.send({
...capture2,
...legacyExtraObjects,
})
.set('Accept', 'application/json')
.set('Authorization', 'jwt_token')
.expect(201);

expect(
legacyAPIApproveTreeStub.calledOnceWithExactly({
id: capture2.reference_id,
speciesId: legacyExtraObjects.species_id_int,
morphology: capture2.morphology,
age: `${capture2.age}`,
captureApprovalTag: legacyExtraObjects.capture_approval_tag,
legacyAPIAuthorizationHeader: 'jwt_token',
organizationId: legacyExtraObjects.organization_id,
}),
).eql(true);

expect(res.body).to.include({
image_url: capture2.image_url,
planting_organization_id: capture2.planting_organization_id,
Expand All @@ -86,10 +121,26 @@ describe('/captures', () => {
it('should not error out when duplicate data is sent', async () => {
const res = await request(app)
.post(`/captures`)
.send(capture2)
.send({
...capture2,
...legacyExtraObjects,
})
.set('Accept', 'application/json')
.set('Authorization', 'jwt_token')
.expect(200);

expect(
legacyAPIApproveTreeStub.calledOnceWithExactly({
morphology: capture2.morphology,
age: `${capture2.age}`,
captureApprovalTag: legacyExtraObjects.capture_approval_tag,
speciesId: legacyExtraObjects.species_id_int,
id: capture2.reference_id,
legacyAPIAuthorizationHeader: 'jwt_token',
organizationId: legacyExtraObjects.organization_id,
}),
).eql(true);

expect(res.body).to.include({
image_url: capture2.image_url,
planting_organization_id: capture2.planting_organization_id,
Expand All @@ -103,10 +154,27 @@ describe('/captures', () => {
it('should resend capture created event if it wasnt successful last time and capture already exists', async () => {
await request(app)
.post(`/captures`)
.send({ ...capture2, id: capture1.id })
.send({
...capture2,
...legacyExtraObjects,
id: capture1.id,
})
.set('Accept', 'application/json')
.set('Authorization', 'jwt_token')
.expect(200);

expect(
legacyAPIApproveTreeStub.calledOnceWithExactly({
morphology: capture2.morphology,
age: `${capture2.age}`,
captureApprovalTag: legacyExtraObjects.capture_approval_tag,
speciesId: legacyExtraObjects.species_id_int,
id: capture2.reference_id,
legacyAPIAuthorizationHeader: 'jwt_token',
organizationId: legacyExtraObjects.organization_id,
}),
).eql(true);

// added a timer to confirm this because the function call in the API is a callback function not 'awaited'
await new Promise((resolve) => setTimeout(resolve, 2000));
const numOfEmittedEvents = await knex('domain_event')
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
1 change: 0 additions & 1 deletion api-tests/setup.js → __tests__/setup.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* eslint-disable import/no-extraneous-dependencies */
const sinon = require('sinon');
const Broker = require('rascal').BrokerAsPromised;

Expand Down
File renamed without changes.
Loading

0 comments on commit 2a5774f

Please sign in to comment.