From 8bf408138f73cbae64eef5b6028a76376a35a20d Mon Sep 17 00:00:00 2001 From: VLE2FE Date: Fri, 24 Apr 2020 10:53:45 +0200 Subject: [PATCH] changed to findById and improved db.loadJson --- package-lock.json | 31 +++++++++++++++++++++++++++++++ package.json | 2 ++ src/db.ts | 6 ++++++ src/helpers/authorize.ts | 4 ++-- src/routes/user.spec.ts | 5 +++-- src/routes/user.ts | 4 ++-- src/test/db.json | 4 ++-- 7 files changed, 48 insertions(+), 8 deletions(-) diff --git a/package-lock.json b/package-lock.json index b8354bc..5249707 100644 --- a/package-lock.json +++ b/package-lock.json @@ -93,6 +93,19 @@ "defer-to-connect": "^1.0.1" } }, + "@types/bcrypt": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/@types/bcrypt/-/bcrypt-3.0.0.tgz", + "integrity": "sha512-nohgNyv+1ViVcubKBh0+XiNJ3dO8nYu///9aJ4cgSqv70gBL+94SNy/iC2NLzKPT2Zt/QavrOkBVbZRLZmw6NQ==" + }, + "@types/bson": { + "version": "4.0.2", + "resolved": "https://registry.npmjs.org/@types/bson/-/bson-4.0.2.tgz", + "integrity": "sha512-+uWmsejEHfmSjyyM/LkrP0orfE2m5Mx9Xel4tXNeqi1ldK5XMQcDsFkBmLDtuyKUbxj2jGDo0H240fbCRJZo7Q==", + "requires": { + "@types/node": "*" + } + }, "@types/color-name": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/@types/color-name/-/color-name-1.1.1.tgz", @@ -103,6 +116,24 @@ "resolved": "https://registry.npmjs.org/@types/mocha/-/mocha-5.2.7.tgz", "integrity": "sha512-NYrtPht0wGzhwe9+/idPaBB+TqkY9AhTvOLMkThm0IoEfLaiVQZwBwyJ5puCkO3AUCWrmcoePjp2mbFocKy4SQ==" }, + "@types/mongodb": { + "version": "3.5.10", + "resolved": "https://registry.npmjs.org/@types/mongodb/-/mongodb-3.5.10.tgz", + "integrity": "sha512-6NkJNfFdFa/njBvN/9eAfq78bWUnapkdR3JbWGGpd7U71PjgKweA4Tlag8psi2mqm973vBYVTD1oc1u0lzRcig==", + "requires": { + "@types/bson": "*", + "@types/node": "*" + } + }, + "@types/mongoose": { + "version": "5.7.12", + "resolved": "https://registry.npmjs.org/@types/mongoose/-/mongoose-5.7.12.tgz", + "integrity": "sha512-yzLJk3cdSwuMXaIacUCWUb8m960YcgnID7S4ZPOOgzT39aSC46670TuunN+ajDio7OUcGG4mGg8eOGs2Z6VmrA==", + "requires": { + "@types/mongodb": "*", + "@types/node": "*" + } + }, "@types/node": { "version": "13.1.6", "resolved": "https://registry.npmjs.org/@types/node/-/node-13.1.6.tgz", diff --git a/package.json b/package.json index d5a2bfe..adc9874 100644 --- a/package.json +++ b/package.json @@ -15,7 +15,9 @@ "dependencies": { "@apidevtools/json-schema-ref-parser": "^8.0.0", "@hapi/joi": "^17.1.1", + "@types/bcrypt": "^3.0.0", "@types/mocha": "^5.2.7", + "@types/mongoose": "^5.7.12", "@types/node": "^13.1.6", "axios": "^0.19.2", "basic-auth": "^2.0.1", diff --git a/src/db.ts b/src/db.ts index b93fb6f..00477da 100644 --- a/src/db.ts +++ b/src/db.ts @@ -88,6 +88,12 @@ export default class db { let loadCounter = 0; // count number of loaded collections to know when to return done() Object.keys(json.collections).forEach(collectionName => { // create each collection + for(let i in json.collections[collectionName]) { // convert $oid fields to actual ObjectIds + console.log(json.collections[collectionName][i]); + Object.keys(json.collections[collectionName][i]).forEach(key => { + json.collections[collectionName][i][key] = json.collections[collectionName][i][key].hasOwnProperty('$oid') ? mongoose.Types.ObjectId(json.collections[collectionName][i][key].$oid) : json.collections[collectionName][i][key]; + }) + } this.state.db.createCollection(collectionName, (err, collection) => { collection.insertMany(json.collections[collectionName], () => { // insert JSON data if (++ loadCounter >= Object.keys(json.collections).length) { // all collections loaded diff --git a/src/helpers/authorize.ts b/src/helpers/authorize.ts index e42f388..d3c7e75 100644 --- a/src/helpers/authorize.ts +++ b/src/helpers/authorize.ts @@ -56,7 +56,7 @@ function basic (req, next): any { // checks basic auth and returns changed user return new Promise(resolve => { const auth = basicAuth(req); if (auth !== undefined) { // basic auth available - UserModel.find({name: auth.name}).lean().exec( 'find', (err, data) => { // find user + UserModel.find({name: auth.name}).lean().exec( (err, data: any) => { // find user if (err) next(err); if (data.length === 1) { // one user found bcrypt.compare(auth.pass, data[0].pass, (err, res) => { // check password @@ -83,7 +83,7 @@ function basic (req, next): any { // checks basic auth and returns changed user function key (req, next): any { // checks API key and returns changed user object return new Promise(resolve => { if (req.query.key !== undefined) { - UserModel.find({key: req.query.key}).lean().exec( 'find', (err, data) => { // find user + UserModel.find({key: req.query.key}).lean().exec( (err, data: any) => { // find user if (err) next(err); if (data.length === 1) { // one user found resolve({level: data[0].level, name: data[0].name}); diff --git a/src/routes/user.spec.ts b/src/routes/user.spec.ts index a6ebec0..c4511ec 100644 --- a/src/routes/user.spec.ts +++ b/src/routes/user.spec.ts @@ -175,7 +175,7 @@ describe('/user/passreset', () => { }); }); it('changes the user password', done => { - UserModel.find({name: 'janedoe'}).lean().exec( 'find', (err, data) => { + UserModel.find({name: 'janedoe'}).lean().exec( 'find', (err, data: any) => { if (err) return done(err); const oldpass = data[0].pass; supertest(server) @@ -189,8 +189,9 @@ describe('/user/passreset', () => { .end((err, res) => { if (err) done(err); should(res.body).be.eql({status: 'OK'}); - UserModel.find({name: 'janedoe'}).lean().exec( 'find', (err, data) => { + UserModel.find({name: 'janedoe'}).lean().exec( (err, data: any) => { if (err) return done(err); + console.log(data); should(data[0].pass).not.eql(oldpass); done(); }); diff --git a/src/routes/user.ts b/src/routes/user.ts index e4a17d5..cd67d14 100644 --- a/src/routes/user.ts +++ b/src/routes/user.ts @@ -42,13 +42,13 @@ router.post('/user/new', (req, res, next) => { router.post('/user/passreset', (req, res, next) => { // check if user/email combo exists - UserModel.find({name: req.body.name, email: req.body.email}).lean().exec( 'find', (err, data) => { + UserModel.find({name: req.body.name, email: req.body.email}).lean().exec( (err, data: any) => { if (err) next(err); if (data.length === 1) { // it exists const newPass = Math.random().toString(36).substring(2); bcrypt.hash(newPass, 10, (err, hash) => { // password hashing if (err) next(err); - UserModel.findOneAndUpdate({name: req.body.name, email: req.body.email}, {pass: hash}, err => { + UserModel.findByIdAndUpdate(data[0]._id, {pass: hash}, err => { // write new password if (err) next(err); mail(data[0].email, 'Your new password for the DFOP database', 'Hi,

You requested to reset your password.
Your new password is:

' + newPass + '

If you did not request a password reset, talk to the sysadmin quickly!

Have a nice day.

The DFOP team', err => { if (err) next(err); diff --git a/src/test/db.json b/src/test/db.json index 7e32395..af2d78f 100644 --- a/src/test/db.json +++ b/src/test/db.json @@ -2,7 +2,7 @@ "collections": { "users": [ { - "_id": "5ea0450ed851c30a90e70894", + "_id": {"$oid":"5ea0450ed851c30a90e70894"}, "email": "jane.doe@bosch.com", "name": "janedoe", "pass": "$2a$10$i872o3qR5V3JnbDArD8Z.eDo.BNPDBaR7dUX9KSEtl9pUjLyucy2K", @@ -13,7 +13,7 @@ "__v": 0 }, { - "_id": "5ea131671feb9c2ee0aafc9b", + "_id": {"$oid":"5ea131671feb9c2ee0aafc9b"}, "email": "a.d.m.i.n@bosch.com", "name": "admin", "pass": "$2a$10$i872o3qR5V3JnbDArD8Z.eDo.BNPDBaR7dUX9KSEtl9pUjLyucy2K",