From 90d34f1e1b27a1b35143247cef203850f28a41d7 Mon Sep 17 00:00:00 2001 From: VLE2FE Date: Wed, 22 Apr 2020 17:38:24 +0200 Subject: [PATCH] cannot add username twice --- oas/others.yaml | 2 +- src/db.ts | 4 +--- src/index.ts | 4 ++-- src/routes/root.spec.ts | 3 ++- src/routes/root.ts | 2 +- src/routes/user.spec.ts | 10 ++++++---- src/routes/user.ts | 21 +++++++++++++++------ 7 files changed, 28 insertions(+), 18 deletions(-) diff --git a/oas/others.yaml b/oas/others.yaml index df322fc..e5f200e 100644 --- a/oas/others.yaml +++ b/oas/others.yaml @@ -11,7 +11,7 @@ application/json: schema: properties: - message: + status: type: string example: 'API server up and running!' 500: diff --git a/src/db.ts b/src/db.ts index 1ac4222..98c8617 100644 --- a/src/db.ts +++ b/src/db.ts @@ -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(); } diff --git a/src/index.ts b/src/index.ts index 8c4af39..cfaf696 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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; \ No newline at end of file diff --git a/src/routes/root.spec.ts b/src/routes/root.spec.ts index 0e2d625..276f159 100644 --- a/src/routes/root.spec.ts +++ b/src/routes/root.spec.ts @@ -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(); }); }); diff --git a/src/routes/root.ts b/src/routes/root.ts index 896f360..bcbb40b 100644 --- a/src/routes/root.ts +++ b/src/routes/root.ts @@ -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; diff --git a/src/routes/user.spec.ts b/src/routes/user.spec.ts index fe5c031..4c50a70 100644 --- a/src/routes/user.spec.ts +++ b/src/routes/user.spec.ts @@ -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(); diff --git a/src/routes/user.ts b/src/routes/user.ts index ea2994f..4c1d8ed 100644 --- a/src/routes/user.ts +++ b/src/routes/user.ts @@ -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())); + }); }); }); });