Archived
2

added test helper and rewrote tests

This commit is contained in:
VLE2FE 2020-04-27 11:44:28 +02:00
parent 7a917c1f6b
commit eaa6484dca
4 changed files with 567 additions and 714 deletions

1
.gitignore vendored
View File

@ -112,3 +112,4 @@ dist
**/.idea/tasks.xml **/.idea/tasks.xml
**/.idea/shelf **/.idea/shelf
**/.idea/*.iml **/.idea/*.iml
/tmp/

88
src/helpers/test.ts Normal file
View File

@ -0,0 +1,88 @@
import supertest from 'supertest';
import should from 'should/as-function';
import db from "../db";
export default class TestHelper {
public static auth = {
admin: {pass: 'Abc123!#', key: '5ea131671feb9c2ee0aafc9a'},
janedoe: {pass: 'Xyz890*)', key: '5ea0450ed851c30a90e70899'}
}
public static res = {
400: {status: 'Bad request'},
401: {status: 'Unauthorized'},
403: {status: 'Forbidden'},
404: {status: 'Not found'},
}
static before (done) {
process.env.port = '2999';
process.env.NODE_ENV = 'test';
db.connect('test', done);
}
static beforeEach (server, done) {
delete require.cache[require.resolve('../index')]; // prevent loading from cache
server = require('../index');
db.drop(err => { // reset database
if (err) return done(err);
db.loadJson(require('../test/db.json'), done);
});
return server
}
static afterEach (server, done) {
server.close(done);
}
static request (server, done, options) { // options in form: {method, url, auth: {key/basic: 'name' or 'key'/{name, pass}}, httpStatus, req, res}
let st = supertest(server);
if (options.hasOwnProperty('auth') && options.auth.hasOwnProperty('key')) {
options.url += '?key=' + (this.auth.hasOwnProperty(options.auth.key)? this.auth[options.auth.key].key : options.auth.key);
}
switch (options.method) {
case 'get':
st = st.get(options.url)
break;
case 'post':
st = st.post(options.url)
break;
case 'put':
st = st.put(options.url)
break;
case 'delete':
st = st.delete(options.url)
break;
}
if (options.hasOwnProperty('req')) {
st = st.send(options.req);
}
if (options.hasOwnProperty('auth') && options.auth.hasOwnProperty('basic')) {
if (this.auth.hasOwnProperty(options.auth.basic)) {
st = st.auth(options.auth.basic, this.auth[options.auth.basic].pass)
}
else {
st = st.auth(options.auth.basic.name, options.auth.basic.pass)
}
}
st = st.expect('Content-type', /json/)
.expect(options.httpStatus);
if (options.hasOwnProperty('res')) {
return st.end((err, res) => {
if (err) return done (err);
should(res.body).be.eql(options.res);
done();
});
}
else if (this.res.hasOwnProperty(options.httpStatus) && options.default !== false) {
return st.end((err, res) => {
if (err) return done (err);
should(res.body).be.eql(this.res[options.httpStatus]);
done();
});
}
else {
return st;
}
}
}

View File

@ -1,145 +1,69 @@
import supertest from 'supertest'; import TestHelper from "../helpers/test";
import should from 'should/as-function';
import db from '../db';
describe('GET /', () => { describe('/', () => {
let server; let server;
before(done => TestHelper.before(done));
beforeEach(done => server = TestHelper.beforeEach(server, done));
afterEach(done => TestHelper.afterEach(server, done));
before(done => { describe('GET /', () => {
process.env.port = '2999';
process.env.NODE_ENV = 'test';
db.connect('test', done);
});
beforeEach(done => {
delete require.cache[require.resolve('../index')]; // prevent loading from cache
server = require('../index');
db.drop(err => { // reset database
if (err) return done(err);
db.loadJson(require('../test/db.json'), done);
});
});
afterEach(done => {
server.close(done);
});
it('returns the root message', done => { it('returns the root message', done => {
supertest(server) TestHelper.request(server, done, {
.get('/') method: 'get',
.expect('Content-type', /json/) url: '/',
.expect(200) httpStatus: 200,
.end((err, res) => { res: {status: 'API server up and running!'}
if (err) done (err); });
should(res.body).be.eql({status: 'API server up and running!'});
done();
}); });
}); });
});
describe('Unknown routes', () => { describe('Unknown routes', () => {
let server;
before(done => {
db.connect('test', done);
});
beforeEach(done => {
delete require.cache[require.resolve('../index')]; // prevent loading from cache
server = require('../index');
db.drop(err => { // reset database
if (err) return done(err);
db.loadJson(require('../test/db.json'), done);
});
});
afterEach(done => {
server.close(done);
});
it('return a 404 message', done => { it('return a 404 message', done => {
supertest(server) TestHelper.request(server, done, {
.get('/unknownroute') method: 'get',
.expect(404) url: '/unknownroute',
.end((err, res) => { httpStatus: 404
if (err) done (err); });
should(res.body).be.eql({status: 'Not found'});
done();
}); });
}); });
});
describe('An unauthorized request', () => { describe('An unauthorized request', () => {
let server;
before(done => {
db.connect('test', done);
});
beforeEach(done => {
delete require.cache[require.resolve('../index')]; // prevent loading from cache
server = require('../index');
db.drop(err => { // reset database
if (err) return done(err);
db.loadJson(require('../test/db.json'), done);
});
});
afterEach(done => {
server.close(done);
});
it('returns a 401 message', done => { it('returns a 401 message', done => {
supertest(server) TestHelper.request(server, done, {
.get('/authorized') method: 'get',
.expect(401) url: '/authorized',
.end((err, res) => { httpStatus: 401
if (err) done (err);
should(res.body).be.eql({status: 'Unauthorized'});
done();
}); });
}); });
it('does not work with correct username', done => { it('does not work with correct username', done => {
supertest(server) TestHelper.request(server, done, {
.get('/authorized') method: 'get',
.auth('admin', 'Abc123!!') url: '/authorized',
.expect(401) auth: {name: 'admin', pass: 'Abc123!!'},
.end((err, res) => { httpStatus: 401
if (err) done (err); });
should(res.body).be.eql({status: 'Unauthorized'});
done();
}); });
}); });
});
describe('An authorized request', () => { describe('An authorized request', () => {
let server;
before(done => {
db.connect('test', done);
});
beforeEach(done => {
delete require.cache[require.resolve('../index')]; // prevent loading from cache
server = require('../index');
db.drop(err => { // reset database
if (err) return done(err);
db.loadJson(require('../test/db.json'), done);
});
});
afterEach(done => {
server.close(done);
});
it('works with an API key', done => { it('works with an API key', done => {
supertest(server) TestHelper.request(server, done, {
.get('/authorized?key=5ea131671feb9c2ee0aafc9a') method: 'get',
.expect(200) url: '/authorized',
.end((err, res) => { auth: {key: 'admin'},
if (err) done (err); httpStatus: 200,
should(res.body).be.eql({status: 'Authorization successful', method: 'key'}); res: {status: 'Authorization successful', method: 'key'}
done();
}); });
}); });
it('works with basic auth', done => { it('works with basic auth', done => {
supertest(server) TestHelper.request(server, done, {
.get('/authorized') method: 'get',
.auth('admin', 'Abc123!#') url: '/authorized',
.expect(200) auth: {basic: 'admin'},
.end((err, res) => { httpStatus: 200,
if (err) done (err); res: {status: 'Authorization successful', method: 'basic'}
should(res.body).be.eql({status: 'Authorization successful', method: 'basic'}); });
done();
}); });
}); });
}); });

View File

@ -1,36 +1,23 @@
import supertest from 'supertest';
import should from 'should/as-function'; import should from 'should/as-function';
import db from '../db';
import UserModel from '../models/user'; import UserModel from '../models/user';
import TestHelper from "../helpers/test";
describe('GET /users', () => { describe('/user', () => {
let server; let server;
before(done => TestHelper.before(done));
beforeEach(done => server = TestHelper.beforeEach(server, done));
afterEach(done => TestHelper.afterEach(server, done));
before(done => { describe('GET /users', () => {
process.env.port = '2999';
process.env.NODE_ENV = 'test';
db.connect('test', done);
});
beforeEach(done => {
delete require.cache[require.resolve('../index')]; // prevent loading from cache
server = require('../index');
db.drop(err => { // reset database
if (err) return done(err);
db.loadJson(require('../test/db.json'), done);
});
});
afterEach(done => {
server.close(done);
});
it('returns all users', done => { it('returns all users', done => {
supertest(server) TestHelper.request(server, done, {
.get('/users') method: 'get',
.auth('admin', 'Abc123!#') url: '/users',
.expect('Content-type', /json/) auth: {basic: 'admin'},
.expect(200) httpStatus: 200
.end((err, res) => { }).end((err, res) => {
if (err) done (err); if (err) return done(err);
const json = require('../test/db.json'); const json = require('../test/db.json');
should(res.body).have.lengthOf(json.collections.users.length); should(res.body).have.lengthOf(json.collections.users.length);
should(res.body).matchEach(user => { should(res.body).matchEach(user => {
@ -46,58 +33,32 @@ describe('GET /users', () => {
}); });
}); });
it('rejects requests from non-admins', done => { it('rejects requests from non-admins', done => {
supertest(server) TestHelper.request(server, done, {
.get('/users') method: 'get',
.auth('janedoe', 'Xyz890*)') url: '/users',
.expect('Content-type', /json/) auth: {basic: 'janedoe'},
.expect(403) httpStatus: 403
.end((err, res) => {
if (err) done (err);
should(res.body).be.eql({status: 'Forbidden'});
done();
}); });
}); });
it('rejects requests from an admin API key', done => { it('rejects requests from an admin API key', done => {
supertest(server) TestHelper.request(server, done, {
.get('/users?key=5ea131671feb9c2ee0aafc9a') method: 'get',
.expect('Content-type', /json/) url: '/users',
.expect(401) auth: {key: 'admin'},
.end((err, res) => { httpStatus: 401
if (err) done (err); });
should(res.body).be.eql({status: 'Unauthorized'});
done();
}); });
}); });
});
describe('GET /user/{name}', () => {
describe('GET /user/{name}', () => {
let server;
before(done => {
process.env.port = '2999';
process.env.NODE_ENV = 'test';
db.connect('test', done);
});
beforeEach(done => {
delete require.cache[require.resolve('../index')]; // prevent loading from cache
server = require('../index');
db.drop(err => { // reset database
if (err) return done(err);
db.loadJson(require('../test/db.json'), done);
});
});
afterEach(done => {
server.close(done);
});
it('returns own user details', done => { it('returns own user details', done => {
supertest(server) TestHelper.request(server, done, {
.get('/user') method: 'get',
.auth('janedoe', 'Xyz890*)') url: '/user',
.expect('Content-type', /json/) auth: {basic: 'janedoe'},
.expect(200) httpStatus: 200
.end((err, res) => { }).end((err, res) => {
if (err) done (err); if (err) return done (err);
should(res.body).have.only.keys('_id', 'email', 'name', 'level', 'location', 'device_name'); should(res.body).have.only.keys('_id', 'email', 'name', 'level', 'location', 'device_name');
should(res.body).have.property('_id').be.type('string'); should(res.body).have.property('_id').be.type('string');
should(res.body).have.property('email', 'jane.doe@bosch.com'); should(res.body).have.property('email', 'jane.doe@bosch.com');
@ -109,13 +70,13 @@ describe('GET /user/{name}', () => {
}); });
}); });
it('returns other user details for admin', done => { it('returns other user details for admin', done => {
supertest(server) TestHelper.request(server, done, {
.get('/user/janedoe') method: 'get',
.auth('admin', 'Abc123!#') url: '/user/janedoe',
.expect('Content-type', /json/) auth: {basic: 'admin'},
.expect(200) httpStatus: 200
.end((err, res) => { }).end((err, res) => {
if (err) done (err); if (err) return done (err);
should(res.body).have.only.keys('_id', 'email', 'name', 'level', 'location', 'device_name'); should(res.body).have.only.keys('_id', 'email', 'name', 'level', 'location', 'device_name');
should(res.body).have.property('_id').be.type('string'); should(res.body).have.property('_id').be.type('string');
should(res.body).have.property('email', 'jane.doe@bosch.com'); should(res.body).have.property('email', 'jane.doe@bosch.com');
@ -127,82 +88,49 @@ describe('GET /user/{name}', () => {
}); });
}); });
it('rejects requests from non-admins for another user', done => { it('rejects requests from non-admins for another user', done => {
supertest(server) TestHelper.request(server, done, {
.get('/user/admin') method: 'get',
.auth('janedoe', 'Xyz890*)') url: '/user/admin',
.expect('Content-type', /json/) auth: {basic: 'janedoe'},
.expect(403) httpStatus: 403
.end((err, res) => {
if (err) done (err);
should(res.body).be.eql({status: 'Forbidden'});
done();
}); });
}); });
it('rejects requests from a user API key', done => { it('rejects requests from a user API key', done => {
supertest(server) TestHelper.request(server, done, {
.get('/user?key=5ea0450ed851c30a90e70899') method: 'get',
.expect('Content-type', /json/) url: '/user',
.expect(401) auth: {key: 'janedoe'},
.end((err, res) => { httpStatus: 401
if (err) done (err);
should(res.body).be.eql({status: 'Unauthorized'});
done();
}); });
}); });
it('rejects requests from an admin API key', done => { it('rejects requests from an admin API key', done => {
supertest(server) TestHelper.request(server, done, {
.get('/user/janedoe?key=5ea131671feb9c2ee0aafc9a') method: 'get',
.expect('Content-type', /json/) url: '/user/janedoe',
.expect(401) auth: {key: 'janedoe'},
.end((err, res) => { httpStatus: 401
if (err) done (err);
should(res.body).be.eql({status: 'Unauthorized'});
done();
}); });
}); });
it('returns 404 for an unknown user', done => { it('returns 404 for an unknown user', done => {
supertest(server) TestHelper.request(server, done, {
.get('/user/unknown') method: 'get',
.auth('admin', 'Abc123!#') url: '/user/unknown',
.expect('Content-type', /json/) auth: {basic: 'admin'},
.expect(404) httpStatus: 404
.end((err, res) => { });
if (err) done (err);
should(res.body).be.eql({status: 'Not found'});
done();
}); });
}); });
});
describe('PUT /user/{name}', () => {
describe('PUT /user/{name}', () => {
let server;
before(done => {
process.env.port = '2999';
process.env.NODE_ENV = 'test';
db.connect('test', done);
});
beforeEach(done => {
delete require.cache[require.resolve('../index')]; // prevent loading from cache
server = require('../index');
db.drop(err => { // reset database
if (err) return done(err);
db.loadJson(require('../test/db.json'), done);
});
});
afterEach(done => {
server.close(done);
});
it('returns own user details', done => { it('returns own user details', done => {
supertest(server) TestHelper.request(server, done, {
.put('/user') method: 'put',
.send({}) url: '/user',
.auth('janedoe', 'Xyz890*)') auth: {basic: 'janedoe'},
.expect('Content-type', /json/) httpStatus: 200,
.expect(200) req: {}
.end((err, res) => { }).end((err, res) => {
if (err) done (err); if (err) return done (err);
should(res.body).have.only.keys('_id', 'email', 'name', 'level', 'location', 'device_name'); should(res.body).have.only.keys('_id', 'email', 'name', 'level', 'location', 'device_name');
should(res.body).have.property('_id').be.type('string'); should(res.body).have.property('_id').be.type('string');
should(res.body).have.property('email', 'jane.doe@bosch.com'); should(res.body).have.property('email', 'jane.doe@bosch.com');
@ -214,14 +142,14 @@ describe('PUT /user/{name}', () => {
}); });
}); });
it('returns other user details for admin', done => { it('returns other user details for admin', done => {
supertest(server) TestHelper.request(server, done, {
.put('/user/janedoe') method: 'put',
.send({}) url: '/user/janedoe',
.auth('admin', 'Abc123!#') auth: {basic: 'admin'},
.expect('Content-type', /json/) httpStatus: 200,
.expect(200) req: {}
.end((err, res) => { }).end((err, res) => {
if (err) done (err); if (err) return done (err);
should(res.body).have.only.keys('_id', 'email', 'name', 'level', 'location', 'device_name'); should(res.body).have.only.keys('_id', 'email', 'name', 'level', 'location', 'device_name');
should(res.body).have.property('_id').be.type('string'); should(res.body).have.property('_id').be.type('string');
should(res.body).have.property('email', 'jane.doe@bosch.com'); should(res.body).have.property('email', 'jane.doe@bosch.com');
@ -233,13 +161,14 @@ describe('PUT /user/{name}', () => {
}); });
}); });
it('changes user details as given', done => { it('changes user details as given', done => {
supertest(server) TestHelper.request(server, done, {
.put('/user') method: 'put',
.auth('admin', 'Abc123!#') url: '/user',
.send({name: 'adminnew', email: 'adminnew@bosch.com', pass: 'Abc123##', location: 'Abt', device_name: 'test'}) auth: {basic: 'admin'},
.expect(200) httpStatus: 200,
.end(err => { req: {name: 'adminnew', email: 'adminnew@bosch.com', pass: 'Abc123##', location: 'Abt', device_name: 'test'}
if (err) done (err); }).end(err => {
if (err) return done (err);
UserModel.find({name: 'adminnew'}).lean().exec( 'find', (err, data) => { UserModel.find({name: 'adminnew'}).lean().exec( 'find', (err, data) => {
if (err) return done(err); if (err) return done(err);
should(data).have.lengthOf(1); should(data).have.lengthOf(1);
@ -256,13 +185,14 @@ describe('PUT /user/{name}', () => {
}); });
}); });
it('lets the admin change a user level', done => { it('lets the admin change a user level', done => {
supertest(server) TestHelper.request(server, done, {
.put('/user/janedoe') method: 'put',
.auth('admin', 'Abc123!#') url: '/user/janedoe',
.send({level: 'read'}) auth: {basic: 'admin'},
.expect(200) httpStatus: 200,
.end(err => { req: {level: 'read'}
if (err) done (err); }).end(err => {
if (err) return done (err);
UserModel.find({name: 'janedoe'}).lean().exec( 'find', (err, data) => { UserModel.find({name: 'janedoe'}).lean().exec( 'find', (err, data) => {
if (err) return done(err); if (err) return done(err);
should(data).have.lengthOf(1); should(data).have.lengthOf(1);
@ -272,13 +202,14 @@ describe('PUT /user/{name}', () => {
}); });
}); });
it('does not change the level', done => { it('does not change the level', done => {
supertest(server) TestHelper.request(server, done, {
.put('/user') method: 'put',
.auth('janedoe', 'Xyz890*)') url: '/user',
.send({level: 'read'}) auth: {basic: 'janedoe'},
.expect(400) httpStatus: 400, default: false,
.end((err, res) => { req: {level: 'read'}
if (err) done (err); }).end((err, res) => {
if (err) return done (err);
should(res.body).be.eql({status: 'Invalid body format'}); should(res.body).be.eql({status: 'Invalid body format'});
UserModel.find({name: 'janedoe'}).lean().exec( 'find', (err, data) => { UserModel.find({name: 'janedoe'}).lean().exec( 'find', (err, data) => {
if (err) return done(err); if (err) return done(err);
@ -289,13 +220,14 @@ describe('PUT /user/{name}', () => {
}); });
}); });
it('rejects a username already in use', done => { it('rejects a username already in use', done => {
supertest(server) TestHelper.request(server, done, {
.put('/user') method: 'put',
.auth('admin', 'Abc123!#') url: '/user',
.send({name: 'janedoe'}) auth: {basic: 'admin'},
.expect(400) httpStatus: 400, default: false,
.end((err, res) => { req: {name: 'janedoe'}
if (err) done (err); }).end((err, res) => {
if (err) return done (err);
should(res.body).be.eql({status: 'Username already taken'}); should(res.body).be.eql({status: 'Username already taken'});
UserModel.find({name: 'janedoe'}).lean().exec( 'find', (err, data) => { UserModel.find({name: 'janedoe'}).lean().exec( 'find', (err, data) => {
if (err) return done(err); if (err) return done(err);
@ -305,121 +237,83 @@ describe('PUT /user/{name}', () => {
}); });
}); });
it('rejects invalid user details', done => { it('rejects invalid user details', done => {
supertest(server) TestHelper.request(server, done, {
.put('/user') method: 'put',
.auth('admin', 'Abc123!#') url: '/user',
.send({email: 'john.doe@bosch.com', name: 'johndoe', pass: 'Abc123!#', location: 44, device_name: 'Alpha II'}) auth: {basic: 'admin'},
.expect(400) httpStatus: 400,
.end((err, res) => { req: {email: 'john.doe@bosch.com', name: 'johndoe', pass: 'Abc123!#', location: 44, device_name: 'Alpha II'},
if (err) done (err); res: {status: 'Invalid body format'}
should(res.body).be.eql({status: 'Invalid body format'});
done();
}); });
}); });
it('rejects an invalid email address', done => { it('rejects an invalid email address', done => {
supertest(server) TestHelper.request(server, done, {
.put('/user') method: 'put',
.auth('admin', 'Abc123!#') url: '/user',
.send({email: 'john.doe'}) auth: {basic: 'admin'},
.expect(400) httpStatus: 400,
.end((err, res) => { req: {email: 'john.doe'},
if (err) done (err); res: {status: 'Invalid body format'}
should(res.body).be.eql({status: 'Invalid body format'});
done();
}); });
}); });
it('rejects an invalid password', done => { it('rejects an invalid password', done => {
supertest(server) TestHelper.request(server, done, {
.put('/user') method: 'put',
.auth('admin', 'Abc123!#') url: '/user',
.send({pass: 'password'}) auth: {basic: 'admin'},
.expect(400) httpStatus: 400,
.end((err, res) => { req: {pass: 'password'},
if (err) done (err); res: {status: 'Invalid body format'}
should(res.body).be.eql({status: 'Invalid body format'});
done();
}); });
}); });
it('rejects requests from non-admins for another user', done => { it('rejects requests from non-admins for another user', done => {
supertest(server) TestHelper.request(server, done, {
.put('/user/admin') method: 'put',
.send({}) url: '/user/admin',
.auth('janedoe', 'Xyz890*)') auth: {basic: 'janedoe'},
.expect('Content-type', /json/) httpStatus: 403,
.expect(403) req: {}
.end((err, res) => {
if (err) done (err);
should(res.body).be.eql({status: 'Forbidden'});
done();
}); });
}); });
it('rejects requests from a user API key', done => { it('rejects requests from a user API key', done => {
supertest(server) TestHelper.request(server, done, {
.put('/user?key=5ea0450ed851c30a90e70899') method: 'put',
.send({}) url: '/user',
.expect('Content-type', /json/) auth: {key: 'janedoe'},
.expect(401) httpStatus: 401,
.end((err, res) => { req: {}
if (err) done (err);
should(res.body).be.eql({status: 'Unauthorized'});
done();
}); });
}); });
it('rejects requests from an admin API key', done => { it('rejects requests from an admin API key', done => {
supertest(server) TestHelper.request(server, done, {
.put('/user/janedoe?key=5ea131671feb9c2ee0aafc9a') method: 'put',
.send({}) url: '/user/janedoe',
.expect('Content-type', /json/) auth: {key: 'admin'},
.expect(401) httpStatus: 401,
.end((err, res) => { req: {}
if (err) done (err);
should(res.body).be.eql({status: 'Unauthorized'});
done();
}); });
}); });
it('returns 404 for an unknown user', done => { it('returns 404 for an unknown user', done => {
supertest(server) TestHelper.request(server, done, {
.put('/user/unknown') method: 'put',
.auth('admin', 'Abc123!#') url: '/user/unknown',
.send({}) auth: {basic: 'admin'},
.expect(404) httpStatus: 404,
.end((err, res) => { req: {}
if (err) done (err); });
should(res.body).be.eql({status: 'Not found'});
done();
}); });
}); });
});
describe('POST /user/new', () => {
describe('POST /user/new', () => {
let server;
before(done => {
process.env.port = '2999';
process.env.NODE_ENV = 'test';
db.connect('test', done);
});
beforeEach(done => {
delete require.cache[require.resolve('../index')]; // prevent loading from cache
server = require('../index');
db.drop(err => { // reset database
if (err) return done(err);
db.loadJson(require('../test/db.json'), done);
});
});
afterEach(done => {
server.close(done);
});
it('returns the added user data', done => { it('returns the added user data', done => {
supertest(server) TestHelper.request(server, done, {
.post('/user/new') method: 'post',
.auth('admin', 'Abc123!#') url: '/user/new',
.send({email: 'john.doe@bosch.com', name: 'johndoe', pass: 'Abc123!#', level: 'read', location: 'Rng', device_name: 'Alpha II'}) auth: {basic: 'admin'},
.expect('Content-type', /json/) httpStatus: 200,
.expect(200) req: {email: 'john.doe@bosch.com', name: 'johndoe', pass: 'Abc123!#', level: 'read', location: 'Rng', device_name: 'Alpha II'}
.end((err, res) => { }).end((err, res) => {
if (err) done (err); if (err) return done (err);
should(res.body).have.only.keys('_id', 'email', 'name', 'level', 'location', 'device_name'); should(res.body).have.only.keys('_id', 'email', 'name', 'level', 'location', 'device_name');
should(res.body).have.property('_id').be.type('string'); should(res.body).have.property('_id').be.type('string');
should(res.body).have.property('email', 'john.doe@bosch.com'); should(res.body).have.property('email', 'john.doe@bosch.com');
@ -431,13 +325,14 @@ describe('POST /user/new', () => {
}); });
}); });
it('stores the data', done => { it('stores the data', done => {
supertest(server) TestHelper.request(server, done, {
.post('/user/new') method: 'post',
.auth('admin', 'Abc123!#') url: '/user/new',
.send({email: 'john.doe@bosch.com', name: 'johndoe', pass: 'Abc123!#', level: 'read', location: 'Rng', device_name: 'Alpha II'}) auth: {basic: 'admin'},
.expect(200) httpStatus: 200,
.end(err => { req: {email: 'john.doe@bosch.com', name: 'johndoe', pass: 'Abc123!#', level: 'read', location: 'Rng', device_name: 'Alpha II'}
if (err) done (err); }).end(err => {
if (err) return done (err);
UserModel.find({name: 'johndoe'}).lean().exec( 'find', (err, data) => { UserModel.find({name: 'johndoe'}).lean().exec( 'find', (err, data) => {
if (err) return done(err); if (err) return done(err);
should(data).have.lengthOf(1); should(data).have.lengthOf(1);
@ -454,13 +349,14 @@ describe('POST /user/new', () => {
}); });
}); });
it('rejects a username already in use', done => { it('rejects a username already in use', done => {
supertest(server) TestHelper.request(server, done, {
.post('/user/new') method: 'post',
.auth('admin', 'Abc123!#') url: '/user/new',
.send({email: 'j.doe@bosch.com', name: 'janedoe', pass: 'Abc123!#', level: 'read', location: 'Rng', device_name: 'Alpha II'}) auth: {basic: 'admin'},
.expect(400) httpStatus: 400, default: false,
.end((err, res) => { req: {email: 'j.doe@bosch.com', name: 'janedoe', pass: 'Abc123!#', level: 'read', location: 'Rng', device_name: 'Alpha II'}
if (err) done (err); }).end((err, res) => {
if (err) return done (err);
should(res.body).be.eql({status: 'Username already taken'}); should(res.body).be.eql({status: 'Username already taken'});
UserModel.find({name: 'janedoe'}).lean().exec( 'find', (err, data) => { UserModel.find({name: 'janedoe'}).lean().exec( 'find', (err, data) => {
if (err) return done(err); if (err) return done(err);
@ -470,159 +366,102 @@ describe('POST /user/new', () => {
}); });
}); });
it('rejects invalid user details', done => { it('rejects invalid user details', done => {
supertest(server) TestHelper.request(server, done, {
.post('/user/new') method: 'post',
.auth('admin', 'Abc123!#') url: '/user/new',
.send({email: 'john.doe@bosch.com', name: 'johndoe', pass: 'Abc123!#', level: 'read', location: 44, device_name: 'Alpha II'}) auth: {basic: 'admin'},
.expect(400) httpStatus: 400,
.end((err, res) => { req: {email: 'john.doe@bosch.com', name: 'johndoe', pass: 'Abc123!#', level: 'read', location: 44, device_name: 'Alpha II'},
if (err) done (err); res: {status: 'Invalid body format'}
should(res.body).be.eql({status: 'Invalid body format'});
done();
}); });
}); });
it('rejects an invalid user level', done => { it('rejects an invalid user level', done => {
supertest(server) TestHelper.request(server, done, {
.post('/user/new') method: 'post',
.auth('admin', 'Abc123!#') url: '/user/new',
.send({email: 'john.doe@bosch.com', name: 'johndoe', pass: 'Abc123!#', level: 'xxx', location: 'Rng', device_name: 'Alpha II'}) auth: {basic: 'admin'},
.expect(400) httpStatus: 400,
.end((err, res) => { req: {email: 'john.doe@bosch.com', name: 'johndoe', pass: 'Abc123!#', level: 'xxx', location: 'Rng', device_name: 'Alpha II'},
if (err) done (err); res: {status: 'Invalid body format'}
should(res.body).be.eql({status: 'Invalid body format'});
done();
}); });
}); });
it('rejects an invalid email address', done => { it('rejects an invalid email address', done => {
supertest(server) TestHelper.request(server, done, {
.post('/user/new') method: 'post',
.auth('admin', 'Abc123!#') url: '/user/new',
.send({email: 'john.doe', name: 'johndoe', pass: 'Abc123!#', level: 'read', location: 'Rng', device_name: 'Alpha II'}) auth: {basic: 'admin'},
.expect(400) httpStatus: 400,
.end((err, res) => { req: {email: 'john.doe', name: 'johndoe', pass: 'Abc123!#', level: 'read', location: 'Rng', device_name: 'Alpha II'},
if (err) done (err); res: {status: 'Invalid body format'}
should(res.body).be.eql({status: 'Invalid body format'});
done();
}); });
}); });
it('rejects an invalid password', done => { it('rejects an invalid password', done => {
supertest(server) TestHelper.request(server, done, {
.post('/user/new') method: 'post',
.auth('admin', 'Abc123!#') url: '/user/new',
.send({email: 'john.doe@bosch.com', name: 'johndoe', pass: 'password', level: 'read', location: 'Rng', device_name: 'Alpha II'}) auth: {basic: 'admin'},
.expect(400) httpStatus: 400,
.end((err, res) => { req: {email: 'john.doe@bosch.com', name: 'johndoe', pass: 'password', level: 'read', location: 'Rng', device_name: 'Alpha II'},
if (err) done (err); res: {status: 'Invalid body format'}
should(res.body).be.eql({status: 'Invalid body format'});
done();
}); });
}); });
it('rejects requests from non-admins', done => { it('rejects requests from non-admins', done => {
supertest(server) TestHelper.request(server, done, {
.post('/user/new') method: 'post',
.auth('janedoe', 'Xyz890*)') url: '/user/new',
.send({email: 'john.doe@bosch.com', name: 'johndoe', pass: 'Abc123!#', level: 'read', location: 'Rng', device_name: 'Alpha II'}) auth: {basic: 'janedoe'},
.expect('Content-type', /json/) httpStatus: 403,
.expect(403) req: {email: 'john.doe@bosch.com', name: 'johndoe', pass: 'Abc123!#', level: 'read', location: 'Rng', device_name: 'Alpha II'}
.end((err, res) => {
if (err) done (err);
should(res.body).be.eql({status: 'Forbidden'});
done();
}); });
}); });
it('rejects requests from an admin API key', done => { it('rejects requests from an admin API key', done => {
supertest(server) TestHelper.request(server, done, {
.post('/user/new?key=5ea131671feb9c2ee0aafc9a') method: 'post',
.send({email: 'john.doe@bosch.com', name: 'johndoe', pass: 'Abc123!#', level: 'read', location: 'Rng', device_name: 'Alpha II'}) url: '/user/new',
.expect('Content-type', /json/) auth: {key: 'admin'},
.expect(401) httpStatus: 401,
.end((err, res) => { req: {email: 'john.doe@bosch.com', name: 'johndoe', pass: 'Abc123!#', level: 'read', location: 'Rng', device_name: 'Alpha II'}
if (err) done (err); });
should(res.body).be.eql({status: 'Unauthorized'});
done();
}); });
}); });
});
describe('POST /user/passreset', () => {
describe('POST /user/passreset', () => {
let server;
before(done => {
process.env.port = '2999';
process.env.NODE_ENV = 'test';
db.connect('test', done);
});
beforeEach(done => {
delete require.cache[require.resolve('../index')]; // prevent loading from cache
server = require('../index');
db.drop(err => { // reset database
if (err) return done(err);
db.loadJson(require('../test/db.json'), done);
});
});
afterEach(done => {
server.close(done);
});
it('returns the ok response', done => { it('returns the ok response', done => {
supertest(server) TestHelper.request(server, done, {
.post('/user/passreset') method: 'post',
.send({ url: '/user/passreset',
email: 'jane.doe@bosch.com', httpStatus: 200,
name: 'janedoe' req: {email: 'jane.doe@bosch.com', name: 'janedoe'},
}) res: {status: 'OK'}
.expect('Content-type', /json/)
.expect(200)
.end((err, res) => {
if (err) done(err);
should(res.body).be.eql({status: 'OK'});
done();
}); });
}); });
it('returns 404 for wrong username/email combo', done => { it('returns 404 for wrong username/email combo', done => {
supertest(server) TestHelper.request(server, done, {
.post('/user/passreset') method: 'post',
.send({ url: '/user/passreset',
email: 'jane.doe@bosch.com', httpStatus: 404,
name: 'admin' req: {email: 'jane.doe@bosch.com', name: 'admin'}
})
.expect('Content-type', /json/)
.expect(404)
.end((err, res) => {
if (err) done(err);
should(res.body).be.eql({status: 'Not found'});
done();
}); });
}); });
it('returns 404 for unknown username', done => { it('returns 404 for unknown username', done => {
supertest(server) TestHelper.request(server, done, {
.post('/user/passreset') method: 'post',
.send({ url: '/user/passreset',
email: 'jane.doe@bosch.com', httpStatus: 404,
name: 'admin' req: {email: 'jane.doe@bosch.com', name: 'username'}
})
.expect('Content-type', /json/)
.expect(404)
.end((err, res) => {
if (err) done(err);
should(res.body).be.eql({status: 'Not found'});
done();
}); });
}); });
it('changes the user password', done => { it('changes the user password', done => {
UserModel.find({name: 'janedoe'}).lean().exec( 'find', (err, data: any) => { UserModel.find({name: 'janedoe'}).lean().exec( 'find', (err, data: any) => {
if (err) return done(err); if (err) return done(err);
const oldpass = data[0].pass; const oldpass = data[0].pass;
supertest(server) TestHelper.request(server, done, {
.post('/user/passreset') method: 'post',
.send({ url: '/user/passreset',
email: 'jane.doe@bosch.com', httpStatus: 200,
name: 'janedoe' req: {email: 'jane.doe@bosch.com', name: 'janedoe'}
}) }).end((err, res) => {
.expect('Content-type', /json/) if (err) return done(err);
.expect(200)
.end((err, res) => {
if (err) done(err);
should(res.body).be.eql({status: 'OK'}); should(res.body).be.eql({status: 'OK'});
UserModel.find({name: 'janedoe'}).lean().exec( (err, data: any) => { UserModel.find({name: 'janedoe'}).lean().exec( (err, data: any) => {
if (err) return done(err); if (err) return done(err);
@ -632,4 +471,5 @@ describe('POST /user/passreset', () => {
}); });
}); });
}); });
});
}); });