Archived
2

cannot add username twice

This commit is contained in:
VLE2FE
2020-04-22 17:38:24 +02:00
parent f23b65d3d8
commit 90d34f1e1b
7 changed files with 28 additions and 18 deletions

View File

@ -8,6 +8,7 @@ describe('/', () => {
before(done => {
process.env.port = '2999';
process.env.NODE_ENV = 'test';
db.connect('test', done);
});
beforeEach(done => {
@ -26,7 +27,7 @@ describe('/', () => {
.get('/')
.expect('Content-type', /json/)
.expect(200, (err, res) => {
should(res.body).be.eql({message: 'API server up and running!'});
should(res.body).be.eql({status: 'API server up and running!'});
done();
});
});

View File

@ -3,7 +3,7 @@ import express from 'express';
const router = express.Router();
router.get('/', (req, res) => {
res.json({message: 'API server up and running!'});
res.json({status: 'API server up and running!'});
});
module.exports = router;

View File

@ -1,7 +1,7 @@
import supertest from 'supertest';
import should from 'should/as-function';
import db from '../db';
import userModel from '../models/user';
import UserModel from '../models/user';
describe('/user/new', () => {
@ -9,6 +9,7 @@ describe('/user/new', () => {
before(done => {
process.env.port = '2999';
process.env.NODE_ENV = 'test';
db.connect('test', done);
});
beforeEach(done => {
@ -44,7 +45,7 @@ describe('/user/new', () => {
.send({email: 'john.doe@bosch.com', name: 'johndoe', pass: 'Abc123!#', level: 'read', location: 'Rng', device_name: 'Alpha II'})
.expect(200, 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);
should(data).have.lengthOf(1);
should(data[0]).have.only.keys('_id', 'name', 'pass', 'email', 'level', 'location', 'device_name', 'key', '__v');
@ -63,9 +64,10 @@ describe('/user/new', () => {
supertest(server)
.post('/user/new')
.send({email: 'j.doe@bosch.com', name: 'janedoe', pass: 'Abc123!#', level: 'read', location: 'Rng', device_name: 'Alpha II'})
.expect(400, err => {
.expect(400, (err, res) => {
if (err) return done(err);
userModel.find({name: 'janedoe'}).lean().exec( 'find', (err, data) => {
should(res.body).be.eql({status: 'Username already taken'});
UserModel.find({name: 'janedoe'}).lean().exec( 'find', (err, data) => {
if (err) return done(err);
should(data).have.lengthOf(1);
done();

View File

@ -18,12 +18,21 @@ router.post('/user/new', (req, res, next) => {
return;
}
user.key = mongoose.Types.ObjectId(); // use object id as unique API key
bcrypt.hash(user.pass, 10, (err, hash) => { // password hashing
user.pass = hash;
new UserModel(user).save((err, data) => { // store user
if (err) next(err);
res.json(UserValidate.output(data.toObject()));
// check that user does not already exist
UserModel.find({name: user.name}).lean().exec( 'find', (err, data) => {
if (err) next(err);
if (data.length > 0) {
res.status(400).json({status: 'Username already taken'});
return;
}
user.key = mongoose.Types.ObjectId(); // use object id as unique API key
bcrypt.hash(user.pass, 10, (err, hash) => { // password hashing
user.pass = hash;
new UserModel(user).save((err, data) => { // store user
if (err) next(err);
res.json(UserValidate.output(data.toObject()));
});
});
});
});