Commit 0ef93171 authored by wim-vantomme's avatar wim-vantomme

Added unit tests on the server code.

parent a5922044
{
"ignore": ["./server/files"]
}
\ No newline at end of file
This diff is collapsed.
......@@ -4,7 +4,8 @@
"description": "Explore OpenCrypto implementation for the [Indienet Engine](https://indienet.info/engine) project.",
"main": "./server/index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"test": "mocha server/**/*.test.js",
"test-watch": "nodemon --exec npm test",
"watch": "nodemon",
"start": "node ./server/index.js"
},
......@@ -16,10 +17,12 @@
"license": "AGPLv3",
"devDependencies": {
"browserify": "^15.1.0",
"expect": "^22.1.0",
"gulp": "^3.9.1",
"gulp-rename": "^1.2.2",
"mocha": "^4.1.0",
"standard": "^10.0.3",
"supertest": "^3.0.0",
"vinyl-source-stream": "^2.0.0"
},
"dependencies": {
......
......@@ -12,7 +12,7 @@ app.use(bodyParser.urlencoded({extended: false}))
app.post('/register', (req, res) => {
const salt = JSON.stringify({salt: req.body.salt})
const publicKey = JSON.stringify({publickKey: req.body.publicKey})
const publicKey = JSON.stringify({publicKey: req.body.publicKey})
const privateKey = JSON.stringify({privateKey: req.body.privateKey})
const writeSalt = fileUtils.writeFile('salt.json', salt, './server/files')
......@@ -48,3 +48,7 @@ app.get('/privatekey', (req, res) => {
console.log('Server started at port 8080')
app.listen(8080)
module.exports = {
app
}
const expect = require('expect')
const request = require('supertest')
const {app} = require('../index')
const fileUtils = require('../utils/fileUtils')
const testSalt = 'TestSalt'
const publicKey = 'TestPublicKey'
const privateKey = 'TestPrivateKey'
describe('POST /register', () => {
it('should create a new salt, publickey and privatekey file', (done) => {
const keys = {
salt: 'REZARFE',
publicKey: 'VAZRFZRTAZE',
privateKey: 'VRAQTYZAF'
}
request(app)
.post('/register')
.send(keys)
.expect(200)
.end((err, res) => {
if (err) {
return done(err)
}
const readSalt = fileUtils.readFile('./server/files/salt.json')
const readPublicKey = fileUtils.readFile('./server/files/publickey.json')
const readPrivateKey = fileUtils.readFile('./server/files/privatekey.json')
Promise.all([readSalt, readPublicKey, readPrivateKey]).then((result) => {
expect(JSON.parse(result[0]).salt).toBe(keys.salt)
expect(JSON.parse(result[2]).privateKey).toBe(keys.privateKey)
expect(JSON.parse(result[1]).publicKey).toBe(keys.publicKey)
done()
}).catch((err) => done(err))
})
})
})
describe('GET /publickey', () => {
beforeEach(() => {
fileUtils.writeFile('publickey.json', JSON.stringify({publicKey: publicKey}), './server/files').catch((err) => console.log(err))
})
it('should return the publickey value', (done) => {
request(app)
.get('/publickey')
.expect(200)
.expect((res) => {
expect(res.body.publicKey).toBe(publicKey)
})
.end(done)
})
})
describe('GET /privatekey', () => {
beforeEach(() => {
fileUtils.writeFile('privatekey.json', JSON.stringify({privateKey: privateKey}), './server/files').catch((err) => console.log(err))
})
it('should return the privatekey value', (done) => {
request(app)
.get('/privatekey')
.expect(200)
.expect((res) => {
expect(res.body.privateKey).toBe(privateKey)
})
.end(done)
})
})
const expect = require('expect')
const fileUtils = require('../fileUtils')
const fs = require('fs')
const testString = 'This is a unit test string'
describe('File utils tests', () => {
after(() => {
fs.unlink('./server/test/test.txt', (err) => {
if (err) {
console.log(err)
return
}
fs.rmdir('./server/test', (err) => {
if (err) {
console.log(err)
}
})
})
})
it('should write a file to the filesystem', (done) => {
fileUtils.writeFile('test.txt', testString, './server/test').then(() => {
fs.access('./server/test/test.txt', (err) => {
if (err) {
return done(err)
}
done()
})
}).catch((err) => done(err))
})
it('should read a file from the filesystem', (done) => {
fileUtils.readFile('./server/test/test.txt').then((file) => {
expect(file).toBe(testString)
done()
}).catch((err) => done(err))
})
})
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment