Skip to content

Commit

Permalink
[#4130] Use libanswers API for SuggestCorrectionForm
Browse files Browse the repository at this point in the history
  • Loading branch information
sandbergja committed Sep 13, 2024
1 parent b0b5bc8 commit 4195bee
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 29 deletions.
15 changes: 10 additions & 5 deletions app/forms/suggest_correction_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,17 @@ class SuggestCorrectionForm
validates :name, :email, :message, :context, presence: true
validates :email, email: true

def email_subject
"[Catalog] #{title}"
end

def submit
ContactMailer.with(form: self).suggestion.deliver unless spam?
unless spam?
RecordFeedbackFormSubmission.new(
message:,
patron_name: name,
patron_email: email,
title: "[Catalog] #{title}",
context:,
quid: Rails.application.config_for(:orangelight)[:suggest_correction_form][:queue_id]
).send_to_libanswers
end
@submitted = true
@name = ""
@email = ""
Expand Down
2 changes: 2 additions & 0 deletions app/models/record_feedback_form_submission.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# form submission to the libanswers API,
# which will create a ticket for us to answer
class RecordFeedbackFormSubmission
# rubocop:disable Metrics/ParameterLists
def initialize(message:, patron_name:, patron_email:, title:, context:, quid:)
@message = message
@patron_name = patron_name
Expand All @@ -11,6 +12,7 @@ def initialize(message:, patron_name:, patron_email:, title:, context:, quid:)
@context = context
@quid = quid
end
# rubocop:enable Metrics/ParameterLists

def send_to_libanswers
Net::HTTP.post uri, body, { Authorization: "Bearer #{token}" }
Expand Down
3 changes: 3 additions & 0 deletions config/orangelight.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ defaults: &defaults
queue_id: <%= ENV['ASK_A_QUESTION_QUEUE_ID'] %>
suggest_correction_form:
to: <%= ENV['OL_CATALOGING_TO'] %>
queue_id: <%= ENV['SUGGEST_CORRECTION_QUEUE_ID'] %>
report_harmful_language_form:
to: <%= ENV['OL_HARMFUL_CONTENT_TO'] %>
queue_id: <%= ENV['REPORT_HARMFUL_LANGUAGE_QUEUE_ID'] %>
Expand Down Expand Up @@ -38,6 +39,7 @@ development:
db: 'orangelight_dev'
suggest_correction_form:
to: 'test-correction@princeton.edu'
queue_id: <%= ENV['SUGGEST_CORRECTION_QUEUE_ID'] %>
report_harmful_language_form:
to: 'test-harmful-content@princeton.edu'
queue_id: <%= ENV['REPORT_HARMFUL_LANGUAGE_QUEUE_ID'] %>
Expand All @@ -56,6 +58,7 @@ test:
db: 'orangelight_test'
suggest_correction_form:
to: 'test-correction@princeton.edu'
queue_id: 3456
report_harmful_language_form:
to: 'test-harmful-content@princeton.edu'
queue_id: 9012
Expand Down
29 changes: 11 additions & 18 deletions spec/forms/suggest_correction_form_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true
require "rails_helper"

RSpec.describe SuggestCorrectionForm do
RSpec.describe SuggestCorrectionForm, libanswers: true do
let(:valid_attributes) do
{
"name" => "Test",
Expand All @@ -26,35 +26,28 @@
end
end

describe "#email_subject" do
it "uses the name of the object" do
form = described_class.new(valid_attributes)

expect(form.email_subject).to eq "[Catalog] Example Record"
end
end

describe "submit" do
it "sends an email and resets its attributes, setting itself as submitted" do
stub_libanswers_api
form = described_class.new(valid_attributes)

form.submit
expect(ActionMailer::Base.deliveries.length).to eq 1
expect(form.name).to eq ""
expect(form.email).to eq ""
expect(form.message).to eq ""
expect(form.context).to eq "http://example.com/catalog/1"
expect(form.title).to eq "Example Record"
expect(form).to be_submitted

mail = ActionMailer::Base.deliveries.first
expect(mail.subject).to eq "[Catalog] Example Record"
expect(mail.from).to eq ["test@test.org"]
expect(mail.body).to include "Name: Test"
expect(mail.body).to include "Email: test@test.org"
expect(mail.body).to include "[Catalog] Example Record"
expect(mail.body).to include "You should fix the thumbnail"
expect(mail.body).to include "Context: http://example.com/catalog/1"
expect(WebMock).to have_requested(
:post,
'https://faq.library.princeton.edu/api/1.1/ticket/create'
).with(body: 'quid=3456&'\
'pquestion=[Catalog] Example Record&'\
"pdetails=You should fix the thumbnail\n\nSent from http://example.com/catalog/1 via LibAnswers API&"\
'pname=Test&'\
'pemail=test@test.org',
headers: { Authorization: 'Bearer abcdef1234567890abcdef1234567890abcdef12' })
end
end

Expand Down
2 changes: 1 addition & 1 deletion spec/models/record_feedback_form_submission_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
stub_libanswers_api

described_class.new(
quid: 12345,
quid: 12_345,
message: '', patron_name: '', patron_email: '', title: '', context: ''
).send_to_libanswers

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true
require 'rails_helper'

RSpec.describe(SuggestCorrectionForm) do
RSpec.describe SuggestCorrectionForm, libanswers: true do
context 'when a robot fills in the hidden honeypot field' do
before do
visit '/suggest_correction?suggest_correction_form[id]=99105509673506421&suggest_correction_form[title]=Princeton+international.'
Expand All @@ -10,10 +10,12 @@
fill_in 'suggest_correction_form_message', with: 'I am a HAL 9000 computer. I became operational at the H.A.L. plant in Urbana, Illinois on the 12th of January 1992.'
find('#suggest_correction_form_feedback_desc', visible: :hidden).set 'Filling in the honeypot field'
end
it 'does not generate an email' do
expect { click_button 'Send' }.not_to change {
ActionMailer::Base.deliveries.count
}
it 'does not send the question to libanswers' do
click_button 'Send'
expect(WebMock).not_to have_requested(
:post,
'https://faq.library.princeton.edu/api/1.1/ticket/create'
)
end
it 'does report success' do
click_button 'Send'
Expand Down

0 comments on commit 4195bee

Please sign in to comment.