Commit a842c62e authored by Frauke's avatar Frauke
Browse files

Added tests for server2

parent b0e971a1
......@@ -59,6 +59,10 @@ Afterwards you go to server 2 and click the receive message button. You're messa
*client code is the same for server 1 and 2, URLS for private and public keys are hardcoded.*
## Tests
You can test server 1 by running '*npm test*'.
You can test server 2 by running '*npm run test2*'
## Changelog
__0.1.0__
......
......@@ -5,6 +5,7 @@
"main": "./server/server.js",
"scripts": {
"test": "mocha server/**/*.test.js",
"test2": "mocha server2/**/*.test.js",
"test-watch": "nodemon --exec npm test",
"watch": "nodemon",
"start": "gulp js:bundle | node ./server/server.js | node ./server2/server.js"
......
......@@ -171,3 +171,7 @@ app.get('/listmessages', (req, res) => {
console.log('Server 2 started on port 8181')
app.listen(8181)
module.exports = {
app
}
\ No newline at end of file
const expect = require('expect')
const request = require('supertest')
const {app} = require('../server')
const fileUtils = require('../utils/fileUtils')
const testSalt = 'TestSalt'
const testSignPublicKey = 'TestSignPublicKey'
const testSignPrivateKey = 'TestSignPrivateKey'
const testEncryptPublicKey = 'testEncryptPublicKey'
const testEncryptPrivateKey = 'TestEncryptPrivateKey'
const sessionKey = 'sessionKey'
const message = 'message'
describe('POST /register', () => {
it('should create a new signpublic, signprivate, encryptpublic and encryptprivatekey file', (done) => {
const keys = {
salt: 'REZARFE',
signPublicKey: 'SDFGHNFGBSDFG',
signPrivateKey: 'DFGSDFHGSDFHSREH',
encryptPublicKey: 'SDFGERUERTZFNYJU',
encryptPrivateKey: 'SDGETYYTKJNBGFDZRETY'
}
request(app)
.post('/register')
.send(keys)
.expect(200)
.end((err, res) => {
if (err) {
return done(err)
}
const readSignPublicKey = fileUtils.readFile('./server2/files/signpublickey.json')
const readSignPrivateKey = fileUtils.readFile('./server2/files/signprivatekey.json')
const readEncryptPublicKey = fileUtils.readFile('./server2/files/encryptpublickey.json')
const readEncryptPrivateKey = fileUtils.readFile('./server2/files/encryptprivatekey.json')
Promise.all([readSignPublicKey, readSignPrivateKey, readEncryptPublicKey, readEncryptPrivateKey]).then((result) => {
expect(JSON.parse(result[0]).publicKey).toBe(keys.signPublicKey)
expect(JSON.parse(result[1]).privateKey).toBe(keys.signPrivateKey)
expect(JSON.parse(result[1]).salt).toBe(keys.salt)
expect(JSON.parse(result[2]).publicKey).toBe(keys.encryptPublicKey)
expect(JSON.parse(result[3]).privateKey).toBe(keys.encryptPrivateKey)
expect(JSON.parse(result[3]).salt).toBe(keys.salt)
done()
}).catch((err) => done(err))
})
})
})
describe('GET /encryptpublickey', () => {
beforeEach(() => {
fileUtils.writeFile('encryptpublickey.json', JSON.stringify({publicKey: testEncryptPublicKey}), './server2/files').catch((err) => console.log(err))
})
it('should return the encryptpublickey value', (done) => {
request(app)
.get('/encryptpublickey')
.expect(200)
.expect((res) => {
expect(res.body.publicKey).toBe(testEncryptPublicKey)
})
.end(done)
})
})
describe('GET /encryptprivatekey', () => {
beforeEach(() => {
fileUtils.writeFile('encryptprivatekey.json', JSON.stringify({publicKey: testEncryptPrivateKey}), './server2/files').catch((err) => console.log(err))
})
it('should return the encryptprivatekey value', (done) => {
request(app)
.get('/encryptprivatekey')
.expect(200)
.expect((res) => {
expect(res.body.publicKey).toBe(testEncryptPrivateKey)
})
.end(done)
})
})
describe('GET /signpublickey', () => {
beforeEach(() => {
fileUtils.writeFile('signpublickey.json', JSON.stringify({publicKey: testSignPublicKey}), './server2/files').catch((err) => console.log(err))
})
it('should return the signpublickey value', (done) => {
request(app)
.get('/signpublickey')
.expect(200)
.expect((res) => {
expect(res.body.publicKey).toBe(testSignPublicKey)
})
.end(done)
})
})
describe('GET /signprivatekey', () => {
beforeEach(() => {
fileUtils.writeFile('signprivatekey.json', JSON.stringify({publicKey: testSignPrivateKey}), './server2/files').catch((err) => console.log(err))
})
it('should return the signprivatekey value', (done) => {
request(app)
.get('/signprivatekey')
.expect(200)
.expect((res) => {
expect(res.body.publicKey).toBe(testSignPrivateKey)
})
.end(done)
})
})
describe('GET /nonce', () => {
it('should return a nonce', (done) => {
request(app)
.get('/nonce')
.expect(200)
.expect((res) => {
expect(res.body).toBeBuffer()
})
.end(done)
})
})
expect.extend({
toBeBuffer (received, argument) {
if (Buffer.isBuffer(received)) {
return {
message: () =>
`expected ${received} to be a Buffer`,
pass: true
}
} else {
return {
message: () => `${received} must be Buffer`,
pass: false
}
}
}
})
describe('POST /send', () => {
it('should save the sessionkey and message to the filesystem', (done) => {
const data = {
sessionKey: 'sessionKey',
message: 'Hello'
}
request(app)
.post('/send')
.send(data)
.expect(200)
.end((err, res) => {
const sessionKey = fileUtils.readFile('./server2/files/sessionkey.json')
const message = fileUtils.readFile('./server2/files/message.json')
Promise.all([sessionKey, message]).then((result) => {
expect(result[0]).toBe(data.sessionKey)
expect(result[1]).toBe(data.message)
done()
}).catch((err) => done(err))
})
})
})
describe('GET /listmessages', () => {
beforeEach(() => {
const pathName = './server2/files/'
fileUtils.writeFile('sessionkey.json', sessionKey, pathName).catch((err) => console.log(err))
fileUtils.writeFile('message.json', message, pathName).catch((err) => console.log(err))
})
it('should read the messages of of the filesystem', (done) => {
request(app)
.get('/listmessages')
.expect(200)
.expect((res) => {
expect(res.body.sessionKey).toBe(sessionKey)
expect(res.body.message).toBe(message)
})
.end(done)
})
})
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