Skip to content

Commit

Permalink
feat(services): testing and bugfixes (#39)
Browse files Browse the repository at this point in the history
* chore(tests): add tests

* chore(decisionService): fix faulty error parsing

* chore(eventBroker): fix validate endpoint
  • Loading branch information
Maurice Dalderup authored Mar 12, 2019
1 parent 63e4c1f commit 92710a3
Show file tree
Hide file tree
Showing 9 changed files with 5,337 additions and 66 deletions.
1 change: 1 addition & 0 deletions decision-service/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ node_modules
.hfc-key-store
.DS_Store
.env
combined.log
3 changes: 2 additions & 1 deletion decision-service/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "lblod decision service",
"main": "app.js",
"scripts": {
"test": "mocha --require babel-core/register",
"test": "mocha --require babel-core/register --timeout 30000",
"lint": "eslint --ignore-pattern '/node_modules/' .",
"start": "node app.js"
},
Expand Down Expand Up @@ -33,6 +33,7 @@
"devDependencies": {
"babel-core": "6.26.3",
"babel-eslint": "9.0.0",
"babel-polyfill": "^6.26.0",
"babel-preset-latest": "6.24.1",
"babel-register": "6.26.0",
"chai": "4.2.0",
Expand Down
85 changes: 40 additions & 45 deletions decision-service/services/userManagement.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,56 +3,51 @@ import CryptoJS from "crypto-js";

import User from "../models/auth.model";

const RegisterUser = async oit => {
const {
encryptedCert,
encryptedKey,
encryptionKey
} = await requestPromise.post("http://authentication/certificate/create", {
method: "POST",
body: {
enrollmentID: oit.identifier,
role: oit.roles.toString(),
id: oit.fullIdentifier,
seed: oit.secret
},
json: true
});

let bytes = CryptoJS.AES.decrypt(
encryptedCert,
JSON.stringify(encryptionKey)
);
const certificatePEM = bytes.toString(CryptoJS.enc.Utf8);

// Decrypt private key
bytes = CryptoJS.AES.decrypt(encryptedKey, JSON.stringify(encryptionKey));
const privateKeyPEM = bytes.toString(CryptoJS.enc.Utf8);

const newUser = new User({
username: oit.identifier,
encryptedCert,
encryptedKey
});

await newUser.save();

return { username: oit.identifier, certificatePEM, privateKeyPEM };
};
const RegisterUser = oit =>
requestPromise
.post("http://authentication/certificate/create", {
method: "POST",
body: {
enrollmentID: oit.identifier,
role: oit.roles.toString(),
id: oit.fullIdentifier,
seed: oit.secret
},
json: true
})
.then(async ({ encryptedCert, encryptedKey, encryptionKey }) => {
let bytes = CryptoJS.AES.decrypt(
encryptedCert,
JSON.stringify(encryptionKey)
);
const certificatePEM = bytes.toString(CryptoJS.enc.Utf8);

// Decrypt private key
bytes = CryptoJS.AES.decrypt(encryptedKey, JSON.stringify(encryptionKey));
const privateKeyPEM = bytes.toString(CryptoJS.enc.Utf8);

const newUser = new User({
username: oit.identifier,
encryptedCert,
encryptedKey
});

await newUser.save();

return { username: oit.identifier, certificatePEM, privateKeyPEM };
})
.catch(e => new Error(e.error.errors[0].title));

const GetUser = async identifier => User.getByName(identifier);

const GetEncryptionKey = async oit => {
const responseRetrieve = await requestPromise.post(
"http://authentication/certificate/retrieveKey",
{
const GetEncryptionKey = oit =>
requestPromise
.post("http://authentication/certificate/retrieveKey", {
method: "POST",
body: { identifier: oit, seed: oit.secret },
json: true
}
);

return JSON.parse(responseRetrieve.encryptionKey);
};
})
.then(retrievedKey => JSON.parse(retrievedKey.encryptionKey))
.catch(e => new Error(e.error.errors[0].title));

export default { RegisterUser, GetUser, GetEncryptionKey };
231 changes: 215 additions & 16 deletions decision-service/test/decision.controller.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
import chai, { expect, should } from "chai";
import chaiHttp from "chai-http";

// import server from "../app";
// import server from "../../app";
const server = "http://localhost:4000";
should();
chai.use(chaiHttp);

describe("Decision", () => {
let id;

beforeEach(done => {
id = Math.random();
done();
});

it("healtcheck should pass", done => {
chai
.request(server)
Expand All @@ -20,35 +27,227 @@ describe("Decision", () => {
});
});

it("publish", done => {
it("should publish", done => {
const requestObject = {
id: "1234",
id: id.toString(),
content: "randomContent",
oit: {
identifier: "1234",
roles: ["1234"],
secret: "1234",
fullIdentifier: "1234"
identifier: id.toString(),
roles: [
"GelinktNotuleren-schrijver",
"GelinktNotuleren-ondertekenaar",
"GelinktNotuleren-publiceerder"
],
secret: id.toString(),
fullIdentifier: id.toString()
},
resourceId: "1234",
subject: "1234",
timestamp: "1234"
resourceId: id.toString(),
subject: "1234567",
timestamp: "1234567"
};

chai
.request(server)
.post(`/decision/publish`)
.send(requestObject)
.end((err, res) => {
console.log("err ", err);
console.log("res.body ", res.body);
res.should.have.status(200);
res.body.should.be.a("object").to.have.property("result");
res.body.result.should.be.a("object").to.have.property("statusCode");
res.body.result.should.be.a("object").to.have.property("tx");
expect(res.body.result.statusCode).to.equal("VALID");
done();
});
});

it("should sign", done => {
const requestObject = {
id: id.toString(),
content: "randomContent",
oit: {
identifier: id.toString(),
roles: [
"GelinktNotuleren-schrijver",
"GelinktNotuleren-ondertekenaar",
"GelinktNotuleren-publiceerder"
],
secret: id.toString(),
fullIdentifier: id.toString()
},
resourceId: id.toString(),
subject: "1234567",
timestamp: "1234567"
};

chai
.request(server)
.post(`/decision/sign`)
.send(requestObject)
.end((err, res) => {
res.should.have.status(200);
// res.body.should.not.have.property("error");
// res.body.should.not.have.property("encryptedCert");
// res.body.should.not.have.property("encryptedKey");
// res.body.should.be.a("object").to.have.property("encryptionKey");
res.body.should.be.a("object").to.have.property("result");
res.body.result.should.be.a("object").to.have.property("statusCode");
res.body.result.should.be.a("object").to.have.property("tx");
expect(res.body.result.statusCode).to.equal("VALID");
done();
});
});

it("should publishes and signs with different IDs", done => {
const requestObject = {
id: id.toString(),
content: "randomContent",
oit: {
identifier: id.toString(),
roles: [
"GelinktNotuleren-schrijver",
"GelinktNotuleren-ondertekenaar",
"GelinktNotuleren-publiceerder"
],
secret: id.toString(),
fullIdentifier: id.toString()
},
resourceId: id.toString(),
subject: "1234567",
timestamp: "1234567"
};

id = Math.random();
const requestObject2 = {
id: id.toString(),
content: "randomContent",
oit: {
identifier: id.toString(),
roles: [
"GelinktNotuleren-schrijver",
"GelinktNotuleren-ondertekenaar",
"GelinktNotuleren-publiceerder"
],
secret: id.toString(),
fullIdentifier: id.toString()
},
resourceId: id.toString(),
subject: "1234567",
timestamp: "1234567"
};

chai
.request(server)
.post(`/decision/publish`)
.send(requestObject)
.end((err, res) => {
res.should.have.status(200);
res.body.should.be.a("object").to.have.property("result");
res.body.result.should.be.a("object").to.have.property("statusCode");
res.body.result.should.be.a("object").to.have.property("tx");
expect(res.body.result.statusCode).to.equal("VALID");
chai
.request(server)
.post(`/decision/sign`)
.send(requestObject2)
.end((err2, res2) => {
res2.should.have.status(200);
res2.body.should.be.a("object").to.have.property("result");
res2.body.result.should.be
.a("object")
.to.have.property("statusCode");
res2.body.result.should.be.a("object").to.have.property("tx");
expect(res2.body.result.statusCode).to.equal("VALID");
done();
});
});
});

it("should publishes and signs with same IDs", done => {
const requestObject = {
id: id.toString(),
content: "randomContent",
oit: {
identifier: id.toString(),
roles: [
"GelinktNotuleren-schrijver",
"GelinktNotuleren-ondertekenaar",
"GelinktNotuleren-publiceerder"
],
secret: id.toString(),
fullIdentifier: id.toString()
},
resourceId: id.toString(),
subject: "1234567",
timestamp: "1234567"
};

chai
.request(server)
.post(`/decision/publish`)
.send(requestObject)
.end((err, res) => {
res.should.have.status(200);
res.body.should.be.a("object").to.have.property("result");
res.body.result.should.be.a("object").to.have.property("statusCode");
res.body.result.should.be.a("object").to.have.property("tx");
expect(res.body.result.statusCode).to.equal("VALID");
chai
.request(server)
.post(`/decision/sign`)
.send(requestObject)
.end((err2, res2) => {
res2.should.have.status(200);
res2.body.should.be.a("object").to.have.property("result");
res2.body.result.should.be
.a("object")
.to.have.property("statusCode");
res2.body.result.should.be.a("object").to.have.property("tx");
expect(res2.body.result.statusCode).to.equal("VALID");
done();
});
});
});

// it("should publishes and signs with same IDs together", done => {
// const requestObject = {
// id: id.toString(),
// content: "randomContent",
// oit: {
// identifier: id.toString(),
// roles: [
// "GelinktNotuleren-schrijver",
// "GelinktNotuleren-ondertekenaar",
// "GelinktNotuleren-publiceerder"
// ],
// secret: id.toString(),
// fullIdentifier: id.toString()
// },
// resourceId: id.toString(),
// subject: "1234567",
// timestamp: "1234567"
// };

// chai
// .request(server)
// .post(`/decision/publish`)
// .send(requestObject)
// .end((err, res) => {
// res.should.have.status(200);
// res.body.should.be.a("object").to.have.property("result");
// res.body.result.should.be.a("object").to.have.property("statusCode");
// res.body.result.should.be.a("object").to.have.property("tx");
// expect(res.body.result.statusCode).to.equal("VALID");
// });

// chai
// .request(server)
// .post(`/decision/sign`)
// .send(requestObject)
// .end((err, res) => {
// console.log(err);
// console.log(res);
// res.should.have.status(200);
// res.body.should.be.a("object").to.have.property("result");
// res.body.result.should.be.a("object").to.have.property("statusCode");
// res.body.result.should.be.a("object").to.have.property("tx");
// expect(res.body.result.statusCode).to.equal("VALID");
// done();
// });
// });
});
Loading

0 comments on commit 92710a3

Please sign in to comment.