-
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 #956 from alphagov/add-raise-zendesk-feature
Allow any app to create support tickets
- Loading branch information
Showing
5 changed files
with
186 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
class SupportTicketsController < ApplicationController | ||
def create | ||
support_ticket = SupportTicket.new(attributes) | ||
|
||
if support_ticket.valid? | ||
GDS_ZENDESK_CLIENT.ticket.create!(support_ticket.attributes) | ||
|
||
render json: { status: "success" }, status: :created | ||
else | ||
render json: { status: "error", errors: support_ticket.errors }, status: :unprocessable_entity | ||
end | ||
end | ||
|
||
private | ||
|
||
def attributes | ||
params.slice(:subject, :tags, :user_agent, :description) | ||
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,34 @@ | ||
class SupportTicket | ||
include ActiveModel::Validations | ||
|
||
validates :subject, :description, presence: true | ||
|
||
def initialize(attributes) | ||
@subject = attributes.fetch(:subject, nil) | ||
@tags = attributes.fetch(:tags, nil) | ||
@user_agent = attributes.fetch(:user_agent, nil) | ||
@description = attributes.fetch(:description, nil) | ||
end | ||
|
||
def attributes | ||
{ | ||
"subject" => subject, | ||
"tags" => tags, | ||
"body" => ticket_body, | ||
} | ||
end | ||
|
||
private | ||
|
||
attr_reader :subject, :tags, :user_agent, :description | ||
|
||
def ticket_body | ||
<<-TICKET_BODY.strip_heredoc | ||
[User agent] | ||
#{user_agent} | ||
[Details] | ||
#{description} | ||
TICKET_BODY | ||
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
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,60 @@ | ||
require "rails_helper" | ||
|
||
describe SupportTicket, "validations" do | ||
it "validates presence of subject" do | ||
support_ticket = described_class.new({}) | ||
support_ticket.valid? | ||
|
||
expect(support_ticket.errors.messages.to_h).to include(subject: include("can't be blank")) | ||
end | ||
|
||
it "validates presence of description" do | ||
support_ticket = described_class.new({}) | ||
support_ticket.valid? | ||
|
||
expect(support_ticket.errors.messages.to_h).to include(description: include("can't be blank")) | ||
end | ||
end | ||
|
||
describe SupportTicket, "#attributes" do | ||
it "generates a hash of attributes to create a Zendesk ticket" do | ||
support_ticket = described_class.new( | ||
subject: "Feedback for app", | ||
tags: %w[app_name], | ||
user_agent: "Safari", | ||
description: "Ticket details go here.", | ||
) | ||
|
||
expect(support_ticket.attributes).to eq( | ||
"subject" => "Feedback for app", | ||
"tags" => %w[app_name], | ||
"body" => <<-TICKET_BODY.strip_heredoc, | ||
[User agent] | ||
Safari | ||
[Details] | ||
Ticket details go here. | ||
TICKET_BODY | ||
) | ||
end | ||
|
||
it "generates a hash of attributes where the body omits the optional user agent" do | ||
support_ticket = described_class.new( | ||
subject: "Feedback for app", | ||
tags: %w[app_name], | ||
description: "Ticket details go here.", | ||
) | ||
|
||
expect(support_ticket.attributes).to eq( | ||
"subject" => "Feedback for app", | ||
"tags" => %w[app_name], | ||
"body" => <<-TICKET_BODY.strip_heredoc, | ||
[User agent] | ||
[Details] | ||
Ticket details go here. | ||
TICKET_BODY | ||
) | ||
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,68 @@ | ||
require "rails_helper" | ||
|
||
describe "Support Tickets" do | ||
it "responds succesfully" do | ||
stub_zendesk_ticket_creation | ||
|
||
post "/support-tickets", | ||
params: { | ||
subject: "Feedback for app", | ||
tags: %w[app_name], | ||
user_agent: "Safari", | ||
description: "Ticket details go here.", | ||
} | ||
|
||
expect(response.code).to eq("201") | ||
expect(response_hash).to include("status" => "success") | ||
end | ||
|
||
it "sends the feedback to Zendesk" do | ||
zendesk_request = expect_zendesk_to_receive_ticket( | ||
"subject" => "Feedback for app", | ||
"tags" => %w[app_name], | ||
"body" => <<-TICKET_BODY.strip_heredoc, | ||
[User agent] | ||
Safari | ||
[Details] | ||
Ticket details go here. | ||
TICKET_BODY | ||
) | ||
|
||
post "/support-tickets", | ||
params: { | ||
subject: "Feedback for app", | ||
tags: %w[app_name], | ||
user_agent: "Safari", | ||
description: "Ticket details go here.", | ||
} | ||
|
||
expect(zendesk_request).to have_been_made | ||
end | ||
|
||
it "responds unsuccessfully if the feedback isn't valid" do | ||
post "/support-tickets", | ||
params: { subject: "Feedback for app" } | ||
|
||
expect(response.code).to eq("422") | ||
expect(response_hash).to include("status" => "error") | ||
end | ||
|
||
it "returns error if the subject field is empty" do | ||
post "/support-tickets", | ||
params: { description: "Ticket details go here." } | ||
|
||
expect(response_hash).to include("errors" => include("subject" => include("can't be blank"))) | ||
end | ||
|
||
it "returns error if the description field is empty" do | ||
post "/support-tickets", | ||
params: { subject: "Feedback for app" } | ||
|
||
expect(response_hash).to include("errors" => include("description" => include("can't be blank"))) | ||
end | ||
|
||
def response_hash | ||
JSON.parse(response.body) | ||
end | ||
end |