Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #38 from alphagov/extracting_zendesk_functionality

Extracting Zendesk functionality into the gds_zendesk gem
  • Loading branch information...
commit 8fde934c2c731c0085f80fbece5bb02b652d7f1b 2 parents 338a0ba + 5f481e9
@heathd heathd authored
View
6 Gemfile
@@ -17,9 +17,13 @@ gem 'gds-sso', '3.0.0'
gem 'jquery-rails'
gem 'jquery-ui-rails', '2.0.2'
gem 'plek', '1.0.0'
-gem 'zendesk_api', '0.1.2'
gem 'formtastic-bootstrap', '2.0.0'
gem 'validates_timeliness', '3.0.14'
+if ENV['GDS_ZENDESK_DEV']
+ gem "gds_zendesk", :path => '../gds_zendesk'
+else
+ gem "gds_zendesk", :git => 'https://github.com/alphagov/gds_zendesk.git', :ref => 'cb88a21e1f'
+end
group :test do
gem "mocha", "0.12.6", require: false
View
14 Gemfile.lock
@@ -1,3 +1,12 @@
+GIT
+ remote: https://github.com/alphagov/gds_zendesk.git
+ revision: cb88a21e1f74bdb8a28f43d124eadbda6ea61f46
+ ref: cb88a21e1f
+ specs:
+ gds_zendesk (0.0.1)
+ null_logger (= 0.0.1)
+ zendesk_api (= 0.1.2)
+
GEM
remote: https://rubygems.org/
remote: https://gems.gemfury.com/vo6ZrmjBQu5szyywDszE/
@@ -72,7 +81,7 @@ GEM
multi_json (~> 1.0)
faraday (0.8.4)
multipart-post (~> 1.1)
- faraday_middleware (0.8.8)
+ faraday_middleware (0.9.0)
faraday (>= 0.7.4, < 0.9)
faye-websocket (0.4.6)
eventmachine (>= 0.12.0)
@@ -127,6 +136,7 @@ GEM
multi_json (1.5.0)
multipart-post (1.1.5)
nokogiri (1.5.5)
+ null_logger (0.0.1)
oauth2 (0.8.0)
faraday (~> 0.8)
httpauth (~> 0.1)
@@ -246,6 +256,7 @@ DEPENDENCIES
exception_notification (~> 2.4.1)
formtastic-bootstrap (= 2.0.0)
gds-sso (= 3.0.0)
+ gds_zendesk!
jquery-rails
jquery-ui-rails (= 2.0.2)
less-rails-bootstrap (= 2.1.1)
@@ -260,4 +271,3 @@ DEPENDENCIES
unicorn (= 4.3.1)
validates_timeliness (= 3.0.14)
webmock (= 1.8.11)
- zendesk_api (= 0.1.2)
View
7 app/controllers/create_new_user_requests_controller.rb
@@ -1,5 +1,6 @@
require 'create_new_user_request_zendesk_ticket'
-require 'zendesk_users'
+require 'gds_zendesk/users'
+require 'gds_zendesk/zendesk_error'
class CreateNewUserRequestsController < RequestsController
protected
@@ -22,8 +23,8 @@ def process_valid_request(submitted_request)
def create_or_update_user_in_zendesk(requested_user)
begin
- ZendeskUsers.new(client).create_or_update_user(requested_user)
- rescue ZendeskError => e
+ GDSZendesk::Users.new(GDS_ZENDESK_CLIENT).create_or_update_user(requested_user)
+ rescue GDSZendesk::ZendeskError => e
ExceptionNotifier::Notifier.exception_notification(request.env, e).deliver
end
end
View
10 app/controllers/requests_controller.rb
@@ -1,5 +1,5 @@
require "zendesk_tickets"
-require "zendesk_client"
+require 'gds_zendesk/zendesk_error'
class RequestsController < ApplicationController
def new
@@ -22,7 +22,7 @@ def process_valid_request(submitted_request)
private
def raise_ticket(ticket)
- ticket = ZendeskTickets.new(client).raise_ticket(ticket)
+ ticket = ZendeskTickets.new(GDS_ZENDESK_CLIENT).raise_ticket(ticket)
if ticket
redirect_to acknowledge_path
@@ -30,8 +30,4 @@ def raise_ticket(ticket)
return render "support/zendesk_error", :locals => {:error_string => "zendesk_error_upon_submit"}
end
end
-
- def client
- ZendeskClient.get_client(logger)
- end
-end
+end
View
11 config/initializers/gds_zendesk.rb
@@ -0,0 +1,11 @@
+require 'yaml'
+require 'gds_zendesk/client'
+require 'gds_zendesk/dummy_client'
+
+GDS_ZENDESK_CLIENT = if Rails.env.development? || Rails.env.test?
+ GDSZendesk::DummyClient.new(logger: Rails.logger)
+else
+ config_yaml_file = File.join(Rails.root, 'config', 'zendesk.yml')
+ config = YAML.load_file(config_yaml_file)[Rails.env]
+ GDSZendesk::Client.new(username: config['username'], password: config['password'], logger: Rails.logger)
+end
View
4 features/step_definitions/zendesk_steps.rb
@@ -1,6 +1,6 @@
Then /^the following ticket is raised in ZenDesk:$/ do |ticket_properties_table|
expected_ticket_props = ticket_properties_table.hashes.first
- @raised_ticket = @zendesk_api.ticket
+ @raised_ticket = GDS_ZENDESK_CLIENT.ticket
assert_equal expected_ticket_props["Subject"], @raised_ticket.subject if expected_ticket_props["Subject"]
assert_equal expected_ticket_props["Requester email"], @raised_ticket.email if expected_ticket_props["Requester email"]
@@ -17,7 +17,7 @@
Then /^the time constraints on the ticket are:$/ do |ticket_properties_table|
expected_ticket_props = ticket_properties_table.hashes.first
- @raised_ticket = @zendesk_api.ticket
+ @raised_ticket = GDS_ZENDESK_CLIENT.ticket
assert_equal expected_ticket_props["Need by date"], @raised_ticket.needed_by_date if expected_ticket_props["Need by date"]
assert_equal expected_ticket_props["Not before date"], @raised_ticket.not_before_date if expected_ticket_props["Not before date"]
View
3  features/support/hooks.rb
@@ -1,3 +0,0 @@
-Before do
- stub_zendesk_ticket_submission
-end
View
3  features/support/zendesk_helper.rb
@@ -1,3 +0,0 @@
-require_relative '../../test/zendesk_api_stubs'
-
-World(ZendeskApiStubsHelper)
View
39 lib/zendesk_client.rb
@@ -1,39 +0,0 @@
-require "zendesk_error"
-
-class ZendeskClient
-
- def self.get_client(logger)
- @client ||= create_new_zendesk_api_client(logger)
- end
-
- private
- def self.create_new_zendesk_api_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.insert_callback do |env|
- logger.info env
-
- status_401 = env[:status].to_s.start_with? "401"
- too_many_login_attempts = env[:body].to_s.start_with? "Too many failed login attempts"
-
- raise ZendeskError, "Authentication Error: #{env.inspect}" if status_401 || too_many_login_attempts
-
- raise ZendeskError, "Error creating ticket: #{env.inspect}" if env[:body]["error"]
- end
-
- client
- end
-
- def self.get_username_password(config_details)
- # GOVUK_ENV should be preview/production/staging. Fall back if we are in dev (or test)
- environment = ENV['GOVUK_ENV'] || Rails.env
- [config_details[environment]["username"].to_s, config_details[environment]["password"].to_s]
- end
-end
View
1  lib/zendesk_error.rb
@@ -1 +0,0 @@
-class ZendeskError < StandardError; end
View
11 lib/zendesk_tickets.rb
@@ -1,15 +1,10 @@
-require_relative "zendesk_ticket"
+require 'gds_zendesk/field_mappings'
class ZendeskTickets
def initialize(client)
@client = client
end
- def self.field_ids
- { needed_by_date: "21485833",
- not_before_date: "21502036" }
- end
-
def raise_ticket(ticket_to_raise)
@client.ticket.create(
:subject => ticket_to_raise.subject,
@@ -17,8 +12,8 @@ def raise_ticket(ticket_to_raise)
:priority => "normal",
:requester => {"locale_id" => 1, "email" => ticket_to_raise.email, "name" => ticket_to_raise.email},
:collaborators => ticket_to_raise.collaborator_emails,
- :fields => [{"id" => ZendeskTickets.field_ids[:needed_by_date], "value" => ticket_to_raise.needed_by_date},
- {"id" => ZendeskTickets.field_ids[:not_before_date], "value" => ticket_to_raise.not_before_date}],
+ :fields => [{"id" => GDSZendesk::FIELD_MAPPINGS[:needed_by_date], "value" => ticket_to_raise.needed_by_date},
+ {"id" => GDSZendesk::FIELD_MAPPINGS[:not_before_date], "value" => ticket_to_raise.not_before_date}],
:tags => ticket_to_raise.tags,
:comment => {:value => ticket_to_raise.comment})
end
View
38 lib/zendesk_users.rb
@@ -1,38 +0,0 @@
-require 'zendesk_client'
-require 'active_support'
-
-class ZendeskUsers
- def initialize(client)
- @client = client
- end
-
- def create_or_update_user(requested_user)
- existing_users = find_by_email(requested_user.email)
- if existing_users.empty?
- create(requested_user)
- else
- existing_user_in_zendesk = existing_users.first
- update(existing_user_in_zendesk, requested_user)
- end
- end
-
- protected
- def find_by_email(email)
- @client.users.search(query: email).to_a
- end
-
- def create(requested_user)
- @client.users.create(email: requested_user.email,
- name: requested_user.name,
- details: "Job title: #{requested_user.job}",
- phone: requested_user.phone,
- verified: true)
- end
-
- def update(existing_user_in_zendesk, requested_user)
- existing_user_in_zendesk.update(details: "Job title: #{requested_user.job}")
- existing_user_in_zendesk.update(phone: requested_user.phone) unless requested_user.phone.blank?
- existing_user_in_zendesk.save
- existing_user_in_zendesk
- end
-end
View
9 test/functional/create_new_user_requests_controller_test.rb
@@ -1,14 +1,9 @@
require "test_helper"
+require 'gds_zendesk/zendesk_error'
class CreateNewUserRequestsControllerTest < ActionController::TestCase
include TestData
- setup do
- login_as_stub_user
- @zendesk_api = ZenDeskAPIClientDouble.new
- ZendeskClient.stubs(:get_client).returns(@zendesk_api)
- end
-
context "submitted user creation request" do
should "submit it to ZenDesk" do
post :create, valid_create_new_user_request_params
@@ -34,7 +29,7 @@ class CreateNewUserRequestsControllerTest < ActionController::TestCase
@zendesk_api.users.should_raise_error
ExceptionNotifier::Notifier.expects(:exception_notification)
- .with(anything, kind_of(ZendeskError))
+ .with(anything, kind_of(GDSZendesk::ZendeskError))
.returns(stub("mailer", deliver: true))
post :create, valid_create_new_user_request_params
View
6 test/functional/general_requests_controller_test.rb
@@ -3,12 +3,6 @@
class GeneralRequestsControllerTest < ActionController::TestCase
include TestData
- setup do
- login_as_stub_user
- @zendesk_api = ZenDeskAPIClientDouble.new
- ZendeskClient.stubs(:get_client).returns(@zendesk_api)
- end
-
context "a submitted general request" do
should "add the user agent to the ticket in the comments" do
request.user_agent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2)"
View
4 test/functional/requests_controller_test.rb
@@ -48,10 +48,6 @@ def valid_params_for_test_request
class RequestsControllerTest < ActionController::TestCase
setup do
- login_as_stub_user
- @zendesk_api = ZenDeskAPIClientDouble.new
- ZendeskClient.stubs(:get_client).returns(@zendesk_api)
-
Rails.application.routes.draw do
match 'new' => "test_requests#new"
match 'create' => "test_requests#create"
View
6 test/functional/support_controller_test.rb
@@ -1,12 +1,6 @@
require "test_helper"
class SupportControllerTest < ActionController::TestCase
- setup do
- login_as_stub_user
- @zendesk_api = ZenDeskAPIClientDouble.new
- ZendeskClient.stubs(:get_client).returns(@zendesk_api)
- end
-
context "GET landing" do
should "render the homepage" do
get :landing
View
8 test/test_helper.rb
@@ -8,12 +8,12 @@
require 'ostruct'
require_relative 'test_data'
-require 'zendesk_api_stubs'
-
class ActiveSupport::TestCase
def setup
super
WebMock.disable_net_connect!
+ login_as_stub_user
+ switch_zendesk_into_dummy_mode
end
def login_as_stub_user
@@ -21,5 +21,9 @@ def login_as_stub_user
name: "Stubby McStubby", remotely_signed_out?: false)
request.env['warden'] = stub(:authenticate! => true, :authenticated? => true, :user => @user)
end
+
+ def switch_zendesk_into_dummy_mode
+ @zendesk_api = GDS_ZENDESK_CLIENT
+ end
end
View
37 test/unit/zendesk/zendesk_users_test.rb
@@ -1,37 +0,0 @@
-require 'test_helper'
-require 'zendesk_users'
-
-class ZendeskUsersTest < Test::Unit::TestCase
- def setup
- @stub_zendesk_users = stub("zendesk API")
- client = stub("Zendesk client", users: @stub_zendesk_users)
- @users = ZendeskUsers.new(client)
- end
-
- context "creating/updating a user that already exists in Zendesk" do
- should "update the phone and job title if those are set" do
- stub_existing_zendesk_user = stub("existing zendesk user")
- @stub_zendesk_users.expects(:search).with(query: "test@test.com").returns([stub_existing_zendesk_user])
- stub_existing_zendesk_user.expects(:update).with(details: "Job title: Developer")
- stub_existing_zendesk_user.expects(:update).with(phone: "12345")
- stub_existing_zendesk_user.expects(:save)
-
- existing_user_being_requested = stub("requested user", email: "test@test.com", phone: "12345", job: "Developer")
- @users.create_or_update_user(existing_user_being_requested)
- end
- end
-
- context "creating/updating a user that doesn't exists in Zendesk" do
- should "create that user" do
- @stub_zendesk_users.expects(:search).with(query: "test@test.com").returns([])
- @stub_zendesk_users.expects(:create).with(email: "test@test.com",
- name: "Abc",
- phone: "12345",
- details: "Job title: Developer",
- verified: true)
-
- existing_user_being_requested = stub("requested user", name: "Abc", email: "test@test.com", phone: "12345", job: "Developer")
- @users.create_or_update_user(existing_user_being_requested)
- end
- end
-end
View
81 test/zendesk_api_stubs.rb
@@ -1,81 +0,0 @@
-require 'zendesk_tickets'
-require 'zendesk_error'
-
-module ZendeskApiStubsHelper
- def stub_zendesk_ticket_submission
- @zendesk_api = ZenDeskAPIClientDouble.new
- ZendeskClient.stubs(:get_client).returns(@zendesk_api)
- end
-end
-
-class ZenDeskAPITicketDouble
- attr_reader :options
-
- def create(options)
- @options = options
- end
-
- [:subject, :tags, :description, :collaborators].each do |property|
- define_method(property) do
- @options[property]
- end
- end
-
- ZendeskTickets.field_ids.each do |field_name, field_id|
- define_method(field_name) do
- value_of_field_with_id(field_id)
- end
- end
-
- def name
- @options[:requester]["name"]
- end
-
- def email
- @options[:requester]["email"]
- end
-
- def comment
- @options[:comment][:value]
- end
-
- protected
- def value_of_field_with_id(field_id)
- correct_field = @options[:fields].detect {|field| field["id"] == field_id}
- correct_field["value"]
- end
-end
-
-class ZenDeskAPIUsersDouble
- attr_reader :created_user_attributes
-
- def initialize
- @created_user_attributes = {}
- @should_raise_error = false
- end
-
- def should_raise_error
- @should_raise_error = true
- end
-
- def search(attributes)
- []
- end
-
- def create(new_user_attributes)
- if @should_raise_error
- raise ZendeskError, "error creating users"
- else
- @created_user_attributes = new_user_attributes
- end
- end
-end
-
-class ZenDeskAPIClientDouble
- attr_reader :ticket, :users
-
- def initialize
- @ticket = ZenDeskAPITicketDouble.new
- @users = ZenDeskAPIUsersDouble.new
- end
-end
Please sign in to comment.
Something went wrong with that request. Please try again.