summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHarald Eilertsen <haraldei@anduin.net>2015-11-21 17:44:31 +0100
committerHarald Eilertsen <haraldei@anduin.net>2015-11-21 17:53:53 +0100
commitf37dedaca58ec0a3673ed7f10bf8f5072455680b (patch)
tree90234af0938773288584f8b3218135f49496520b
parent732a5b9c15c4b8e96d7cd3d5fb0e8d6463dc8928 (diff)
downloadnorsk-urskog-registrations-f37dedaca58ec0a3673ed7f10bf8f5072455680b.tar.gz
norsk-urskog-registrations-f37dedaca58ec0a3673ed7f10bf8f5072455680b.tar.bz2
norsk-urskog-registrations-f37dedaca58ec0a3673ed7f10bf8f5072455680b.zip
revert back to form if email delivery fails.
-rw-r--r--lib/band.rb2
-rw-r--r--lib/registration.rb2
-rw-r--r--registration.rb3
-rw-r--r--spec/email_spec.rb30
4 files changed, 36 insertions, 1 deletions
diff --git a/lib/band.rb b/lib/band.rb
index a951059..efccc5f 100644
--- a/lib/band.rb
+++ b/lib/band.rb
@@ -4,7 +4,7 @@ require_relative 'song'
class Band
attr_reader :name, :city, :website, :label, :short_bio, :contact, :members, :songs
- attr_reader :errors
+ attr_accessor :errors
def initialize(params = nil)
@errors = []
diff --git a/lib/registration.rb b/lib/registration.rb
index e38f0fe..6afe8b6 100644
--- a/lib/registration.rb
+++ b/lib/registration.rb
@@ -14,6 +14,8 @@ def send_registration_emails_for(band, pdf_url)
mail.delivery_method :smtp, :address => settings.smtp['address'], :port => settings.smtp['port']
end
mail.deliver!
+rescue Net::SMTPError => e
+ band.errors << e.message
end
def generate_pdf_for(band, filename)
diff --git a/registration.rb b/registration.rb
index 2801a64..69c074a 100644
--- a/registration.rb
+++ b/registration.rb
@@ -24,6 +24,9 @@ class RegistrationApp < Sinatra::Base
pdf_file = File.join("/uploads", create_pdf_file_name(@band))
generate_pdf_for(@band, File.join(settings.public_folder, pdf_file))
send_registration_emails_for(@band, url_for(pdf_file, :full))
+ end
+
+ if @band.valid?
erb :submitted
else
erb :index
diff --git a/spec/email_spec.rb b/spec/email_spec.rb
new file mode 100644
index 0000000..5c4d310
--- /dev/null
+++ b/spec/email_spec.rb
@@ -0,0 +1,30 @@
+require 'spec_helper'
+require 'net/smtp'
+
+RSpec.describe "sending email" do
+ include Rack::Test::Methods
+
+ def app
+ RegistrationApp
+ end
+
+ context "with delivery error" do
+ let(:band_params) { create_band_params }
+
+ before :each do
+ @mail_spy = spy("Mail")
+ allow(Mail).to receive('new') { @mail_spy }
+ allow(@mail_spy).to receive(:deliver!).and_raise(Net::SMTPFatalError.new("Some error message"))
+
+ post '/submit', 'band' => band_params
+ end
+
+ it "goes back to the registration form" do
+ expect(last_response.body).to match(/form id="registration-form"/)
+ end
+
+ it "shows what went wrong" do
+ expect(last_response.body).to match(/Some error message/)
+ end
+ end
+end