2017-05-10 11:04:06 +02:00
|
|
|
let mongoose = require("mongoose");
|
|
|
|
let UserModel = require('../models/user');
|
|
|
|
let AppUserModel = require('../models/app-user');
|
2017-05-11 18:36:32 +02:00
|
|
|
let urls = require('../config/api-url');
|
2017-05-11 15:12:17 +02:00
|
|
|
let codes = require('../routes/http-codes');
|
|
|
|
|
2017-05-10 11:04:06 +02:00
|
|
|
|
|
|
|
//Require the dev-dependencies
|
|
|
|
let chai = require('chai');
|
|
|
|
let chaiHttp = require('chai-http');
|
|
|
|
let server = require('../server');
|
|
|
|
let should = chai.should();
|
|
|
|
|
|
|
|
chai.use(chaiHttp);
|
|
|
|
//Our parent block
|
|
|
|
describe('Users', () => {
|
|
|
|
beforeEach((done) => { //Before each test we empty the database
|
|
|
|
UserModel.remove({}, (err) => {
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
/*
|
|
|
|
* Test the /GET route
|
|
|
|
*/
|
|
|
|
describe('/GET users', () => {
|
|
|
|
it('it should GET all the users', (done) => {
|
|
|
|
chai.request(server)
|
2017-05-11 15:12:17 +02:00
|
|
|
.get(urls.users)
|
2017-05-10 11:04:06 +02:00
|
|
|
.end((err, res) => {
|
2017-05-11 15:12:17 +02:00
|
|
|
res.should.have.status(codes.success);
|
2017-05-10 11:04:06 +02:00
|
|
|
res.body.should.be.a('array');
|
|
|
|
res.body.length.should.be.eql(0);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Test the /POST route
|
|
|
|
*/
|
|
|
|
describe('/POST users', () => {
|
|
|
|
|
|
|
|
let token;
|
|
|
|
|
|
|
|
before(function (done) {
|
|
|
|
AppUserModel.remove({}, (err) => {
|
|
|
|
done();
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
|
|
|
before(function (done) {
|
|
|
|
let appUser = {
|
|
|
|
username: 'testUsr',
|
|
|
|
password: '$2a$10$i9cBC06uGJnnrqQCh8COkuZLMChLQqw5j4K0yfDQn1udTDAompHka'
|
|
|
|
};
|
|
|
|
let appUserEncoded = {
|
|
|
|
username: appUser.username,
|
|
|
|
password: 'simplePass'
|
|
|
|
};
|
|
|
|
let appUserModel = new AppUserModel(appUser);
|
|
|
|
appUserModel.save();
|
|
|
|
|
|
|
|
chai.request(server)
|
2017-05-11 15:12:17 +02:00
|
|
|
.post(urls.auth)
|
2017-05-10 11:04:06 +02:00
|
|
|
.send(appUserEncoded)
|
|
|
|
.end(function (err, res) {
|
|
|
|
const result = JSON.parse(res.text);
|
|
|
|
token = result.token;
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-05-11 15:12:17 +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)
|
2017-05-11 15:12:17 +02:00
|
|
|
.post(urls.users)
|
2017-05-10 11:04:06 +02:00
|
|
|
.send({})
|
|
|
|
.end((err, res) => {
|
2017-05-11 15:12:17 +02:00
|
|
|
res.should.have.status(codes.forbidden);
|
2017-05-10 11:04:06 +02:00
|
|
|
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();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('it should POST a user with provided username', (done) => {
|
|
|
|
const user = {username: 'john'};
|
|
|
|
chai.request(server)
|
2017-05-11 15:12:17 +02:00
|
|
|
.post(urls.users)
|
2017-05-10 11:04:06 +02:00
|
|
|
.set('x-access-token', token)
|
|
|
|
.send(user)
|
|
|
|
.end((err, res) => {
|
2017-05-11 15:12:17 +02:00
|
|
|
res.should.have.status(codes.created);
|
2017-05-10 11:04:06 +02:00
|
|
|
res.body.should.be.a('object');
|
|
|
|
res.body.should.have.property('username').eql(user.username);
|
|
|
|
res.body.should.have.property('squadId').eql(null);
|
|
|
|
res.body.should.have.property('rankLvl').eql(0);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-05-11 15:12:17 +02:00
|
|
|
/*
|
|
|
|
* Test the /PATCH route
|
|
|
|
*/
|
|
|
|
describe('/PATCH users', () => {
|
|
|
|
|
|
|
|
it('it should not PATCH a user without auth-token provided', (done) => {
|
|
|
|
chai.request(server)
|
|
|
|
.patch(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();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Test the /DELETE route
|
|
|
|
*/
|
|
|
|
describe('/DELETE users', () => {
|
|
|
|
it('it should not accept DELETE method without id in url', (done) => {
|
|
|
|
chai.request(server)
|
|
|
|
.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)
|
|
|
|
.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
|
|
|
});
|