Skip to content

Commit

Permalink
Add email interceptor (AgileVentures#2363)
Browse files Browse the repository at this point in the history
* Add email interceptor

* Add environment variable intercept_emails

* Update unit test for alert_project_creator_about_new_member

* Add unit test for delivering_email model method of SandboxEmailInterceptor
  • Loading branch information
Mariam Ahhttouche authored and tansaku committed May 14, 2018
1 parent cbd4455 commit ac4c01a
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 15 deletions.
5 changes: 5 additions & 0 deletions app/mailers/sandbox_email_interceptor.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class SandboxEmailInterceptor
def self.delivering_email(message)
message.to = ENV['USER_EMAIL']
end
end
3 changes: 3 additions & 0 deletions config/initializers/sandbox_email_interceptor.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
if (Rails.env.development? || Rails.env.staging?) and ENV['INTERCEPT_EMAILS'] == 'true'
ActionMailer::Base.register_interceptor(SandboxEmailInterceptor)
end
15 changes: 15 additions & 0 deletions spec/mailers/mailer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,19 @@
expect(mail).to have_default_cc_addresses
end
end
describe '#alert_project_creator_about_new_member' do
before(:each) do
@user1 = FactoryBot.create(:user, first_name: 'Reva', last_name: 'Satterfield')
@project = FactoryBot.create(:project, title: 'Title 1', user: @user1)
@user2 = FactoryBot.create(:user, email: 'leif.kozey@bruen.org')
end
it 'sends an email to project creator about new member' do
email = Mailer.alert_project_creator_about_new_member(@project, @user2).deliver_now
assert !ActionMailer::Base.deliveries.empty?
assert_equal [@user1.email], email.to
assert_equal "#{@user2.display_name} just joined #{@project.title} project", email.subject
assert_equal read_fixture('project_creator_notification_text').join, email.text_part.body.to_s
assert_equal read_fixture('project_creator_notification_html').join, email.html_part.body.to_s
end
end
end
17 changes: 17 additions & 0 deletions spec/mailers/sandbox_email_interceptor_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
require 'spec_helper'

describe SandboxEmailInterceptor do
describe '#delivering_email' do
before(:each) do
@user1 = FactoryBot.create(:user)
@project = FactoryBot.create(:project, user: @user1)
@user2 = FactoryBot.create(:user)
end
it 'delivers all emails to user when intercept_emails is set to true' do
stub_const('ENV', {'USER_EMAIL' => 'me@ymail.com'})
mail = Mailer.alert_project_creator_about_new_member(@project, @user2).deliver_now
SandboxEmailInterceptor.delivering_email(mail)
expect(ActionMailer::Base.deliveries[0].to).to include(ENV['USER_EMAIL'])
end
end
end
15 changes: 0 additions & 15 deletions spec/models/mailer_spec.rb

This file was deleted.

20 changes: 20 additions & 0 deletions test/fixtures/mailer/project_creator_notification_html
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<html>
<body>
<!DOCTYPE html>
<html>
<head>
<meta content='text/html; charset=UTF-8' http-equiv='Content-Type' />
</head>
<body>
<p>Hi Reva Satterfield,<br></p>
<p>
<a href="mailto:leif.kozey@bruen.org">leif.kozey@bruen.org</a> just joined your project Title 1, you can reach out and personally welcome them.<br>
</p>
<p>
You are receiving this email because you registered at <a href="http://www.agileventures.org/">www.agileventures.org</a>. If you wish to be removed from this mailing list, please login at av.org and go to your profile page and edit "Receive site emails" to be unchecked - if you are already logged in go directly to: <a href="http://www.agileventures.org/users/edit">www.agileventures.org/users/edit</a>.
</p>
</body>
</html>

</body>
</html>
9 changes: 9 additions & 0 deletions test/fixtures/mailer/project_creator_notification_text
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Hi Reva Satterfield,
===========================================================================

leif.kozey@bruen.org just joined your project Title 1, you can reach out and personally welcome them.
===========================================================================

You are receiving this email because you registered at http://www.agileventures.org. If you wish to be removed from this mailing list, please login at av.org and go to your profile page and edit "Receive site emails" to be unchecked - if you are already logged in go directly to: http://www.agileventures.org/users/edit/.


0 comments on commit ac4c01a

Please sign in to comment.