cannot add username twice
This commit is contained in:
parent
f23b65d3d8
commit
90d34f1e1b
@ -11,7 +11,7 @@
|
||||
application/json:
|
||||
schema:
|
||||
properties:
|
||||
message:
|
||||
status:
|
||||
type: string
|
||||
example: 'API server up and running!'
|
||||
500:
|
||||
|
@ -40,7 +40,7 @@ export default class db {
|
||||
});
|
||||
mongoose.connection.on('error', console.error.bind(console, 'connection error:'));
|
||||
mongoose.connection.once('open', () => {
|
||||
console.log(`Connected to ${connectionString}`);
|
||||
console.log(process.env.NODE_ENV === 'test' ? '' : `Connected to ${connectionString}`);
|
||||
this.state.db = mongoose.connection;
|
||||
done();
|
||||
});
|
||||
@ -60,7 +60,6 @@ export default class db {
|
||||
let dropCounter = 0; // count number of dropped collections to know when to return done()
|
||||
collections.forEach(collection => { // drop each collection
|
||||
this.state.db.dropCollection(collection.name, () => {
|
||||
console.log('dropped collection ' + collection.name);
|
||||
if (++ dropCounter >= collections.length) { // all collections dropped
|
||||
done();
|
||||
}
|
||||
@ -79,7 +78,6 @@ export default class db {
|
||||
Object.keys(json.collections).forEach(collectionName => { // create each collection
|
||||
this.state.db.createCollection(collectionName, (err, collection) => {
|
||||
collection.insertMany(json.collections[collectionName], () => { // insert JSON data
|
||||
console.log('loaded collection ' + collectionName);
|
||||
if (++ loadCounter >= Object.keys(json.collections).length) { // all collections loaded
|
||||
done();
|
||||
}
|
||||
|
@ -6,7 +6,7 @@ import db from './db';
|
||||
|
||||
|
||||
// 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
|
||||
@ -53,7 +53,7 @@ app.use((err, req, res, ignore) => { // internal server error handling
|
||||
|
||||
// hook up server to 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;
|
@ -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();
|
||||
});
|
||||
});
|
||||
|
@ -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;
|
||||
|
@ -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();
|
||||
|
@ -18,6 +18,14 @@ router.post('/user/new', (req, res, next) => {
|
||||
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
|
||||
bcrypt.hash(user.pass, 10, (err, hash) => { // password hashing
|
||||
user.pass = hash;
|
||||
@ -27,5 +35,6 @@ router.post('/user/new', (req, res, next) => {
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
module.exports = router;
|
Reference in New Issue
Block a user