-
Notifications
You must be signed in to change notification settings - Fork 328
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix contact form not sending e-mails.
There were a couple issues with the contact form on the example static site that was preventing it from sending messages: - In the example static site, the form was missing a couple of new fields that the API now requires (a subject and the name of the API): NREL/api-umbrella-static-site@c02b886 - Since the Rails 4 upgrade in API Umbrella v0.14, the serialization of the e-mail messages into delayed job was broken (since ActiveJob won't serialize arbitrary models). This is fixed by serializing the data as a plain hash instead. This adds missing tests for this whole contact API, and also adds integration tests to make sure the example static site form works in the browser. 18F/api.data.gov#390
- Loading branch information
Showing
10 changed files
with
244 additions
and
9 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
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,11 +1,12 @@ | ||
class ContactMailer < ActionMailer::Base | ||
default :from => "noreply@#{ApiUmbrellaConfig[:web][:default_host]}" | ||
|
||
def contact_email(contact) | ||
@contact = contact | ||
def contact_email(contact_params) | ||
Rails.logger.info("CONTACT PARAMS: #{contact_params.inspect}") | ||
@contact = Contact.new(contact_params) | ||
|
||
mail :reply_to => contact.email, | ||
:subject => "#{ApiUmbrellaConfig[:site_name]} Contact Message from #{contact.email}", | ||
mail :reply_to => @contact.email, | ||
:subject => "#{ApiUmbrellaConfig[:site_name]} Contact Message from #{@contact.email}", | ||
:to => ApiUmbrellaConfig[:web][:contact_form_email] | ||
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,50 @@ | ||
require_relative "../../../test_helper" | ||
|
||
class Test::Apis::V1::Contact::TestEmail < Minitest::Test | ||
include ApiUmbrellaTestHelpers::Setup | ||
include ApiUmbrellaTestHelpers::DelayedJob | ||
|
||
def setup | ||
super | ||
setup_server | ||
|
||
response = Typhoeus.delete("http://127.0.0.1:#{$config["mailhog"]["api_port"]}/api/v1/messages") | ||
assert_response_code(200, response) | ||
end | ||
|
||
def test_sends_email | ||
user = FactoryGirl.create(:api_user, { | ||
:roles => ["api-umbrella-contact-form"], | ||
}) | ||
|
||
response = Typhoeus.post("https://127.0.0.1:9081/api-umbrella/v1/contact.json", http_options.deep_merge({ | ||
:headers => { | ||
"X-Api-Key" => user["api_key"], | ||
"Content-Type" => "application/x-www-form-urlencoded", | ||
}, | ||
:body => { | ||
:contact => { | ||
:name => "Foo", | ||
:email => "[email protected]", | ||
:api => "Example API", | ||
:subject => "Support", | ||
:message => "Message body", | ||
}, | ||
}, | ||
})) | ||
assert_response_code(200, response) | ||
data = MultiJson.load(response.body) | ||
assert_equal(["submitted"], data.keys) | ||
|
||
messages = delayed_job_sent_messages | ||
assert_equal(1, messages.length) | ||
message = messages.first | ||
|
||
assert_equal(["[email protected]"], message["Content"]["Headers"]["To"]) | ||
assert_equal(["API Umbrella Contact Message from [email protected]"], message["Content"]["Headers"]["Subject"]) | ||
assert_equal(["noreply@localhost"], message["Content"]["Headers"]["From"]) | ||
assert_equal(["[email protected]"], message["Content"]["Headers"]["Reply-To"]) | ||
assert_equal(["text/plain; charset=UTF-8"], message["Content"]["Headers"]["Content-Type"]) | ||
assert_equal("Name: Foo\r\nEmail: [email protected]\r\nAPI: Example API\r\nSubject: Support\r\n\r\n-------------------------------------\r\n\r\nMessage body\r\n\r\n-------------------------------------", message["Content"]["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,49 @@ | ||
require_relative "../../../test_helper" | ||
|
||
class Test::Apis::V1::Contact::TestPermissions < Minitest::Test | ||
include ApiUmbrellaTestHelpers::Setup | ||
parallelize_me! | ||
|
||
def setup | ||
super | ||
setup_server | ||
end | ||
|
||
def test_forbids_api_key_without_role | ||
user = FactoryGirl.create(:api_user, { | ||
:roles => ["xapi-umbrella-contact-form", "api-umbrella-contact-formx"], | ||
}) | ||
|
||
response = make_request(user) | ||
assert_response_code(401, response) | ||
end | ||
|
||
def test_allows_api_key_with_role | ||
user = FactoryGirl.create(:api_user, { | ||
:roles => ["api-umbrella-contact-form"], | ||
}) | ||
|
||
response = make_request(user) | ||
assert_response_code(200, response) | ||
end | ||
|
||
private | ||
|
||
def make_request(user) | ||
Typhoeus.post("https://127.0.0.1:9081/api-umbrella/v1/contact.json", http_options.deep_merge({ | ||
:headers => { | ||
"X-Api-Key" => user["api_key"], | ||
"Content-Type" => "application/x-www-form-urlencoded", | ||
}, | ||
:body => { | ||
:contact => { | ||
:name => "Foo", | ||
:email => "[email protected]", | ||
:api => "Example API", | ||
:subject => "Support", | ||
:message => "Message 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,89 @@ | ||
require_relative "../../../test_helper" | ||
|
||
class Test::Apis::V1::Contact::TestValidations < Minitest::Test | ||
include ApiUmbrellaTestHelpers::Setup | ||
parallelize_me! | ||
|
||
def setup | ||
super | ||
setup_server | ||
end | ||
|
||
def test_required | ||
response = make_request({}) | ||
assert_response_code(422, response) | ||
data = MultiJson.load(response.body) | ||
assert_equal(["errors"], data.keys) | ||
assert_equal([ | ||
{ | ||
"code" => "INVALID_INPUT", | ||
"message" => "Provide your first name.", | ||
"field" => "name", | ||
"full_message" => "Name: Provide your first name.", | ||
}, | ||
{ | ||
"code" => "INVALID_INPUT", | ||
"message" => "Provide your email address.", | ||
"field" => "email", | ||
"full_message" => "Email: Provide your email address.", | ||
}, | ||
{ | ||
"code" => "INVALID_INPUT", | ||
"message" => "Provide the API.", | ||
"field" => "api", | ||
"full_message" => "Api: Provide the API.", | ||
}, | ||
{ | ||
"code" => "INVALID_INPUT", | ||
"message" => "Provide a subject.", | ||
"field" => "subject", | ||
"full_message" => "Subject: Provide a subject.", | ||
}, | ||
{ | ||
"code" => "INVALID_INPUT", | ||
"message" => "Provide a message.", | ||
"field" => "message", | ||
"full_message" => "Message: Provide a message.", | ||
}, | ||
].sort_by { |e| e["full_message"] }, data["errors"].sort_by { |e| e["full_message"] }) | ||
end | ||
|
||
def test_email_format | ||
response = make_request({ | ||
:name => "Foo", | ||
:email => "foo@example", | ||
:api => "Example API", | ||
:subject => "Support", | ||
:message => "Message body", | ||
}) | ||
assert_response_code(422, response) | ||
data = MultiJson.load(response.body) | ||
assert_equal(["errors"], data.keys) | ||
assert_equal([ | ||
{ | ||
"code" => "INVALID_INPUT", | ||
"message" => "Provide a valid email address.", | ||
"field" => "email", | ||
"full_message" => "Email: Provide a valid email address.", | ||
}, | ||
], data["errors"]) | ||
end | ||
|
||
private | ||
|
||
def make_request(attributes) | ||
user = FactoryGirl.create(:api_user, { | ||
:roles => ["api-umbrella-contact-form"], | ||
}) | ||
|
||
Typhoeus.post("https://127.0.0.1:9081/api-umbrella/v1/contact.json", http_options.deep_merge({ | ||
:headers => { | ||
"X-Api-Key" => user["api_key"], | ||
"Content-Type" => "application/x-www-form-urlencoded", | ||
}, | ||
:body => { | ||
:contact => attributes, | ||
}, | ||
})) | ||
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,32 @@ | ||
require_relative "../test_helper" | ||
|
||
class Test::StaticSite::TestContact < Minitest::Capybara::Test | ||
include Capybara::Screenshot::MiniTestPlugin | ||
include ApiUmbrellaTestHelpers::Setup | ||
include ApiUmbrellaTestHelpers::DelayedJob | ||
|
||
def setup | ||
super | ||
setup_server | ||
|
||
response = Typhoeus.delete("http://127.0.0.1:#{$config["mailhog"]["api_port"]}/api/v1/messages") | ||
assert_response_code(200, response) | ||
end | ||
|
||
def test_submission | ||
visit "/contact/" | ||
assert_text("Contact us directly") | ||
|
||
fill_in "Name", :with => "Foo" | ||
fill_in "Email", :with => "[email protected]" | ||
fill_in "Which API are you inquiring about?", :with => "My API" | ||
select "Other", :from => "What can we help you with?" | ||
fill_in "Message", :with => "Test message" | ||
click_button "Send" | ||
|
||
assert_text("Thanks for sending your message. We'll be in touch.") | ||
|
||
messages = delayed_job_sent_messages | ||
assert_equal(1, messages.length) | ||
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