Skip to content

Commit

Permalink
Merge pull request #1006 from alphagov/suspended-user-check
Browse files Browse the repository at this point in the history
Check if requester is suspended before creating Zendesk ticket
  • Loading branch information
AgaDufrat authored Aug 8, 2024
2 parents 272036b + 770d9fc commit 32e0fdb
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 4 deletions.
16 changes: 16 additions & 0 deletions app/models/support_ticket.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ class SupportTicket

validates :subject, :description, presence: true

validate :requester_not_suspended, if: :requester_email?

def initialize(attributes)
@subject = attributes.fetch(:subject, nil)
@description = attributes.fetch(:description, nil)
Expand All @@ -29,5 +31,19 @@ def zendesk_ticket_attributes

private

def requester_not_suspended
errors.add(:requester, "is suspended in Zendesk") if suspended_in_zendesk?
end

def suspended_in_zendesk?
user_search_result = GDS_ZENDESK_CLIENT.users.search(query: requester[:email])

user_search_result.empty? ? false : user_search_result.first["suspended"]
end

def requester_email?
requester && requester[:email]
end

attr_reader :subject, :description, :priority, :requester, :collaborators, :tags, :custom_fields, :ticket_form_id
end
18 changes: 18 additions & 0 deletions spec/helpers/zendesk_test_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,24 @@ def expect_zendesk_to_receive_ticket(opts)
stub_zendesk_ticket_creation_with_body("ticket" => hash_including(opts))
end

def zendesk_has_no_user_with_email(email)
stub_request(:get, "#{zendesk_endpoint}/users/search?query=#{email}")
.to_return(body: { users: [], previous_page: nil, next_page: nil, count: 0 }.to_json,
headers: { "Content-Type" => "application/json" })
end

def zendesk_has_suspended_user_with_email(email)
stub_request(:get, "#{zendesk_endpoint}/users/search?query=#{email}")
.to_return(body: { users: [{ email:, suspended: true }] }.to_json,
headers: { "Content-Type" => "application/json" })
end

def zendesk_has_valid_user_with_email(email)
stub_request(:get, "#{zendesk_endpoint}/users/search?query=#{email}")
.to_return(body: { users: [{ email:, suspended: false }] }.to_json,
headers: { "Content-Type" => "application/json" })
end

def zendesk_endpoint
"https://govuk.zendesk.com/api/v2"
end
Expand Down
64 changes: 62 additions & 2 deletions spec/models/support_ticket_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
require "rails_helper"

describe SupportTicket, "validations" do
let(:required_atrributes) do
{
subject: "Feedback for app",
description: "Ticket details go here.",
}
end

it "validates presence of subject" do
support_ticket = described_class.new({})
support_ticket.valid?
Expand All @@ -14,6 +21,59 @@

expect(support_ticket.errors.messages.to_h).to include(description: include("can't be blank"))
end

describe "#requester_not_suspended validation" do
it "is invalid if requester is suspended in Zendesk" do
zendesk_has_suspended_user_with_email("[email protected]")

support_ticket = described_class.new(
required_atrributes.merge(
requester: { "locale_id" => 1, email: "[email protected]", "name" => "Naughtly user" },
),
)
support_ticket.valid?

expect(support_ticket.errors.messages.to_h).to include(requester: include("is suspended in Zendesk"))
end

it "is valid if requester is not suspended in Zendesk" do
zendesk_has_valid_user_with_email("[email protected]")

support_ticket = described_class.new(
required_atrributes.merge(
{ requester: { email: "[email protected]" } },
),
)
expect(support_ticket.valid?).to eq(true)
end

it "is valid if Zendesk doesn't have user with this email address" do
zendesk_has_no_user_with_email("[email protected]")

support_ticket = described_class.new(
required_atrributes.merge(
{ requester: { "email" => "[email protected]", "name" => "User" } },
),
)

expect(support_ticket.valid?).to eq(true)
end

it "doesn't validate if requester attribute is not provided" do
support_ticket = described_class.new(required_atrributes)

expect(support_ticket.valid?).to eq(true)
end

it "doesn't validate if requester email attribute is not provided" do
support_ticket = described_class.new(
required_atrributes.merge(
{ requester: { "name" => "Some app" } },
),
)
expect(support_ticket.valid?).to eq(true)
end
end
end

describe SupportTicket, "#zendesk_ticket_attributes" do
Expand All @@ -22,7 +82,7 @@
subject: "Feedback for app",
description: "Ticket details go here.",
priority: "normal",
requester: { "locale_id" => 1, "email" => "someone@exampe.com", "name" => "Some user" },
requester: { "locale_id" => 1, "email" => "someone@example.com", "name" => "Some user" },
collaborators: %w[[email protected] [email protected]],
tags: %w[app_name],
custom_fields: [
Expand All @@ -38,7 +98,7 @@
"body" => "Ticket details go here.",
},
"priority" => "normal",
"requester" => { "locale_id" => 1, "email" => "someone@exampe.com", "name" => "Some user" },
"requester" => { "locale_id" => 1, "email" => "someone@example.com", "name" => "Some user" },
"collaborators" => %w[[email protected] [email protected]],
"tags" => %w[app_name],
"custom_fields" => [
Expand Down
6 changes: 4 additions & 2 deletions spec/requests/support_tickets_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
describe "Support Tickets" do
it "responds succesfully" do
stub_zendesk_ticket_creation
zendesk_has_valid_user_with_email("[email protected]")

post "/support-tickets",
params: {
subject: "Feedback for app",
tags: %w[app_name],
description: "Ticket details go here.",
priority: "normal",
requester: { locale_id: 1, email: "someone@exampe.com", name: "Some user" },
requester: { locale_id: 1, email: "someone@example.com", name: "Some user" },
collaborators: %w[[email protected] [email protected]],
custom_fields: [
{ id: 7_948_652_819_356, value: "cr_inaccuracy" },
Expand All @@ -24,6 +25,7 @@
end

it "sends the feedback to Zendesk" do
zendesk_has_valid_user_with_email("[email protected]")
zendesk_request = expect_zendesk_to_receive_ticket(
"subject" => "Feedback for app",
"tags" => %w[app_name],
Expand All @@ -36,7 +38,7 @@
params: {
subject: "Feedback for app",
tags: %w[app_name],
requester: { locale_id: 1, email: "someone@exampe.com", name: "Some user" },
requester: { locale_id: 1, email: "someone@example.com", name: "Some user" },
description: "Ticket details go here.",
}

Expand Down

0 comments on commit 32e0fdb

Please sign in to comment.