diff --git a/.eslintignore b/.eslintignore index 89059b1..9c8d714 100755 --- a/.eslintignore +++ b/.eslintignore @@ -1,8 +1,7 @@ -/client/ -/coverage/ -/node_modules/ -server/dropdb.js -/lib/expression-language/expression-syntax-parser.js -/test/ -/drop.js - +build/ +client/ +coverage/ +node_modules/ +test/ +drop.js +Gruntfile.js diff --git a/.eslintrc b/.eslintrc index 8f2f7cc..6edc78f 100755 --- a/.eslintrc +++ b/.eslintrc @@ -111,7 +111,7 @@ "no-undefined": 1, // http://eslint.org/docs/rules/no-undefined "no-with": 2, // http://eslint.org/docs/rules/no-with "handle-callback-err": 1, // http://eslint.org/docs/rules/handle-callback-err - "radix": 2, // http://eslint.org/docs/rules/radix + "radix": 0, // http://eslint.org/docs/rules/radix "wrap-iife": [2, "any"], // http://eslint.org/docs/rules/wrap-iife "yoda": 2, // http://eslint.org/docs/rules/yoda diff --git a/.gitignore b/.gitignore index 908522f..4631a8a 100755 --- a/.gitignore +++ b/.gitignore @@ -41,3 +41,4 @@ out/ *.zip /common/models/test package-lock.json +oracle-user.sh diff --git a/Gruntfile.js b/Gruntfile.js index 8e2271f..4cec46e 100755 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -10,38 +10,6 @@ module.exports = function GruntConfig(grunt) { grunt.initConfig({ pkg: grunt.file.readJSON('package.json'), - mkdir: { - all: { - options: { - create: ['dist'] - } - } - }, - - copy: { - main: { - files: [ - // includes files within path and its sub-directories - { - expand: true, - src: ['**', '!node_modules/**', '!coverage/**'], - dest: 'dist/' - } - ] - } - }, - - mochaTest: { - test: { - options: { - quiet: false, - clearRequireCache: true, - timeout: 100000 - }, - src: ['test/test.js'] - } - }, - clean: { coverage: { src: ['coverage/'] @@ -72,12 +40,8 @@ module.exports = function GruntConfig(grunt) { }); // Add the grunt-mocha-test tasks. - grunt.loadNpmTasks('grunt-mocha-test'); - grunt.loadNpmTasks('grunt-mocha-istanbul'); grunt.loadNpmTasks('grunt-contrib-clean'); - - grunt.loadNpmTasks('grunt-mkdir'); - grunt.loadNpmTasks('grunt-contrib-copy'); + grunt.loadNpmTasks('grunt-mocha-istanbul'); grunt.registerTask('test-with-coverage', ['clean:coverage', 'mocha_istanbul']); }; diff --git a/package.json b/package.json index f2eb712..a181360 100755 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "oe-datasource-personalization", - "version": "2.1.0", + "version": "2.2.0", "description": "oe-cloud modularization project aka oecloud.io", "engines": { "node": ">=6" @@ -19,27 +19,22 @@ "devDependencies": { "babel-eslint": "7.2.3", "chai": "3.4.1", - "chai-datetime": "1.4.0", "chai-things": "0.2.0", "chalk": "1.1.1", "eslint": "4.10.0", "grunt": "1.0.4", - "grunt-banner": "0.6.0", "grunt-cli": "1.3.2", "grunt-contrib-clean": "2.0.0", - "grunt-contrib-copy": "1.0.0", - "grunt-jsbeautifier": "0.2.13", - "grunt-mkdir": "1.0.0", "grunt-mocha-istanbul": "5.0.2", - "grunt-mocha-test": "0.13.3", "istanbul": "0.4.5", "mocha": "5.2.0", - "superagent-defaults": "0.1.14", - "supertest": "3.4.2", - "loopback-connector-mongodb": "3.9.2", "oe-cloud": "^2.0.0", + "oe-connector-mongodb": "^2.0.0", + "oe-connector-oracle": "^2.0.0", "oe-connector-postgresql": "^2.0.0", - "oe-multi-tenancy": "^2.0.0" + "oe-multi-tenancy": "^2.0.0", + "superagent-defaults": "0.1.14", + "supertest": "3.4.2" }, "repository": { "type": "git", diff --git a/test/app-list.json b/test/app-list.json index 8416c3c..c0d08a0 100755 --- a/test/app-list.json +++ b/test/app-list.json @@ -5,6 +5,7 @@ }, { "path": "oe-multi-tenancy", + "autoEnableMixins" : true, "enabled": true }, { diff --git a/test/datasources.json b/test/datasources.json index 0b3d9f2..af8b366 100755 --- a/test/datasources.json +++ b/test/datasources.json @@ -7,16 +7,13 @@ "name": "transient", "connector": "transient" }, - "db": { "host": "localhost", "port": 27017, "url": "mongodb://localhost:27017/oe-datasource-personalization-test", "database": "oe-datasource-personalization-test", - "password": "admin", "name": "db", - "connector": "mongodb", - "user": "admin", + "connector": "oe-connector-mongodb", "connectionTimeout": 500000, "connectTimeoutMS": 500000, "socketTimeoutMS": 500000 @@ -27,10 +24,8 @@ "port": 27017, "url": "mongodb://localhost:27017/oe-datasource-appdb", "database": "oe-datasource-appdb", - "password": "admin", "name": "appdb", - "connector": "mongodb", - "user": "admin", + "connector": "oe-connector-mongodb", "connectionTimeout": 500000, "connectTimeoutMS": 500000, "socketTimeoutMS": 500000 diff --git a/test/datasources.mongo.js b/test/datasources.mongo.js index 4d222b0..866178b 100755 --- a/test/datasources.mongo.js +++ b/test/datasources.mongo.js @@ -5,44 +5,36 @@ * */ var mongoHost = process.env.MONGO_HOST || 'localhost'; +var mongoPort = process.env.MONGO_PORT ? parseInt(process.env.MONGO_PORT) : 27017; var dbName = process.env.DB_NAME || 'oe-datasource-personalization-test'; -if (process.env.DB_NAME) { - dbName2 = process.env.DB_NAME + '-appdb'; -} -else - dbName2 = 'oe-datasource-appdb'; +var dbName2 = process.env.DB_NAME ? process.env.DB_NAME + '-appdb' : 'oe-datasource-appdb'; -module.exports = -{ - "memdb": { - "name": "memdb", - "connector": "memory" +module.exports = { + 'memdb': { + 'name': 'memdb', + 'connector': 'memory' }, - "transient": { - "name": "transient", - "connector": "transient" + 'transient': { + 'name': 'transient', + 'connector': 'transient' }, - "db": { - "host": mongoHost, - "port": 27017, - "url": "mongodb://" + mongoHost + ":27017/" + dbName, - "database": dbName, - "password": "admin", - "name": "db", - "connector": "mongodb", - "user": "admin", - "connectionTimeout": 500000 + 'db': { + 'host': mongoHost, + 'port': mongoPort, + 'url': 'mongodb://' + mongoHost + ':' + mongoPort + '/' + dbName, + 'database': dbName, + 'name': 'db', + 'connector': 'oe-connector-mongodb', + 'connectionTimeout': 500000 }, - "appdb": { - "host": mongoHost, - "port": 27017, - "url": "mongodb://" + mongoHost + ":27017/" + dbName2, - "database": dbName2, - "password": "admin", - "name": "appdb", - "connector": "mongodb", - "user": "admin", - "connectionTimeout": 500000 + 'appdb': { + 'host': mongoHost, + 'port': mongoPort, + 'url': 'mongodb://' + mongoHost + ':' + mongoPort + '/' + dbName2, + 'database': dbName2, + 'name': 'appdb', + 'connector': 'oe-connector-mongodb', + 'connectionTimeout': 500000 } }; diff --git a/test/datasources.oracle.js b/test/datasources.oracle.js index 572e7f8..c8fd663 100755 --- a/test/datasources.oracle.js +++ b/test/datasources.oracle.js @@ -4,16 +4,10 @@ * Bangalore, India. All Rights Reserved. * */ -/** - ** - ** ©2016-2017 EdgeVerve Systems Limited (a fully owned Infosys subsidiary), - ** Bangalore, India. All Rights Reserved. - ** - **/ -var oracleSID = process.env.ORACLE_SID || 'orclpdb.ad.infosys.com'; +var oracleSID = process.env.ORACLE_SID || 'ORCLCDB'; var oracleHost = process.env.ORACLE_HOST || 'localhost'; -var oraclePort = process.env.ORACLE_PORT || 1521; +var oraclePort = process.env.ORACLE_PORT ? parseInt(process.env.ORACLE_PORT) : 1521; var oracleUserName = process.env.ORACLE_USERNAME || 'oeadmin'; var oracleUserPassword = process.env.ORACLE_PASSWORD || 'oeadmin'; diff --git a/test/datasources.postgres.js b/test/datasources.postgres.js index ea6f9f6..73ea75b 100755 --- a/test/datasources.postgres.js +++ b/test/datasources.postgres.js @@ -5,49 +5,43 @@ * */ var postgresHost = process.env.POSTGRES_HOST || 'localhost'; +var postgresPort = process.env.POSTGRES_PORT ? parseInt(process.env.POSTGRES_PORT) : 5432; var dbName = process.env.DB_NAME || 'oe-datasource-personalization-test'; -var dbName2; -if (process.env.DB_NAME) { - dbName2 = process.env.DB_NAME + '-appdb'; -} -else - dbName2 = 'oe-datasource-appdb'; +var dbName2 = process.env.DB_NAME ? process.env.DB_NAME + '-appdb' : 'oe-datasource-appdb'; -module.exports = -{ - "memdb": { - "name": "memdb", - "connector": "memory" +module.exports = { + 'memdb': { + 'name': 'memdb', + 'connector': 'memory' }, - "transient": { - "name": "transient", - "connector": "transient" + 'transient': { + 'name': 'transient', + 'connector': 'transient' }, - - "db": { - "host": postgresHost, - "port": 5432, - "url": "postgres://postgres:postgres@" + postgresHost + ":5432/" + dbName, - "database": dbName, - "password": "postgres", - "enableDbCreation" : true, - "name": "db", - "connector": "oe-connector-postgresql", - "user": "postgres", - "connectionTimeout": 50000 + 'db': { + 'host': postgresHost, + 'port': 5432, + 'url': 'postgres://postgres:postgres@' + postgresHost + ':5432/' + dbName, + 'database': dbName, + 'password': 'postgres', + 'enableDbCreation': true, + 'name': 'db', + 'connector': 'oe-connector-postgresql', + 'user': 'postgres', + 'connectionTimeout': 50000 }, - "appdb": { - "host": postgresHost, - "port": 5432, - "url": "postgres://postgres:postgres@" + postgresHost + ":5432/" + dbName2, - "database": dbName2, - "password": "postgres", - "enableDbCreation": true, - "name": "appdb", - "connector": "oe-connector-postgresql", - "user": "postgres", - "connectionTimeout": 50000 + 'appdb': { + 'host': postgresHost, + 'port': 5432, + 'url': 'postgres://postgres:postgres@' + postgresHost + ':5432/' + dbName2, + 'database': dbName2, + 'password': 'postgres', + 'enableDbCreation': true, + 'name': 'appdb', + 'connector': 'oe-connector-postgresql', + 'user': 'postgres', + 'connectionTimeout': 50000 } }; diff --git a/test/oracle-utility.js b/test/oracle-utility.js index 48a22b7..2153302 100755 --- a/test/oracle-utility.js +++ b/test/oracle-utility.js @@ -15,23 +15,42 @@ // ORACLE_SYSUSER // ORACLE_SYSPASSWORD // ORACLE_SID -// CI_PROJECT_NAME // CI_PROJECT_NAMESPACE +// CI_PROJECT_NAME var oracledb = require('oracledb'); var async = require('async'); +var fs = require('fs'); +var os = require('os'); + var oracleHost = process.env.ORACLE_HOST || 'localhost'; -var oraclePort = process.env.ORACLE_PORT || 1521; +var oraclePort = process.env.ORACLE_PORT ? parseInt(process.env.ORACLE_PORT) : 1521; +var oracleSID = process.env.ORACLE_SID || 'ORCLCDB'; var oracleConnectSettings = { 'password': process.env.ORACLE_SYSPASSWORD || 'manager1', 'user': process.env.ORACLE_SYSUSER || 'sys', - 'connectString': oracleHost + ':' + oraclePort + '/' + (process.env.ORACLE_SID || 'orclpdb.ad.infosys.com') + 'connectString': oracleHost + ':' + oraclePort + '/' + oracleSID +}; + +var namespace = process.env.CI_PROJECT_NAMESPACE ? process.env.CI_PROJECT_NAMESPACE.replace(/[^a-zA-Z0-9]/g, '') : 'oecloudio'; +var name = process.env.CI_PROJECT_NAME ? process.env.CI_PROJECT_NAME.replace(/[^a-zA-Z0-9]/g, '') : 'oecloud'; + +var userName = namespace.toUpperCase() + '_' + name.toUpperCase(); +var userName2 = userName + '-ICICI'; +var password = namespace.toLowerCase(); + +var oracleUserConnectSettings = { + 'password': password, + 'user': userName, + 'connectString': oracleHost + ':' + oraclePort + '/' + oracleSID }; -var userName = process.env.CI_PROJECT_NAMESPACE.toUpperCase() + '-' + (process.env.CI_PROJECT_NAME || 'oecloud').toUpperCase(); -var userName2 = process.env.CI_PROJECT_NAMESPACE.toUpperCase() + '-' + (process.env.CI_PROJECT_NAME || 'oecloud').toUpperCase() + '-ICICI'; -var password = process.env.CI_PROJECT_NAMESPACE.toLowerCase(); +var oracleUser2ConnectSettings = { + 'password': password, + 'user': userName2, + 'connectString': oracleHost + ':' + oraclePort + '/' + oracleSID +}; var grants = [ 'CREATE VIEW', @@ -44,217 +63,136 @@ var grants = [ 'CREATE PROCEDURE' ]; -function createUser2(connection, cb) { - var sql = 'alter session set "_ORACLE_SCRIPT"=true'; - connection.execute(sql, function (e, r) { - if (e) { - console.error('Ignoring error of alter session. UserName : ' + userName2 + ' Error :' + e); +function createUser(connection, oracleUser, oraclePassword, cb) { + var alterSQL = 'alter session set "_ORACLE_SCRIPT"=true'; + connection.execute(alterSQL, function (alterErr, alterRes) { + if (alterErr) { + console.error('Ignoring error of alter session. UserName : ' + oracleUser + ' Error :' + alterErr); } - console.log(sql, ' ......... ok'); - var sql = 'CREATE USER "' + userName2 + '" IDENTIFIED BY ' + password; + console.log(alterSQL, ' ......... ok'); - connection.execute(sql, function (err, result) { - if (err) { - throw new Error('Unable to create user ' + userName2 + ' Error :' + err); + var createUserSQL = 'CREATE USER "' + oracleUser + '" IDENTIFIED BY ' + oraclePassword; + connection.execute(createUserSQL, function (createErr, createRes) { + if (createErr) { + console.error(createErr); + throw new Error('Unable to create user ' + oracleUser); } - console.log(sql, ' ......... ok'); - async.each(grants, function (g, callback) { - var sql = 'GRANT ' + g + ' to "' + userName2 + '"'; + console.log(createUserSQL, ' ......... ok'); - connection.execute(sql, function (err2, result2) { - if (err2) { - throw new Error('Unable to execute grant ' + sql); + async.each(grants, function (g, callback) { + var grantSQL = 'GRANT ' + g + ' to "' + oracleUser + '"'; + connection.execute(grantSQL, function (grantErr, grantRes) { + if (grantErr) { + console.error(grantErr); + throw new Error('Unable to execute grant ' + grantSQL); } - console.log(sql, ' ......... ok'); + console.log(grantSQL, ' ......... ok'); return callback(); }); - }, function (err) { - console.log('User ' + userName2 + ' Created successfully'); + }, function (grantAsyncErr) { + console.log('User ' + oracleUser + ' Created successfully'); return cb(); }); }); }); } -function createUser(connection, cb) { - var sql = 'alter session set "_ORACLE_SCRIPT"=true'; - connection.execute(sql, function (e, r) { - if (e) { - console.error('Ignoring error of alter session. UserName : ' + userName + ' Error :' + e); + +function dropTables(oracleUserConnectSettings, cb) { + oracledb.getConnection(oracleUserConnectSettings, function (userConnectionErr, connection) { + if (userConnectionErr) { + console.error(userConnectionErr); + throw new Error('Unable to connect to Oracle Database ' + JSON.stringify(oracleUserConnectSettings)); } - console.log(sql, ' ......... ok'); - var sql = 'CREATE USER "' + userName + '" IDENTIFIED BY ' + password; - connection.execute(sql, function (err, result) { - if (err) { - throw new Error('Unable to create user ' + userName + ' Error :' + err); + var totalRows = 1000; + var selectDropTableSQL = "select 'drop table \"' || table_name || '\"' from all_tables where owner = '" + userName + "'"; + connection.execute(selectDropTableSQL, {}, { maxRows: totalRows }, function (selectDropErr, selectDropRes) { + if (selectDropErr) { + console.error(selectDropErr); + throw new Error('Unable to find tables ' + userName); } - console.log(sql, ' ......... ok'); - async.each(grants, function (g, callback) { - var sql = 'GRANT ' + g + ' to "' + userName + '"'; - connection.execute(sql, function (err2, result2) { - if (err2) { - throw new Error('Unable to execute grant ' + sql); + if (!selectDropRes || !selectDropRes.rows || selectDropRes.rows.length === 0) { + return cb(); + } + + async.each(selectDropRes.rows, function (row, callback) { + var dropTableSQL = row[0]; + connection.execute(dropTableSQL, function (dropTableErr, dropTableRes) { + if (dropTableErr) { + console.error(dropTableErr); + throw new Error('Unable to drop table\nSQL: ' + sql); } - console.log(sql, ' ......... ok'); + console.log(dropTableSQL, ' ......... ok'); return callback(); }); - }, function (err) { - console.log('User ' + userName + ' Created successfully'); + }, function (dropAsyncErr) { + console.log('Tables of user ' + userName + ' dropped successfully'); return cb(); }); }); }); } -function dropTables2(cb) { - var oracleConnectSettings2 = Object.assign({}, oracleConnectSettings); - oracleConnectSettings2.user = userName2; - oracleConnectSettings2.password = password; - oracledb.getConnection( - oracleConnectSettings2, - function (err, connection) { - if (err) { - console.log(err); - throw new Error(err + ' Unable to connect to Oracle Database ' + JSON.stringify(oracleConnectSettings2)); - } - var sql = "select 'drop table \"' || table_name || '\"' from all_tables where owner = '" + userName2 + "'"; - var totalRows = 1000; - - connection.execute(sql, {}, { maxRows: totalRows }, function (err, result) { - if (err) { - throw new Error('Unable to find tables ' + userName2 + ' Error :' + err); - } - connection.execute(sql, {}, { maxRows: totalRows }, function (err2, result2) { - if (err2) { - throw new Error('Unable to execute droping of table ' + sql); - } - if (!result2 || !result2.rows || result2.rows.length === 0) { - return cb(); - } - async.each(result2.rows, function (row, callback) { - var sql = row[0]; - connection.execute(sql, function (err2, result2) { - if (err2) { - throw new Error('Unable to drop table\nERROR : ' + err2 + '\nSQL : ' + sql); - } - console.log(sql, ' ......... ok'); - return callback(); - }); - }, function (err) { - console.log('Tables of user ' + userName2 + ' dropped successfully'); - return cb(); - }); - }); - }); - }); +function generateUserBundle(user, password) { + console.log("Generating oracle user details shell script"); + fs.writeFileSync('./oracle-user.sh', '#!/bin/sh' + os.EOL); + fs.appendFileSync('./oracle-user.sh', 'export ORACLE_USERNAME=' + user + os.EOL); + fs.appendFileSync('./oracle-user.sh', 'export ORACLE_PASSWORD=' + password + os.EOL); } -function dropTables(cb) { - var oracleConnectSettings2 = Object.assign({}, oracleConnectSettings); - oracleConnectSettings2.user = userName; - oracleConnectSettings2.password = password; - - oracledb.getConnection( - oracleConnectSettings2, - function (err, connection) { +function util(oracleUserConnectSettings, isGenerate, cb) { + var userName = oracleUserConnectSettings.user; + var password = oracleUserConnectSettings.password; + oracledb.getConnection(oracleConnectSettings, function (connectionErr, connection) { + if (connectionErr) { + console.error(connectionErr); + throw new Error('Unable to connect to Oracle Database ' + JSON.stringify(oracleConnectSettings)); + } + var sql = "select username, user_id from dba_users where username = '" + userName + "'"; + console.log(sql); + connection.execute(sql, function (err, result) { if (err) { - throw new Error('Unable to connect to Oracle Database ' + JSON.stringify(oracleConnectSettings)); + console.error(err); return; } - var sql = "select 'drop table \"' || table_name || '\"' from all_tables where owner = '" + userName + "'"; - var totalRows = 1000; - - connection.execute(sql, {}, {maxRows: totalRows}, function (err, result) { - if (err) { - throw new Error('Unable to find tables ' + userName + ' Error :' + err); - } - connection.execute(sql, {}, {maxRows: totalRows}, function (err2, result2) { - if (err2) { - throw new Error('Unable to execute droping of table ' + sql); - } - if (!result2 || !result2.rows || result2.rows.length === 0) { - return cb(); + if (!result.rows || result.rows.length == 0) { + createUser(connection, userName, password, function (err) { + if (err) { + // return process.exit(1); + cb(err); } - async.each(result2.rows, function (row, callback) { - var sql = row[0]; - connection.execute(sql, function (err2, result2) { - if (err2) { - throw new Error('Unable to drop table\nERROR : ' + err2 + '\nSQL : ' + sql); - } - console.log(sql, ' ......... ok'); - return callback(); - }); - }, function (err) { - console.log('Tables of user ' + userName + ' dropped successfully'); - return cb(); - }); + if (isGenerate) generateUserBundle(userName, password); + cb(); + // return process.exit(); }); - }); - }); -} - - -function createAnotherSchema(){ - oracledb.getConnection( - oracleConnectSettings, - function (err, connection) { - if (err) { - throw new Error('Unable to connect to Oracle Database ' + JSON.stringify(oracleConnectSettings)); - } - var sql = "select username, user_id from dba_users where username = '" + userName2 + "'"; - console.log(sql); - connection.execute(sql, - function (err, result) { + } else { + dropTables(oracleUserConnectSettings, function (err) { if (err) { - console.error(err); return; - } - if (!result.rows || result.rows.length == 0) { - createUser2(connection, function (err) { - if (err) { - return process.exit(1); - } - return process.exit(); - }); - } else { - dropTables2(function (err) { - if (err) { - return process.exit(1); - } - return process.exit(); - }); + cb(err); + // return process.exit(1); } + if (isGenerate) generateUserBundle(userName, password); + cb(); + // return process.exit(); }); + } }); -} - -oracledb.getConnection( - oracleConnectSettings, - function (err, connection) { - if (err) { - throw new Error('Unable to connect to Oracle Database ' + JSON.stringify(oracleConnectSettings)); - } - var sql = "select username, user_id from dba_users where username = '" + userName + "'"; - console.log(sql); - connection.execute(sql, - function (err, result) { - if (err) { - console.error(err); return; - } - if (!result.rows || result.rows.length == 0) { - createUser(connection, function (err) { - if (err) { - return process.exit(1); - } - return createAnotherSchema(); //process.exit(); - }); - } else { - dropTables(function (err) { - if (err) { - return process.exit(1); - } - return createAnotherSchema(); //return process.exit(); - }); - } - }); }); +} +async.parallel([function (cb) { + try { + util(oracleUserConnectSettings, true, cb); + } catch (err) { + cb(err); + } +}, function (cb) { + try { + util(oracleUser2ConnectSettings, false, cb); + } catch (err) { + cb(err); + } +}], function (err, res) { + if (err) process.exit(1); + process.exit(); +}) \ No newline at end of file diff --git a/test/server.js b/test/server.js index bc7930b..8a13cc0 100755 --- a/test/server.js +++ b/test/server.js @@ -1,9 +1,9 @@ var oecloud = require('oe-cloud'); -var loopback=require('loopback'); +var loopback = require('loopback'); oecloud.observe('loaded', function (ctx, next) { return next(); -}) +}); oecloud.boot(__dirname, function (err) { oecloud.start(); diff --git a/test/test.js b/test/test.js index 84b893c..fc59edf 100755 --- a/test/test.js +++ b/test/test.js @@ -1,6 +1,6 @@ /** * - * ©2018-2019 EdgeVerve Systems Limited (a fully owned Infosys subsidiary), + * �2018-2019 EdgeVerve Systems Limited (a fully owned Infosys subsidiary), * Bangalore, India. All Rights Reserved. * */ @@ -16,12 +16,12 @@ var loopback = require('loopback'); // 3. Demonstrate that when record of Model is being created using ICICI tenant, it uses different Data Source // 4. While Model record being created using tenant other than ICICI, it uses regular 'db' data source. oecloud.observe('loaded', function (ctx, next) { - //oecloud.attachMixinsToBaseEntity("DataSourcePersonalizationMixin"); - oecloud.setDataSourceDefinitionAutoscope(["tenantId"]); + // oecloud.attachMixinsToBaseEntity("DataSourcePersonalizationMixin"); + oecloud.setDataSourceDefinitionAutoscope(['tenantId']); return next(); -}) +}); oecloud.addContextField('tenantId', { - type: "string" + type: 'string' }); var Customer; @@ -32,25 +32,23 @@ oecloud.boot(__dirname, function (err) { process.exit(1); } var accessToken = loopback.findModel('AccessToken'); - accessToken.observe("before save", function (ctx, next) { - var userModel = loopback.findModel("User"); + accessToken.observe('before save', function (ctx, next) { + var userModel = loopback.findModel('User'); var instance = ctx.instance; userModel.find({ where: { id: instance.userId } }, {}, function (err, result) { - var ctx={}; + var ctx = {}; if (err) { return next(err); } if (result.length != 1) { - return next(new Error("No User Found")); + return next(new Error('No User Found')); } var user = result[0]; - if (user.username === "admin") { + if (user.username === 'admin') { ctx.tenantId = '/default'; - } - else if (user.username === "iciciuser") { + } else if (user.username === 'iciciuser') { ctx.tenantId = '/default/icici'; - } - else if (user.username === "citiuser") { + } else if (user.username === 'citiuser') { ctx.tenantId = '/default/citi'; } instance.ctx = ctx; @@ -62,8 +60,6 @@ oecloud.boot(__dirname, function (err) { }); - - var chalk = require('chalk'); var chai = require('chai'); var async = require('async'); @@ -78,7 +74,7 @@ var api = defaults(supertest(app)); var basePath = app.get('restApiRoot'); function deleteAllUsers(done) { - var userModel = loopback.findModel("User"); + var userModel = loopback.findModel('User'); userModel.destroyAll({}, {}, function (err) { return done(err); }); @@ -107,8 +103,8 @@ describe(chalk.blue('oe-datasource-personalization Started'), function (done) { this.timeout(10000); before('wait for boot scripts to complete', function (done) { app.on('test-start', function () { - Customer = loopback.findModel("Customer"); - DataSourceDefinition = loopback.findModel("DataSourceDefinition"); + Customer = loopback.findModel('Customer'); + DataSourceDefinition = loopback.findModel('DataSourceDefinition'); deleteAllUsers(function (err) { return done(err); }); @@ -123,12 +119,11 @@ describe(chalk.blue('oe-datasource-personalization Started'), function (done) { var url = basePath + '/users'; api.set('Accept', 'application/json') .post(url) - .send([{ username: "admin", password: "admin", email: "admin@admin.com" }, - { username: "iciciuser", password: "iciciuser", email: "iciciuser@iciciuser.com" }, - { username: "citiuser", password: "citiuser", email: "citiuser@citiuser.com" } + .send([{ username: 'admin', password: 'admin', email: 'admin@admin.com' }, + { username: 'iciciuser', password: 'iciciuser', email: 'iciciuser@iciciuser.com' }, + { username: 'citiuser', password: 'citiuser', email: 'citiuser@citiuser.com' } ]) - .end(function (err, response) { - + .end(function (err, response) { var result = response.body; expect(result[0].id).to.be.defined; expect(result[1].id).to.be.defined; @@ -142,7 +137,7 @@ describe(chalk.blue('oe-datasource-personalization Started'), function (done) { var url = basePath + '/users/login'; api.set('Accept', 'application/json') .post(url) - .send({ username: "admin", password: "admin" }) + .send({ username: 'admin', password: 'admin' }) .end(function (err, response) { var result = response.body; adminToken = result.id; @@ -156,7 +151,7 @@ describe(chalk.blue('oe-datasource-personalization Started'), function (done) { var url = basePath + '/users/login'; api.set('Accept', 'application/json') .post(url) - .send({ username: "iciciuser", password: "iciciuser" }) + .send({ username: 'iciciuser', password: 'iciciuser' }) .end(function (err, response) { var result = response.body; icicitoken = result.id; @@ -171,7 +166,7 @@ describe(chalk.blue('oe-datasource-personalization Started'), function (done) { var url = basePath + '/users/login'; api.set('Accept', 'application/json') .post(url) - .send({ username: "citiuser", password: "citiuser" }) + .send({ username: 'citiuser', password: 'citiuser' }) .end(function (err, response) { var result = response.body; cititoken = result.id; @@ -181,70 +176,70 @@ describe(chalk.blue('oe-datasource-personalization Started'), function (done) { }); - it("t5 - creating default record in Customer model", function (done) { - Customer.create({ name: "A", age: 10 }, defaultContext, function (err, r) { + it('t5 - creating default record in Customer model', function (done) { + Customer.create({ name: 'A', age: 10 }, defaultContext, function (err, r) { return done(err); }); }); - it("t6 - creating icici record in Customer model", function (done) { - Customer.create({ name: "Icici", age: 10 }, iciciCtx, function (err, r) { + it('t6 - creating icici record in Customer model', function (done) { + Customer.create({ name: 'Icici', age: 10 }, iciciCtx, function (err, r) { return done(err); }); }); - it("t7 - creating citi record in Customer model", function (done) { - Customer.create({ name: "citi", age: 10 }, citiCtx, function (err, r) { + it('t7 - creating citi record in Customer model', function (done) { + Customer.create({ name: 'citi', age: 10 }, citiCtx, function (err, r) { return done(err); }); }); - it("t8.1 - fetching records as default tenant", function (done) { + it('t8.1 - fetching records as default tenant', function (done) { api .set('Accept', 'application/json') .get(basePath + '/Customers?access_token=' + adminToken) .send() .expect(200).end(function (err, res) { - //console.log('response body : ' + JSON.stringify(res.body, null, 4)); + // console.log('response body : ' + JSON.stringify(res.body, null, 4)); if (err || res.body.error) { return done(err || (new Error(res.body.error))); } var results = res.body; expect(results.length).to.equal(1); - expect(results[0].name).to.equal("A"); + expect(results[0].name).to.equal('A'); done(); }); }); - it("t8.2 - fetching records as icici tenant", function (done) { + it('t8.2 - fetching records as icici tenant', function (done) { api .set('Accept', 'application/json') .get(basePath + '/Customers?access_token=' + icicitoken) .send() .expect(200).end(function (err, res) { - //console.log('response body : ' + JSON.stringify(res.body, null, 4)); + // console.log('response body : ' + JSON.stringify(res.body, null, 4)); if (err || res.body.error) { return done(err || (new Error(res.body.error))); } var results = res.body; expect(results.length).to.equal(1); - expect(results[0].name).to.equal("Icici"); + expect(results[0].name).to.equal('Icici'); done(); }); }); - it("t8.3 - fetching records as citi tenant", function (done) { + it('t8.3 - fetching records as citi tenant', function (done) { api .set('Accept', 'application/json') .get(basePath + '/Customers?access_token=' + cititoken) .send() .expect(200).end(function (err, res) { - //console.log('response body : ' + JSON.stringify(res.body, null, 4)); + // console.log('response body : ' + JSON.stringify(res.body, null, 4)); if (err || res.body.error) { return done(err || (new Error(res.body.error))); } var results = res.body; expect(results.length).to.equal(1); - expect(results[0].name).to.equal("citi"); + expect(results[0].name).to.equal('citi'); done(); }); }); @@ -255,8 +250,7 @@ describe(chalk.blue('oe-datasource-personalization Started'), function (done) { if (!currentDB) { datasourceFile = './datasources.json'; - } - else { + } else { datasourceFile = './datasources.' + currentDB + '.js'; } var temp = require(datasourceFile); @@ -264,7 +258,7 @@ describe(chalk.blue('oe-datasource-personalization Started'), function (done) { icicidb.id = icicidb.name + '-' + newName; icicidb.name = icicidb.name + '-' + newName; - + if (modelName) { icicidb.modelName = modelName; } @@ -276,92 +270,85 @@ describe(chalk.blue('oe-datasource-personalization Started'), function (done) { var y = temp[srcdb].url.split('/'); var len = y.length; var last = y[len - 1]; - //last = last + '-' + newName; + // last = last + '-' + newName; y[len - 1] = icicidb.database; icicidb.url = y.join('/'); - //newds.url = db2.db.url.replace('oe-cloud-test', 'oe-cloud-test-newdb'); + // newds.url = db2.db.url.replace('oe-cloud-test', 'oe-cloud-test-newdb'); } - } - else if (currentDB && currentDB.toLowerCase().indexOf('oracle') >= 0) { + } else if (currentDB && currentDB.toLowerCase().indexOf('oracle') >= 0) { icicidb.user = icicidb.user + '-' + newName; - } - else { + } else { icicidb.url = temp[srcdb].url.replace(temp[srcdb].database, temp[srcdb].database + '-' + newName); - icicidb.database = temp[srcdb].database + '-' + newName;; + icicidb.database = temp[srcdb].database + '-' + newName; } console.log(JSON.stringify(icicidb)); return icicidb; } - it("t9.1 - Create new DataSource for icici tenant", function (done) { - var icicidb = createNewDb("db", "icici", "Customer"); + it('t9.1 - Create new DataSource for icici tenant', function (done) { + var icicidb = createNewDb('db', 'icici', 'Customer'); DataSourceDefinition.create(icicidb, iciciCtx, function (err, r) { return done(err); }); }); - it("t9.2 Create record in Customer with icici tenant and it should go in new database for icici", function (done) { - Customer.create({ name: "IciciA", age: 10 }, iciciCtx, function (err, r) { + it('t9.2 Create record in Customer with icici tenant and it should go in new database for icici', function (done) { + Customer.create({ name: 'IciciA', age: 10 }, iciciCtx, function (err, r) { return done(err); }); }); - it("t9.3 Create record in Customer with citi tenant and it should go to default database", function (done) { - Customer.create({ name: "CitiA", age: 10 }, citiCtx, function (err, r) { + it('t9.3 Create record in Customer with citi tenant and it should go to default database', function (done) { + Customer.create({ name: 'CitiA', age: 10 }, citiCtx, function (err, r) { return done(err); }); }); - it("t9.4 Create new DataSource for citi tenant", function (done) { + it('t9.4 Create new DataSource for citi tenant', function (done) { if (process.env.NODE_ENV !== 'oracle') { - var citidb = createNewDb("db", "citidb", "Customer"); + var citidb = createNewDb('db', 'citidb', 'Customer'); DataSourceDefinition.create(citidb, citiCtx, function (err, r) { return done(err); }); - } - else { + } else { return done(); } }); - it("t9.5 Create record in Customer with citi tenant and it should go to citi specific database", function (done) { + it('t9.5 Create record in Customer with citi tenant and it should go to citi specific database', function (done) { if (process.env.NODE_ENV !== 'oracle') { - Customer.create({ name: "CitiA", age: 10 }, citiCtx, function (err, r) { + Customer.create({ name: 'CitiA', age: 10 }, citiCtx, function (err, r) { return done(err); }); - } - else { + } else { return done(); } }); - it("t9.6 fetching records as citi tenant (HTTP) - only record which went to personalized database should be retrieved", function (done) { + it('t9.6 fetching records as citi tenant (HTTP) - only record which went to personalized database should be retrieved', function (done) { if (process.env.NODE_ENV !== 'oracle') { api .set('Accept', 'application/json') .get(basePath + '/Customers?access_token=' + cititoken) .send() .expect(200).end(function (err, res) { - //console.log('response body : ' + JSON.stringify(res.body, null, 4)); + // console.log('response body : ' + JSON.stringify(res.body, null, 4)); if (err || res.body.error) { return done(err || (new Error(res.body.error))); } var results = res.body; expect(results.length).to.equal(1); - expect(results[0].name).to.equal("CitiA"); + expect(results[0].name).to.equal('CitiA'); done(); }); - } - else { + } else { return done(); } - }); - it("t9.7 - fetching records as icici tenant(HTTP) - only record which went to personalized database should be retrieved", function (done) { - + it('t9.7 - fetching records as icici tenant(HTTP) - only record which went to personalized database should be retrieved', function (done) { if (process.env.NODE_ENV === 'oracle') { return done(); } @@ -371,25 +358,24 @@ describe(chalk.blue('oe-datasource-personalization Started'), function (done) { .get(basePath + '/Customers?access_token=' + icicitoken) .send() .expect(200).end(function (err, res) { - //console.log('response body : ' + JSON.stringify(res.body, null, 4)); + // console.log('response body : ' + JSON.stringify(res.body, null, 4)); if (err || res.body.error) { return done(err || (new Error(res.body.error))); } var results = res.body; expect(results.length).to.equal(1); - expect(results[0].name).to.equal("IciciA"); + expect(results[0].name).to.equal('IciciA'); done(); }); }); it("t10.1 - Create employee record 'appdb' datasource by posting into Employee model as icici user", function (done) { - if (process.env.NODE_ENV === 'oracle') { return done(); } var data = { - name: "IciciEmployee", + name: 'IciciEmployee', age: 10 }; api @@ -410,7 +396,7 @@ describe(chalk.blue('oe-datasource-personalization Started'), function (done) { } var data = { - name: "CitiEmployee", + name: 'CitiEmployee', age: 10 }; api @@ -430,8 +416,8 @@ describe(chalk.blue('oe-datasource-personalization Started'), function (done) { return done(); } - var icicidb = createNewDb("appdb", "icicidb"); - icicidb.name = "appdb"; + var icicidb = createNewDb('appdb', 'icicidb'); + icicidb.name = 'appdb'; DataSourceDefinition.create(icicidb, iciciCtx, function (err, r) { return done(err); }); @@ -443,7 +429,7 @@ describe(chalk.blue('oe-datasource-personalization Started'), function (done) { var data = { - name: "IciciEmployee2", + name: 'IciciEmployee2', age: 10 }; api @@ -458,21 +444,21 @@ describe(chalk.blue('oe-datasource-personalization Started'), function (done) { }); }); - it("t11.2 - fetching Employee records as icici tenant - only record which went to personalized appdb database should be retrieved", function (done) { + it('t11.2 - fetching Employee records as icici tenant - only record which went to personalized appdb database should be retrieved', function (done) { if (process.env.NODE_ENV === 'oracle') { return done(); } - var employeeModel = loopback.findModel("Employee"); + var employeeModel = loopback.findModel('Employee'); employeeModel.find({}, iciciCtx, function (err, results) { expect(results.length).to.equal(1); - expect(results[0].name).to.equal("IciciEmployee2"); + expect(results[0].name).to.equal('IciciEmployee2'); done(); }); }); - it("t11.3 - fetching Employee records as icici tenant(HTTP) - only record which went to personalized appdb database should be retrieved", function (done) { + it('t11.3 - fetching Employee records as icici tenant(HTTP) - only record which went to personalized appdb database should be retrieved', function (done) { if (process.env.NODE_ENV === 'oracle') { return done(); } @@ -482,56 +468,54 @@ describe(chalk.blue('oe-datasource-personalization Started'), function (done) { .get(basePath + '/Employees?access_token=' + icicitoken) .send() .expect(200).end(function (err, res) { - //console.log('response body : ' + JSON.stringify(res.body, null, 4)); + // console.log('response body : ' + JSON.stringify(res.body, null, 4)); if (err || res.body.error) { return done(err || (new Error(res.body.error))); } var results = res.body; console.log(results); expect(results.length).to.equal(1); - expect(results[0].name).to.equal("IciciEmployee2"); + expect(results[0].name).to.equal('IciciEmployee2'); done(); }); }); - it("t12.1 - Test case to use all other methods", function (done) { + it('t12.1 - Test case to use all other methods', function (done) { if (process.env.NODE_ENV === 'oracle') { return done(); } - var employeeModel = loopback.findModel("Employee"); + var employeeModel = loopback.findModel('Employee'); employeeModel.find({}, iciciCtx, function (err, results) { expect(results.length).to.equal(1); - expect(results[0].name).to.equal("IciciEmployee2"); + expect(results[0].name).to.equal('IciciEmployee2'); var inst = results[0]; - var data = { age: 103 , id : inst.id}; + var data = { age: 103, id: inst.id}; inst.updateAttributes(data, iciciCtx, function (err, r) { if (err) { return done(err); } - employeeModel.upsert({ name: "Chagnedname" + r.name, age: 555 }, iciciCtx, function (err2, r2) { + employeeModel.upsert({ name: 'Chagnedname' + r.name, age: 555 }, iciciCtx, function (err2, r2) { return done(err2); }); }); }); }); - it("t12.2 - Test case to use all other methods", function (done) { + it('t12.2 - Test case to use all other methods', function (done) { if (process.env.NODE_ENV === 'oracle') { return done(); } - var employeeModel = loopback.findModel("Employee"); + var employeeModel = loopback.findModel('Employee'); employeeModel.findOrCreate({ - where: { id: "x" } - }, { id: "x", name: "xname", age: 44 }, iciciCtx, function (err, results) { + where: { id: 'x' } + }, { id: 'x', name: 'xname', age: 44 }, iciciCtx, function (err, results) { employeeModel.replaceById('x', { id: 'x', name: 'yname', age: 55 }, iciciCtx, function (e2, r2) { return done(e2); }); }); - }); }); -