Skip to content

Commit

Permalink
fix(dashboard): gh #731 move queue param from RequestTrackerService t…
Browse files Browse the repository at this point in the history
…o SupportTicket class
  • Loading branch information
whorka committed May 23, 2022
1 parent 5518434 commit 1653517
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ def create
end

rts = RequestTrackerService.new ::Configuration.request_tracker_config
rts.selected_queue = queue
ticket_id = rts.create_ticket(@support_ticket)
logger.info "action=createSupportTicket result=success user=#{@user} subject=#{@support_ticket.subject} ticket=#{ticket_id} queue=#{queue}"
redirect_to root_url, :flash => { :notice => "Support ticket created - Ticket id: #{ticket_id}" }
Expand Down
2 changes: 1 addition & 1 deletion dashboard/application/app/models/support_ticket.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class SupportTicket
include ActiveModel::Model

attr_accessor :username, :email, :cc, :subject, :session_id, :description, :attachments
attr_accessor :username, :email, :cc, :subject, :session_id, :description, :attachments, :queue
validates :username, :email, :subject, :description, presence: { message: 'is required' }
validates :email, :cc, format: { with: URI::MailTo::EMAIL_REGEXP, allow_blank: true, message: 'format is invalid' }
validates :attachments, attachments: true
Expand Down
25 changes: 11 additions & 14 deletions dashboard/application/app/services/request_tracker_service.rb
Original file line number Diff line number Diff line change
@@ -1,25 +1,12 @@
class RequestTrackerService

attr_reader :selected_queue

def selected_queue=(queue)
# no-op on null string (form default)
return if queue==""
if @queues.include?(queue)
@selected_queue = queue
else
raise ArgumentError, "invalid queue selection"
end
end

def initialize(config)
@queues = config[:queues]
@priority = config[:priority]

if !@queues || @queues.empty? || !@priority
raise ArgumentError, "queues and priority are required options for RT service"
end
@selected_queue = @queues[0]
end

def create_ticket(support_ticket_request)
Expand All @@ -41,8 +28,18 @@ def create_ticket(support_ticket_request)
private

def create_payload(support_ticket_request, ticket_text)
# default to first configured queue
queue = @queues[0]
if support_ticket_request.queue && support_ticket_request.queue!=""
if @queues.include?(support_ticket_request.queue)
queue = support_ticket_request.queue
else
raise ArgumentError, "invalid queue selection"
end
end

payload = {
Queue: @selected_queue,
Queue: queue,
Requestor: support_ticket_request.email,
Cc: support_ticket_request.cc,
Priority: @priority,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<div class="form-container">
<form id="new_support_ticket" role="form" action="<%= support_path %>" accept-charset="UTF-8" method="post" enctype="multipart/form-data">
<input type="hidden" name="<%= request_forgery_protection_token %>" value="<%= form_authenticity_token %>" />
<input type="hidden" id="queue" name="queue" value="" />
<input type="hidden" id="queue" name="support_ticket[queue]" value="" />

<div class="form-group">
<label class="control-label" for="username">User name</label>
Expand Down
1 change: 1 addition & 0 deletions dashboard/application/config/rt_config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ user: "root"
pass: "password"
queues:
- "General"
- "Alternate"
69 changes: 50 additions & 19 deletions dashboard/application/test/services/request_tracker_service_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,42 +20,48 @@ class RequestTrackerServiceTest < ActiveSupport::TestCase
assert_raises(ArgumentError) { RequestTrackerService.new(config) }
end

test "should permit the selection of an alternate queue" do
config = {
queues: [ "Standard", "Alternate" ],
priority: "33",
test "create_ticket should run with no errors" do
service_config = {
queues: [ "Standard" ],
priority: "10",
}

rts = RequestTrackerService.new(config)
rts.selected_queue = "Alternate"
assert_equal rts.selected_queue, "Alternate"
end
support_ticket = SupportTicket.new(email: "email@example.com", cc: "cc@example.com", subject: "Subject")

test "should throw exception when selecting an invalid queue" do
config = {
queues: [ "Standard", "Alternate" ],
priority: "33",
}
mock_rt_client = mock("rt_client")
mock_rt_client.expects(:create).with do |param_hash|

rts = RequestTrackerService.new(config)
assert_raises(ArgumentError) { rts.selected_queue = "Bogus" }
param_hash[:Requestor] == support_ticket.email &&
param_hash[:Cc] == support_ticket.cc &&
param_hash[:Subject] == support_ticket.subject &&
param_hash[:Queue] == service_config[:queues][0] &&
param_hash[:Priority] == service_config[:priority]
end
.returns("support_ticket_id")

BatchConnect::Session.stubs(:find).returns(mock("session"))
RequestTrackerClient.stubs(:create).returns(mock_rt_client)

result = RequestTrackerService.new(service_config).create_ticket(support_ticket)

assert_equal "support_ticket_id", result
end

test "create_ticket should run with no errors" do
test "create_ticket should run with no errors when selecting an alternate queue" do
service_config = {
queues: [ "Standard" ],
queues: [ "Standard", "Alternate" ],
priority: "10",
}

support_ticket = SupportTicket.new(email: "email@example.com", cc: "cc@example.com", subject: "Subject")
support_ticket = SupportTicket.new(email: "email@example.com", cc: "cc@example.com", subject: "Subject", queue: "Alternate")

mock_rt_client = mock("rt_client")
mock_rt_client.expects(:create).with do |param_hash|

param_hash[:Requestor] == support_ticket.email &&
param_hash[:Cc] == support_ticket.cc &&
param_hash[:Subject] == support_ticket.subject &&
param_hash[:Queue] == service_config[:queues][0] &&
param_hash[:Queue] == support_ticket.queue &&
param_hash[:Priority] == service_config[:priority]
end
.returns("support_ticket_id")
Expand All @@ -68,4 +74,29 @@ class RequestTrackerServiceTest < ActiveSupport::TestCase
assert_equal "support_ticket_id", result
end

test "create_ticket should raise an error when selecting an invalid queue" do
service_config = {
queues: [ "Standard", "Alternate" ],
priority: "10",
}

support_ticket = SupportTicket.new(email: "email@example.com", cc: "cc@example.com", subject: "Subject", queue: "Not_A_Queue")

# mock_rt_client = mock("rt_client")
# mock_rt_client.expects(:create).with do |param_hash|
#
# param_hash[:Requestor] == support_ticket.email &&
# param_hash[:Cc] == support_ticket.cc &&
# param_hash[:Subject] == support_ticket.subject &&
# param_hash[:Queue] == support_ticket.queue &&
# param_hash[:Priority] == service_config[:priority]
# end
# .returns("support_ticket_id")
#
# BatchConnect::Session.stubs(:find).returns(mock("session"))
# RequestTrackerClient.stubs(:create).returns(mock_rt_client)

assert_raises(ArgumentError) { RequestTrackerService.new(service_config).create_ticket(support_ticket) }

end
end

0 comments on commit 1653517

Please sign in to comment.