From 6b305dd1de25cc408c579abdd24b96feec36abb5 Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Mon, 1 Feb 2016 10:23:55 +0100 Subject: [PATCH] Added form browser tests and form email validation. --- Gemfile | 1 + Gemfile.lock | 4 + .../app/views/channel/form.jst.eco | 2 +- app/controllers/form_controller.rb | 23 +- public/assets/form/form.html | 8 +- public/assets/form/form.js | 153 +++++++--- script/build/test_slice_tests.sh | 2 + test/browser/form_test.rb | 281 ++++++++++++++++++ 8 files changed, 425 insertions(+), 49 deletions(-) create mode 100644 test/browser/form_test.rb diff --git a/Gemfile b/Gemfile index 12e7be0d8..171b39718 100644 --- a/Gemfile +++ b/Gemfile @@ -37,6 +37,7 @@ gem 'zendesk_api' gem 'twitter' gem 'koala' gem 'mail' +gem 'email_verifier' gem 'mime-types' diff --git a/Gemfile.lock b/Gemfile.lock index bbf90e2a1..da7a015a6 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -80,6 +80,7 @@ GEM activerecord (>= 3.0, < 5) delayed_job (>= 3.0, < 5) diffy (3.1.0) + dnsruby (1.59.1) docile (1.1.5) domain_name (0.5.25) unf (>= 0.0.5, < 1.0.0) @@ -91,6 +92,8 @@ GEM em-websocket (0.5.1) eventmachine (>= 0.12.9) http_parser.rb (~> 0.6.0) + email_verifier (0.1.0) + dnsruby (>= 1.5) equalizer (0.0.10) erubis (2.7.0) eventmachine (1.0.9.1) @@ -333,6 +336,7 @@ DEPENDENCIES diffy eco em-websocket + email_verifier eventmachine execjs guard (>= 2.2.2) diff --git a/app/assets/javascripts/app/views/channel/form.jst.eco b/app/assets/javascripts/app/views/channel/form.jst.eco index fe6351abb..fe417ebcd 100644 --- a/app/assets/javascripts/app/views/channel/form.jst.eco +++ b/app/assets/javascripts/app/views/channel/form.jst.eco @@ -101,7 +101,7 @@ <script> $(function() { - $('#feedback-form').zammad_form({ + $('#feedback-form').ZammadForm({ }); }); diff --git a/app/controllers/form_controller.rb b/app/controllers/form_controller.rb index 1f7c105df..c65f56fb0 100644 --- a/app/controllers/form_controller.rb +++ b/app/controllers/form_controller.rb @@ -43,6 +43,19 @@ class FormController < ApplicationController errors['body'] = 'required' end + # realtime verify + if !errors['email'] + begin + checker = EmailVerifier::Checker.new(params[:email]) + checker.connect + if !checker.verify + errors['email'] = "Unable to send to '#{params[:email]}'" + end + rescue => e + errors['email'] = e.to_s + end + end + if errors && !errors.empty? render json: { errors: errors @@ -55,7 +68,7 @@ class FormController < ApplicationController customer = User.find_by(email: email) if !customer - roles = Role.where( name: 'Customer' ) + roles = Role.where(name: 'Customer') customer = User.create( firstname: name, lastname: '', @@ -72,16 +85,16 @@ class FormController < ApplicationController group_id: 1, customer_id: customer.id, title: params[:title], - state_id: Ticket::State.find_by( name: 'new' ).id, - priority_id: Ticket::Priority.find_by( name: '2 normal' ).id, + state_id: Ticket::State.find_by(name: 'new').id, + priority_id: Ticket::Priority.find_by(name: '2 normal').id, updated_by_id: customer.id, created_by_id: customer.id, ) article = Ticket::Article.create( ticket_id: ticket.id, - type_id: Ticket::Article::Type.find_by( name: 'web' ).id, - sender_id: Ticket::Article::Sender.find_by( name: 'Customer' ).id, + type_id: Ticket::Article::Type.find_by(name: 'web').id, + sender_id: Ticket::Article::Sender.find_by(name: 'Customer').id, body: params[:body], from: email, subject: params[:title], diff --git a/public/assets/form/form.html b/public/assets/form/form.html index fdbb2bb5f..7442a7140 100644 --- a/public/assets/form/form.html +++ b/public/assets/form/form.html @@ -14,6 +14,8 @@
+
+

Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.

Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi.

@@ -39,12 +41,14 @@ diff --git a/public/assets/form/form.js b/public/assets/form/form.js index 11eae8ced..f79dbd7e4 100644 --- a/public/assets/form/form.js +++ b/public/assets/form/form.js @@ -10,19 +10,43 @@ */ - var pluginName = 'zammad_form', + var pluginName = 'ZammadForm', defaults = { debug: false, noCSS: false, @@ -30,6 +54,30 @@ $(function() { messageTitle: 'Zammad Form', messageSubmit: 'Submit', messageThankYou: 'Thank you for your inquiry! We\'ll contact you soon as possible.', + messageNoConfig: 'Unable to load form config from server. Maybe featrue is disabled.', + attributes: [ + { + display: 'Name', + name: 'name', + tag: 'input', + type: 'text', + placeholder: 'Your Name', + }, + { + display: 'Email', + name: 'email', + tag: 'input', + type: 'email', + placeholder: 'Your Email', + }, + { + display: 'Message', + name: 'body', + tag: 'textarea', + placeholder: 'Your Message...', + rows: 7, + }, + ] }; function Plugin( element, options ) { @@ -53,30 +101,6 @@ $(function() { this._config = {} - this.attributes = [ - { - display: 'Name', - name: 'name', - tag: 'input', - type: 'text', - placeholder: 'Your Name', - }, - { - display: 'Email', - name: 'email', - tag: 'input', - type: 'email', - placeholder: 'Your Email', - }, - { - display: 'Message', - name: 'body', - tag: 'textarea', - placeholder: 'Your Message...', - rows: 7, - }, - ] - this.init(); } @@ -84,23 +108,29 @@ $(function() { Plugin.prototype.init = function () { var _this = this - _this.log('init') + _this.log('debug', 'init') if (!_this.options.noCSS) { _this.loadCss(_this.css_location) } - _this.log('endpoint_config: ' + _this.endpoint_config) - _this.log('endpoint_submit: ' + _this.endpoint_submit) + _this.log('debug', 'endpoint_config: ' + _this.endpoint_config) + _this.log('debug', 'endpoint_submit: ' + _this.endpoint_submit) // load config $.ajax({ url: _this.endpoint_config, }).done(function(data) { - _this.log('config:', data) + _this.log('debug', 'config:', data) _this._config = data - }).fail(function() { - alert('Faild to load form config!') + }).fail(function(jqXHR, textStatus, errorThrown) { + if (jqXHR.status == 401) { + _this.log('error', 'Faild to load form config, feature is disabled!') + } + else { + _this.log('error', 'Faild to load form config!') + } + _this.noConfig() }); // show form @@ -132,6 +162,19 @@ $(function() { Plugin.prototype.submit = function() { var _this = this + // check min modal open time + if (_this.modalOpenTime) { + var currentTime = new Date().getTime() + var diff = currentTime - _this.modalOpenTime.getTime() + _this.log('debug', 'currentTime', currentTime) + _this.log('debug', 'modalOpenTime', _this.modalOpenTime.getTime()) + _this.log('debug', 'diffTime', diff) + if (diff < 1000*8) { + alert('Sorry, you look like an robot!') + return + } + } + $.ajax({ method: 'post', url: _this.endpoint_submit, @@ -169,9 +212,7 @@ $(function() { if (!params.title) { params.title = this.options.messageTitle } - - _this.log('params', params) - + _this.log('debug', 'params', params) return params } @@ -185,6 +226,8 @@ $(function() { Plugin.prototype.render = function(e) { var _this = this _this.closeModal() + _this.modalOpenTime = new Date() + _this.log('debug', 'modalOpenTime:', _this.modalOpenTime) var element = '