cannot add username twice
This commit is contained in:
parent
f23b65d3d8
commit
90d34f1e1b
@ -11,7 +11,7 @@
|
|||||||
application/json:
|
application/json:
|
||||||
schema:
|
schema:
|
||||||
properties:
|
properties:
|
||||||
message:
|
status:
|
||||||
type: string
|
type: string
|
||||||
example: 'API server up and running!'
|
example: 'API server up and running!'
|
||||||
500:
|
500:
|
||||||
|
@ -40,7 +40,7 @@ export default class db {
|
|||||||
});
|
});
|
||||||
mongoose.connection.on('error', console.error.bind(console, 'connection error:'));
|
mongoose.connection.on('error', console.error.bind(console, 'connection error:'));
|
||||||
mongoose.connection.once('open', () => {
|
mongoose.connection.once('open', () => {
|
||||||
console.log(`Connected to ${connectionString}`);
|
console.log(process.env.NODE_ENV === 'test' ? '' : `Connected to ${connectionString}`);
|
||||||
this.state.db = mongoose.connection;
|
this.state.db = mongoose.connection;
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
@ -60,7 +60,6 @@ export default class db {
|
|||||||
let dropCounter = 0; // count number of dropped collections to know when to return done()
|
let dropCounter = 0; // count number of dropped collections to know when to return done()
|
||||||
collections.forEach(collection => { // drop each collection
|
collections.forEach(collection => { // drop each collection
|
||||||
this.state.db.dropCollection(collection.name, () => {
|
this.state.db.dropCollection(collection.name, () => {
|
||||||
console.log('dropped collection ' + collection.name);
|
|
||||||
if (++ dropCounter >= collections.length) { // all collections dropped
|
if (++ dropCounter >= collections.length) { // all collections dropped
|
||||||
done();
|
done();
|
||||||
}
|
}
|
||||||
@ -79,7 +78,6 @@ export default class db {
|
|||||||
Object.keys(json.collections).forEach(collectionName => { // create each collection
|
Object.keys(json.collections).forEach(collectionName => { // create each collection
|
||||||
this.state.db.createCollection(collectionName, (err, collection) => {
|
this.state.db.createCollection(collectionName, (err, collection) => {
|
||||||
collection.insertMany(json.collections[collectionName], () => { // insert JSON data
|
collection.insertMany(json.collections[collectionName], () => { // insert JSON data
|
||||||
console.log('loaded collection ' + collectionName);
|
|
||||||
if (++ loadCounter >= Object.keys(json.collections).length) { // all collections loaded
|
if (++ loadCounter >= Object.keys(json.collections).length) { // all collections loaded
|
||||||
done();
|
done();
|
||||||
}
|
}
|
||||||
|
@ -6,7 +6,7 @@ import db from './db';
|
|||||||
|
|
||||||
|
|
||||||
// tell if server is running in debug or production environment
|
// tell if server is running in debug or production environment
|
||||||
console.log(process.env.NODE_ENV === 'production' ? '===== PRODUCTION =====' : '===== DEVELOPMENT =====');
|
console.log(process.env.NODE_ENV === 'production' ? '===== PRODUCTION =====' : process.env.NODE_ENV === 'test' ? '' :'===== DEVELOPMENT =====');
|
||||||
|
|
||||||
|
|
||||||
// mongodb connection
|
// mongodb connection
|
||||||
@ -53,7 +53,7 @@ app.use((err, req, res, ignore) => { // internal server error handling
|
|||||||
|
|
||||||
// hook up server to port
|
// hook up server to port
|
||||||
const server = app.listen(port, () => {
|
const server = app.listen(port, () => {
|
||||||
console.log(`Listening on http://localhost:${port}`);
|
console.log(process.env.NODE_ENV === 'test' ? '' : `Listening on http://localhost:${port}`);
|
||||||
});
|
});
|
||||||
|
|
||||||
module.exports = server;
|
module.exports = server;
|
@ -8,6 +8,7 @@ describe('/', () => {
|
|||||||
|
|
||||||
before(done => {
|
before(done => {
|
||||||
process.env.port = '2999';
|
process.env.port = '2999';
|
||||||
|
process.env.NODE_ENV = 'test';
|
||||||
db.connect('test', done);
|
db.connect('test', done);
|
||||||
});
|
});
|
||||||
beforeEach(done => {
|
beforeEach(done => {
|
||||||
@ -26,7 +27,7 @@ describe('/', () => {
|
|||||||
.get('/')
|
.get('/')
|
||||||
.expect('Content-type', /json/)
|
.expect('Content-type', /json/)
|
||||||
.expect(200, (err, res) => {
|
.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();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -3,7 +3,7 @@ import express from 'express';
|
|||||||
const router = express.Router();
|
const router = express.Router();
|
||||||
|
|
||||||
router.get('/', (req, res) => {
|
router.get('/', (req, res) => {
|
||||||
res.json({message: 'API server up and running!'});
|
res.json({status: 'API server up and running!'});
|
||||||
});
|
});
|
||||||
|
|
||||||
module.exports = router;
|
module.exports = router;
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
import supertest from 'supertest';
|
import supertest from 'supertest';
|
||||||
import should from 'should/as-function';
|
import should from 'should/as-function';
|
||||||
import db from '../db';
|
import db from '../db';
|
||||||
import userModel from '../models/user';
|
import UserModel from '../models/user';
|
||||||
|
|
||||||
|
|
||||||
describe('/user/new', () => {
|
describe('/user/new', () => {
|
||||||
@ -9,6 +9,7 @@ describe('/user/new', () => {
|
|||||||
|
|
||||||
before(done => {
|
before(done => {
|
||||||
process.env.port = '2999';
|
process.env.port = '2999';
|
||||||
|
process.env.NODE_ENV = 'test';
|
||||||
db.connect('test', done);
|
db.connect('test', done);
|
||||||
});
|
});
|
||||||
beforeEach(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'})
|
.send({email: 'john.doe@bosch.com', name: 'johndoe', pass: 'Abc123!#', level: 'read', location: 'Rng', device_name: 'Alpha II'})
|
||||||
.expect(200, err => {
|
.expect(200, err => {
|
||||||
if (err) return done(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);
|
||||||
should(data[0]).have.only.keys('_id', 'name', 'pass', 'email', 'level', 'location', 'device_name', 'key', '__v');
|
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)
|
supertest(server)
|
||||||
.post('/user/new')
|
.post('/user/new')
|
||||||
.send({email: 'j.doe@bosch.com', name: 'janedoe', pass: 'Abc123!#', level: 'read', location: 'Rng', device_name: 'Alpha II'})
|
.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);
|
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);
|
if (err) return done(err);
|
||||||
should(data).have.lengthOf(1);
|
should(data).have.lengthOf(1);
|
||||||
done();
|
done();
|
||||||
|
@ -18,6 +18,14 @@ router.post('/user/new', (req, res, next) => {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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
|
user.key = mongoose.Types.ObjectId(); // use object id as unique API key
|
||||||
bcrypt.hash(user.pass, 10, (err, hash) => { // password hashing
|
bcrypt.hash(user.pass, 10, (err, hash) => { // password hashing
|
||||||
user.pass = hash;
|
user.pass = hash;
|
||||||
@ -26,6 +34,7 @@ router.post('/user/new', (req, res, next) => {
|
|||||||
res.json(UserValidate.output(data.toObject()));
|
res.json(UserValidate.output(data.toObject()));
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
module.exports = router;
|
module.exports = router;
|
Reference in New Issue
Block a user