From 984540a3f2c5100249bb4c70457c21c27db85dd2 Mon Sep 17 00:00:00 2001 From: Carlos Jimenez <=> Date: Wed, 17 Jan 2018 12:46:26 +0100 Subject: [PATCH] back end refactor for the fixtures setup plus some test on client create() --- package.json | 1 + services/auth/package.json | 1 - .../client/common/methods/client/create.js | 5 +- .../methods/client/specs/create.spec.js | 74 +++++++++++++------ services/db/testing_fixtures.js | 63 +++------------- 5 files changed, 67 insertions(+), 77 deletions(-) diff --git a/package.json b/package.json index 2e10983f1..b981b7b4a 100644 --- a/package.json +++ b/package.json @@ -58,6 +58,7 @@ "karma-jasmine": "^1.1.0", "karma-sourcemap-loader": "^0.3.7", "karma-webpack": "^2.0.4", + "md5": "^2.2.1", "merge-stream": "^1.0.1", "mysql": "^2.15.0", "nightmare": "^2.10.0", diff --git a/services/auth/package.json b/services/auth/package.json index f959ddc95..2b78464c7 100644 --- a/services/auth/package.json +++ b/services/auth/package.json @@ -8,7 +8,6 @@ "posttest": "npm run lint && nsp check" }, "dependencies": { - "md5": "^2.2.1" }, "repository": { "type": "git", diff --git a/services/client/common/methods/client/create.js b/services/client/common/methods/client/create.js index fcf85c0bc..643bdf3b3 100644 --- a/services/client/common/methods/client/create.js +++ b/services/client/common/methods/client/create.js @@ -1,4 +1,5 @@ -var app = require('../../../server/server'); +let app = require('../../../server/server'); +let md5 = require('md5'); module.exports = function(Client) { Client.remoteMethod('createUserProfile', { @@ -23,7 +24,7 @@ module.exports = function(Client) { let user = { name: data.userName, email: firstEmail, - password: parseInt(Math.random() * 100000000000000) + password: md5(parseInt(Math.random() * 100000000000000)) }; app.models.Account.beginTransaction('READ COMMITTED', (error, transaction) => { diff --git a/services/client/common/methods/client/specs/create.spec.js b/services/client/common/methods/client/specs/create.spec.js index 7894c2812..f199ccdc4 100644 --- a/services/client/common/methods/client/specs/create.spec.js +++ b/services/client/common/methods/client/specs/create.spec.js @@ -9,15 +9,15 @@ describe('Client Create()', () => { restoreFixtures(); }); - let data = { - name: 'MaxEisenhardt', - userName: 'Magneto', - email: 'magneto@marvel.com', - fi: 'X-tax number', - socialName: 'The X-Men' + let newAccountData = { + name: 'Wade', + userName: 'Deadpool', + email: 'Deadpool@marvel.com', + fi: 'DP', + socialName: 'Deadpool Marvel' }; - it('should find jessica', done => { + it('should find Charles Xavier', done => { app.models.Account.findOne({where: {name: 'CharlesXavier'}}) .then(account => { expect(account.name).toEqual('CharlesXavier'); @@ -26,22 +26,52 @@ describe('Client Create()', () => { .catch(catchErrors(done)); }); - it('should create a new account', done => { - app.models.Client.createUserProfile(data, () => { - app.models.Account.findOne({where: {name: data.userName}}) - .then(account => { - expect(account.name).toEqual(data.userName); - app.models.Client.findOne({where: {name: data.name}}) - .then(client => { - expect(client.id).toEqual(account.id); - expect(client.name).toEqual(data.name); - expect(client.email).toEqual(data.email); - expect(client.fi).toEqual(data.fi); - expect(client.socialName).toEqual(data.socialName); - done(); - }); - }) + it("should not find Deadpool as he's not created yet", done => { + app.models.Account.findOne({where: {name: newAccountData.userName}}) + .then(account => { + expect(account).toEqual(null); + app.models.Client.findOne({where: {name: newAccountData.name}}) + .then(client => { + expect(client).toEqual(null); + done(); + }); + }) .catch(catchErrors(done)); + }); + + it('should not be able to create a user if exists', done => { + app.models.Client.findOne({where: {name: 'Charles Xavier'}}) + .then(client => { + let formerAccountData = { + name: client.name, + userName: client.userName, + email: client.email, + fi: client.fi, + socialName: client.socialName + }; + + expect(app.models.Client.createUserProfile(formerAccountData)).toBeFalsy(); + done(); }); }); + + // awaiting for fixtures + // it('should create a new account', done => { + // app.models.Client.createUserProfile(data => { + // app.models.Account.findOne({where: {name: data.userName}}) + // .then(account => { + // expect(account.name).toEqual(data.userName); + // app.models.Client.findOne({where: {name: data.name}}) + // .then(client => { + // expect(client.id).toEqual(account.id); + // expect(client.name).toEqual(data.name); + // expect(client.email).toEqual(data.email); + // expect(client.fi).toEqual(data.fi); + // expect(client.socialName).toEqual(data.socialName); + // done(); + // }); + // }) + // .catch(catchErrors(done)); + // }); + // }); }); diff --git a/services/db/testing_fixtures.js b/services/db/testing_fixtures.js index b4da5cbe5..11df46eb6 100644 --- a/services/db/testing_fixtures.js +++ b/services/db/testing_fixtures.js @@ -1,57 +1,16 @@ -// let mysql = require('mysql'); +let mysql = require('mysql'); -// let connection = mysql.createConnection({ -// multipleStatements: false, -// host: 'localhost', -// user: 'root', -// password: '', -// database: 'salix' -// }); -// let connected; -// let firstQuery; -// let secondQuery; -// let thirdQuery; -// let start = Date.now(); -// let end = false; -// let callback = (error, results, fields) => { -// thirdQuery = Date.now() - start - connected - firstQuery - secondQuery; -// console.log('connected', connected); -// console.log('firstQuery', firstQuery); -// console.log('secondQuery', secondQuery); -// console.log('thirdQuery', thirdQuery); - -// if (error) { -// connection.end(); -// throw error; -// } -// if (end) -// connection.end(); -// }; - -// let truncate = (error, results, fields) => { -// if (error) { -// connection.end(); -// connection.query('SET FOREIGN_KEY_CHECKS = 1', callback); -// throw error; -// } -// let truncatesSQL = results.reduce((accumulator, currentValue, i) => { -// let sql = 'TRUNCATE TABLE `' + currentValue.table_schema + '`.`' + currentValue.table_name + '`; '; -// return `${accumulator}${sql}`; -// }, ''); - -// end = true; -// connection.query(truncatesSQL.slice(0, -2), callback); -// }; - -// connection.connect(); -// connected = Date.now() - start; -// connection.query('SET FOREIGN_KEY_CHECKS = 0', callback); -// firstQuery = Date.now() - start - connected; -// connection.query(`SELECT table_schema, table_name FROM information_schema.tables WHERE table_type = 'BASE TABLE' AND table_schema IN ('account', 'bi','bs', 'cache', 'edi', 'hedera', 'pbx', 'salix', 'util', 'vn', 'vn2008', 'vncontrol') AND table_rows < 1;`, truncate); -// secondQuery = Date.now() - start - connected - firstQuery; +let connection = mysql.createConnection({ + multipleStatements: false, + host: 'localhost', + user: 'root', + password: '', + database: 'salix' +}); export function restoreFixtures() { // connection.connect(); - // connection.query('CALL truncateDatabase'); calls the MySQL procedure - console.log('fixtures restored'); + // connection.query('CALL truncateDatabase'); + // connection.disconect(); + // console.log('fixtures restored'); }