Permalink
Browse files

Merge commit '181ebc8b94d33bdb594e5a16c64c8bd97f342393' into HEAD

  • Loading branch information...
2 parents 85d0441 + 181ebc8 commit 1fccf05f3a311c8b5bdd16756f57dc9636700a05 Jenkins committed Sep 13, 2012
View
@@ -6,7 +6,6 @@ gem "rake"
gem "unicorn"
gem 'slimmer', '1.2.4'
-
group :test do
gem "rack-test"
gem "mocha"
View
View
@@ -2,10 +2,22 @@
Bundler.require
require_relative "zendesk_client"
+require_relative "zendesk_request"
+require_relative "zendesk_error"
require_relative "validations"
class App < Sinatra::Base
+ configure do
+ disable :show_exceptions
+ disable :raise_errors
+ enable :logging
+ end
+
+ before do
+ @client = ZendeskClient.get_client(logger)
+ end
+
get '/' do
erb :landing
end
@@ -116,7 +128,7 @@ class App < Sinatra::Base
end
def on_get(head, head_message_form, template)
- @departments = ZendeskClient.get_departments
+ @departments = ZendeskRequest.get_departments(@client)
@header = head
@header_message = :"#{head_message_form}"
@formdata = {}
@@ -125,11 +137,11 @@ def on_get(head, head_message_form, template)
end
def on_post(params, route)
- @departments = ZendeskClient.get_departments
+ @departments = ZendeskRequest.get_departments(@client)
@formdata = params
if @errors.empty?
- ticket = ZendeskClient.raise_zendesk_request(params, route)
+ ticket = ZendeskRequest.raise_zendesk_request(@client, params, route)
if ticket
redirect '/acknowledge'
else
@@ -140,4 +152,13 @@ def on_post(params, route)
end
end
+ error do
+ @error_msg = "And error IS: #{request.env['sinatra.error']}"
+ erb :error_page
+ end
+
+ error ZendeskError do
+ redirect '/failed-submission'
+ end
+
end
@@ -144,7 +144,7 @@ textarea {
background: transparent;
margin-left: 5px;
margin-bottom: 0.2em;
- width: 90%;
+ max-width: 700px;
border: 1px solid #BBB;
}
@@ -162,7 +162,7 @@ section#author input, section#content input, section#campaign input, section#use
margin-bottom: 10px;
margin-top: 5px;;
width: 15em;
- padding: 0em;
+ padding: 0;
}
input[type="text"], input[type="email"], input[type="tel"] {
@@ -189,7 +189,7 @@ select {
margin-right: 0.3em;
margin-top: 5px;
margin-bottom: 10px;
- margin-left: 0px;
+ margin-left: 0;
padding: 0;
border: 1px solid #BBB;
}
@@ -0,0 +1,17 @@
+<div class="inner">
+ <header class="content-header group">
+ <hgroup><h1>Welcome To GovUK Support</h1></hgroup>
+ </header>
+
+ <%= erb :menu %>
+
+ <div id="form">
+ <p class="error">
+ There has been a problem
+ </p>
+ <p>
+ <%= @error_msg %>
+ </p>
+ </div>
+ <div class="toclear"></div>
+</div>
View
@@ -3,74 +3,41 @@
require "yaml"
require_relative "zendesk_ticket"
+require_relative "zendesk_error"
class ZendeskClient
- def self.get_username_password(config_details)
- environment = ENV['GOVUK_ENV'] || "development"
- [config_details[environment]["username"].to_s, config_details[environment]["password"].to_s]
- end
-
- def self.zendesk_client
+ def self.get_client(logger)
@client ||= ZendeskAPI::Client.new { |config|
file = YAML::load_file(File.open('./config/zendesk.yml'))
login_details = self.get_username_password(file)
config.url = "https://govuk.zendesk.com/api/v2/"
config.username = login_details[0]
config.password = login_details[1]
+ config.logger = logger
}
- @client
- end
-
- def self.get_departments
- departments_hash = {"Select Department" => ""}
- self.zendesk_client.ticket_fields.find(:id => '21494928').custom_field_options.each { |tf| departments_hash[tf.name] = tf.value }
- departments_hash
- end
-
- def self.raise_zendesk_request(params, from_route)
- ticket_to_raise = ZendeskTicket.new(params, from_route)
-
- if ticket_to_raise.has_attachments
- create_ticket_with_attachment(ticket_to_raise)
- else
- self.zendesk_client.ticket.create(
- :subject => ticket_to_raise.subject,
- :description => "Created via Govt API",
- :priority => "normal",
- :requester => {"locale_id" => 1, "name" => ticket_to_raise.name, "email" => ticket_to_raise.email},
- :fields => [{"id" => "21494928", "value" => ticket_to_raise.department},
- {"id" => "21487987", "value" => ticket_to_raise.job},
- {"id" => "21471291", "value" => ticket_to_raise.phone},
- {"id" => "21485833", "value" => ticket_to_raise.need_by_date},
- {"id" => "21502036", "value" => ticket_to_raise.not_before_date}],
- :tags => [ticket_to_raise.tag],
- :comment => {:value => ticket_to_raise.comment})
+ @client.insert_callback do |env|
+ logger.info env
+ if env[:body]["user"]
+ if env[:body]["id"].nil?
+ raise ZendeskError.new("Authentication Error")
+ end
+ end
+ status_401 = env[:status].to_s.start_with? "401"
+ too_many_login_attempts = env[:body].to_s.start_with? "Too many failed login attempts"
+ if status_401 || too_many_login_attempts
+ raise ZendeskError.new("Authentication Error")
+ end
end
- end
- def self.upload_file(path)
- upload = ZendeskAPI::Upload.create(self.zendesk_client, :file => File.open(path))
- upload.token
+ @client
end
- def self.create_ticket_with_attachment(ticket_to_raise)
- self.zendesk_client.ticket.create(
- :subject => ticket_to_raise.subject,
- :description => "Created via Govt API",
- :priority => "normal",
- :requester => {"locale_id" => 1, "name" => ticket_to_raise.name, "email" => ticket_to_raise.email},
- :fields => [{"id" => "21494928", "value" => ticket_to_raise.department},
- {"id" => "21487987", "value" => ticket_to_raise.job},
- {"id" => "21471291", "value" => ticket_to_raise.phone},
- {"id" => "21485833", "value" => ticket_to_raise.need_by_date},
- {"id" => "21502036", "value" => ticket_to_raise.not_before_date}],
- :tags => [ticket_to_raise.tag],
- :comment => {:value => ticket_to_raise.comment, :uploads => ticket_to_raise.file_token})
- end
+ private
- def self.remove_space_from_phone_number(number)
- number.gsub(/\s+/, "")
+ def self.get_username_password(config_details)
+ environment = ENV['GOVUK_ENV'] || "development"
+ [config_details[environment]["username"].to_s, config_details[environment]["password"].to_s]
end
-end
+end
@@ -0,0 +1,9 @@
+class ZendeskError < StandardError
+
+ attr_reader :message
+
+ def initialize(message)
+ @message = message.to_s
+ end
+
+end
@@ -0,0 +1,53 @@
+require 'bundler'
+Bundler.require
+
+require "yaml"
+require_relative "zendesk_ticket"
+
+class ZendeskRequest
+
+ def self.get_departments(client)
+ departments_hash = {"Select Department" => ""}
+ #if client && client.current_user && client.current_user.id
+ client.ticket_fields.find(:id => '21494928').custom_field_options.each { |tf| departments_hash[tf.name] = tf.value }
+ #end
+ departments_hash
+ end
+
+ def self.raise_zendesk_request(client, params, from_route)
+ ticket_to_raise = ZendeskTicket.new(client, params, from_route)
+
+ if ticket_to_raise.has_attachments
+ create_ticket_with_attachment(client, ticket_to_raise)
+ else
+ client.ticket.create(
+ :subject => ticket_to_raise.subject,
+ :description => "Created via Govt API",
+ :priority => "normal",
+ :requester => {"locale_id" => 1, "name" => ticket_to_raise.name, "email" => ticket_to_raise.email},
+ :fields => [{"id" => "21494928", "value" => ticket_to_raise.department},
+ {"id" => "21487987", "value" => ticket_to_raise.job},
+ {"id" => "21471291", "value" => ticket_to_raise.phone},
+ {"id" => "21485833", "value" => ticket_to_raise.need_by_date},
+ {"id" => "21502036", "value" => ticket_to_raise.not_before_date}],
+ :tags => [ticket_to_raise.tag],
+ :comment => {:value => ticket_to_raise.comment})
+ end
+ end
+
+ def self.create_ticket_with_attachment(client, ticket_to_raise)
+ client.ticket.create(
+ :subject => ticket_to_raise.subject,
+ :description => "Created via Govt API",
+ :priority => "normal",
+ :requester => {"locale_id" => 1, "name" => ticket_to_raise.name, "email" => ticket_to_raise.email},
+ :fields => [{"id" => "21494928", "value" => ticket_to_raise.department},
+ {"id" => "21487987", "value" => ticket_to_raise.job},
+ {"id" => "21471291", "value" => ticket_to_raise.phone},
+ {"id" => "21485833", "value" => ticket_to_raise.need_by_date},
+ {"id" => "21502036", "value" => ticket_to_raise.not_before_date}],
+ :tags => [ticket_to_raise.tag],
+ :comment => {:value => ticket_to_raise.comment, :uploads => ticket_to_raise.file_token})
+ end
+
+end
View
@@ -31,7 +31,7 @@ class ZendeskTicket
"publish-tool" => "publishing_tool_tech"
}
- def initialize(params, from_route)
+ def initialize(client, params, from_route)
#author information
@name = params[:name]
@email = params[:email]
@@ -54,7 +54,7 @@ def initialize(params, from_route)
@not_before_date = params[:not_before_day] + "/" + params[:not_before_month] + "/" + params[:not_before_year]
end
- check_for_attachments(params)
+ check_for_attachments(client, params)
end
@@ -146,21 +146,26 @@ def build_full_url_path(partial_path)
end
# attachments
- def upload_file_to_create_file_token(tempfile, filename)
+ def upload_file_to_create_file_token(client, tempfile, filename)
directory = "./tmp"
path = File.join(directory, filename)
File.open(path, "wb") { |f| f.write(tempfile.read) }
- file_token = ZendeskClient::upload_file(path)
+ file_token = upload_file(client, path)
File.delete(path)
file_token
end
- def check_for_attachments(params)
+ def upload_file(client, path)
+ upload = ZendeskAPI::Upload.create(client, :file => File.open(path))
+ upload.token
+ end
+
+ def check_for_attachments(client, params)
@file_token = []
if params[:uploaded_data] && doesFieldHaveValue(params[:uploaded_data][:filename])
tempfile = params[:uploaded_data][:tempfile]
filename = params[:uploaded_data][:filename]
- @file_token << upload_file_to_create_file_token(tempfile, filename)
+ @file_token << upload_file_to_create_file_token(client, tempfile, filename)
end
if !@file_token.empty? && @comment.empty?
@@ -2,7 +2,7 @@
require "test/unit"
require "mocha"
require_relative "../lib/app"
-require_relative "../lib/zendesk_client"
+require_relative "../lib/zendesk_request"
require_relative "../spec/page_helper"
@@ -13,12 +13,19 @@ def app
App
end
+ def setup
+ ZendeskClient.expects(:get_client)
+ end
+
def teardown
Mocha::Mockery.instance.teardown
Mocha::Mockery.reset_instance
end
def test_page_contain_required_fields
+ #Given
+ ZendeskRequest.expects(:get_departments).returns([{"key1" => "value1"}, {"key2" => "value2"}])
+
#When
get '/amend-content'
@@ -32,7 +39,7 @@ def test_page_contain_required_fields
def test_departments_list_shown_on_page
#Given
- ZendeskClient.expects(:get_departments).returns([{"key1" => "value1"}, {"key2" => "value2"}])
+ ZendeskRequest.expects(:get_departments).returns([{"key1" => "value1"}, {"key2" => "value2"}])
#When
get '/amend-content'
@@ -44,11 +51,13 @@ def test_departments_list_shown_on_page
def test_zendesk_create_ticket_triggered_by_post_request
form_parameters = PageHelper.fill_content_form
- ZendeskClient.expects(:get_departments).returns([{"key1" => "value1"}, {"key2" => "value2"}])
- ZendeskClient.expects(:raise_zendesk_request).returns("fake ticket")
+ ZendeskRequest.expects(:get_departments).returns([{"key1" => "value1"}, {"key2" => "value2"}])
+ Guard.expects(:validationsForCreateUser).returns({});
+ ZendeskRequest.expects(:raise_zendesk_request).returns("fake ticket")
#When
post '/create-user', form_parameters
+ ZendeskClient.expects(:get_client)
follow_redirect!
#Then
Oops, something went wrong. Retry.

0 comments on commit 1fccf05

Please sign in to comment.