Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Update FIREARMS to use beta version of HOFF framework using axios. (Do not merge these changes to the master branch) #473

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
68 changes: 68 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,71 @@ $ npm run test:acceptance
```bash
$ yarn test:unit
```
### Running firearms service locally
1. Start file-vault
a. Update .env file. Copy the following key values from test environment configuration file
* FILE_VAULT_URL=http://localhost:3000
* PORT='3000'
* AWS_ACCESS_KEY_ID=
* AWS_SECRET_ACCESS_KEY=
* AWS_KMS_KEY_ID=
* AWS_BUCKET=
* AWS_PASSWORD=
* RETURN_ORIGINAL_SIGNED_URL=
* ALLOW_GENERATE_LINK_ROUTE=
* AWS_REGION=
* AWS_SIGNATURE_VERSION=
* AWS_EXPIRY_TIME=
b. If you are getting error related to virus scan while running firearms service.Comment line number 170 in router.post (clamAV) in file.js.
c. Run bellow commands on terminal
* yarn install
* yarn start:dev

2. Start html-pdf-converter
a. Change port in config.js to 8082.
b. Run bellow commands on terminal
* yarn install.
* yarn dev.

3. Start Hof-rds-api
a. Update .env file. Copy the following key values from test environment configuration file
* PORT=3001
* SERVICE_NAME=acq
* NODE_ENV=local
* HOF_USER_PASS=
* RESOLVER_USER_PASS=
* REPORTS_USER_PASS=
* GRAFANA_USER_PASS=
b. Run bellow commands on terminal
* brew install postgresql
* brew services start postgresql
* psql postgres
* CREATE ROLE postgres WITH LOGIN PASSWORD 'postgres'; ALTER ROLE postgres WITH SUPERUSER; CREATE ROLE knex WITH LOGIN PASSWORD 'knex'; ALTER ROLE knex WITH SUPERUSER; CREATE DATABASE acq;
* yarn install
* yarn start:dev

4. Start firearms
a. Update .env file. Copy the following key values from test environment configuration file
* NOTIFY_KEY=
* CASEWORKER_EMAIL=
* TEMPLATE_MUSEUM=
* TEMPLATE_SECTION5=
* TEMPLATE_SHOOTING_CLUB=
* TEMPLATE_SUPPORTING_DOCUMENTS=
* EMAIL_REPLY_TO_DEFAULT=
* EMAIL_REPLY_TO_FIREARMS=
* PDF_CONVERTER_URL=http://localhost:8082/convert
* #NOTIFY_KEY=
* #NODE_ENV=
* ICASEWORK_TIMEOUT=6000
* FILE_VAULT_URL=http://localhost:3000/file
* KEYCLOAK_SECRET=
* KEYCLOAK_CLIENT_ID=
* KEYCLOAK_USERNAME=
* KEYCLOAK_PASSWORD=
* KEYCLOAK_TOKEN_URL=
* AWS_BUCKET=acq-notprod
b. Run bellow commands on terminal
* yarn install
* yarn dev.
c. If you are getting error related to virus scan while running firearms service.Comment line number 170 in router.post (clamAV) in file.js in file-vault.
4 changes: 3 additions & 1 deletion apps/common/behaviours/pdf-upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ module.exports = superclass => class PDFUpload extends superclass {
name: 'application_form.pdf',
data: pdfBuffer,
mimetype: 'application/pdf'
}).catch(err => next(new Error(err.body)));
}).catch(err => {
next(new Error(err));
});
})
.then(result => {
req.log('info', 'Saved PDF document to S3');
Expand Down
13 changes: 5 additions & 8 deletions apps/common/models/auth-token.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

const Model = require('hof').model;
const config = require('../../../config');
const axios = require('axios');

module.exports = class AuthToken extends Model {
auth() {
Expand All @@ -15,7 +16,8 @@ module.exports = class AuthToken extends Model {
}
const tokenReq = {
url: config.keycloak.token,
form: {
headers: { 'content-type': 'application/x-www-form-urlencoded' },
data: {
username: config.keycloak.username,
password: config.keycloak.password,
grant_type: 'password',
Expand All @@ -25,13 +27,8 @@ module.exports = class AuthToken extends Model {
method: 'POST'
};

return new Promise((resolve, reject) => {
this._request(tokenReq, (err, response) => {
if (err) {
return reject(err);
}
return resolve({ bearer: JSON.parse(response.body).access_token });
});
return axios(tokenReq).then(response => {
return { bearer: response.data.access_token };
});
}
};
21 changes: 11 additions & 10 deletions apps/common/models/file-upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
'use strict';

const url = require('url');

const FormData = require('form-data');
const AuthToken = require('./auth-token');
const config = require('../../../config');

Expand All @@ -13,16 +13,17 @@ module.exports = class UploadModel extends AuthToken {
url: config.upload.hostname
};
const reqConf = url.parse(this.url(attributes));
reqConf.formData = {
document: {
value: this.get('data'),
options: {
filename: this.get('name'),
contentType: this.get('mimetype')
}
}
};
const formData = new FormData();
formData.append( 'document', this.get('data'), {
filename: this.get('name'),
contentType: this.get('mimetype')
});
reqConf.data = formData;
reqConf.method = 'POST';
reqConf.headers = {
...formData.getHeaders()
};

// uses 'request' function available in HOF model. auth() is used by this process and
// thus needs to be declared in the parent class which extends off the HOF model too.
this.request(reqConf, (err, data) => {
Expand Down
24 changes: 13 additions & 11 deletions apps/common/models/pdf.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,32 @@ module.exports = class PDFModel extends Model {
requestConfig(options) {
const settings = super.requestConfig(options);
settings.encoding = null;
settings.rejectUnauthorized = false;
return settings;
}

url() {
return config.pdf.url;
}


handleResponse(response, callback) {
if (_.isPlainObject(response.body)) {
debug('Response: %O', response.body);
if (_.isPlainObject(response.data)) {
debug('Response: %O', response.data);
} else {
debug('Response: %s', response.body);
debug('Response: %s', response.data);
}
if (isPdf(Buffer.from(response.body))) {
return this.parseResponse(response.statusCode, response.body, callback);
if (isPdf(Buffer.from(response.data))) {
return this.parseResponse(response.status, response.data, callback);
}
const err = new Error();
if (parseInt(response.statusCode, 10) === 400) {
err.title = response.body.code;
err.message = response.body.message;
if (parseInt(response.status, 10) === 400) {
err.title = response.data.code;
err.message = response.data.message;
} else {
err.body = response.body;
err.body = response.data;
}
err.status = response.statusCode;
return callback(err, null, response.statusCode);
err.status = response.status;
return callback(err, null, response.status);
}
};
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@
},
"dependencies": {
"busboy-body-parser": "^0.3.0",
"axios": "^1.3.4",
"debug": "^2.6.8",
"express": "4.17.3",
"hof": "^20.1.17",
"express-fileupload": "^1.4.1",
"hof": "21.0.0-axios-beta",
"hot-shots": "^5.9.0",
"i18n-lookup": "^1.0.0",
"is-pdf": "^1.0.0",
Expand All @@ -47,10 +49,11 @@
"@cucumber/pretty-formatter": "^1.0.0-alpha.1",
"chai": "^4.3.4",
"chai-as-promised": "^7.1.1",
"chromedriver": "^118.0.1",
"eslint": "^7.32.0",
"eslint-config-hof": "^1.2.1",
"funkie": "^0.0.6",
"funkie-chromedriver": "0.0.1",
"funkie-phantom": "^0.0.1",
"mocha": "^9.0.3",
"mock-fs": "^5.0.0",
"nyc": "^15.1.0",
Expand Down
1 change: 0 additions & 1 deletion server.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ const config = require('./config.js');
const mockAPIs = require('./mock-apis.js');
const BaseController = require('./apps/common/controllers/base');


let appName = '';

let settings = require('./hof.settings');
Expand Down
30 changes: 18 additions & 12 deletions test/_unit/common/models/file-upload.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

const Model = require('../../../../apps/common/models/file-upload');
const config = require('../../../../config');
const FormData = require('form-data');

describe('File Upload Model', () => {
let sandbox;
Expand All @@ -14,6 +15,7 @@ describe('File Upload Model', () => {
api: 'response',
url: '/file/12341212132123?foo=bar'
});

sandbox.stub(Model.prototype, 'auth').returns(new Promise(resolve => {
resolve({bearer: 'myaccesstoken'});
}));
Expand All @@ -29,7 +31,11 @@ describe('File Upload Model', () => {
});

it('makes a call to file upload api', () => {
const model = new Model();
const model = new Model({
data: 'foo',
name: 'myfile.png',
mimetype: 'image/png'
});
const response = model.save();
return response.then(() => {
expect(model.request).to.have.been.calledOnce;
Expand All @@ -43,7 +49,11 @@ describe('File Upload Model', () => {
});

it('resolves with response from api endpoint', () => {
const model = new Model();
const model = new Model({
data: 'foo',
name: 'myfile.png',
mimetype: 'image/png'
});
const response = model.save();
return expect(response).to.eventually.deep.equal({
api: 'response',
Expand All @@ -52,7 +62,11 @@ describe('File Upload Model', () => {
});

it('rejects if api call fails', () => {
const model = new Model();
const model = new Model({
data: 'foo',
name: 'myfile.png',
mimetype: 'image/png'
});
const err = new Error('test error');
model.request.yieldsAsync(err);
const response = model.save();
Expand All @@ -68,15 +82,7 @@ describe('File Upload Model', () => {
const response = uploadedFile.save();
return response.then(() => {
expect(uploadedFile.request).to.have.been.calledWith(sinon.match({
formData: {
document: {
value: 'foo',
options: {
filename: 'myfile.png',
contentType: 'image/png'
}
}
}
data: sinon.match.instanceOf(FormData)
}));
});
});
Expand Down
20 changes: 10 additions & 10 deletions test/_unit/common/models/pdf.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,28 +29,28 @@ describe('PDF Model', () => {
const model = new Model();
isPdf.returns(true);
const res = {
statusCode: 200,
body: Buffer(100)
status: 200,
data: Buffer(100)
};
const callback = sinon.stub();
model.handleResponse(res, callback);
expect(Model.prototype.parseResponse).to.have.been.calledWith(res.statusCode, res.body, callback);
expect(Model.prototype.parseResponse).to.have.been.calledWith(res.status, res.data, callback);
});

it('passes errors to the callback', done => {
const model = new Model();
isPdf.returns(false);
const res = {
statusCode: 500,
body: JSON.stringify({
status: 500,
data: JSON.stringify({
title: 'Error',
message: 'There is an error'
})
};
model.handleResponse(res, (err, body, statusCode) => {
expect(err).to.be.an('error');
expect(body).to.be.null;
expect(statusCode).to.equal(res.statusCode);
expect(statusCode).to.equal(res.status);
done();
});
});
Expand All @@ -59,16 +59,16 @@ describe('PDF Model', () => {
const model = new Model();
isPdf.returns(false);
const res = {
statusCode: 400,
body: JSON.stringify({
status: 400,
data: JSON.stringify({
code: 'ClientError',
message: 'There is an error'
})
};
model.handleResponse(res, (err, body, statusCode) => {
expect(err).to.be.an('error');
expect(err.title).to.equal(res.body.code);
expect(err.message).to.equal(res.body.message);
expect(err.title).to.equal(res.data.code);
expect(err.message).to.equal(res.data.message);
expect(body).to.be.null;
expect(statusCode).to.equal(400);
done();
Expand Down
Loading