aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoachim Filip Ignacy Bartosik <jbartosik@gmail.com>2010-07-13 20:28:43 +0200
committerJoachim Filip Ignacy Bartosik <jbartosik@gmail.com>2010-07-29 19:49:16 +0200
commit9641aa1478f7a604eff2704edbfb7b6186bcb5e5 (patch)
tree7ad4ecf020174537db0415586d6ff4179d3b50da
parentAdd ability to receive emails through HTTP (diff)
downloadrecruiting-webapp-9641aa1478f7a604eff2704edbfb7b6186bcb5e5.tar.gz
recruiting-webapp-9641aa1478f7a604eff2704edbfb7b6186bcb5e5.tar.bz2
recruiting-webapp-9641aa1478f7a604eff2704edbfb7b6186bcb5e5.zip
Notify users trying to answer email question if question wasn't recognised
-rw-r--r--app/models/email_answer.rb7
-rw-r--r--app/models/user_mailer.rb18
-rw-r--r--app/views/user_mailer/unrecognized_email.erb7
-rw-r--r--spec/models/email_answer.rb23
-rw-r--r--spec/models/user_mailer_spec.rb20
5 files changed, 73 insertions, 2 deletions
diff --git a/app/models/email_answer.rb b/app/models/email_answer.rb
index 730f483..db2af75 100644
--- a/app/models/email_answer.rb
+++ b/app/models/email_answer.rb
@@ -18,8 +18,11 @@ class EmailAnswer < Answer
return unless user.token == subject.captures[1]
question = Question.first :conditions => { :id => subject.captures[0] }
- return if question.nil?
- return unless question.content.is_a? QuestionContentEmail
+
+ if(question.nil? || !question.content.is_a?(QuestionContentEmail))
+ UserMailer.deliver_unrecognized_email(user, email)
+ return
+ end
# Fetch existing answer, if it doesn't exist create a new one
# them mark it as incorrect (if it passes all tests it will be correct)
diff --git a/app/models/user_mailer.rb b/app/models/user_mailer.rb
index 1c9521e..7773c38 100644
--- a/app/models/user_mailer.rb
+++ b/app/models/user_mailer.rb
@@ -39,6 +39,24 @@ class UserMailer < ActionMailer::Base
@body = { :question_title=> question_title(comment.answer), :id => comment.answer.id }
end
+ def unrecognized_email(user, email)
+ common(user, "Your email sent to #{@app_name} wasn't recognized")
+
+ fields = [:subject, :from, :to].inject(String.new) do |res, cur|
+ field_name = cur.to_s.camelize
+ field_content = email.send(cur)
+
+ if field_content.class == Array # string with comma-separated values
+ field_content = field_content.inject(String.new){ |r,c| r += "#{c.to_s}, " }
+ field_content = field_content[0..-3]
+ end
+
+ res += "#{field_name}: #{field_content}\n"
+ end
+
+ @body = { :email => email, :app_name => @app_name, :fields => fields }
+ end
+
def receive(email)
# For now email answers for questions are only emails app receives
# so try use any received email as answer.
diff --git a/app/views/user_mailer/unrecognized_email.erb b/app/views/user_mailer/unrecognized_email.erb
new file mode 100644
index 0000000..ae2fd5a
--- /dev/null
+++ b/app/views/user_mailer/unrecognized_email.erb
@@ -0,0 +1,7 @@
+You sent email to <%= @app_name %> with following headers:
+
+<%= @fields %>
+It was not recognized by <%= @app_name %>.
+
+If you are answering question check if your message has proper subject.
+You will see proper subject for answer at bottom of question page.
diff --git a/spec/models/email_answer.rb b/spec/models/email_answer.rb
new file mode 100644
index 0000000..60b2730
--- /dev/null
+++ b/spec/models/email_answer.rb
@@ -0,0 +1,23 @@
+require 'spec_helper.rb'
+describe EmailAnswer do
+ it "should send email notification when parses answer for unrecognized question from known user" do
+ recruit = Factory(:recruit)
+ mail = TMail::Mail.new
+ mail.subject = "some subject"
+ mail.from = recruit.email_address
+
+ UserMailer.should_receive(:deliver_unrecognized_email).with(recruit, mail)
+ EmailAnswer.answer_from_email(mail)
+ end
+
+ it "should send email notification when parses answer for non-email question from known user" do
+ recruit = Factory(:recruit)
+ question = Factory(:question)
+ mail = TMail::Mail.new
+ mail.subject = "#{question.id}-#{recruit.token}"
+ mail.from = recruit.email_address
+
+ UserMailer.should_receive(:deliver_unrecognized_email).with(recruit, mail)
+ EmailAnswer.answer_from_email(mail)
+ end
+end
diff --git a/spec/models/user_mailer_spec.rb b/spec/models/user_mailer_spec.rb
index b3c631f..4e7c613 100644
--- a/spec/models/user_mailer_spec.rb
+++ b/spec/models/user_mailer_spec.rb
@@ -46,4 +46,24 @@ describe UserMailer do
notification.should have_text(/http:\/\/localhost:3000\/answers\/#{comment.answer.id}/)
notification.should have_subject('New comment')
end
+
+ it "should prepare proper unrecognized message email" do
+ user = Factory(:recruit)
+ mail = TMail::Mail.new
+ mail.subject = "some subject"
+ mail.from = user.email_address
+ mail.to = ["a@a.a", "b@b.b"]
+ notification = UserMailer.create_unrecognized_email(user, mail)
+
+ notification.should have_subject("Your email sent to #{@app_name} wasn't recognized")
+ notification.should deliver_from("no-reply@localhost")
+ notification.should deliver_to(user.email_address)
+
+ notification.should have_text(/You sent email to #{@app_name} with following headers:/)
+ notification.should have_text(/From: #{user.email_address}/)
+ notification.should have_text(/To: a@a.a, b@b.b/)
+ notification.should have_text(/Subject: some subject/)
+ # don't test rest of the message
+ notification.should have_text(/If you are answering question check if your message has proper subject./)
+ end
end