From 2993c6e4f883d7d064d654130f81201a3ace5d76 Mon Sep 17 00:00:00 2001 From: David Baldwynn Date: Tue, 21 Nov 2017 07:57:15 -0800 Subject: [PATCH 01/17] removed reCAPTCHA token from setup script --- scripts/setup.js | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/scripts/setup.js b/scripts/setup.js index d8e5350c..1fb48f61 100644 --- a/scripts/setup.js +++ b/scripts/setup.js @@ -98,14 +98,12 @@ var questions = [ { type: 'confirm', name: 'SIGNUP_DISABLED', - message: 'Do you want to disable signups?', - default: false + message: 'Do you want to disable signups?' }, { - type: 'list', + type: 'confirm', name: 'SUBDOMAINS_DISABLED', - message: 'Do you want to have subdomains? (i.e. are you using a custom domain)', - choices: bool_options + message: 'Do you want to have subdomains? (i.e. are you using a custom domain)' }, { type: 'list', @@ -160,11 +158,6 @@ var questions = [ name: 'COVERALLS_REPO_TOKEN', message: 'What is your Coveralls.io token? (optional)' }, - { - type: 'input', - name: 'COVERALLS_REPO_TOKEN', - message: 'What is your reCAPTCHA token? (optional)' - }, { type: 'input', name: 'email', From 7107bab6147977c600a5768b69afc9aed6272c97 Mon Sep 17 00:00:00 2001 From: David Baldwynn Date: Tue, 21 Nov 2017 09:58:44 -0800 Subject: [PATCH 02/17] added support for custom smtp server in setup script --- app/libs/constants.js | 38 +++++ config/env/all.js | 2 +- config/express.js | 1 - package-lock.json | 5 + scripts/setup.js | 215 ++++++++++++++++++---------- selenium/.editorconfig | 17 +++ selenium/.gitignore | 9 ++ selenium/.vscode/launch.json | 27 ++++ selenium/README.md | 28 ++++ selenium/commons/commons.md | 1 + selenium/config.json | 14 ++ selenium/hosts | 0 selenium/install.sh | 4 + selenium/package.json | 25 ++++ selenium/run.bat | 7 + selenium/run.sh | 5 + selenium/screenshots/screenshots.md | 1 + selenium/uploadfiles/uploadfiles.md | 1 + 18 files changed, 321 insertions(+), 79 deletions(-) create mode 100644 selenium/.editorconfig create mode 100644 selenium/.gitignore create mode 100644 selenium/.vscode/launch.json create mode 100644 selenium/README.md create mode 100644 selenium/commons/commons.md create mode 100644 selenium/config.json create mode 100644 selenium/hosts create mode 100644 selenium/install.sh create mode 100644 selenium/package.json create mode 100644 selenium/run.bat create mode 100644 selenium/run.sh create mode 100644 selenium/screenshots/screenshots.md create mode 100644 selenium/uploadfiles/uploadfiles.md diff --git a/app/libs/constants.js b/app/libs/constants.js index 9b64918c..3771e0db 100644 --- a/app/libs/constants.js +++ b/app/libs/constants.js @@ -1,6 +1,44 @@ 'use strict'; module.exports = { + + + nodemailer_providers: [ + '1und1', + 'AOL', + 'DebugMail.io', + 'DynectEmail', + 'FastMail', + 'GandiMail', + 'Gmail', + 'Godaddy', + 'GodaddyAsia', + 'GodaddyEurope', + 'hot.ee', + 'Hotmail', + 'iCloud', + 'mail.ee', + 'Mail.ru', + 'Mailgun', + 'Mailjet', + 'Mandrill', + 'Naver', + 'OpenMailBox', + 'Postmark', + 'QQ', + 'QQex', + 'SendCloud', + 'SendGrid', + 'SES', + 'SES-US-EAST-1', + 'SES-US-WEST-1', + 'SES-EU-WEST-1', + 'Sparkpost', + 'Yahoo', + 'Yandex', + 'Zoho' + ], + extraneousFormFieldProps: [ 'validFieldTypes', 'disabled', diff --git a/config/env/all.js b/config/env/all.js index bf0cc22b..27dd00d2 100755 --- a/config/env/all.js +++ b/config/env/all.js @@ -17,7 +17,7 @@ module.exports = { }, - admin:{ + admin: { email: process.env.ADMIN_EMAIL || 'admin@admin.com', username: process.env.ADMIN_USERNAME || 'root', password: process.env.ADMIN_PASSWORD || 'root', diff --git a/config/express.js b/config/express.js index febb14c1..87b84bd7 100755 --- a/config/express.js +++ b/config/express.js @@ -232,7 +232,6 @@ module.exports = function(db) { // CookieParser should be above session app.use(cookieParser()); - debugger; // Express MongoDB session storage app.use(session({ saveUninitialized: true, diff --git a/package-lock.json b/package-lock.json index 8516edc5..ebd0238c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9857,6 +9857,11 @@ "resolved": "https://registry.npmjs.org/nodemailer/-/nodemailer-4.0.1.tgz", "integrity": "sha1-uVhksH+s7oKH6CMu/9bx1W7HWrI=" }, + "nodemailer-wellknown": { + "version": "0.2.3", + "resolved": "https://registry.npmjs.org/nodemailer-wellknown/-/nodemailer-wellknown-0.2.3.tgz", + "integrity": "sha1-ZA7SBKAWYnZD+Yc5qU+O+dOcoKk=" + }, "nodemon": { "version": "1.12.1", "resolved": "https://registry.npmjs.org/nodemon/-/nodemon-1.12.1.tgz", diff --git a/scripts/setup.js b/scripts/setup.js index 1fb48f61..d8be5236 100644 --- a/scripts/setup.js +++ b/scripts/setup.js @@ -10,7 +10,8 @@ var config = require('../config/config'), inquirer = require('inquirer'), envfile = require('envfile'), fs = require('fs-extra'), - chalk = require('chalk'); + chalk = require('chalk'), + _ = require('lodash'); // Bootstrap db connection var db = mongoose.connect(config.db.uri, config.db.options, function(err) { @@ -34,6 +35,7 @@ var User = mongoose.model('User'); require('../app/models/user.server.model.js'); var nodemailer_providers = [ + 'Custom Mailserver', '1und1', 'AOL', 'DebugMail.io', @@ -69,16 +71,20 @@ var nodemailer_providers = [ 'Zoho' ]; -var bool_options = [ - "TRUE", - "FALSE" -]; +var replaceENVQuestion = { + type: 'confirm', + name: 'replaceENVFile', + message: 'An older .env file already exists. Do you want to replace it?', + default: false +}; -var questions = [ +var questionsPart1 = [ { - type: 'confirm', - name: 'shouldContinue', - message: 'Do you wish to configure your deployment now?' + type: 'list', + name: 'NODE_ENV', + message: 'What mode do you want to run TellForm in?', + choices: ['development', 'production', 'test'], + default: 'development' }, { type: 'input', @@ -90,26 +96,63 @@ var questions = [ name: 'APP_DESC', message: 'Describe your project (for SEO) (optional)' }, - { - type: 'input', - name: 'APP_KEYWORDS', - message: 'What keywords are relevant to your project (seperate by commas) (optional)' - }, { type: 'confirm', name: 'SIGNUP_DISABLED', - message: 'Do you want to disable signups?' + message: 'Do you want to disable signups?', + default: false }, { type: 'confirm', name: 'SUBDOMAINS_DISABLED', - message: 'Do you want to have subdomains? (i.e. are you using a custom domain)' + message: 'Do you want to disable subdomains? (i.e. are you using a custom domain)' }, { type: 'list', name: 'MAILER_SERVICE_PROVIDER', message: 'What email service provider are you using?', choices: nodemailer_providers + } +] + +var mailerWellKnownQuestions = [ + { + type: 'input', + name: 'MAILER_EMAIL_ID', + message: 'What is your SMTP username?' + }, + { + type: 'password', + name: 'MAILER_PASSWORD', + message: 'What is your SMTP password?' + }, + { + type: 'input', + name: 'MAILER_FROM', + message: 'What do you want the default "from" email address to be?' + } +]; + +var mailerCustomQuestions = [ + { + type: 'input', + name: 'MAILER_SMTP_HOST', + message: 'What is your SMTP server url?' + }, + { + type: 'input', + name: 'MAILER_SMTP_PORT', + message: 'What is your SMTP server port?' + }, + { + type: 'confirm', + name: 'MAILER_SMTP_SECURE', + message: 'Is your SMTP server using SSL/TLS?' + }, + { + type: 'input', + name: 'MAILER_SMTP_HOST', + message: 'What is your SMTP host domain?' }, { type: 'input', @@ -125,11 +168,26 @@ var questions = [ type: 'input', name: 'MAILER_FROM', message: 'What do you want the default "from" email address to be?' + } +]; + +var questionsPart2 = [ + { + type: 'input', + name: 'MONGODB_URI', + message: 'What is the URI of your Mongo database?', + default: 'mongodb://localhost/mean' + }, + { + type: 'input', + name: 'MONGODB_URI', + message: 'What is the URI of your Redis installation?', + default: 'redis://127.0.0.1:6379' }, { type: 'input', name: 'BASE_URL', - message: 'What is the url your TellForm will be hosted at?', + message: 'What is the (root) url your TellForm will be hosted at?', default: 'localhost' }, { @@ -138,26 +196,6 @@ var questions = [ message: 'What port should the TellForm server run on?', default: '3000' }, - { - type: 'input', - name: 'GOOGLE_ANALYTICS_ID', - message: 'What is your Google Analytics Tag? (optional)' - }, - { - type: 'input', - name: 'RAVEN_DSN', - message: 'What is your Private Raven DSN key? (optional)' - }, - { - type: 'input', - name: 'PRERENDER_TOKEN', - message: 'What is your Prerender.io token? (optional)' - }, - { - type: 'input', - name: 'COVERALLS_REPO_TOKEN', - message: 'What is your Coveralls.io token? (optional)' - }, { type: 'input', name: 'email', @@ -175,60 +213,83 @@ var questions = [ } ]; -if(!fs.existsSync('./\.env')) { - console.log(chalk.green('\n\nHi, welcome to TellForm Setup')); +var exitProcess = function() { + console.log(chalk.green('Have fun using TellForm!')); + process.exit(1); +} - console.log(chalk.green('You should only run this the first time you run TellForm\n--------------------------------------------------\n\n')); +var removeENVFile = function(){ + fs.unlinkSync('./\.env') +} - inquirer.prompt([questions[0]]).then(function (confirmAns) { - if (confirmAns['shouldContinue']) { +var createENVFile = function() { + inquirer.prompt(questionsPart1).then(function (answersPart1) { + var nextQuestions = mailerWellKnownQuestions.concat(questionsPart2); + if(answersPart1['MAILER_SERVICE_PROVIDER'] === 'Custom Mailserver'){ + nextQuestions = mailerCustomQuestions.concat(questionsPart2); + } - inquirer.prompt(questions.slice(1)).then(function (answers) { - answers['NODE_ENV'] = 'production'; + inquirer.prompt(nextQuestions).then(function (answersPart2) { + var answers = _.extend(answersPart1, answersPart2); + answers = _.mapValues(answers, function(val){ + if(_.isBoolean(val)){ + return val ? 'TRUE' : 'FALSE'; + } + return val; + }); - var email = answers['email']; - var username = answers['username']; - var pass = answers['password']; - delete answers['email']; - delete answers['password']; + var email = answers['email']; + var username = answers['username']; + var pass = answers['password']; + delete answers['email']; + delete answers['password']; - envfile.stringify(answers, function (err, str) { - try { - fs.outputFileSync('./\.env', str); - } catch (fileErr) { - return console.error(chalk.red(fileErr)); - } + envfile.stringify(answers, function (err, str) { + try { + fs.outputFileSync('./\.env', str); + } catch (fileErr) { + return console.error(chalk.red(fileErr)); + } - console.log(chalk.green('Successfully created .env file')); + console.log(chalk.green('Successfully created .env file')); - user = new User({ - firstName: 'Admin', - lastName: 'Account', - email: email, - username: username, - password: pass, - provider: 'local', - roles: ['admin', 'user'] - }); + user = new User({ + firstName: 'Admin', + lastName: 'Account', + email: email, + username: username, + password: pass, + provider: 'local', + roles: ['admin', 'user'] + }); - user.save(function (userSaveErr) { - if (err) { - return console.error(chalk.red(userSaveErr)); - } + user.save(function (userSaveErr) { + if (err) { + return console.error(chalk.red(userSaveErr)); + } - console.log(chalk.green('Successfully created user')); + console.log(chalk.green('Successfully created user')); - console.log(chalk.green('Have fun using TellForm!')); - process.exit(1); - }); + exitProcess(); }); }); + }); + }); +} + +console.log(chalk.green('\n\nWelcome to TellForm Setup')); + +console.log(chalk.green('You should only need to run this script the first time you run TellForm\n------------------------------------------------------------------------\n\n')); + +if(fs.existsSync('./\.env')) { + inquirer.prompt([replaceENVQuestion]).then(function (envAnswer) { + if (envAnswer['replaceENVFile']) { + removeENVFile(); + createENVFile(); } else { - console.log(chalk.green('Have fun using TellForm!')); - process.exit(1); + exitProcess(); } }); } else { - console.log(chalk.red('You already have a .env file')); - process.exit(1); + createENVFile(); } diff --git a/selenium/.editorconfig b/selenium/.editorconfig new file mode 100644 index 00000000..5a13b97d --- /dev/null +++ b/selenium/.editorconfig @@ -0,0 +1,17 @@ +# EditorConfig helps developers define and maintain consistent +# coding styles between different editors and IDEs +# editorconfig.org + +root = true + +# Apply for all files +[*] + +charset = utf-8 + +indent_style = space +indent_size = 4 + +end_of_line = lf +insert_final_newline = true +trim_trailing_whitespace = true diff --git a/selenium/.gitignore b/selenium/.gitignore new file mode 100644 index 00000000..704c683c --- /dev/null +++ b/selenium/.gitignore @@ -0,0 +1,9 @@ +.DS_Store +.idea +node_modules +npm-debug.log +uirecorder.log +reports +screenshots/**/*.png +screenshots/**/*.html +screenshots/**/*.json diff --git a/selenium/.vscode/launch.json b/selenium/.vscode/launch.json new file mode 100644 index 00000000..ec025318 --- /dev/null +++ b/selenium/.vscode/launch.json @@ -0,0 +1,27 @@ +{ + // Use IntelliSense to learn about possible Node.js debug attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "type": "node", + "request": "launch", + "name": "Debug UIRecorder Local", + "program": "${workspaceRoot}/node_modules/mocha/bin/_mocha", + "cwd": "${workspaceRoot}", + "args": ["--reporter", "mochawesome-uirecorder", "${file}"], + "env": { + "webdriver": "127.0.0.1" + } + }, + { + "type": "node", + "request": "launch", + "name": "Debug UIRecorder Default", + "program": "${workspaceRoot}/node_modules/mocha/bin/_mocha", + "cwd": "${workspaceRoot}", + "args": ["--reporter", "mochawesome-uirecorder", "${file}"] + } + ] +} \ No newline at end of file diff --git a/selenium/README.md b/selenium/README.md new file mode 100644 index 00000000..62ab2ba6 --- /dev/null +++ b/selenium/README.md @@ -0,0 +1,28 @@ +UI Recorder test sample project +================ + +It's a UI Recorder test sample project. + +Save your test code here. + +Get more info: [http://uirecorder.com/](http://uirecorder.com/) + +How to run test case? +================ + +1. npm install +2. source run.sh ( Linux|Mac ) or run.bat ( Windows ) + +How to dock jenkins? +================ + +1. Add commands + + source ./install.sh + source ./run.sh + +2. Add reports + + > JUnit: reports/index.xml + + > HTML: reports/ diff --git a/selenium/commons/commons.md b/selenium/commons/commons.md new file mode 100644 index 00000000..730f82ee --- /dev/null +++ b/selenium/commons/commons.md @@ -0,0 +1 @@ +Please save common test case here. \ No newline at end of file diff --git a/selenium/config.json b/selenium/config.json new file mode 100644 index 00000000..a6e6c57a --- /dev/null +++ b/selenium/config.json @@ -0,0 +1,14 @@ +{ + "webdriver": { + "host": "127.0.0.1", + "port": "4444", + "browsers": "chrome" + }, + "vars": {}, + "recorder": { + "pathAttrs": "data-id,data-name,type,data-type,role,data-role,data-value", + "attrValueBlack": "", + "classValueBlack": "", + "hideBeforeExpect": "" + } +} \ No newline at end of file diff --git a/selenium/hosts b/selenium/hosts new file mode 100644 index 00000000..e69de29b diff --git a/selenium/install.sh b/selenium/install.sh new file mode 100644 index 00000000..67759d80 --- /dev/null +++ b/selenium/install.sh @@ -0,0 +1,4 @@ +ls ~/nvm || git clone https://github.com/creationix/nvm.git ~/nvm +source ~/nvm/nvm.sh +nvm install v7.10.0 +npm install diff --git a/selenium/package.json b/selenium/package.json new file mode 100644 index 00000000..03727c90 --- /dev/null +++ b/selenium/package.json @@ -0,0 +1,25 @@ +{ + "name": "uirecorderTest", + "version": "1.0.0", + "description": "", + "main": "", + "dependencies": { + "chai": "3.5.0", + "jwebdriver": "2.2.4", + "mocha": "3.1.2", + "mocha-parallel-tests": "1.2.4", + "mochawesome-uirecorder": "1.5.22", + "resemblejs-node": "1.0.0", + "selenium-standalone": "6.x.x" + }, + "devDependencies": { + }, + "scripts": { + "installdriver": "./node_modules/.bin/selenium-standalone install --drivers.firefox.baseURL=http://npm.taobao.org/mirrors/geckodriver --baseURL=http://npm.taobao.org/mirrors/selenium --drivers.chrome.baseURL=http://npm.taobao.org/mirrors/chromedriver --drivers.ie.baseURL=http://npm.taobao.org/mirrors/selenium", + "server": "./node_modules/.bin/selenium-standalone start", + "test": "./node_modules/.bin/mocha \"*/**/*.spec.js\" --reporter mochawesome-uirecorder --bail", + "singletest": "./node_modules/.bin/mocha --reporter mochawesome-uirecorder --bail", + "paralleltest": "./node_modules/.bin/mocha-parallel-tests \"*/**/*.spec.js\" --reporter mochawesome-uirecorder --max-parallel 5 --bail" + }, + "author": "" +} diff --git a/selenium/run.bat b/selenium/run.bat new file mode 100644 index 00000000..da1ec242 --- /dev/null +++ b/selenium/run.bat @@ -0,0 +1,7 @@ +@echo off + +if "%1" neq "" ( + npm run singletest %1 %2 +) else ( + npm run paralleltest +) diff --git a/selenium/run.sh b/selenium/run.sh new file mode 100644 index 00000000..6f479e13 --- /dev/null +++ b/selenium/run.sh @@ -0,0 +1,5 @@ +if [ "$1" = "" ]; then + npm run paralleltest +else + npm run singletest $1 $2 +fi diff --git a/selenium/screenshots/screenshots.md b/selenium/screenshots/screenshots.md new file mode 100644 index 00000000..ec2da3ba --- /dev/null +++ b/selenium/screenshots/screenshots.md @@ -0,0 +1 @@ +Test screenshots saved here. \ No newline at end of file diff --git a/selenium/uploadfiles/uploadfiles.md b/selenium/uploadfiles/uploadfiles.md new file mode 100644 index 00000000..b9f2a65c --- /dev/null +++ b/selenium/uploadfiles/uploadfiles.md @@ -0,0 +1 @@ +Please save upload files here. From a08e5c5317a5e1568ff11a47f6fe94741eb12af4 Mon Sep 17 00:00:00 2001 From: David Baldwynn Date: Tue, 21 Nov 2017 09:59:42 -0800 Subject: [PATCH 03/17] removed username from .env file in setup script --- scripts/setup.js | 1 + selenium/.editorconfig | 17 ----------------- selenium/.gitignore | 9 --------- selenium/.vscode/launch.json | 27 --------------------------- selenium/README.md | 28 ---------------------------- selenium/commons/commons.md | 1 - selenium/config.json | 14 -------------- selenium/hosts | 0 selenium/install.sh | 4 ---- selenium/package.json | 25 ------------------------- selenium/run.bat | 7 ------- selenium/run.sh | 5 ----- selenium/screenshots/screenshots.md | 1 - selenium/uploadfiles/uploadfiles.md | 1 - 14 files changed, 1 insertion(+), 139 deletions(-) delete mode 100644 selenium/.editorconfig delete mode 100644 selenium/.gitignore delete mode 100644 selenium/.vscode/launch.json delete mode 100644 selenium/README.md delete mode 100644 selenium/commons/commons.md delete mode 100644 selenium/config.json delete mode 100644 selenium/hosts delete mode 100644 selenium/install.sh delete mode 100644 selenium/package.json delete mode 100644 selenium/run.bat delete mode 100644 selenium/run.sh delete mode 100644 selenium/screenshots/screenshots.md delete mode 100644 selenium/uploadfiles/uploadfiles.md diff --git a/scripts/setup.js b/scripts/setup.js index d8be5236..70c005ef 100644 --- a/scripts/setup.js +++ b/scripts/setup.js @@ -242,6 +242,7 @@ var createENVFile = function() { var username = answers['username']; var pass = answers['password']; delete answers['email']; + delete answers['username']; delete answers['password']; envfile.stringify(answers, function (err, str) { diff --git a/selenium/.editorconfig b/selenium/.editorconfig deleted file mode 100644 index 5a13b97d..00000000 --- a/selenium/.editorconfig +++ /dev/null @@ -1,17 +0,0 @@ -# EditorConfig helps developers define and maintain consistent -# coding styles between different editors and IDEs -# editorconfig.org - -root = true - -# Apply for all files -[*] - -charset = utf-8 - -indent_style = space -indent_size = 4 - -end_of_line = lf -insert_final_newline = true -trim_trailing_whitespace = true diff --git a/selenium/.gitignore b/selenium/.gitignore deleted file mode 100644 index 704c683c..00000000 --- a/selenium/.gitignore +++ /dev/null @@ -1,9 +0,0 @@ -.DS_Store -.idea -node_modules -npm-debug.log -uirecorder.log -reports -screenshots/**/*.png -screenshots/**/*.html -screenshots/**/*.json diff --git a/selenium/.vscode/launch.json b/selenium/.vscode/launch.json deleted file mode 100644 index ec025318..00000000 --- a/selenium/.vscode/launch.json +++ /dev/null @@ -1,27 +0,0 @@ -{ - // Use IntelliSense to learn about possible Node.js debug attributes. - // Hover to view descriptions of existing attributes. - // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 - "version": "0.2.0", - "configurations": [ - { - "type": "node", - "request": "launch", - "name": "Debug UIRecorder Local", - "program": "${workspaceRoot}/node_modules/mocha/bin/_mocha", - "cwd": "${workspaceRoot}", - "args": ["--reporter", "mochawesome-uirecorder", "${file}"], - "env": { - "webdriver": "127.0.0.1" - } - }, - { - "type": "node", - "request": "launch", - "name": "Debug UIRecorder Default", - "program": "${workspaceRoot}/node_modules/mocha/bin/_mocha", - "cwd": "${workspaceRoot}", - "args": ["--reporter", "mochawesome-uirecorder", "${file}"] - } - ] -} \ No newline at end of file diff --git a/selenium/README.md b/selenium/README.md deleted file mode 100644 index 62ab2ba6..00000000 --- a/selenium/README.md +++ /dev/null @@ -1,28 +0,0 @@ -UI Recorder test sample project -================ - -It's a UI Recorder test sample project. - -Save your test code here. - -Get more info: [http://uirecorder.com/](http://uirecorder.com/) - -How to run test case? -================ - -1. npm install -2. source run.sh ( Linux|Mac ) or run.bat ( Windows ) - -How to dock jenkins? -================ - -1. Add commands - - source ./install.sh - source ./run.sh - -2. Add reports - - > JUnit: reports/index.xml - - > HTML: reports/ diff --git a/selenium/commons/commons.md b/selenium/commons/commons.md deleted file mode 100644 index 730f82ee..00000000 --- a/selenium/commons/commons.md +++ /dev/null @@ -1 +0,0 @@ -Please save common test case here. \ No newline at end of file diff --git a/selenium/config.json b/selenium/config.json deleted file mode 100644 index a6e6c57a..00000000 --- a/selenium/config.json +++ /dev/null @@ -1,14 +0,0 @@ -{ - "webdriver": { - "host": "127.0.0.1", - "port": "4444", - "browsers": "chrome" - }, - "vars": {}, - "recorder": { - "pathAttrs": "data-id,data-name,type,data-type,role,data-role,data-value", - "attrValueBlack": "", - "classValueBlack": "", - "hideBeforeExpect": "" - } -} \ No newline at end of file diff --git a/selenium/hosts b/selenium/hosts deleted file mode 100644 index e69de29b..00000000 diff --git a/selenium/install.sh b/selenium/install.sh deleted file mode 100644 index 67759d80..00000000 --- a/selenium/install.sh +++ /dev/null @@ -1,4 +0,0 @@ -ls ~/nvm || git clone https://github.com/creationix/nvm.git ~/nvm -source ~/nvm/nvm.sh -nvm install v7.10.0 -npm install diff --git a/selenium/package.json b/selenium/package.json deleted file mode 100644 index 03727c90..00000000 --- a/selenium/package.json +++ /dev/null @@ -1,25 +0,0 @@ -{ - "name": "uirecorderTest", - "version": "1.0.0", - "description": "", - "main": "", - "dependencies": { - "chai": "3.5.0", - "jwebdriver": "2.2.4", - "mocha": "3.1.2", - "mocha-parallel-tests": "1.2.4", - "mochawesome-uirecorder": "1.5.22", - "resemblejs-node": "1.0.0", - "selenium-standalone": "6.x.x" - }, - "devDependencies": { - }, - "scripts": { - "installdriver": "./node_modules/.bin/selenium-standalone install --drivers.firefox.baseURL=http://npm.taobao.org/mirrors/geckodriver --baseURL=http://npm.taobao.org/mirrors/selenium --drivers.chrome.baseURL=http://npm.taobao.org/mirrors/chromedriver --drivers.ie.baseURL=http://npm.taobao.org/mirrors/selenium", - "server": "./node_modules/.bin/selenium-standalone start", - "test": "./node_modules/.bin/mocha \"*/**/*.spec.js\" --reporter mochawesome-uirecorder --bail", - "singletest": "./node_modules/.bin/mocha --reporter mochawesome-uirecorder --bail", - "paralleltest": "./node_modules/.bin/mocha-parallel-tests \"*/**/*.spec.js\" --reporter mochawesome-uirecorder --max-parallel 5 --bail" - }, - "author": "" -} diff --git a/selenium/run.bat b/selenium/run.bat deleted file mode 100644 index da1ec242..00000000 --- a/selenium/run.bat +++ /dev/null @@ -1,7 +0,0 @@ -@echo off - -if "%1" neq "" ( - npm run singletest %1 %2 -) else ( - npm run paralleltest -) diff --git a/selenium/run.sh b/selenium/run.sh deleted file mode 100644 index 6f479e13..00000000 --- a/selenium/run.sh +++ /dev/null @@ -1,5 +0,0 @@ -if [ "$1" = "" ]; then - npm run paralleltest -else - npm run singletest $1 $2 -fi diff --git a/selenium/screenshots/screenshots.md b/selenium/screenshots/screenshots.md deleted file mode 100644 index ec2da3ba..00000000 --- a/selenium/screenshots/screenshots.md +++ /dev/null @@ -1 +0,0 @@ -Test screenshots saved here. \ No newline at end of file diff --git a/selenium/uploadfiles/uploadfiles.md b/selenium/uploadfiles/uploadfiles.md deleted file mode 100644 index b9f2a65c..00000000 --- a/selenium/uploadfiles/uploadfiles.md +++ /dev/null @@ -1 +0,0 @@ -Please save upload files here. From 2bd1598a01044a60e4df85c419686c07f7d49f94 Mon Sep 17 00:00:00 2001 From: David Baldwynn Date: Tue, 21 Nov 2017 10:11:53 -0800 Subject: [PATCH 04/17] Handled case in setup script where admin user already exists --- .gitignore | 1 + dump.rdb | Bin 76 -> 0 bytes scripts/setup.js | 20 +++++++++++++++----- 3 files changed, 16 insertions(+), 5 deletions(-) delete mode 100644 dump.rdb diff --git a/.gitignore b/.gitignore index 91df1f8d..bbc3c9f2 100755 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +selenium dist .vagrant npm-debug.* diff --git a/dump.rdb b/dump.rdb deleted file mode 100644 index c6e61e2af22236acfff1aa86fad52a75ef8fba48..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 76 zcmWG?b@2=~FfcIx#aWb^l3A= Date: Tue, 21 Nov 2017 11:50:35 -0800 Subject: [PATCH 05/17] fixed incorrect ENV variable name for redis url --- scripts/setup.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/setup.js b/scripts/setup.js index ba5015af..f3f91049 100644 --- a/scripts/setup.js +++ b/scripts/setup.js @@ -180,7 +180,7 @@ var questionsPart2 = [ }, { type: 'input', - name: 'MONGODB_URI', + name: 'REDIS_URL', message: 'What is the URI of your Redis installation?', default: 'redis://127.0.0.1:6379' }, From e27f27f7f0d639155f7fe3a5d53ffd8fa5d9393f Mon Sep 17 00:00:00 2001 From: David Baldwynn Date: Tue, 21 Nov 2017 12:31:13 -0800 Subject: [PATCH 06/17] added email validation to setup script --- app/libs/constants.js | 39 +--- config/env/all.js | 3 +- scripts/setup.js | 359 +++++++++++-------------------------- scripts/setup_constants.js | 192 ++++++++++++++++++++ 4 files changed, 298 insertions(+), 295 deletions(-) create mode 100644 scripts/setup_constants.js diff --git a/app/libs/constants.js b/app/libs/constants.js index 3771e0db..50d748bf 100644 --- a/app/libs/constants.js +++ b/app/libs/constants.js @@ -1,44 +1,7 @@ 'use strict'; -module.exports = { +var constants = module.exports = { - - nodemailer_providers: [ - '1und1', - 'AOL', - 'DebugMail.io', - 'DynectEmail', - 'FastMail', - 'GandiMail', - 'Gmail', - 'Godaddy', - 'GodaddyAsia', - 'GodaddyEurope', - 'hot.ee', - 'Hotmail', - 'iCloud', - 'mail.ee', - 'Mail.ru', - 'Mailgun', - 'Mailjet', - 'Mandrill', - 'Naver', - 'OpenMailBox', - 'Postmark', - 'QQ', - 'QQex', - 'SendCloud', - 'SendGrid', - 'SES', - 'SES-US-EAST-1', - 'SES-US-WEST-1', - 'SES-EU-WEST-1', - 'Sparkpost', - 'Yahoo', - 'Yandex', - 'Zoho' - ], - extraneousFormFieldProps: [ 'validFieldTypes', 'disabled', diff --git a/config/env/all.js b/config/env/all.js index 27dd00d2..35456020 100755 --- a/config/env/all.js +++ b/config/env/all.js @@ -15,8 +15,7 @@ module.exports = { useMongoClient: true } }, - - + admin: { email: process.env.ADMIN_EMAIL || 'admin@admin.com', username: process.env.ADMIN_USERNAME || 'root', diff --git a/scripts/setup.js b/scripts/setup.js index f3f91049..3cc3ee6f 100644 --- a/scripts/setup.js +++ b/scripts/setup.js @@ -3,304 +3,153 @@ /** * Module dependencies. */ -process.env.NODE_ENV = 'production'; -var config = require('../config/config'), - mongoose = require('mongoose'), +var mongoose = require('mongoose'), inquirer = require('inquirer'), envfile = require('envfile'), fs = require('fs-extra'), chalk = require('chalk'), + constants = require('./setup_constants'), _ = require('lodash'); -// Bootstrap db connection -var db = mongoose.connect(config.db.uri, config.db.options, function(err) { - if (err) { - console.error(chalk.red('Could not connect to MongoDB!')); - console.log(chalk.red(err)); - } -}); -mongoose.connection.on('error', function(err) { - console.error(chalk.red('MongoDB connection error: ' + err)); - process.exit(-1); -}); - -// Init the express application -require('../config/express')(db); - -// Bootstrap passport config -require('../config/passport')(); - -var User = mongoose.model('User'); -require('../app/models/user.server.model.js'); - -var nodemailer_providers = [ - 'Custom Mailserver', - '1und1', - 'AOL', - 'DebugMail.io', - 'DynectEmail', - 'FastMail', - 'GandiMail', - 'Gmail', - 'Godaddy', - 'GodaddyAsia', - 'GodaddyEurope', - 'hot.ee', - 'Hotmail', - 'iCloud', - 'mail.ee', - 'Mail.ru', - 'Mailgun', - 'Mailjet', - 'Mandrill', - 'Naver', - 'OpenMailBox', - 'Postmark', - 'QQ', - 'QQex', - 'SendCloud', - 'SendGrid', - 'SES', - 'SES-US-EAST-1', - 'SES-US-WEST-1', - 'SES-EU-WEST-1', - 'Sparkpost', - 'Yahoo', - 'Yandex', - 'Zoho' -]; - -var replaceENVQuestion = { - type: 'confirm', - name: 'replaceENVFile', - message: 'An older .env file already exists. Do you want to replace it?', - default: false -}; - -var questionsPart1 = [ - { - type: 'list', - name: 'NODE_ENV', - message: 'What mode do you want to run TellForm in?', - choices: ['development', 'production', 'test'], - default: 'development' - }, - { - type: 'input', - name: 'APP_NAME', - message: 'What do you want to name your TellForm deployment?' - }, - { - type: 'input', - name: 'APP_DESC', - message: 'Describe your project (for SEO) (optional)' - }, - { - type: 'confirm', - name: 'SIGNUP_DISABLED', - message: 'Do you want to disable signups?', - default: false - }, - { - type: 'confirm', - name: 'SUBDOMAINS_DISABLED', - message: 'Do you want to disable subdomains? (i.e. are you using a custom domain)' - }, - { - type: 'list', - name: 'MAILER_SERVICE_PROVIDER', - message: 'What email service provider are you using?', - choices: nodemailer_providers - } -] - -var mailerWellKnownQuestions = [ - { - type: 'input', - name: 'MAILER_EMAIL_ID', - message: 'What is your SMTP username?' - }, - { - type: 'password', - name: 'MAILER_PASSWORD', - message: 'What is your SMTP password?' - }, - { - type: 'input', - name: 'MAILER_FROM', - message: 'What do you want the default "from" email address to be?' - } -]; - -var mailerCustomQuestions = [ - { - type: 'input', - name: 'MAILER_SMTP_HOST', - message: 'What is your SMTP server url?' - }, - { - type: 'input', - name: 'MAILER_SMTP_PORT', - message: 'What is your SMTP server port?' - }, - { - type: 'confirm', - name: 'MAILER_SMTP_SECURE', - message: 'Is your SMTP server using SSL/TLS?' - }, - { - type: 'input', - name: 'MAILER_SMTP_HOST', - message: 'What is your SMTP host domain?' - }, - { - type: 'input', - name: 'MAILER_EMAIL_ID', - message: 'What is your SMTP username?' - }, - { - type: 'password', - name: 'MAILER_PASSWORD', - message: 'What is your SMTP password?' - }, - { - type: 'input', - name: 'MAILER_FROM', - message: 'What do you want the default "from" email address to be?' - } -]; - -var questionsPart2 = [ - { - type: 'input', - name: 'MONGODB_URI', - message: 'What is the URI of your Mongo database?', - default: 'mongodb://localhost/mean' - }, - { - type: 'input', - name: 'REDIS_URL', - message: 'What is the URI of your Redis installation?', - default: 'redis://127.0.0.1:6379' - }, - { - type: 'input', - name: 'BASE_URL', - message: 'What is the (root) url your TellForm will be hosted at?', - default: 'localhost' - }, - { - type: 'input', - name: 'PORT', - message: 'What port should the TellForm server run on?', - default: '3000' - }, - { - type: 'input', - name: 'email', - message: 'What should be the email for your admin account?' - }, - { - type: 'input', - name: 'username', - message: 'What should be the username for your admin account?' - }, - { - type: 'password', - name: 'password', - message: 'What should be the password for your admin account?' - } -]; - var exitProcess = function() { + console.log(chalk.green('TellForm has been successfully setup')); console.log(chalk.green('Have fun using TellForm!')); process.exit(1); } -var removeENVFile = function(){ +var removeENVFile = function() { fs.unlinkSync('./\.env') } +var createOrUpdateAdminUser = function(username, email, password, cb){ + //Command Line Bootstrapping Code + if (require.main === module) { + var config = require('../config/config'); + + // Bootstrap db connection + var db = mongoose.connect(config.db.uri, config.db.options, function(err) { + if (err) { + console.error(chalk.red('Could not connect to MongoDB!')); + return cb(new Error(err)); + } + }); + mongoose.connection.on('error', function(err) { + return cb(new Error('MongoDB connection error: ' + err)); + }); + + // Init the express application + require('../config/express')(db); + + // Bootstrap passport config + require('../config/passport')(); + } + + var User = require('../app/models/user.server.model.js'); + + var updateObj = { + firstName: 'Admin', + lastName: 'Account', + username: username, + email: email, + password: pass, + provider: 'local', + roles: ['admin', 'user'] + } + + var options = { + upsert: true, + new: true, + setDefaultsOnInsert: true + } + + User.findOneAndUpdate({ username: username }, updateObj, options, function (err, result) { + if (err) { + return cb(err); + } + + if(!result){ + return cb(new Error('Admin User could not be created')); + } + + delete pass; + delete email; + delete username; + + console.log(chalk.green('Successfully created user')); + + cb(); + }); + +} + var createENVFile = function() { - inquirer.prompt(questionsPart1).then(function (answersPart1) { - var nextQuestions = mailerWellKnownQuestions.concat(questionsPart2); + inquirer.prompt(constants.questionsPart1).then(function (answersPart1) { + var nextQuestions = constants.mailerWellKnownQuestions.concat(constants.questionsPart2); if(answersPart1['MAILER_SERVICE_PROVIDER'] === 'Custom Mailserver'){ - nextQuestions = mailerCustomQuestions.concat(questionsPart2); + nextQuestions = constants.mailerCustomQuestions.concat(constants.questionsPart2); } inquirer.prompt(nextQuestions).then(function (answersPart2) { - var answers = _.extend(answersPart1, answersPart2); - answers = _.mapValues(answers, function(val){ + var answers = _.chain(anwsersPart1)._extend(answersPart2).mapValues(function(val){ if(_.isBoolean(val)){ return val ? 'TRUE' : 'FALSE'; } return val; - }); + }).values(); var email = answers['email']; var username = answers['username']; var pass = answers['password']; delete answers['email']; - delete answers['username']; + delete answers['username']; delete answers['password']; envfile.stringify(answers, function (err, str) { - try { - fs.outputFileSync('./\.env', str); - } catch (fileErr) { - return console.error(chalk.red(fileErr)); - } + try { + fs.outputFileSync('./\.env', str); + } catch (fileErr) { + console.error(chalk.red(fileErr)); + process.exit(-1); + } - console.log(chalk.green('Successfully created .env file')); + console.log(chalk.green('Successfully created .env file')); - var updateObj = { - firstName: 'Admin', - lastName: 'Account', - username: username, - email: email, - password: pass, - provider: 'local', - roles: ['admin', 'user'] - } - - var options = { - upsert: true, - new: true, - setDefaultsOnInsert: true - } - - User.findOneAndUpdate({ username: username }, updateObj, options, function (userSaveErr, result) { - if (err || !result) { - return console.error(chalk.red(userSaveErr)); + createOrUpdateAdminUser(username, email, pass, function(err){ + if(err) { + console.error(chalk.red(err.message)); + process.exit(-1); } - delete pass; - delete email; - delete username; - - console.log(chalk.green('Successfully created user')); - exitProcess(); }); + }); }); }); } -console.log(chalk.green('\n\nWelcome to TellForm Setup')); +var runSetup = function(){ + console.log(chalk.green('\n\nWelcome to TellForm Setup')); -console.log(chalk.green('You should only need to run this script the first time you run TellForm\n------------------------------------------------------------------------\n\n')); + console.log(chalk.green('You should only need to run this script the first time you run TellForm\n------------------------------------------------------------------------\n\n')); -if(fs.existsSync('./\.env')) { - inquirer.prompt([replaceENVQuestion]).then(function (envAnswer) { - if (envAnswer['replaceENVFile']) { - removeENVFile(); - createENVFile(); - } else { - exitProcess(); - } - }); -} else { - createENVFile(); + if(fs.existsSync('./\.env') && require.main === module) { + inquirer.prompt([constants.replaceENVQuestion]).then(function (envAnswer) { + if (envAnswer['replaceENVFile']) { + removeENVFile(); + createENVFile(); + } else { + exitProcess(); + } + }); + } else { + createENVFile(); + } +} + +module.exports.runSetup = runSetup; + +if(require.main === module) { + runSetup(); } diff --git a/scripts/setup_constants.js b/scripts/setup_constants.js new file mode 100644 index 00000000..bbe466ec --- /dev/null +++ b/scripts/setup_constants.js @@ -0,0 +1,192 @@ +var constants = require('../app/libs/constants'); + +var validateEmail = function(value) { + var isValidEmail = new RegExp(constants.regex.email, 'g').test(value); + + if(!isValidEmail){ + return 'Please enter a valid email' + } else { + return true; + } +} + +module.exports = { + replaceENVQuestion: { + type: 'confirm', + name: 'replaceENVFile', + message: 'An older .env file already exists. Do you want to replace it?', + default: false + }, + + questionsPart1: [ + { + type: 'list', + name: 'NODE_ENV', + message: 'What mode do you want to run TellForm in?', + choices: ['development', 'production', 'test'], + default: 'development' + }, + { + type: 'input', + name: 'APP_NAME', + message: 'What do you want to name your TellForm deployment?' + }, + { + type: 'input', + name: 'APP_DESC', + message: 'Describe your project (for SEO) (optional)' + }, + { + type: 'confirm', + name: 'SIGNUP_DISABLED', + message: 'Do you want to disable signups?', + default: false + }, + { + type: 'confirm', + name: 'SUBDOMAINS_DISABLED', + message: 'Do you want to disable subdomains? (i.e. are you using a custom domain)' + }, + { + type: 'list', + name: 'MAILER_SERVICE_PROVIDER', + message: 'What email service provider are you using?', + choices: [ + 'Custom Mailserver', + '1und1', + 'AOL', + 'DebugMail.io', + 'DynectEmail', + 'FastMail', + 'GandiMail', + 'Gmail', + 'Godaddy', + 'GodaddyAsia', + 'GodaddyEurope', + 'hot.ee', + 'Hotmail', + 'iCloud', + 'mail.ee', + 'Mail.ru', + 'Mailgun', + 'Mailjet', + 'Mandrill', + 'Naver', + 'OpenMailBox', + 'Postmark', + 'QQ', + 'QQex', + 'SendCloud', + 'SendGrid', + 'SES', + 'SES-US-EAST-1', + 'SES-US-WEST-1', + 'SES-EU-WEST-1', + 'Sparkpost', + 'Yahoo', + 'Yandex', + 'Zoho' + ] + } + ], + + mailerWellKnownQuestions: [ + { + type: 'input', + name: 'MAILER_EMAIL_ID', + message: 'What is your SMTP username?' + }, + { + type: 'password', + name: 'MAILER_PASSWORD', + message: 'What is your SMTP password?' + }, + { + type: 'input', + name: 'MAILER_FROM', + message: 'What do you want the default "from" email address to be?' + } + ], + + mailerCustomQuestions: [ + { + type: 'input', + name: 'MAILER_SMTP_HOST', + message: 'What is your SMTP server url?' + }, + { + type: 'input', + name: 'MAILER_SMTP_PORT', + message: 'What is your SMTP server port?' + }, + { + type: 'confirm', + name: 'MAILER_SMTP_SECURE', + message: 'Is your SMTP server using SSL/TLS?' + }, + { + type: 'input', + name: 'MAILER_SMTP_HOST', + message: 'What is your SMTP host domain?' + }, + { + type: 'input', + name: 'MAILER_EMAIL_ID', + message: 'What is your SMTP username?' + }, + { + type: 'password', + name: 'MAILER_PASSWORD', + message: 'What is your SMTP password?' + }, + { + type: 'input', + name: 'MAILER_FROM', + message: 'What do you want the default "from" email address to be?', + validate: validateEmail + } + ], + + questionsPart2: [ + { + type: 'input', + name: 'MONGODB_URI', + message: 'What is the URI of your Mongo database?', + default: 'mongodb://localhost/mean' + }, + { + type: 'input', + name: 'REDIS_URL', + message: 'What is the URI of your Redis installation?', + default: 'redis://127.0.0.1:6379' + }, + { + type: 'input', + name: 'BASE_URL', + message: 'What is the (root) url your TellForm will be hosted at?', + default: 'localhost' + }, + { + type: 'input', + name: 'PORT', + message: 'What port should the TellForm server run on?', + default: '3000' + }, + { + type: 'input', + name: 'email', + message: 'What should be the email for your admin account?', + validate: validateEmail + }, + { + type: 'input', + name: 'username', + message: 'What should be the username for your admin account?' + }, + { + type: 'password', + name: 'password', + message: 'What should be the password for your admin account?' + } + ] +}; \ No newline at end of file From c97f20769955bb593ea6fd8b5abec9bc03dd7900 Mon Sep 17 00:00:00 2001 From: David Baldwynn Date: Tue, 21 Nov 2017 12:33:47 -0800 Subject: [PATCH 07/17] added username validation to setup script --- app/libs/constants.js | 1 + app/models/user.server.model.js | 2 +- scripts/setup_constants.js | 20 +++++++++++++------- 3 files changed, 15 insertions(+), 8 deletions(-) diff --git a/app/libs/constants.js b/app/libs/constants.js index 50d748bf..0391dac8 100644 --- a/app/libs/constants.js +++ b/app/libs/constants.js @@ -98,6 +98,7 @@ var constants = module.exports = { userRoleTypes: ['user', 'admin', 'superuser'], regex: { + username: /^[a-zA-Z0-9\-]+$/, url: /((([A-Za-z]{3,9}:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+|(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?(?:[\w]*))?)/, hexCode: /^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/, email: /^(([^<>()\[\]\\.,;:\s@']+(\.[^<>()\[\]\\.,;:\s@']+)*)|('.+'))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/, diff --git a/app/models/user.server.model.js b/app/models/user.server.model.js index fd80602d..50872eec 100755 --- a/app/models/user.server.model.js +++ b/app/models/user.server.model.js @@ -38,7 +38,7 @@ var UserSchema = new Schema({ type: String, unique: true, lowercase: true, - match: [/^[a-zA-Z0-9\-]+$/, 'Username can only contain alphanumeric characters and \'-\''], + match: [constants.regex.username, 'Username can only contain alphanumeric characters and \'-\''], required: [true, 'Username is required'] }, passwordHash: { diff --git a/scripts/setup_constants.js b/scripts/setup_constants.js index bbe466ec..54b2102a 100644 --- a/scripts/setup_constants.js +++ b/scripts/setup_constants.js @@ -1,15 +1,20 @@ var constants = require('../app/libs/constants'); -var validateEmail = function(value) { - var isValidEmail = new RegExp(constants.regex.email, 'g').test(value); +var createRegexValidator = function(regex){ + return function(value) { + var isValid = new RegExp(regex, 'g').test(value); - if(!isValidEmail){ - return 'Please enter a valid email' - } else { - return true; + if(!isValid){ + return 'Please enter a valid email' + } else { + return true; + } } } +var validateEmail = createRegexValidator(constants.regex.email); +var validateUsername = createRegexValidator(constants.regex.username); + module.exports = { replaceENVQuestion: { type: 'confirm', @@ -181,7 +186,8 @@ module.exports = { { type: 'input', name: 'username', - message: 'What should be the username for your admin account?' + message: 'What should be the username for your admin account?', + validate: validateUsername }, { type: 'password', From 563b8c6cd23bb5a2c5422ec6cec244783adf8bf8 Mon Sep 17 00:00:00 2001 From: David Baldwynn Date: Tue, 21 Nov 2017 12:37:57 -0800 Subject: [PATCH 08/17] Modified setup script to search for admin user by email AND username --- scripts/setup.js | 2 +- scripts/setup_constants.js | 23 +++++++++-------------- 2 files changed, 10 insertions(+), 15 deletions(-) diff --git a/scripts/setup.js b/scripts/setup.js index 3cc3ee6f..031e9207 100644 --- a/scripts/setup.js +++ b/scripts/setup.js @@ -63,7 +63,7 @@ var createOrUpdateAdminUser = function(username, email, password, cb){ setDefaultsOnInsert: true } - User.findOneAndUpdate({ username: username }, updateObj, options, function (err, result) { + User.findOneAndUpdate({ username: username, email: email }, updateObj, options, function (err, result) { if (err) { return cb(err); } diff --git a/scripts/setup_constants.js b/scripts/setup_constants.js index 54b2102a..235350c9 100644 --- a/scripts/setup_constants.js +++ b/scripts/setup_constants.js @@ -1,19 +1,19 @@ var constants = require('../app/libs/constants'); -var createRegexValidator = function(regex){ +var createRegexValidator = function(regex, message){ return function(value) { var isValid = new RegExp(regex, 'g').test(value); if(!isValid){ - return 'Please enter a valid email' + return message } else { return true; } } } -var validateEmail = createRegexValidator(constants.regex.email); -var validateUsername = createRegexValidator(constants.regex.username); +var validateEmail = createRegexValidator(constants.regex.email, 'Please enter a valid email'); +var validateUsername = createRegexValidator(constants.regex.username, 'Usernames can only contain alphanumeric characters and \'-\''); module.exports = { replaceENVQuestion: { @@ -105,11 +105,6 @@ module.exports = { type: 'password', name: 'MAILER_PASSWORD', message: 'What is your SMTP password?' - }, - { - type: 'input', - name: 'MAILER_FROM', - message: 'What do you want the default "from" email address to be?' } ], @@ -143,16 +138,16 @@ module.exports = { type: 'password', name: 'MAILER_PASSWORD', message: 'What is your SMTP password?' - }, + } + ], + + questionsPart2: [ { type: 'input', name: 'MAILER_FROM', message: 'What do you want the default "from" email address to be?', validate: validateEmail - } - ], - - questionsPart2: [ + }, { type: 'input', name: 'MONGODB_URI', From 31985ddda7a77a8f824e22c0350b125dd58cc937 Mon Sep 17 00:00:00 2001 From: David Baldwynn Date: Tue, 21 Nov 2017 13:10:43 -0800 Subject: [PATCH 09/17] added setup script that runs on first run of server --- scripts/setup.js | 63 ++++++++++++++-------- server.js | 133 +++++++++++++++++++++++------------------------ start.js | 16 ++++++ 3 files changed, 123 insertions(+), 89 deletions(-) create mode 100644 start.js diff --git a/scripts/setup.js b/scripts/setup.js index 031e9207..4eeec3cf 100644 --- a/scripts/setup.js +++ b/scripts/setup.js @@ -12,10 +12,24 @@ var mongoose = require('mongoose'), constants = require('./setup_constants'), _ = require('lodash'); -var exitProcess = function() { +var exitSuccess = function(cb) { console.log(chalk.green('TellForm has been successfully setup')); console.log(chalk.green('Have fun using TellForm!')); - process.exit(1); + + if(require.main === module){ + process.exit(1); + } else if(cb && typeof cb === 'function'){ + cb(); + } +} + +var exitError = function(err, cb){ + console.error(chalk.red(err.message || err)); + if(require.main === module){ + process.exit(-1); + } else if(cb && typeof cb === 'function'){ + cb(); + } } var removeENVFile = function() { @@ -83,7 +97,7 @@ var createOrUpdateAdminUser = function(username, email, password, cb){ } -var createENVFile = function() { +var createENVFile = function(cb) { inquirer.prompt(constants.questionsPart1).then(function (answersPart1) { var nextQuestions = constants.mailerWellKnownQuestions.concat(constants.questionsPart2); if(answersPart1['MAILER_SERVICE_PROVIDER'] === 'Custom Mailserver'){ @@ -106,22 +120,20 @@ var createENVFile = function() { delete answers['password']; envfile.stringify(answers, function (err, str) { - try { - fs.outputFileSync('./\.env', str); - } catch (fileErr) { - console.error(chalk.red(fileErr)); - process.exit(-1); - } + try { + fs.outputFileSync('./\.env', str); + } catch (fileErr) { + console.error(chalk.red(fileErr)); + process.exit(-1); + } - console.log(chalk.green('Successfully created .env file')); + console.log(chalk.green('Successfully created .env file')); createOrUpdateAdminUser(username, email, pass, function(err){ if(err) { - console.error(chalk.red(err.message)); - process.exit(-1); + return exitError(err, cb); } - - exitProcess(); + exitSuccess(cb); }); }); @@ -129,27 +141,34 @@ var createENVFile = function() { }); } -var runSetup = function(){ - console.log(chalk.green('\n\nWelcome to TellForm Setup')); - - console.log(chalk.green('You should only need to run this script the first time you run TellForm\n------------------------------------------------------------------------\n\n')); +var checkENVAndRunSetup = function(cb) { + if(require.main === module){ + console.log(chalk.green('\n\nWelcome to TellForm\'s Setup Tool')); + console.log(chalk.green('Follow the prompts to begin.\n---------------------------------------------------------------------\n\n')); + } if(fs.existsSync('./\.env') && require.main === module) { inquirer.prompt([constants.replaceENVQuestion]).then(function (envAnswer) { if (envAnswer['replaceENVFile']) { removeENVFile(); - createENVFile(); + createENVFile(cb); } else { - exitProcess(); + exitSuccess(cb); } }); } else { + + if(require.main !== module){ + console.log(chalk.green('\nWelcome to TellForm!\n')); + console.log(chalk.green('The following prompts will help you configure your TellForm instance for your needs')); + console.log(chalk.green('These prompts won\'t show up again after the initial setup.\n---------------------------------------------------------------------\n\n')); + } createENVFile(); } } -module.exports.runSetup = runSetup; +module.exports.checkENVAndRunSetup = checkENVAndRunSetup; if(require.main === module) { - runSetup(); + checkENVAndRunSetup(); } diff --git a/server.js b/server.js index 725a6842..e221d055 100755 --- a/server.js +++ b/server.js @@ -1,22 +1,9 @@ 'use strict'; + /** * Module dependencies. */ - -if(!process.env.NODE_ENV){ - process.env.NODE_ENV = 'development'; -} - -//Don't check .env file if we are in travis-ci -if(!process.env.TRAVIS){ - require('dotenv').config({path: './.env'}); -} - - -require('events').EventEmitter.prototype._maxListeners = 0; - -var config = require('./config/config'), - mongoose = require('mongoose'), +var mongoose = require('mongoose'), chalk = require('chalk'), nodemailer = require('nodemailer'); @@ -24,62 +11,74 @@ var config = require('./config/config'), * Main application entry file. * Please note that the order of loading is important. */ - -// Bootstrap db connection -var db = mongoose.connect(config.db.uri, config.db.options, function (err) { - if (err) { - console.error(chalk.red('Could not connect to MongoDB!')); - console.log(chalk.red(err)); +module.exports.bootstrap = function() { + //Don't check .env file if we are in travis-ci + if(!process.env.TRAVIS) { + require('dotenv').config({path: './.env'}); } -}); -mongoose.connection.on('error', function (err) { - console.error(chalk.red('MongoDB connection error: ' + err)); - process.exit(-1); -}); - -const smtpTransport = nodemailer.createTransport(config.mailer.options); - -// verify connection configuration on startup -smtpTransport.verify(function(error, success) { - if (error) { - console.error(chalk.red('Your mail configuration is incorrect: ' + error)); - process.exit(-1); + + if(!process.env.NODE_ENV) { + process.env.NODE_ENV = 'development'; } -}); -// Init the express application -var app = require('./config/express')(db); + var config = require('./config/config'); -//Create admin account -if (process.env.CREATE_ADMIN_ACCOUNT === 'TRUE') { - var create_admin = require('./scripts/create_admin'); - - create_admin.run(app, db, function(err){ - if(err){ - console.error(chalk.red('Could not create Admin Account: ' + err)); + // Bootstrap db connection + var db = mongoose.connect(config.db.uri, config.db.options, function (err) { + if (err) { + console.error(chalk.red('Could not connect to MongoDB!')); + console.log(chalk.red(err)); } }); + mongoose.connection.on('error', function (err) { + console.error(chalk.red('MongoDB connection error: ' + err)); + process.exit(-1); + }); + + const smtpTransport = nodemailer.createTransport(config.mailer.options); + + // verify connection configuration on startup + smtpTransport.verify(function(error, success) { + if (error) { + console.error(chalk.red('Your mail configuration is incorrect: ' + error)); + process.exit(-1); + } + }); + + // Init the express application + var app = require('./config/express')(db); + + //Create admin account + if (process.env.CREATE_ADMIN_ACCOUNT === 'TRUE') { + var create_admin = require('./scripts/create_admin'); + + create_admin.run(app, db, function(err){ + if(err){ + console.error(chalk.red('Could not create Admin Account: ' + err)); + } + }); + } + + + // Bootstrap passport config + require('./config/passport')(); + + // Start the app by listening on + app.listen(config.port); + + // Expose app + exports = module.exports = app; + + // Logging initialization + console.log('--'); + console.log(chalk.green('Environment:\t\t\t' + process.env.NODE_ENV)); + console.log(chalk.green('Port:\t\t\t\t' + config.port)); + console.log(chalk.green('Database:\t\t\t' + config.db.uri)); + console.log('--'); + + process.on('uncaughtException', function (err) { + console.error((new Date()).toUTCString() + ' uncaughtException:', err.message); + console.error(err.stack); + process.exit(1); + }); } - - -// Bootstrap passport config -require('./config/passport')(); - -// Start the app by listening on -app.listen(config.port); - -// Expose app -exports = module.exports = app; - -// Logging initialization -console.log('--'); -console.log(chalk.green('Environment:\t\t\t' + process.env.NODE_ENV)); -console.log(chalk.green('Port:\t\t\t\t' + config.port)); -console.log(chalk.green('Database:\t\t\t' + config.db.uri)); -console.log('--'); - -process.on('uncaughtException', function (err) { - console.error((new Date()).toUTCString() + ' uncaughtException:', err.message); - console.error(err.stack); - process.exit(1); -}); diff --git a/start.js b/start.js new file mode 100644 index 00000000..83ecc986 --- /dev/null +++ b/start.js @@ -0,0 +1,16 @@ +var fs = require('fs'), + setup = require('./scripts/setup'), + server = require('./server'); + +//Set this to infinity to increase server capacity +require('events').EventEmitter.prototype._maxListeners = 0; + + +//Run setup script if no .env file is detected +if(process.stdout.isTTY) { + setup.checkENVAndRunSetup(function() { + server.bootstrap(); + }); +} else { + server.bootstrap(); +} From 39125972c3c8e589802319db93387ca2855f2281 Mon Sep 17 00:00:00 2001 From: David Baldwynn Date: Tue, 21 Nov 2017 13:12:34 -0800 Subject: [PATCH 10/17] removed user and pass from mongoose options --- config/env/all.js | 2 -- config/env/development.js | 2 -- config/env/test.js | 2 -- server.js | 7 ++++++- 4 files changed, 6 insertions(+), 7 deletions(-) diff --git a/config/env/all.js b/config/env/all.js index 35456020..4f92ada1 100755 --- a/config/env/all.js +++ b/config/env/all.js @@ -10,8 +10,6 @@ module.exports = { db: { uri: process.env.MONGOLAB_URI || process.env.MONGODB_URI || 'mongodb://'+ (process.env.DB_PORT_27017_TCP_ADDR || '127.0.0.1') + '/mean', options: { - user: '', - pass: '', useMongoClient: true } }, diff --git a/config/env/development.js b/config/env/development.js index 202ea79a..56e175a5 100755 --- a/config/env/development.js +++ b/config/env/development.js @@ -6,8 +6,6 @@ module.exports = { db: { uri: process.env.MONGODB_URI || 'mongodb://'+( process.env.DB_PORT_27017_TCP_ADDR || '127.0.0.1') +'/mean', options: { - user: '', - pass: '', useMongoClient: true } }, diff --git a/config/env/test.js b/config/env/test.js index fceeb365..433ffa13 100755 --- a/config/env/test.js +++ b/config/env/test.js @@ -5,8 +5,6 @@ module.exports = { db: { uri: 'mongodb://localhost/mean-test', options: { - user: '', - pass: '', useMongoClient: true } }, diff --git a/server.js b/server.js index e221d055..10b7e936 100755 --- a/server.js +++ b/server.js @@ -11,7 +11,7 @@ var mongoose = require('mongoose'), * Main application entry file. * Please note that the order of loading is important. */ -module.exports.bootstrap = function() { +var bootstrap = module.exports.bootstrap = function() { //Don't check .env file if we are in travis-ci if(!process.env.TRAVIS) { require('dotenv').config({path: './.env'}); @@ -82,3 +82,8 @@ module.exports.bootstrap = function() { process.exit(1); }); } + +// To maintain backwards compatibility, run bootstrap when called as a file +if(require.main === module) { + bootstrap(); +} \ No newline at end of file From 7c6dc90592be18be574c9e6fef7fa38f94438a01 Mon Sep 17 00:00:00 2001 From: David Baldwynn Date: Tue, 21 Nov 2017 13:33:25 -0800 Subject: [PATCH 11/17] added ASCII art for setup script --- scripts/setup.js | 11 ++++++----- scripts/setup_constants.js | 8 ++++++++ 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/scripts/setup.js b/scripts/setup.js index 4eeec3cf..84c0e837 100644 --- a/scripts/setup.js +++ b/scripts/setup.js @@ -142,9 +142,10 @@ var createENVFile = function(cb) { } var checkENVAndRunSetup = function(cb) { + console.log(chalk.green(constants.asciiArt)); if(require.main === module){ - console.log(chalk.green('\n\nWelcome to TellForm\'s Setup Tool')); - console.log(chalk.green('Follow the prompts to begin.\n---------------------------------------------------------------------\n\n')); + console.log(chalk.green('Welcome to TellForm\'s Setup Tool')); + console.log(chalk.green('Follow the prompts to begin.\n-------------------------------------------\n\n')); } if(fs.existsSync('./\.env') && require.main === module) { @@ -159,9 +160,9 @@ var checkENVAndRunSetup = function(cb) { } else { if(require.main !== module){ - console.log(chalk.green('\nWelcome to TellForm!\n')); - console.log(chalk.green('The following prompts will help you configure your TellForm instance for your needs')); - console.log(chalk.green('These prompts won\'t show up again after the initial setup.\n---------------------------------------------------------------------\n\n')); + console.log(chalk.green('Welcome to TellForm\'s Initial Setup\n')); + console.log(chalk.green('The following prompts will help you properly configure your TellForm instance.')); + console.log(chalk.green('If you want to run this tool after your inital setup, run `node scripts/setup.js`.\n---------------------------------------------------------------------\n\n')); } createENVFile(); } diff --git a/scripts/setup_constants.js b/scripts/setup_constants.js index 235350c9..29397906 100644 --- a/scripts/setup_constants.js +++ b/scripts/setup_constants.js @@ -16,6 +16,14 @@ var validateEmail = createRegexValidator(constants.regex.email, 'Please enter a var validateUsername = createRegexValidator(constants.regex.username, 'Usernames can only contain alphanumeric characters and \'-\''); module.exports = { + asciiArt: " _____ _ _______ \n" + + " |_ _| | | | ___| \n" + + " | | ___| | | |_ ___ _ __ _ __ ___ \n" + + " | |/ _ \\ | | _/ _ \\| '__| '_ ` _ \\ \n" + + " | | __/ | | || (_) | | | | | | | |\n" + + " \\_/\\___|_|_\\_| \\___/|_| |_| |_| |_|\n", + + replaceENVQuestion: { type: 'confirm', name: 'replaceENVFile', From 188c48b26cc694431165e76a3d83f9f58a915f7a Mon Sep 17 00:00:00 2001 From: David Baldwynn Date: Tue, 21 Nov 2017 15:33:07 -0800 Subject: [PATCH 12/17] fixed password issue with setup script admin creation --- scripts/create_admin.js | 2 +- scripts/setup.js | 31 +++++++++++++++++++++++-------- 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/scripts/create_admin.js b/scripts/create_admin.js index 6d5ee5c6..1e224834 100644 --- a/scripts/create_admin.js +++ b/scripts/create_admin.js @@ -23,7 +23,7 @@ exports.run = function(app, db, cb) { } if(!user){ - newUser.save(function (userErr) { + user.save(function (userErr) { if (userErr) { return cb(userErr); } diff --git a/scripts/setup.js b/scripts/setup.js index 84c0e837..e91f29fa 100644 --- a/scripts/setup.js +++ b/scripts/setup.js @@ -66,7 +66,6 @@ var createOrUpdateAdminUser = function(username, email, password, cb){ lastName: 'Account', username: username, email: email, - password: pass, provider: 'local', roles: ['admin', 'user'] } @@ -77,22 +76,38 @@ var createOrUpdateAdminUser = function(username, email, password, cb){ setDefaultsOnInsert: true } - User.findOneAndUpdate({ username: username, email: email }, updateObj, options, function (err, result) { + User.findOneAndUpdate({ username: username }, updateObj, options, function (err, user) { if (err) { + delete pass; + delete email; + delete username; return cb(err); } - if(!result){ + if(!user){ + delete pass; + delete email; + delete username; return cb(new Error('Admin User could not be created')); } - delete pass; - delete email; - delete username; + user.password = password + user.save(function(err) { + if(err){ + delete pass; + delete email; + delete username; + return cb(err); + } - console.log(chalk.green('Successfully created user')); + delete pass; + delete email; + delete username; - cb(); + console.log(chalk.green('Successfully created user')); + + cb(); + }); }); } From 4d4bb65298626b423558fed58e99b269c1e3c115 Mon Sep 17 00:00:00 2001 From: David Baldwynn Date: Tue, 21 Nov 2017 15:49:58 -0800 Subject: [PATCH 13/17] disabled create_admin script in test mode --- app/tests/form.server.model.test.js | 4 ++-- app/tests/form.server.routes.test.js | 6 +++--- app/tests/form_submission.model.test.js | 2 +- app/tests/user.server.model.test.js | 4 ++-- app/tests/user.server.routes.test.js | 2 +- server.js | 12 ++++++------ start.js | 7 +++---- 7 files changed, 18 insertions(+), 19 deletions(-) diff --git a/app/tests/form.server.model.test.js b/app/tests/form.server.model.test.js index 573e631b..aa3a69ed 100644 --- a/app/tests/form.server.model.test.js +++ b/app/tests/form.server.model.test.js @@ -7,8 +7,8 @@ require('../../server.js'); */ var should = require('should'), mongoose = require('mongoose'), - User = mongoose.model('User'), - Form = mongoose.model('Form'); + User = require('../models/user.server.model.js'), + Form = require('../models/form.server.model.js'); /** * Globals diff --git a/app/tests/form.server.routes.test.js b/app/tests/form.server.routes.test.js index a4c16889..c2dc0e0f 100644 --- a/app/tests/form.server.routes.test.js +++ b/app/tests/form.server.routes.test.js @@ -6,10 +6,10 @@ var should = require('should'), request = require('supertest'), Session = require('supertest-session'), mongoose = require('mongoose'), - User = mongoose.model('User'), - Form = mongoose.model('Form'), + User = require('../models/user.server.model.js'), + Form = require('../models/form.server.model.js'), + FormSubmission = require('../models/form_submission.server.model.js'), Field = mongoose.model('Field'), - FormSubmission = mongoose.model('FormSubmission'), async = require('async'); /** diff --git a/app/tests/form_submission.model.test.js b/app/tests/form_submission.model.test.js index b857e9a9..912b23b1 100644 --- a/app/tests/form_submission.model.test.js +++ b/app/tests/form_submission.model.test.js @@ -11,7 +11,7 @@ var should = require('should'), _ = require('lodash'), async = require('async'), config = require('../../config/config'), - FormSubmission = mongoose.model('FormSubmission'); + FormSubmission = require('../models/form_submission.server.model.js'); var exampleDemo = { address: '880-9650 Velit. St.', diff --git a/app/tests/user.server.model.test.js b/app/tests/user.server.model.test.js index 5a7cb9ad..b15fb3b1 100755 --- a/app/tests/user.server.model.test.js +++ b/app/tests/user.server.model.test.js @@ -5,8 +5,8 @@ */ var should = require('should'), mongoose = require('mongoose'), - User = mongoose.model('User'); - + User = require('../models/user.server.model.js'); + /** * Globals */ diff --git a/app/tests/user.server.routes.test.js b/app/tests/user.server.routes.test.js index 7198d449..7ed5e006 100644 --- a/app/tests/user.server.routes.test.js +++ b/app/tests/user.server.routes.test.js @@ -4,7 +4,7 @@ var should = require('should'), app = require('../../server'), Session = require('supertest-session'), mongoose = require('mongoose'), - User = mongoose.model('User'), + User = require('../models/user.server.model.js'), config = require('../../config/config'), tmpUser = mongoose.model(config.tempUserCollection), async = require('async'); diff --git a/server.js b/server.js index 10b7e936..235ae562 100755 --- a/server.js +++ b/server.js @@ -11,7 +11,7 @@ var mongoose = require('mongoose'), * Main application entry file. * Please note that the order of loading is important. */ -var bootstrap = module.exports.bootstrap = function() { +var bootstrap = function() { //Don't check .env file if we are in travis-ci if(!process.env.TRAVIS) { require('dotenv').config({path: './.env'}); @@ -49,7 +49,7 @@ var bootstrap = module.exports.bootstrap = function() { var app = require('./config/express')(db); //Create admin account - if (process.env.CREATE_ADMIN_ACCOUNT === 'TRUE') { + if (process.env.CREATE_ADMIN_ACCOUNT === 'TRUE' && process.env.NODE_ENV !== 'test') { var create_admin = require('./scripts/create_admin'); create_admin.run(app, db, function(err){ @@ -59,16 +59,12 @@ var bootstrap = module.exports.bootstrap = function() { }); } - // Bootstrap passport config require('./config/passport')(); // Start the app by listening on app.listen(config.port); - // Expose app - exports = module.exports = app; - // Logging initialization console.log('--'); console.log(chalk.green('Environment:\t\t\t' + process.env.NODE_ENV)); @@ -81,8 +77,12 @@ var bootstrap = module.exports.bootstrap = function() { console.error(err.stack); process.exit(1); }); + + return app; } +module.exports = bootstrap(); + // To maintain backwards compatibility, run bootstrap when called as a file if(require.main === module) { bootstrap(); diff --git a/start.js b/start.js index 83ecc986..d1458d0e 100644 --- a/start.js +++ b/start.js @@ -1,6 +1,5 @@ var fs = require('fs'), - setup = require('./scripts/setup'), - server = require('./server'); + setup = require('./scripts/setup'); //Set this to infinity to increase server capacity require('events').EventEmitter.prototype._maxListeners = 0; @@ -9,8 +8,8 @@ require('events').EventEmitter.prototype._maxListeners = 0; //Run setup script if no .env file is detected if(process.stdout.isTTY) { setup.checkENVAndRunSetup(function() { - server.bootstrap(); + require('./server'); }); } else { - server.bootstrap(); + require('./server'); } From 1163debc3376c29403c54b63cfd40244b08bb26d Mon Sep 17 00:00:00 2001 From: David Baldwynn Date: Tue, 21 Nov 2017 15:51:24 -0800 Subject: [PATCH 14/17] made all server models exportable --- app/models/form.server.model.js | 1 + app/models/form_submission.server.model.js | 2 ++ app/models/user.server.model.js | 4 +++- 3 files changed, 6 insertions(+), 1 deletion(-) diff --git a/app/models/form.server.model.js b/app/models/form.server.model.js index 4aa58f89..d22fc270 100644 --- a/app/models/form.server.model.js +++ b/app/models/form.server.model.js @@ -265,3 +265,4 @@ FormSchema.index({created: 1}); mongoose.model('Form', FormSchema); +module.exports = mongoose.model('Form'); \ No newline at end of file diff --git a/app/models/form_submission.server.model.js b/app/models/form_submission.server.model.js index 8c674818..e9620d84 100644 --- a/app/models/form_submission.server.model.js +++ b/app/models/form_submission.server.model.js @@ -81,3 +81,5 @@ FormSubmissionSchema.plugin(timeStampPlugin, { }); mongoose.model('FormSubmission', FormSubmissionSchema); + +module.exports = mongoose.model('FormSubmission'); \ No newline at end of file diff --git a/app/models/user.server.model.js b/app/models/user.server.model.js index 50872eec..4d4208ab 100755 --- a/app/models/user.server.model.js +++ b/app/models/user.server.model.js @@ -165,4 +165,6 @@ UserSchema.methods.isAdmin = function() { return false; }; -module.exports = mongoose.model('User', UserSchema); +mongoose.model('User', UserSchema); + +module.exports = mongoose.model('User'); \ No newline at end of file From 6213ec227d7b9223c0c7b6e529772297b0ff44a5 Mon Sep 17 00:00:00 2001 From: David Baldwynn Date: Tue, 21 Nov 2017 15:56:10 -0800 Subject: [PATCH 15/17] updated field-icon test --- .../tests/unit/directives/field-icon.client.directive.test.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/public/modules/forms/tests/unit/directives/field-icon.client.directive.test.js b/public/modules/forms/tests/unit/directives/field-icon.client.directive.test.js index 7dfc6823..728e16db 100644 --- a/public/modules/forms/tests/unit/directives/field-icon.client.directive.test.js +++ b/public/modules/forms/tests/unit/directives/field-icon.client.directive.test.js @@ -97,7 +97,9 @@ currType = FormFields.types[i]; currClass = faClasses[currType.name]; - var element = $compile('')(scope); + scope.currType = currType; + + var element = $compile('')(scope); scope.$digest(); expect(currClass).toBeDefined(); From 784ae9011eb9b20b1deab3d2ab724c6a539e49d4 Mon Sep 17 00:00:00 2001 From: David Baldwynn Date: Tue, 21 Nov 2017 17:26:52 -0800 Subject: [PATCH 16/17] fixed security flaw with removeSensitiveModelData --- app/controllers/forms.server.controller.js | 17 +- app/controllers/helpers.server.controller.js | 32 +- .../users.authentication.server.controller.js | 2 +- .../users/users.profile.server.controller.js | 6 +- config/env/all.js | 4 +- scripts/create_admin.js | 3 - selenium/config.json | 4 +- selenium/package.json | 3 +- selenium/test/loginAndChangeProfile.js | 65 +-- selenium_config.json | 23 + selenium_test/loginAndChangeProfile.js | 398 ++++++++++++++++++ selenium_test/loginAndCreateForm.spec.js | 363 ++++++++++++++++ server.js | 4 +- 13 files changed, 829 insertions(+), 95 deletions(-) create mode 100644 selenium_config.json create mode 100644 selenium_test/loginAndChangeProfile.js create mode 100644 selenium_test/loginAndCreateForm.spec.js diff --git a/app/controllers/forms.server.controller.js b/app/controllers/forms.server.controller.js index 8beb8c3e..f3092d9d 100644 --- a/app/controllers/forms.server.controller.js +++ b/app/controllers/forms.server.controller.js @@ -308,7 +308,7 @@ exports.create = function(req, res) { }); } - createdForm = helpers.removeSensitiveModelData('private_form', createdForm); + createdForm = helpers.removeSensitiveModelData('private_form', createdForm.toJSON()); return res.json(createdForm); }); }; @@ -326,13 +326,8 @@ exports.read = function(req, res) { }); } - var newForm = req.form.toJSON(); - - if(newForm.admin === req.user._id){ - return res.json(newForm); - } - - newForm = helpers.removeSensitiveModelData('private_form', newForm); + newForm = helpers.removeSensitiveModelData('private_form', req.form.toJSON()); + return res.json(newForm); } }; @@ -348,7 +343,7 @@ var readForRender = exports.readForRender = function(req, res) { }); } - newForm = helpers.removeSensitiveModelData('public_form', newForm); + newForm = helpers.removeSensitiveModelData('public_form', newForm.toJSON()); if(newForm.startPage && !newForm.startPage.showStart){ delete newForm.startPage; @@ -413,7 +408,7 @@ exports.update = function(req, res) { message: errorHandler.getErrorMessage(err) }); } else { - savedForm = helpers.removeSensitiveModelData('private_form', savedForm); + savedForm = helpers.removeSensitiveModelData('private_form', savedForm.toJSON()); res.json(savedForm); } }); @@ -526,7 +521,7 @@ exports.formByID = function(req, res, next, id) { } else { //Remove sensitive information from User object - req.form = helpers.removeSensitiveModelData('private_form', form); + req.form = helpers.removeSensitiveModelData('private_form', form.toJSON()); return next(); } }); diff --git a/app/controllers/helpers.server.controller.js b/app/controllers/helpers.server.controller.js index c25f6682..9b4c3792 100644 --- a/app/controllers/helpers.server.controller.js +++ b/app/controllers/helpers.server.controller.js @@ -1,6 +1,7 @@ 'use strict'; const constants = require('../libs/constants'); +const _ = require('lodash'); module.exports = { removeKeysFromDict: function(dict, keys){ @@ -10,29 +11,18 @@ module.exports = { delete dict[curr_key]; } } + return dict; }, - removeSensitiveModelData: function(type, object){ - switch(type){ - case 'private_form': - this.removeKeysFromDict(object, constants.privateFields[type]); - if(object.admin){ - this.removeKeysFromDict(object.admin, constants.privateFields.private_user); - } - break; + removeSensitiveModelData: function(type, actual_object){ + var object = _.cloneDeep(actual_object); - case 'public_form': - this.removeKeysFromDict(object, constants.privateFields[type]); - if(object.admin){ - this.removeKeysFromDict(object.admin, constants.privateFields.public_user); - } - break; - - default: - if(constants.privateFields.hasOwnProperty(type)){ - this.removeKeysFromDict(object, constants.privateFields[type]); - } - break; - } + if(constants.privateFields.hasOwnProperty(type)) { + object = this.removeKeysFromDict(object, constants.privateFields[type]); + } + if(object.admin){ + object.admin = this.removeKeysFromDict(object.admin, constants.privateFields.private_user); + } + debugger; return object; } diff --git a/app/controllers/users/users.authentication.server.controller.js b/app/controllers/users/users.authentication.server.controller.js index 852c20c7..4bb853f5 100755 --- a/app/controllers/users/users.authentication.server.controller.js +++ b/app/controllers/users/users.authentication.server.controller.js @@ -180,7 +180,7 @@ exports.signin = function(req, res, next) { res.cookie('langCookie', user.language, { maxAge: 90000, httpOnly: true }); - user = helpers.removeSensitiveModelData('private_user', user); + user = helpers.removeSensitiveModelData('private_user', user.toJSON()); return res.json(user); }); } diff --git a/app/controllers/users/users.profile.server.controller.js b/app/controllers/users/users.profile.server.controller.js index d6176f30..b03bcbc0 100755 --- a/app/controllers/users/users.profile.server.controller.js +++ b/app/controllers/users/users.profile.server.controller.js @@ -18,6 +18,8 @@ exports.update = function(req, res) { // To improve security we remove the roles from the req.body object delete req.body.roles; + debugger; + // Merge existing user user = _.extend(user, req.body); user.updated = Date.now(); @@ -32,7 +34,7 @@ exports.update = function(req, res) { if (err) { res.status(500).send(loginErr); } else { - user = helpers.removeSensitiveModelData('private_user', user); + user = helpers.removeSensitiveModelData('private_user', user.toJSON()); res.json(user); } }); @@ -44,7 +46,7 @@ exports.update = function(req, res) { * Send User */ exports.getUser = function(req, res) { - var user = helpers.removeSensitiveModelData('private_user', req.user); + var user = helpers.removeSensitiveModelData('private_user', req.user.toJSON()); return res.json(user); }; diff --git a/config/env/all.js b/config/env/all.js index 8f0a087e..ce66a168 100755 --- a/config/env/all.js +++ b/config/env/all.js @@ -15,8 +15,8 @@ module.exports = { }, admin: { email: process.env.ADMIN_EMAIL || 'admin@admin.com', - username: process.env.ADMIN_USERNAME || 'admin', - password: process.env.ADMIN_PASSWORD || 'admin', + username: process.env.ADMIN_USERNAME || 'root', + password: process.env.ADMIN_PASSWORD || 'root', roles: ['user', 'admin'] }, diff --git a/scripts/create_admin.js b/scripts/create_admin.js index ea3b1469..1ce9a76c 100644 --- a/scripts/create_admin.js +++ b/scripts/create_admin.js @@ -7,9 +7,6 @@ exports.run = function(app, db, cb) { var User = mongoose.model('User'); var username = config.admin.username; - - console.log('username: ' + config.admin.username); - console.log('password: ' + config.admin.password); var newUserObj = { firstName: 'Admin', diff --git a/selenium/config.json b/selenium/config.json index f382f84d..6a55c28d 100644 --- a/selenium/config.json +++ b/selenium/config.json @@ -5,8 +5,8 @@ "browsers": "chrome" }, "vars": { - "LoginUsername": "admin", - "LoginPassword": "admin", + "LoginUsername": "root", + "LoginPassword": "root", "ShortTextTitle": "SeleniumShortText", "Profile_NewFirstName": "SeleniumUser_FirstName", "Profile_NewLastName": "SeleniumUser_LastName", diff --git a/selenium/package.json b/selenium/package.json index 03727c90..ae2c41f2 100644 --- a/selenium/package.json +++ b/selenium/package.json @@ -12,8 +12,7 @@ "resemblejs-node": "1.0.0", "selenium-standalone": "6.x.x" }, - "devDependencies": { - }, + "devDependencies": {}, "scripts": { "installdriver": "./node_modules/.bin/selenium-standalone install --drivers.firefox.baseURL=http://npm.taobao.org/mirrors/geckodriver --baseURL=http://npm.taobao.org/mirrors/selenium --drivers.chrome.baseURL=http://npm.taobao.org/mirrors/chromedriver --drivers.ie.baseURL=http://npm.taobao.org/mirrors/selenium", "server": "./node_modules/.bin/selenium-standalone start", diff --git a/selenium/test/loginAndChangeProfile.js b/selenium/test/loginAndChangeProfile.js index 83e3fda8..fdf5c0d9 100644 --- a/selenium/test/loginAndChangeProfile.js +++ b/selenium/test/loginAndChangeProfile.js @@ -49,13 +49,8 @@ module.exports = function(){ }); it('click: Sign in ( button, 174, 18, 0 )', async function(){ - await driver.sleep(300).wait('button', 30000) - .sleep(300).mouseMove(174, 18).click(0); - }); - - it('mouseDown: My Settings ( a > span.ng-binding, 41.375, 12, 0 )', async function(){ - await driver.sleep(300).wait('a > span.ng-binding', 30000) - .sleep(300).mouseMove(41.375, 12).mouseDown(0); + await driver.sleep(300).wait('button.btn-signup', 30000) + .sleep(300).click(); }); it('expect: displayed, div.new-button, equal, true', async function(){ @@ -72,33 +67,28 @@ module.exports = function(){ .should.equal(_(true)); }); - it('click: My Settings ( a.dropdown-toggle, 95, 29, 0 )', async function(){ + it('click: My Settings ( a.dropdown-toggle )', async function(){ await driver.sleep(300).wait('a.dropdown-toggle', 30000) - .sleep(300).mouseMove(95, 29).click(0); + .sleep(300).click(); }); - it('× expect: display, li:nth-child(1) > a.ng-binding, equal, true', async function(){ - await driver.sleep(300).wait('li:nth-child(1) > a.ng-binding', 30000) + it('× expect: display, ul.dropdown-menu > li:nth-child(1) > a.ng-binding, equal, true', async function(){ + await driver.sleep(300).wait('ul.dropdown-menu > li:nth-child(1) > a.ng-binding', 30000) .displayed() .should.not.be.a('error') .should.equal(_(true)); }); - it('× expect: display, li:nth-child(3) > a.ng-binding, equal, true', async function(){ - await driver.sleep(300).wait('li:nth-child(3) > a.ng-binding', 30000) + it('× expect: display, ul.dropdown-menu > li:nth-child(3) > a.ng-binding, equal, true', async function(){ + await driver.sleep(300).wait('ul.dropdown-menu > li:nth-child(3) > a.ng-binding', 30000) .displayed() .should.not.be.a('error') .should.equal(_(true)); }); - it('mouseDown: Edit Profile ( li:nth-child(1) > a.ng-binding, 54.59375, 14, 0 )', async function(){ - await driver.sleep(300).wait('li:nth-child(1) > a.ng-binding', 30000) - .sleep(300).mouseMove(54.59375, 14).mouseDown(0); - }); - - it('click: Edit Profile ( li:nth-child(1) > a.ng-binding, 52, 13, 0 )', async function(){ - await driver.sleep(300).wait('li:nth-child(1) > a.ng-binding', 30000) - .sleep(300).mouseMove(52, 13).click(0); + it('click: Edit Profile ( ul.dropdown-menu > li:nth-child(1) > a.ng-binding )', async function(){ + await driver.sleep(300).wait('ul.dropdown-menu > li:nth-child(1) > a.ng-binding', 30000) + .sleep(300).click(); }); it('waitBody: ', async function(){ @@ -117,14 +107,9 @@ module.exports = function(){ .val(_(`{{Profile_NewLastName}}`)); }); - it('× click: Save Changes ( button.btn-signup, 95, 10, 0 )', async function(){ + it('× click: Save Changes ( button.btn-signup )', async function(){ await driver.sleep(300).wait('button.btn-signup', 30000) - .sleep(300).mouseMove(95, 10).click(0); - }); - - it('× mouseUp: Profile saved succes... ( section.row, 333, 78, 0 )', async function(){ - await driver.sleep(300).wait('section.row', 30000) - .sleep(300).mouseMove(333, 78).mouseMove(333, 78).mouseUp(0); + .sleep(300).click(); }); it('× expect: displayed, div.text-success, equal, true', async function(){ @@ -157,7 +142,7 @@ module.exports = function(){ it('× click: Save Changes ( button.btn-signup, 95, 10, 0 )', async function(){ await driver.sleep(300).wait('button.btn-signup', 30000) - .sleep(300).mouseMove(95, 10).click(0); + .sleep(300).click(); }); it('× expect: displayed, .text-danger, notEqual, true', async function(){ @@ -174,13 +159,9 @@ module.exports = function(){ .val(_(`{{Profile_NewInvalidEmail}}`)); }); - it('× click: Save Changes ( button.btn-signup, 90, 16, 0 )', async function(){ + it('× click: Save Changes ( button.btn-signup )', async function(){ await driver.sleep(300).wait('button.btn-signup', 30000) - .sleep(300).mouseMove(90, 16).click(0); - }); - - it('url: http://localhost:5000/#!/settings/profile', async function(){ - await driver.url(_(`http://localhost:5000/#!/settings/profile`)); + .sleep(300).click(); }); it('url: http://localhost:5000/#!/settings/profile', async function(){ @@ -193,20 +174,6 @@ module.exports = function(){ }); }); - it('scrollTo: 0, 114', async function(){ - await driver.scrollTo(0, 114); - }); - - it('click: Email ( div:nth-child(8) > div.field-title, 352, 0, 0 )', async function(){ - await driver.sleep(300).wait('div:nth-child(8) > div.field-title', 30000) - .sleep(300).mouseMove(352, 0).click(0); - }); - - it('click: Email ( //h4[text()="Email"], 323, 5, 0 )', async function(){ - await driver.sleep(300).wait('//h4[text()="Email"]', 30000) - .sleep(300).mouseMove(323, 5).click(0); - }); - it('expect: text, #email, notEqual, {{Profile_NewInvalidEmail}}', async function(){ await driver.sleep(300).wait('#email', 300) .text() diff --git a/selenium_config.json b/selenium_config.json new file mode 100644 index 00000000..6a55c28d --- /dev/null +++ b/selenium_config.json @@ -0,0 +1,23 @@ +{ + "webdriver": { + "host": "127.0.0.1", + "port": "4444", + "browsers": "chrome" + }, + "vars": { + "LoginUsername": "root", + "LoginPassword": "root", + "ShortTextTitle": "SeleniumShortText", + "Profile_NewFirstName": "SeleniumUser_FirstName", + "Profile_NewLastName": "SeleniumUser_LastName", + "Profile_OldFirstName": "Admin", + "Profile_OldLastName": "Account", + "Profile_NewInvalidEmail": "SeleniumInvalidEmail" + }, + "recorder": { + "pathAttrs": "data-id,data-name,type,data-type,role,data-role,data-value", + "attrValueBlack": "", + "classValueBlack": "", + "hideBeforeExpect": "" + } +} \ No newline at end of file diff --git a/selenium_test/loginAndChangeProfile.js b/selenium_test/loginAndChangeProfile.js new file mode 100644 index 00000000..f2282dfe --- /dev/null +++ b/selenium_test/loginAndChangeProfile.js @@ -0,0 +1,398 @@ +const fs = require('fs'); +const path = require('path'); +const chai = require("chai"); +const should = chai.should(); +const JWebDriver = require('jwebdriver'); +chai.use(JWebDriver.chaiSupportChainPromise); +const resemble = require('resemblejs-node'); +resemble.outputSettings({ + errorType: 'flatDifferenceIntensity' +}); + +const rootPath = getRootPath(); + +module.exports = function(){ + + let driver, testVars; + + before(function(){ + let self = this; + driver = self.driver; + testVars = self.testVars; + }); + + it('url: http://localhost:5000', async function(){ + await driver.url(_(`http://localhost:5000`)); + }); + + it('waitBody: ', async function(){ + await driver.sleep(500).wait('body', 30000).html().then(function(code){ + isPageError(code).should.be.false; + }); + }); + + it('insertVar: username ( #username, {{LoginUsername}} )', async function(){ + await driver.sleep(300).wait('#username', 30000) + .val(_(`{{LoginUsername}}`)); + }); + + it('insertVar: password ( #password, {{LoginUsername}} )', async function(){ + await driver.sleep(300).wait('#password', 30000) + .val(_(`{{LoginUsername}}`)); + }); + + it('expect: displayed, .btn-signup, equal, true', async function(){ + await driver.sleep(300).wait('.btn-signup', 30000) + .displayed() + .should.not.be.a('error') + .should.equal(_(true)); + }); + + it('click: Sign in ( button, 174, 18, 0 )', async function(){ + await driver.sleep(300).wait('button.btn-signup', 30000) + .sleep(300).click(); + }); + + it('expect: displayed, div.new-button, equal, true', async function(){ + await driver.sleep(300).wait('div.new-button', 30000) + .displayed() + .should.not.be.a('error') + .should.equal(_(true)); + }); + + it('expect: displayed, a.dropdown-toggle, equal, true', async function(){ + await driver.sleep(300).wait('a.dropdown-toggle', 30000) + .displayed() + .should.not.be.a('error') + .should.equal(_(true)); + }); + + it('click: My Settings ( a.dropdown-toggle )', async function(){ + await driver.sleep(300).wait('a.dropdown-toggle', 30000) + .sleep(300).click(); + }); + + it('× expect: display, li:nth-child(1) > a.ng-binding, equal, true', async function(){ + await driver.sleep(300).wait('li:nth-child(1) > a.ng-binding', 30000) + .displayed() + .should.not.be.a('error') + .should.equal(_(true)); + }); + + it('× expect: display, li:nth-child(3) > a.ng-binding, equal, true', async function(){ + await driver.sleep(300).wait('li:nth-child(3) > a.ng-binding', 30000) + .displayed() + .should.not.be.a('error') + .should.equal(_(true)); + }); + + it('click: Edit Profile ( li:nth-child(1) > a.ng-binding)', async function(){ + await driver.sleep(300).wait('li:nth-child(1) > a.ng-binding', 30000) + .sleep(300).mouseMove(54.59375, 14).mouseDown(0); + }); + + it('click: Edit Profile ( li:nth-child(1) > a.ng-binding, 52, 13, 0 )', async function(){ + await driver.sleep(300).wait('li:nth-child(1) > a.ng-binding', 30000) + .sleep(300).mouseMove(52, 13).click(0); + }); + + it('waitBody: ', async function(){ + await driver.sleep(500).wait('body', 30000).html().then(function(code){ + isPageError(code).should.be.false; + }); + }); + + it('× insertVar: firstName ( #firstName, {{Profile_NewFirstName}} )', async function(){ + await driver.sleep(300).wait('#firstName', 30000) + .val(_(`{{Profile_NewFirstName}}`)); + }); + + it('× insertVar: lastName ( #lastName, {{Profile_NewLastName}} )', async function(){ + await driver.sleep(300).wait('#lastName', 30000) + .val(_(`{{Profile_NewLastName}}`)); + }); + + it('× click: Save Changes ( button.btn-signup, 95, 10, 0 )', async function(){ + await driver.sleep(300).wait('button.btn-signup', 30000) + .sleep(300).mouseMove(95, 10).click(0); + }); + + it('× mouseUp: Profile saved succes... ( section.row, 333, 78, 0 )', async function(){ + await driver.sleep(300).wait('section.row', 30000) + .sleep(300).mouseMove(333, 78).mouseMove(333, 78).mouseUp(0); + }); + + it('× expect: displayed, div.text-success, equal, true', async function(){ + await driver.sleep(300).wait('div.text-success', 30000) + .displayed() + .should.not.be.a('error') + .should.equal(_(true)); + }); + + it('× expect: displayed, .text-danger, notEqual, true', async function(){ + await driver.sleep(300).wait('.text-danger', 300) + .displayed() + .should.not.be.a('error') + .should.not.equal(_(true)); + }); + + /* + ** Revert back to expected names + */ + + it('× insertVar: firstName ( #firstName, {{Profile_OldFirstName}} )', async function(){ + await driver.sleep(300).wait('#firstName', 30000) + .val(_(`{{Profile_OldFirstName}}`)); + }); + + it('× insertVar: lastName ( #lastName, {{Profile_OldLastName}} )', async function(){ + await driver.sleep(300).wait('#lastName', 30000) + .val(_(`{{Profile_OldLastName}}`)); + }); + + it('× click: Save Changes ( button.btn-signup, 95, 10, 0 )', async function(){ + await driver.sleep(300).wait('button.btn-signup', 30000) + .sleep(300).click(0); + }); + + it('× expect: displayed, .text-danger, notEqual, true', async function(){ + await driver.sleep(300).wait('.text-danger', 300) + .displayed() + .should.not.be.a('error') + .should.not.equal(_(true)); + }); + + + //Check that we can't save an invalid email + it('× insertVar: email ( #email, {{Profile_NewInvalidEmail}} )', async function(){ + await driver.sleep(300).wait('#email', 30000) + .val(_(`{{Profile_NewInvalidEmail}}`)); + }); + + it('× click: Save Changes ( button.btn-signup, 90, 16, 0 )', async function(){ + await driver.sleep(300).wait('button.btn-signup', 30000) + .sleep(300).click(0); + }); + + it('url: http://localhost:5000/#!/settings/profile', async function(){ + await driver.url(_(`http://localhost:5000/#!/settings/profile`)); + }); + + it('waitBody: ', async function(){ + await driver.sleep(500).wait('body', 30000).html().then(function(code){ + isPageError(code).should.be.false; + }); + }); + + it('click: Email ( div:nth-child(8) > div.field-title, 352, 0, 0 )', async function(){ + await driver.sleep(300).wait('div:nth-child(8) > div.field-title', 30000) + .sleep(300).click(0); + }); + + it('click: Email ( //h4[text()="Email"], 323, 5, 0 )', async function(){ + await driver.sleep(300).wait('//h4[text()="Email"]', 30000) + .sleep(300).click(0); + }); + + it('expect: text, #email, notEqual, {{Profile_NewInvalidEmail}}', async function(){ + await driver.sleep(300).wait('#email', 300) + .text() + .should.not.be.a('error') + .should.not.equal(_(`{{Profile_NewInvalidEmail}}`)); + }); + + + /* + ** Logout + */ + it('click: Signout ( //a[text()="Signout"], 31, 31, 0 )', async function(){ + await driver.sleep(300).wait('//a[text()="Signout"]', 30000) + .sleep(300).mouseMove(31, 31).click(0); + }); + + it('expect: displayed, button.btn-signup, equal, true', async function(){ + await driver.sleep(300).wait('button.btn-signup', 30000) + .displayed() + .should.not.be.a('error') + .should.equal(_(true)); + }); + + function _(str){ + if(typeof str === 'string'){ + return str.replace(/\{\{(.+?)\}\}/g, function(all, key){ + return testVars[key] || ''; + }); + } + else{ + return str; + } + } + +}; + +if(module.parent && /mocha\.js/.test(module.parent.id)){ + runThisSpec(); +} + +function runThisSpec(){ + // read config + let webdriver = process.env['webdriver'] || ''; + let proxy = process.env['wdproxy'] || ''; + let config = require(rootPath + '/config.json'); + let webdriverConfig = Object.assign({},config.webdriver); + let host = webdriverConfig.host; + let port = webdriverConfig.port || 4444; + let match = webdriver.match(/([^\:]+)(?:\:(\d+))?/); + if(match){ + host = match[1] || host; + port = match[2] || port; + } + let testVars = config.vars; + let browsers = webdriverConfig.browsers; + browsers = browsers.replace(/^\s+|\s+$/g, ''); + delete webdriverConfig.host; + delete webdriverConfig.port; + delete webdriverConfig.browsers; + + // read hosts + let hostsPath = rootPath + '/hosts'; + let hosts = ''; + if(fs.existsSync(hostsPath)){ + hosts = fs.readFileSync(hostsPath).toString(); + } + let specName = path.relative(rootPath, __filename).replace(/\\/g,'/').replace(/\.js$/,''); + + browsers.split(/\s*,\s*/).forEach(function(browserName){ + let caseName = specName + ' : ' + browserName; + + let browserInfo = browserName.split(' '); + browserName = browserInfo[0]; + let browserVersion = browserInfo[1]; + + describe(caseName, function(){ + + this.timeout(600000); + this.slow(1000); + + let driver; + before(function(){ + let self = this; + let driver = new JWebDriver({ + 'host': host, + 'port': port + }); + let sessionConfig = Object.assign({}, webdriverConfig, { + 'browserName': browserName, + 'version': browserVersion, + 'ie.ensureCleanSession': true, + 'chromeOptions': { + 'args': ['--enable-automation'] + } + }); + if(proxy){ + sessionConfig.proxy = { + 'proxyType': 'manual', + 'httpProxy': proxy, + 'sslProxy': proxy + } + } + else if(hosts){ + sessionConfig.hosts = hosts; + } + self.driver = driver.session(sessionConfig).maximize().config({ + pageloadTimeout: 30000, // page onload timeout + scriptTimeout: 5000, // sync script timeout + asyncScriptTimeout: 10000 // async script timeout + }); + self.testVars = testVars; + let casePath = path.dirname(caseName); + self.screenshotPath = rootPath + '/screenshots/' + casePath; + self.diffbasePath = rootPath + '/diffbase/' + casePath; + self.caseName = caseName.replace(/.*\//g, '').replace(/\s*[:\.\:\-\s]\s*/g, '_'); + mkdirs(self.screenshotPath); + mkdirs(self.diffbasePath); + self.stepId = 0; + return self.driver; + }); + + module.exports(); + + beforeEach(function(){ + let self = this; + self.stepId ++; + if(self.skipAll){ + self.skip(); + } + }); + + afterEach(async function(){ + let self = this; + let currentTest = self.currentTest; + let title = currentTest.title; + if(currentTest.state === 'failed' && /^(url|waitBody|switchWindow|switchFrame):/.test(title)){ + self.skipAll = true; + } + if(!/^(closeWindow):/.test(title)){ + let filepath = self.screenshotPath + '/' + self.caseName + '_' + self.stepId; + let driver = self.driver; + try{ + // catch error when get alert msg + await driver.getScreenshot(filepath + '.png'); + let url = await driver.url(); + let html = await driver.source(); + html = '\n' + html; + fs.writeFileSync(filepath + '.html', html); + let cookies = await driver.cookies(); + fs.writeFileSync(filepath + '.cookie', JSON.stringify(cookies)); + } + catch(e){} + } + }); + + after(function(){ + return this.driver.close(); + }); + + }); + }); +} + +function getRootPath(){ + let rootPath = path.resolve(__dirname); + while(rootPath){ + if(fs.existsSync(rootPath + '/config.json')){ + break; + } + rootPath = rootPath.substring(0, rootPath.lastIndexOf(path.sep)); + } + return rootPath; +} + +function mkdirs(dirname){ + if(fs.existsSync(dirname)){ + return true; + }else{ + if(mkdirs(path.dirname(dirname))){ + fs.mkdirSync(dirname); + return true; + } + } +} + +function callSpec(name){ + try{ + require(rootPath + '/' + name)(); + } + catch(e){ + console.log(e) + process.exit(1); + } +} + +function isPageError(code){ + return code == '' || / jscontent="errorCode" jstcache="\d+"|diagnoseConnectionAndRefresh|dnserror_unavailable_header|id="reportCertificateErrorRetry"|400 Bad Request|403 Forbidden|404 Not Found|500 Internal Server Error|502 Bad Gateway|503 Service Temporarily Unavailable|504 Gateway Time-out/i.test(code); +} + +function catchError(error){ + +} diff --git a/selenium_test/loginAndCreateForm.spec.js b/selenium_test/loginAndCreateForm.spec.js new file mode 100644 index 00000000..db307227 --- /dev/null +++ b/selenium_test/loginAndCreateForm.spec.js @@ -0,0 +1,363 @@ +const fs = require('fs'); +const path = require('path'); +const chai = require("chai"); +const should = chai.should(); +const JWebDriver = require('jwebdriver'); +chai.use(JWebDriver.chaiSupportChainPromise); +const resemble = require('resemblejs-node'); +resemble.outputSettings({ + errorType: 'flatDifferenceIntensity' +}); + +const rootPath = getRootPath(); + +module.exports = function(){ + + let driver, testVars; + + before(function(){ + let self = this; + driver = self.driver; + testVars = self.testVars; + }); + + it('url: http://localhost:5000', async function(){ + await driver.url(_(`http://localhost:5000`)); + }); + + it('waitBody: ', async function(){ + await driver.sleep(500).wait('body', 30000).html().then(function(code){ + isPageError(code).should.be.false; + }); + }); + + it('insertVar: username ( #username, {{LoginUsername}} )', async function(){ + await driver.sleep(300).wait('#username', 30000) + .val(_(`{{LoginUsername}}`)); + }); + + it('insertVar: password ( #password, {{LoginPassword}} )', async function(){ + await driver.sleep(300).wait('#password', 30000) + .val(_(`{{LoginPassword}}`)); + }); + + it('mouseUp: Sign in ( button, 375, 45, 0 )', async function(){ + await driver.sleep(300).wait('button', 30000) + .sleep(300).mouseMove(375, 45).mouseMove(375, 45).mouseUp(0); + }); + + it('expect: displayed, button.btn.btn-signup, equal, true', async function(){ + await driver.sleep(300).wait('button.btn.btn-signup', 30000) + .displayed() + .should.not.be.a('error') + .should.equal(_(true)); + }); + + it('click: Sign in ( button, 217, 21, 0 )', async function(){ + await driver.sleep(300).wait('button', 30000) + .sleep(300).mouseMove(217, 21).click(0); + }); + + it('expect: displayed, div.new-button, equal, true', async function(){ + await driver.sleep(500).wait('div.new-button', 30000) + .displayed() + .should.not.be.a('error') + .should.equal(_(true)); + }); + + it('expect: displayed, section.navbar.navbar-inverse, equal, true', async function(){ + await driver.sleep(300).wait('section.navbar.navbar-inverse', 30000) + .displayed() + .should.not.be.a('error') + .should.equal(_(true)); + }); + + it('click: h4.fa, 76, 96, 0', async function(){ + await driver.sleep(300).wait('h4.fa', 30000) + .sleep(300).mouseMove(76, 96).click(0); + }); + + it('sleep: 100', async function(){ + await driver.sleep(100); + }); + + it('expect: displayed, form[name="forms.createForm"], equal, true', async function(){ + await driver.sleep(300).wait('form[name="forms.createForm"]', 30000) + .displayed() + .should.not.be.a('error') + .should.equal(_(true)); + }); + + it('× click: title ( input[name="title"], 115, 12, 0 )', async function(){ + await driver.sleep(300).wait('input[name="title"]', 30000) + .sleep(300).mouseMove(115, 12).click(0); + }); + + it('× expect: attr, .form-item.create-new.new-form button.btn,disabled, equal, true', async function(){ + await driver.sleep(300).wait('.form-item.create-new.new-form button.btn', 30000) + .attr('disabled') + .should.not.be.a('error') + .should.equal(_(`true`)); + }); + + it('sendKeys: aeoaoe', async function(){ + await driver.sendKeys('aeoaoe'); + }); + + it('× expect: attr, .form-item.create-new.new-form button.btn,disabled, equal, undefined', async function(){ + await driver.sleep(300).wait('.form-item.create-new.new-form button.btn', 30000) + .attr('disabled') + .should.not.be.a('error') + .should.equal(_(null)); + }); + + it('× click: Create this TellForm ( button, 57, 16, 0 )', async function(){ + await driver.sleep(300).wait('button', 30000) + .sleep(300).mouseMove(57, 16).click(0); + }); + + it('× expect: displayed, i.status-light.status-light-on, equal, true', async function(){ + await driver.sleep(300).wait('i.status-light.status-light-on', 30000) + .displayed() + .should.not.be.a('error') + .should.equal(_(true)); + }); + + it('× expect: displayed, .btn.btn-danger > .fa.fa-trash-o, equal, true', async function(){ + await driver.sleep(300).wait('.btn.btn-danger > .fa.fa-trash-o', 30000) + .displayed() + .should.not.be.a('error') + .should.equal(_(true)); + }); + + it('× click: Create ( //a[text()="Create"], 163, 25, 2 )', async function(){ + await driver.sleep(300).wait('//a[text()="Create"]', 30000) + .sleep(300).mouseMove(163, 25).click(2); + }); + + it('× mouseUp: aeoaoe ( section.admin-form, 467, 53, 0 )', async function(){ + await driver.sleep(300).wait('section.admin-form', 30000) + .sleep(300).mouseMove(467, 53).mouseMove(467, 53).mouseUp(0); + }); + + it('× click: Short Text ( div:nth-child(1) > div.panel-default > div.panel-heading > span.ng-binding, 39, 4, 0 )', async function(){ + await driver.sleep(300).wait('div:nth-child(1) > div.panel-default > div.panel-heading > span.ng-binding', 30000) + .sleep(300).mouseMove(39, 4).click(0); + }); + + it('× insertVar: title ( input[name="title"], {{ShortTextTitle}} )', async function(){ + await driver.sleep(300).wait('input[name="title"]', 30000) + .val(_(`{{ShortTextTitle}}`)); + }); + + it('mouseUp: 1 SeleniumShortText... ( body, 740, 97, 0 )', async function(){ + await driver.sleep(300).wait('body', 30000) + .sleep(300).mouseMove(740, 97).mouseMove(740, 97).mouseUp(0); + }); + + it('× expect: text, field-directive .field-title h3, equal, 1 {{ShortTextTitle}}', async function(){ + await driver.sleep(300).wait('field-directive .field-title h3', 30000) + .text() + .should.not.be.a('error') + .should.equal(_(`1 {{ShortTextTitle}}`)); + }); + + it('× click: Save ( button.btn-signup, 33, 23, 0 )', async function(){ + await driver.sleep(300).wait('button.btn-signup', 30000) + .sleep(300).mouseMove(33, 23).click(0); + }); + + it('× click: Delete Form ( span.hidden-sm, 55, 8, 0 )', async function(){ + await driver.sleep(300).wait('span.hidden-sm', 30000) + .sleep(300).mouseMove(55, 8).click(0); + }); + + it('× dblClick: aeoaoe ( //strong[text()="aeoaoe"], 26, 4, 0 )', async function(){ + await driver.sleep(300).wait('//strong[text()="aeoaoe"]', 30000) + .sleep(300).mouseMove(26, 4).click(0).click(0); + }); + + it('× click: input.input-block, 399, 3, 0', async function(){ + await driver.sleep(300).wait('input.input-block', 30000) + .sleep(300).mouseMove(399, 3).click(0); + }); + + function _(str){ + if(typeof str === 'string'){ + return str.replace(/\{\{(.+?)\}\}/g, function(all, key){ + return testVars[key] || ''; + }); + } + else{ + return str; + } + } + +}; + +if(module.parent && /mocha\.js/.test(module.parent.id)){ + runThisSpec(); +} + +function runThisSpec(){ + // read config + let webdriver = process.env['webdriver'] || ''; + let proxy = process.env['wdproxy'] || ''; + let config = require(rootPath + '/config.json'); + let webdriverConfig = Object.assign({},config.webdriver); + let host = webdriverConfig.host; + let port = webdriverConfig.port || 4444; + let match = webdriver.match(/([^\:]+)(?:\:(\d+))?/); + if(match){ + host = match[1] || host; + port = match[2] || port; + } + let testVars = config.vars; + let browsers = webdriverConfig.browsers; + browsers = browsers.replace(/^\s+|\s+$/g, ''); + delete webdriverConfig.host; + delete webdriverConfig.port; + delete webdriverConfig.browsers; + + // read hosts + let hostsPath = rootPath + '/hosts'; + let hosts = ''; + if(fs.existsSync(hostsPath)){ + hosts = fs.readFileSync(hostsPath).toString(); + } + let specName = path.relative(rootPath, __filename).replace(/\\/g,'/').replace(/\.js$/,''); + + browsers.split(/\s*,\s*/).forEach(function(browserName){ + let caseName = specName + ' : ' + browserName; + + let browserInfo = browserName.split(' '); + browserName = browserInfo[0]; + let browserVersion = browserInfo[1]; + + describe(caseName, function(){ + + this.timeout(600000); + this.slow(1000); + + let driver; + before(function(){ + let self = this; + let driver = new JWebDriver({ + 'host': host, + 'port': port + }); + let sessionConfig = Object.assign({}, webdriverConfig, { + 'browserName': browserName, + 'version': browserVersion, + 'ie.ensureCleanSession': true, + 'chromeOptions': { + 'args': ['--enable-automation'] + } + }); + if(proxy){ + sessionConfig.proxy = { + 'proxyType': 'manual', + 'httpProxy': proxy, + 'sslProxy': proxy + } + } + else if(hosts){ + sessionConfig.hosts = hosts; + } + self.driver = driver.session(sessionConfig).maximize().config({ + pageloadTimeout: 30000, // page onload timeout + scriptTimeout: 5000, // sync script timeout + asyncScriptTimeout: 10000 // async script timeout + }); + self.testVars = testVars; + let casePath = path.dirname(caseName); + self.screenshotPath = rootPath + '/screenshots/' + casePath; + self.diffbasePath = rootPath + '/diffbase/' + casePath; + self.caseName = caseName.replace(/.*\//g, '').replace(/\s*[:\.\:\-\s]\s*/g, '_'); + mkdirs(self.screenshotPath); + mkdirs(self.diffbasePath); + self.stepId = 0; + return self.driver; + }); + + module.exports(); + + beforeEach(function(){ + let self = this; + self.stepId ++; + if(self.skipAll){ + self.skip(); + } + }); + + afterEach(async function(){ + let self = this; + let currentTest = self.currentTest; + let title = currentTest.title; + if(currentTest.state === 'failed' && /^(url|waitBody|switchWindow|switchFrame):/.test(title)){ + self.skipAll = true; + } + if(!/^(closeWindow):/.test(title)){ + let filepath = self.screenshotPath + '/' + self.caseName + '_' + self.stepId; + let driver = self.driver; + try{ + // catch error when get alert msg + await driver.getScreenshot(filepath + '.png'); + let url = await driver.url(); + let html = await driver.source(); + html = '\n' + html; + fs.writeFileSync(filepath + '.html', html); + let cookies = await driver.cookies(); + fs.writeFileSync(filepath + '.cookie', JSON.stringify(cookies)); + } + catch(e){} + } + }); + + after(function(){ + return this.driver.close(); + }); + + }); + }); +} + +function getRootPath(){ + let rootPath = path.resolve(__dirname); + while(rootPath){ + if(fs.existsSync(rootPath + '/config.json')){ + break; + } + rootPath = rootPath.substring(0, rootPath.lastIndexOf(path.sep)); + } + return rootPath; +} + +function mkdirs(dirname){ + if(fs.existsSync(dirname)){ + return true; + }else{ + if(mkdirs(path.dirname(dirname))){ + fs.mkdirSync(dirname); + return true; + } + } +} + +function callSpec(name){ + try{ + require(rootPath + '/' + name)(); + } + catch(e){ + console.log(e) + process.exit(1); + } +} + +function isPageError(code){ + return code == '' || / jscontent="errorCode" jstcache="\d+"|diagnoseConnectionAndRefresh|dnserror_unavailable_header|id="reportCertificateErrorRetry"|400 Bad Request|403 Forbidden|404 Not Found|500 Internal Server Error|502 Bad Gateway|503 Service Temporarily Unavailable|504 Gateway Time-out/i.test(code); +} + +function catchError(error){ + +} diff --git a/server.js b/server.js index 235ae562..042b26ae 100755 --- a/server.js +++ b/server.js @@ -81,9 +81,9 @@ var bootstrap = function() { return app; } -module.exports = bootstrap(); - // To maintain backwards compatibility, run bootstrap when called as a file if(require.main === module) { bootstrap(); +} else { + module.exports = bootstrap(); } \ No newline at end of file From 8bda1a3a97f0b2c9216727e785557f955c26ea09 Mon Sep 17 00:00:00 2001 From: David Baldwynn Date: Tue, 21 Nov 2017 17:40:05 -0800 Subject: [PATCH 17/17] fixed hasAuthorization bug --- selenium/test/loginAndCreateForm.spec.js | 363 ----------------------- 1 file changed, 363 deletions(-) delete mode 100644 selenium/test/loginAndCreateForm.spec.js diff --git a/selenium/test/loginAndCreateForm.spec.js b/selenium/test/loginAndCreateForm.spec.js deleted file mode 100644 index db307227..00000000 --- a/selenium/test/loginAndCreateForm.spec.js +++ /dev/null @@ -1,363 +0,0 @@ -const fs = require('fs'); -const path = require('path'); -const chai = require("chai"); -const should = chai.should(); -const JWebDriver = require('jwebdriver'); -chai.use(JWebDriver.chaiSupportChainPromise); -const resemble = require('resemblejs-node'); -resemble.outputSettings({ - errorType: 'flatDifferenceIntensity' -}); - -const rootPath = getRootPath(); - -module.exports = function(){ - - let driver, testVars; - - before(function(){ - let self = this; - driver = self.driver; - testVars = self.testVars; - }); - - it('url: http://localhost:5000', async function(){ - await driver.url(_(`http://localhost:5000`)); - }); - - it('waitBody: ', async function(){ - await driver.sleep(500).wait('body', 30000).html().then(function(code){ - isPageError(code).should.be.false; - }); - }); - - it('insertVar: username ( #username, {{LoginUsername}} )', async function(){ - await driver.sleep(300).wait('#username', 30000) - .val(_(`{{LoginUsername}}`)); - }); - - it('insertVar: password ( #password, {{LoginPassword}} )', async function(){ - await driver.sleep(300).wait('#password', 30000) - .val(_(`{{LoginPassword}}`)); - }); - - it('mouseUp: Sign in ( button, 375, 45, 0 )', async function(){ - await driver.sleep(300).wait('button', 30000) - .sleep(300).mouseMove(375, 45).mouseMove(375, 45).mouseUp(0); - }); - - it('expect: displayed, button.btn.btn-signup, equal, true', async function(){ - await driver.sleep(300).wait('button.btn.btn-signup', 30000) - .displayed() - .should.not.be.a('error') - .should.equal(_(true)); - }); - - it('click: Sign in ( button, 217, 21, 0 )', async function(){ - await driver.sleep(300).wait('button', 30000) - .sleep(300).mouseMove(217, 21).click(0); - }); - - it('expect: displayed, div.new-button, equal, true', async function(){ - await driver.sleep(500).wait('div.new-button', 30000) - .displayed() - .should.not.be.a('error') - .should.equal(_(true)); - }); - - it('expect: displayed, section.navbar.navbar-inverse, equal, true', async function(){ - await driver.sleep(300).wait('section.navbar.navbar-inverse', 30000) - .displayed() - .should.not.be.a('error') - .should.equal(_(true)); - }); - - it('click: h4.fa, 76, 96, 0', async function(){ - await driver.sleep(300).wait('h4.fa', 30000) - .sleep(300).mouseMove(76, 96).click(0); - }); - - it('sleep: 100', async function(){ - await driver.sleep(100); - }); - - it('expect: displayed, form[name="forms.createForm"], equal, true', async function(){ - await driver.sleep(300).wait('form[name="forms.createForm"]', 30000) - .displayed() - .should.not.be.a('error') - .should.equal(_(true)); - }); - - it('× click: title ( input[name="title"], 115, 12, 0 )', async function(){ - await driver.sleep(300).wait('input[name="title"]', 30000) - .sleep(300).mouseMove(115, 12).click(0); - }); - - it('× expect: attr, .form-item.create-new.new-form button.btn,disabled, equal, true', async function(){ - await driver.sleep(300).wait('.form-item.create-new.new-form button.btn', 30000) - .attr('disabled') - .should.not.be.a('error') - .should.equal(_(`true`)); - }); - - it('sendKeys: aeoaoe', async function(){ - await driver.sendKeys('aeoaoe'); - }); - - it('× expect: attr, .form-item.create-new.new-form button.btn,disabled, equal, undefined', async function(){ - await driver.sleep(300).wait('.form-item.create-new.new-form button.btn', 30000) - .attr('disabled') - .should.not.be.a('error') - .should.equal(_(null)); - }); - - it('× click: Create this TellForm ( button, 57, 16, 0 )', async function(){ - await driver.sleep(300).wait('button', 30000) - .sleep(300).mouseMove(57, 16).click(0); - }); - - it('× expect: displayed, i.status-light.status-light-on, equal, true', async function(){ - await driver.sleep(300).wait('i.status-light.status-light-on', 30000) - .displayed() - .should.not.be.a('error') - .should.equal(_(true)); - }); - - it('× expect: displayed, .btn.btn-danger > .fa.fa-trash-o, equal, true', async function(){ - await driver.sleep(300).wait('.btn.btn-danger > .fa.fa-trash-o', 30000) - .displayed() - .should.not.be.a('error') - .should.equal(_(true)); - }); - - it('× click: Create ( //a[text()="Create"], 163, 25, 2 )', async function(){ - await driver.sleep(300).wait('//a[text()="Create"]', 30000) - .sleep(300).mouseMove(163, 25).click(2); - }); - - it('× mouseUp: aeoaoe ( section.admin-form, 467, 53, 0 )', async function(){ - await driver.sleep(300).wait('section.admin-form', 30000) - .sleep(300).mouseMove(467, 53).mouseMove(467, 53).mouseUp(0); - }); - - it('× click: Short Text ( div:nth-child(1) > div.panel-default > div.panel-heading > span.ng-binding, 39, 4, 0 )', async function(){ - await driver.sleep(300).wait('div:nth-child(1) > div.panel-default > div.panel-heading > span.ng-binding', 30000) - .sleep(300).mouseMove(39, 4).click(0); - }); - - it('× insertVar: title ( input[name="title"], {{ShortTextTitle}} )', async function(){ - await driver.sleep(300).wait('input[name="title"]', 30000) - .val(_(`{{ShortTextTitle}}`)); - }); - - it('mouseUp: 1 SeleniumShortText... ( body, 740, 97, 0 )', async function(){ - await driver.sleep(300).wait('body', 30000) - .sleep(300).mouseMove(740, 97).mouseMove(740, 97).mouseUp(0); - }); - - it('× expect: text, field-directive .field-title h3, equal, 1 {{ShortTextTitle}}', async function(){ - await driver.sleep(300).wait('field-directive .field-title h3', 30000) - .text() - .should.not.be.a('error') - .should.equal(_(`1 {{ShortTextTitle}}`)); - }); - - it('× click: Save ( button.btn-signup, 33, 23, 0 )', async function(){ - await driver.sleep(300).wait('button.btn-signup', 30000) - .sleep(300).mouseMove(33, 23).click(0); - }); - - it('× click: Delete Form ( span.hidden-sm, 55, 8, 0 )', async function(){ - await driver.sleep(300).wait('span.hidden-sm', 30000) - .sleep(300).mouseMove(55, 8).click(0); - }); - - it('× dblClick: aeoaoe ( //strong[text()="aeoaoe"], 26, 4, 0 )', async function(){ - await driver.sleep(300).wait('//strong[text()="aeoaoe"]', 30000) - .sleep(300).mouseMove(26, 4).click(0).click(0); - }); - - it('× click: input.input-block, 399, 3, 0', async function(){ - await driver.sleep(300).wait('input.input-block', 30000) - .sleep(300).mouseMove(399, 3).click(0); - }); - - function _(str){ - if(typeof str === 'string'){ - return str.replace(/\{\{(.+?)\}\}/g, function(all, key){ - return testVars[key] || ''; - }); - } - else{ - return str; - } - } - -}; - -if(module.parent && /mocha\.js/.test(module.parent.id)){ - runThisSpec(); -} - -function runThisSpec(){ - // read config - let webdriver = process.env['webdriver'] || ''; - let proxy = process.env['wdproxy'] || ''; - let config = require(rootPath + '/config.json'); - let webdriverConfig = Object.assign({},config.webdriver); - let host = webdriverConfig.host; - let port = webdriverConfig.port || 4444; - let match = webdriver.match(/([^\:]+)(?:\:(\d+))?/); - if(match){ - host = match[1] || host; - port = match[2] || port; - } - let testVars = config.vars; - let browsers = webdriverConfig.browsers; - browsers = browsers.replace(/^\s+|\s+$/g, ''); - delete webdriverConfig.host; - delete webdriverConfig.port; - delete webdriverConfig.browsers; - - // read hosts - let hostsPath = rootPath + '/hosts'; - let hosts = ''; - if(fs.existsSync(hostsPath)){ - hosts = fs.readFileSync(hostsPath).toString(); - } - let specName = path.relative(rootPath, __filename).replace(/\\/g,'/').replace(/\.js$/,''); - - browsers.split(/\s*,\s*/).forEach(function(browserName){ - let caseName = specName + ' : ' + browserName; - - let browserInfo = browserName.split(' '); - browserName = browserInfo[0]; - let browserVersion = browserInfo[1]; - - describe(caseName, function(){ - - this.timeout(600000); - this.slow(1000); - - let driver; - before(function(){ - let self = this; - let driver = new JWebDriver({ - 'host': host, - 'port': port - }); - let sessionConfig = Object.assign({}, webdriverConfig, { - 'browserName': browserName, - 'version': browserVersion, - 'ie.ensureCleanSession': true, - 'chromeOptions': { - 'args': ['--enable-automation'] - } - }); - if(proxy){ - sessionConfig.proxy = { - 'proxyType': 'manual', - 'httpProxy': proxy, - 'sslProxy': proxy - } - } - else if(hosts){ - sessionConfig.hosts = hosts; - } - self.driver = driver.session(sessionConfig).maximize().config({ - pageloadTimeout: 30000, // page onload timeout - scriptTimeout: 5000, // sync script timeout - asyncScriptTimeout: 10000 // async script timeout - }); - self.testVars = testVars; - let casePath = path.dirname(caseName); - self.screenshotPath = rootPath + '/screenshots/' + casePath; - self.diffbasePath = rootPath + '/diffbase/' + casePath; - self.caseName = caseName.replace(/.*\//g, '').replace(/\s*[:\.\:\-\s]\s*/g, '_'); - mkdirs(self.screenshotPath); - mkdirs(self.diffbasePath); - self.stepId = 0; - return self.driver; - }); - - module.exports(); - - beforeEach(function(){ - let self = this; - self.stepId ++; - if(self.skipAll){ - self.skip(); - } - }); - - afterEach(async function(){ - let self = this; - let currentTest = self.currentTest; - let title = currentTest.title; - if(currentTest.state === 'failed' && /^(url|waitBody|switchWindow|switchFrame):/.test(title)){ - self.skipAll = true; - } - if(!/^(closeWindow):/.test(title)){ - let filepath = self.screenshotPath + '/' + self.caseName + '_' + self.stepId; - let driver = self.driver; - try{ - // catch error when get alert msg - await driver.getScreenshot(filepath + '.png'); - let url = await driver.url(); - let html = await driver.source(); - html = '\n' + html; - fs.writeFileSync(filepath + '.html', html); - let cookies = await driver.cookies(); - fs.writeFileSync(filepath + '.cookie', JSON.stringify(cookies)); - } - catch(e){} - } - }); - - after(function(){ - return this.driver.close(); - }); - - }); - }); -} - -function getRootPath(){ - let rootPath = path.resolve(__dirname); - while(rootPath){ - if(fs.existsSync(rootPath + '/config.json')){ - break; - } - rootPath = rootPath.substring(0, rootPath.lastIndexOf(path.sep)); - } - return rootPath; -} - -function mkdirs(dirname){ - if(fs.existsSync(dirname)){ - return true; - }else{ - if(mkdirs(path.dirname(dirname))){ - fs.mkdirSync(dirname); - return true; - } - } -} - -function callSpec(name){ - try{ - require(rootPath + '/' + name)(); - } - catch(e){ - console.log(e) - process.exit(1); - } -} - -function isPageError(code){ - return code == '' || / jscontent="errorCode" jstcache="\d+"|diagnoseConnectionAndRefresh|dnserror_unavailable_header|id="reportCertificateErrorRetry"|400 Bad Request|403 Forbidden|404 Not Found|500 Internal Server Error|502 Bad Gateway|503 Service Temporarily Unavailable|504 Gateway Time-out/i.test(code); -} - -function catchError(error){ - -}