opt-cc/server/test/users.spec.js

154 lines
4.5 KiB
JavaScript
Raw Permalink Normal View History

2017-05-10 11:04:06 +02:00
let UserModel = require('../models/user');
let urls = require('../config/api-url');
let codes = require('../routes/http-codes');
2017-05-10 11:04:06 +02:00
2018-03-12 09:26:44 +01:00
// Require the dev-dependencies
2017-05-10 11:04:06 +02:00
let chai = require('chai');
let chaiHttp = require('chai-http');
let server = require('../server');
2018-03-12 12:35:19 +01:00
// chai methods
require('chai').should();
2017-05-10 11:04:06 +02:00
chai.use(chaiHttp);
2018-03-12 09:26:44 +01:00
// Our parent block
2017-05-10 11:04:06 +02:00
describe('Users', () => {
2018-03-12 09:26:44 +01:00
beforeEach((done) => { // Before each test we empty the database
UserModel.deleteMany({}, (err) => {
2017-05-10 11:04:06 +02:00
done();
});
});
/*
* Test the /GET route
*/
describe('/GET users', () => {
it('it should GET all the users', (done) => {
chai.request(server)
2018-02-26 09:04:27 +01:00
.get(urls.users)
.end((err, res) => {
res.should.have.status(codes.success);
res.body.should.be.a('array');
res.body.length.should.be.eql(0);
done();
});
2017-05-10 11:04:06 +02:00
});
});
/*
* Test the /POST route
*/
describe('/POST users', () => {
2017-07-09 18:45:07 +02:00
// let token;
//
// before(function (done) {
// AppUserModel.remove({}, (err) => {
// done();
// })
// });
//
// before(function (done) {
// let appUser = {
// username: 'testUsr',
// password: '$2a$10$i9cBC06uGJnnrqQCh8COkuZLMChLQqw5j4K0yfDQn1udTDAompHka',
// permission: 2
// };
// let appUserEncoded = {
// username: appUser.username,
// password: 'simplePass'
// };
// let appUserModel = new AppUserModel(appUser);
// appUserModel.save();
//
// chai.request(server)
// .post(urls.auth)
// .send(appUserEncoded)
// .end(function (err, res) {
// const result = JSON.parse(res.text);
// token = result.token;
// done();
// });
// });
2017-05-10 11:04:06 +02:00
it('it should not POST a user without auth-token provided', (done) => {
2017-05-10 11:04:06 +02:00
chai.request(server)
2018-02-26 09:04:27 +01:00
.post(urls.users)
.send({})
.end((err, res) => {
res.should.have.status(codes.forbidden);
res.body.should.be.a('object');
res.body.should.have.property('success').eql(false);
res.body.should.have.property('message').eql('No token provided.');
done();
});
2017-05-10 11:04:06 +02:00
});
2017-07-09 18:45:07 +02:00
// it('it should POST a user with provided username', (done) => {
// const user = {username: 'john'};
// chai.request(server)
// .post(urls.users)
// .set('x-access-token', token)
// .send(user)
// .end((err, res) => {
// res.should.have.status(codes.created);
// res.body.should.be.a('object');
// res.body.should.have.property('username').eql(user.username);
// res.body.should.have.property('squad').eql(null);
// res.body.should.have.property('rank').property('level').eql(0);
// res.body.should.have.property('awards').eql([]);
// done();
// });
// });
2017-05-10 11:04:06 +02:00
});
/*
2018-03-30 09:16:11 +02:00
* Test the /PUT route
*/
2018-03-30 09:16:11 +02:00
describe('/PUT users', () => {
it('it should not PUT a user without auth-token provided', (done) => {
chai.request(server)
2018-03-30 09:16:11 +02:00
.put(urls.users + '/someId')
2018-03-30 10:14:39 +02:00
.send({_id: 'someId'})
2018-02-26 09:04:27 +01:00
.end((err, res) => {
res.should.have.status(codes.forbidden);
res.body.should.be.a('object');
res.body.should.have.property('success').eql(false);
res.body.should.have.property('message').eql('No token provided.');
done();
});
});
});
/*
* Test the /DELETE route
*/
describe('/DELETE users', () => {
it('it should not accept DELETE method without id in url', (done) => {
chai.request(server)
2018-02-26 09:04:27 +01:00
.delete(urls.users)
.send({})
.end((err, res) => {
res.should.have.status(codes.wrongmethod);
res.body.should.be.a('object');
res.body.should.have.property('error').property('message')
.eql('this method is not allowed at ' + urls.users);
done();
});
});
it('it should not DELETE a user without auth-token provided', (done) => {
chai.request(server)
2018-02-26 09:04:27 +01:00
.delete(urls.users + '/someId')
.send({})
.end((err, res) => {
res.should.have.status(codes.forbidden);
res.body.should.be.a('object');
res.body.should.have.property('success').eql(false);
res.body.should.have.property('message').eql('No token provided.');
done();
});
});
});
2017-05-10 11:04:06 +02:00
});