Skip to content

Commit

Permalink
DIV-3491 - create DN case for integration testing (#1)
Browse files Browse the repository at this point in the history
* WIP: over to Hosain

* DIV-3491 - included missing require and removes `this` reference

Also ignored idea files

* Organising dependencies

* Reusing test method between DN and AOS cases

* DIV-3491 - rethrow error if there is an issue creating a case

* DIV-3491 - create case should get caseData instead of file path

* Addressed comments from code review and added Travis CI files. Also added test case and adjusted tests after changes were made to source code.

* Using resolves for returning a promise from Sinon
  • Loading branch information
hosainnet authored and J. Victor Ferreira committed Nov 14, 2018
1 parent 10c4630 commit a459aaa
Show file tree
Hide file tree
Showing 8 changed files with 883 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,5 @@ typings/

# next.js build output
.next

.idea/
1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
8.12.0
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
language: node_js
93 changes: 93 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
const util = require('util');
const url = require('url');
const request = require('request-promise-native');
const logger = require('@hmcts/nodejs-logging').Logger.getLogger(__filename);

const createAosCase = (params, proxy) => {
params.eventId = 'testAosAwaiting';
return _createCase(params, proxy);
};

const createDnCase = (params, proxy) => {
params.eventId = 'testAwaitingDecreeNisi';
return _createCase(params, proxy);
};

function _createCase(params, proxy) {
return _createCaseForUser(params, proxy)
.then(createCaseResponse => {
logger.info(`Created case ${createCaseResponse.id}`);
return _updateCase(params, createCaseResponse.id, params.eventId, proxy);
})
.catch(error => {
logger.info(`Error creating case: ${util.inspect(error)}`);
throw error;
});
}

function _createCaseForUser(params, proxy) {
const uri = `${params.baseUrl}/casemaintenance/version/1/submit`;
const headers = {
Authorization: `Bearer ${params.authToken}`,
'Content-Type': 'application/json'
};

const options = {
uri,
headers,
body: params.caseData,
json: true
};

if (proxy) {
Object.assign(options, _setupProxy(proxy));
}
return request.post(options);
};

const _updateCase = (params, caseId, eventId, proxy) => {
logger.info(`Issuing event ${eventId} against case ${caseId}.`);
const baseUrl = params.baseUrl;
const uri = `${baseUrl}/casemaintenance/version/1/updateCase/${caseId}/${eventId}`;
const headers = {
Authorization: `Bearer ${params.authToken}`,
'Content-Type': 'application/json'
};

const options = {
uri,
headers,
body: {},
json: true
};

if (proxy) {
Object.assign(options, _setupProxy(proxy));
}
return request.post(options);
};

const _setupProxy = proxy => {
const proxyUrl = url.parse(proxy);

let proxyOptions = {};

if (proxyUrl.protocol.indexOf('socks') >= 0) {
proxyOptions = {
strictSSL: false,
agentClass: socksAgent,
socksHost: proxyUrl.hostname || 'localhost',
socksPort: proxyUrl.port || 9000
};
} else {
proxyOptions = { proxy: proxyUrl.href };
}

return proxyOptions;
};


module.exports = {
createAosCase,
createDnCase
};
29 changes: 29 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "@hmcts/div-test-harness",
"version": "1.0.0",
"description": "Module used for functional tests to create a case and progress it to a certain state so that other tests can build upon it",
"main": "index.js",
"scripts": {
"test": "mocha"
},
"repository": {
"type": "git",
"url": "git+https://github.com/hmcts/div-test-harness.git"
},
"author": "",
"license": "MIT",
"bugs": {
"url": "https://github.com/hmcts/div-test-harness/issues"
},
"homepage": "https://github.com/hmcts/div-test-harness#readme",
"devDependencies": {
"mocha": "^5.2.0",
"chai": "^4.2.0",
"sinon": "^7.1.1"
},
"dependencies": {
"@hmcts/nodejs-logging": "^3.0.0",
"request": "^2.88.0",
"request-promise-native": "^1.0.5"
}
}
41 changes: 41 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
const sinon = require('sinon');
const fs = require('fs');
const expect = require('chai').expect;
const request = require('request-promise-native');
const divTestHarness = require('../index.js');

describe('index.js', function () {
beforeEach(function () {
sinon.stub(request, 'post').resolves({ id: '123' });
});

afterEach(function () {
request.post.restore();
});

it('should create a DN case', function () {
const proxy = '';
const params = {
baseUrl: '',
authToken: '',
caseData: fs.readFileSync('test/test.json')
};
return divTestHarness.createDnCase(params, proxy)
.then(() => {
expect(request.post.callCount).to.equal(2);
});
});

it('should create a AOS case', function () {
const proxy = '';
const params = {
baseUrl: '',
authToken: '',
caseData: fs.readFileSync('test/test.json')
};
return divTestHarness.createAosCase(params, proxy)
.then(() => {
expect(request.post.callCount).to.equal(2);
});
});
});
1 change: 1 addition & 0 deletions test/test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
Loading

0 comments on commit a459aaa

Please sign in to comment.