-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1003 from alphagov/stop-using-gds-zendesk
Use "zendesk_api" gem instead of "gds_zendesk"
- Loading branch information
Showing
7 changed files
with
147 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,17 @@ | ||
require "yaml" | ||
require "gds_zendesk/client" | ||
require "gds_zendesk/dummy_client" | ||
require "zendesk_api" | ||
require "zendesk/dummy_client" | ||
|
||
GDS_ZENDESK_URL = "https://govuk.zendesk.com/api/v2/".freeze | ||
ZENDESK_ANONYMOUS_TICKETS_REQUESTER_EMAIL = ENV["ZENDESK_ANONYMOUS_TICKET_EMAIL"] || "[email protected]" | ||
|
||
GDS_ZENDESK_CLIENT = if Rails.env.development? | ||
GDSZendesk::DummyClient.new(logger: Rails.logger) | ||
Zendesk::DummyClient.new(logger: Rails.logger) | ||
else | ||
GDSZendesk::Client.new( | ||
username: ENV["ZENDESK_CLIENT_USERNAME"] || "abc", | ||
password: ENV["ZENDESK_CLIENT_PASSWORD"] || "def", | ||
logger: Rails.logger, | ||
) | ||
ZendeskAPI::Client.new do |config| | ||
config.url = GDS_ZENDESK_URL | ||
config.username = ENV["ZENDESK_CLIENT_USERNAME"] || "abc" | ||
config.password = ENV["ZENDESK_CLIENT_PASSWORD"] || "def" | ||
config.logger = Rails.logger | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
require "zendesk_api/error" | ||
|
||
module Zendesk | ||
class DummyClient | ||
attr_reader :ticket, :users | ||
|
||
def initialize(options) | ||
@logger = options[:logger] | ||
@ticket = DummyTicket.new(@logger) | ||
@users = DummyUsers.new(@logger) | ||
end | ||
end | ||
|
||
class DummyTicket | ||
attr_reader :options | ||
|
||
def initialize(logger) | ||
@logger = logger | ||
end | ||
|
||
def create!(options) | ||
@options = options | ||
if should_raise_error? | ||
@logger.info("Simulating Zendesk ticket creation failure: #{options.inspect}") | ||
raise ZendeskAPI::Error::RecordInvalid.new(body: { "details" => "sample error message from Zendesk" }) | ||
else | ||
@logger.info("Zendesk ticket created: #{options.inspect}") | ||
end | ||
end | ||
|
||
protected | ||
|
||
def should_raise_error? | ||
description =~ /break_zendesk/ or comment =~ /break_zendesk/ | ||
end | ||
|
||
def description | ||
@options[:description] | ||
end | ||
|
||
def comment | ||
@options[:comment][:value] unless @options[:comment].nil? | ||
end | ||
end | ||
|
||
class DummyUsers | ||
def initialize(logger) | ||
@logger = logger | ||
end | ||
|
||
def search(_attributes) | ||
[] | ||
end | ||
|
||
def suspended?(_user_email) | ||
false | ||
end | ||
|
||
def create_or_update_user(new_attributes) | ||
@logger.info("Zendesk user created or updated: #{new_attributes.inspect}") | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
require "json" | ||
|
||
module Zendesk | ||
module TestHelpers | ||
def stub_zendesk_ticket_creation(ticket_properties = nil) | ||
stub = stub_request(:post, "#{zendesk_endpoint}/tickets") | ||
stub.with(body: { ticket: ticket_properties }) unless ticket_properties.nil? | ||
stub.to_return(status: 201, body: { ticket: { id: 12_345 } }.to_json, | ||
headers: { "Content-Type" => "application/json" }) | ||
end | ||
|
||
def stub_zendesk_ticket_creation_with_body(body) | ||
stub_request(:post, "#{zendesk_endpoint}/tickets") | ||
.with(body:) | ||
.to_return(status: 201, body: { ticket: { id: 12_345 } }.to_json, | ||
headers: { "Content-Type" => "application/json" }) | ||
end | ||
|
||
def expect_zendesk_to_receive_ticket(opts) | ||
stub_zendesk_ticket_creation_with_body("ticket" => hash_including(opts)) | ||
end | ||
|
||
def zendesk_endpoint | ||
"https://govuk.zendesk.com/api/v2" | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
require "zendesk/dummy_client" | ||
|
||
module Zendesk | ||
describe DummyClient do | ||
context "when a ticket has been raised" do | ||
let(:ticket_options) { { opt1: "val1" } } | ||
|
||
it "logs the ticket details" do | ||
logger = instance_double(Logger) | ||
expect(logger).to receive(:info).with("Zendesk ticket created: #{ticket_options.inspect}") | ||
|
||
client = described_class.new(logger:) | ||
client.ticket.create!(ticket_options) | ||
end | ||
|
||
it "can simulate failures, triggered by a specific description or comment" do | ||
logger = instance_double(Logger) | ||
client = described_class.new(logger:) | ||
expect(logger).to receive(:info).with(/Simulating Zendesk ticket creation failure/).twice | ||
|
||
expect { | ||
client.ticket.create!(description: "break_zendesk") | ||
}.to raise_error(ZendeskAPI::Error::RecordInvalid) | ||
|
||
expect { | ||
client.ticket.create!(comment: { value: "break_zendesk" }) | ||
}.to raise_error(ZendeskAPI::Error::RecordInvalid) | ||
end | ||
end | ||
|
||
context "when a user has been created" do | ||
let(:options) { { email: "[email protected]" } } | ||
|
||
it "logs the user details" do | ||
logger = instance_double(Logger) | ||
expect(logger).to receive(:info).with("Zendesk user created or updated: #{options.inspect}") | ||
|
||
client = described_class.new(logger:) | ||
client.users.create_or_update_user(options) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,5 @@ | ||
require "gds_zendesk/test_helpers" | ||
|
||
module ZendeskRequestMockingExtensions | ||
def expect_zendesk_to_receive_ticket(opts) | ||
stub_zendesk_ticket_creation_with_body("ticket" => hash_including(opts)) | ||
end | ||
end | ||
require "helpers/zendesk_test_helpers" | ||
|
||
RSpec.configure do |c| | ||
c.include GDSZendesk::TestHelpers | ||
c.include ZendeskRequestMockingExtensions | ||
c.include Zendesk::TestHelpers | ||
end |