diff --git a/Gemfile b/Gemfile index 1cb5520ad3bdd5c801bcbb2ebccb041e5d12a86a..af5b0241397351d56fadf60e90af1b1679bf5e76 100644 --- a/Gemfile +++ b/Gemfile @@ -66,4 +66,5 @@ group :test do gem "turn", :require => false gem "simplecov", :require => false gem "shoulda", "3.0.1" + gem 'email_spec' end diff --git a/Gemfile.lock b/Gemfile.lock index 182cf669197d198dd513735d944ab41b14b0256e..6a18f7d17d618afeb876242ebea3e0dff6722af9 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -114,6 +114,9 @@ GEM warden (~> 1.1) diff-lcs (1.1.3) drapper (0.8.4) + email_spec (1.2.1) + mail (~> 2.2) + rspec (~> 2.0) erubis (2.7.0) escape_utils (0.2.4) eventmachine (0.12.10) @@ -330,6 +333,7 @@ DEPENDENCIES database_cleaner devise (~> 1.5) drapper + email_spec faker foreman git diff --git a/app/mailers/notify.rb b/app/mailers/notify.rb index 967be900e07ddb1f7185f35e8dcb88c9bc450553..05fd17b511ab6038d740b946a441b63ca93da3d6 100644 --- a/app/mailers/notify.rb +++ b/app/mailers/notify.rb @@ -7,71 +7,60 @@ class Notify < ActionMailer::Base default from: EMAIL_OPTS["from"] - def new_user_email(user, password) - @user = user + def new_user_email(user_id, password) + @user = User.find(user_id) @password = password - mail(:to => @user['email'], :subject => "gitlab | Account was created for you") + mail(:to => @user.email, :subject => "gitlab | Account was created for you") end - def new_issue_email(issue) - @issue = Issue.find(issue['id']) - @user = @issue.assignee - @project = @issue.project - - mail(:to => @user.email, :subject => "gitlab | New Issue was created") + def new_issue_email(issue_id) + @issue = Issue.find(issue_id) + mail(:to => @issue.assignee_email, :subject => "gitlab | New Issue was created") end - def note_wall_email(user, note) - @user = user - @note = Note.find(note['id']) - @project = @note.project - mail(:to => @user['email'], :subject => "gitlab | #{@note.project.name} ") + def note_wall_email(recipient_id, note_id) + recipient = User.find(recipient_id) + @note = Note.find(note_id) + mail(:to => recipient.email, :subject => "gitlab | #{@note.project_name} ") end - def note_commit_email(user, note) - @user = user - @note = Note.find(note['id']) - @project = @note.project + def note_commit_email(recipient_id, note_id) + recipient = User.find(recipient_id) + @note = Note.find(note_id) @commit = @note.target - mail(:to => @user['email'], :subject => "gitlab | note for commit | #{@note.project.name} ") + mail(:to => recipient.email, :subject => "gitlab | note for commit | #{@note.project_name} ") end - - def note_merge_request_email(user, note) - @user = user - @note = Note.find(note['id']) - @project = @note.project + + def note_merge_request_email(recipient_id, note_id) + recipient = User.find(recipient_id) + @note = Note.find(note_id) @merge_request = @note.noteable - mail(:to => @user['email'], :subject => "gitlab | note for merge request | #{@note.project.name} ") + mail(:to => recipient.email, :subject => "gitlab | note for merge request | #{@note.project_name} ") end - def note_issue_email(user, note) - @user = user - @note = Note.find(note['id']) - @project = @note.project + def note_issue_email(recipient_id, note_id) + recipient = User.find(recipient_id) + @note = Note.find(note_id) @issue = @note.noteable - mail(:to => @user['email'], :subject => "gitlab | note for issue #{@issue.id} | #{@note.project.name} ") + mail(:to => recipient.email, :subject => "gitlab | note for issue #{@issue.id} | #{@note.project_name} ") end - - def new_merge_request_email(merge_request) - @merge_request = MergeRequest.find(merge_request['id']) - @user = @merge_request.assignee - @project = @merge_request.project - mail(:to => @user.email, :subject => "gitlab | new merge request | #{@merge_request.title} ") + + def new_merge_request_email(merge_request_id) + @merge_request = MergeRequest.find(merge_request_id) + mail(:to => @merge_request.assignee_email, :subject => "gitlab | new merge request | #{@merge_request.title} ") end - - def changed_merge_request_email(user, merge_request) - @user = user - @merge_request = MergeRequest.find(merge_request.id) - @assignee_was ||= User.find(@merge_request.assignee_id_was) - @project = @merge_request.project - mail(:to => @user['email'], :subject => "gitlab | merge request changed | #{@merge_request.title} ") + + def reassigned_merge_request_email(recipient_id, merge_request_id, previous_assignee_id) + recipient = User.find(recipient_id) + @merge_request = MergeRequest.find(merge_request_id) + @previous_assignee ||= User.find(previous_assignee_id) + mail(:to => recipient.email, :subject => "gitlab | merge request changed | #{@merge_request.title} ") end - - def changed_issue_email(user, issue) - @issue = Issue.find(issue['id']) - @user = user - @assignee_was ||= User.find(@issue.assignee_id_was) - @project = @issue.project - mail(:to => @user['email'], :subject => "gitlab | changed issue | #{@issue.title} ") + + def reassigned_issue_email(recipient_id, issue_id, previous_assignee_id) + recipient = User.find(recipient_id) + @issue = Issue.find(issue_id) + @previous_assignee ||= User.find(previous_assignee_id) + mail(:to => recipient.email, :subject => "gitlab | changed issue | #{@issue.title} ") end end diff --git a/app/models/mailer_observer.rb b/app/models/mailer_observer.rb index f84cbdead59b9b3ea63db38c5b62b0abbf62cfed..e581ae804e8a78890e99c117b5ae60b9097de4ff 100644 --- a/app/models/mailer_observer.rb +++ b/app/models/mailer_observer.rb @@ -18,12 +18,12 @@ class MailerObserver < ActiveRecord::Observer def new_issue(issue) if issue.assignee != current_user - Notify.new_issue_email(issue).deliver + Notify.new_issue_email(issue.id).deliver end end def new_user(user) - Notify.new_user_email(user, user.password).deliver + Notify.new_user_email(user.id, user.password).deliver end def new_note(note) @@ -32,26 +32,26 @@ class MailerObserver < ActiveRecord::Observer note.project.users.reject { |u| u.id == current_user.id } .each do |u| case note.noteable_type when "Commit" then - Notify.note_commit_email(u, note).deliver + Notify.note_commit_email(u.id, note.id).deliver when "Issue" then - Notify.note_issue_email(u, note).deliver + Notify.note_issue_email(u.id, note.id).deliver when "MergeRequest" then - Notify.note_merge_request_email(u, note).deliver + Notify.note_merge_request_email(u.id, note.id).deliver when "Snippet" true else - Notify.note_wall_email(u, note).deliver + Notify.note_wall_email(u.id, note.id).deliver end end # Notify only author of resource elsif note.notify_author - Notify.note_commit_email(note.commit_author, note).deliver + Notify.note_commit_email(note.commit_author.id, note.id).deliver end end def new_merge_request(merge_request) if merge_request.assignee != current_user - Notify.new_merge_request_email(merge_request).deliver + Notify.new_merge_request_email(merge_request.id).deliver end end @@ -61,7 +61,7 @@ class MailerObserver < ActiveRecord::Observer recipients_ids.delete current_user.id User.find(recipients_ids).each do |user| - Notify.changed_merge_request_email(user, merge_request).deliver + Notify.reassigned_merge_request_email(user.id, merge_request.id, merge_request.assignee_id_was).deliver end end @@ -78,8 +78,8 @@ class MailerObserver < ActiveRecord::Observer recipients_ids = issue.assignee_id_was, issue.assignee_id recipients_ids.delete current_user.id - User.find(recipients_ids).each do |user| - Notify.changed_issue_email(user, issue).deliver + recipients_ids.each do |recipient_id| + Notify.reassigned_issue_email(recipient_id, issue.id, issue.assignee_id_was).deliver end end diff --git a/app/models/note.rb b/app/models/note.rb index cee726ea0e511ec44e5f70c93efb732c6c9b93ff..c655eb807eb5abe9cff9a8a2cb02e799afd62921 100644 --- a/app/models/note.rb +++ b/app/models/note.rb @@ -7,6 +7,10 @@ class Note < ActiveRecord::Base belongs_to :author, :class_name => "User" + delegate :name, + :to => :project, + :prefix => true + delegate :name, :email, :to => :author, diff --git a/app/views/notify/new_issue_email.html.haml b/app/views/notify/new_issue_email.html.haml index 64c5aa611eb6d3af90f69cc7e8ff949aaa34a083..dd6f50c0686bafc1c2276ddee120de144f621141 100644 --- a/app/views/notify/new_issue_email.html.haml +++ b/app/views/notify/new_issue_email.html.haml @@ -10,7 +10,7 @@ %td{:style => "font-size: 1px; line-height: 1px;", :width => "21"} %td{:align => "left", :style => "padding: 20px 0 0;"} %h2{:style => "color:#646464 !important; font-weight: bold; margin: 0; padding: 0; line-height: 26px; font-size: 18px; font-family: Helvetica, Arial, sans-serif; "} - = link_to project_issue_url(@project, @issue), :title => @issue.title do + = link_to project_issue_url(@issue.project, @issue), :title => @issue.title do = "Issue ##{@issue.id.to_s}" = truncate(@issue.title, :length => 45) %br diff --git a/app/views/notify/new_merge_request_email.html.haml b/app/views/notify/new_merge_request_email.html.haml index 57c35db50cb8f4fc1a8c13b3932c0f2d1fc56735..f7ec01e84b3e0e4e0c69d1ada01f64fbbe294407 100644 --- a/app/views/notify/new_merge_request_email.html.haml +++ b/app/views/notify/new_merge_request_email.html.haml @@ -5,16 +5,14 @@ %td{:align => "left", :style => "padding: 20px 0 0;"} %h2{:style => "color:#646464; font-weight: bold; margin: 0; padding: 0; line-height: 26px; font-size: 18px; font-family: Helvetica, Arial, sans-serif; "} New Merge Request - = link_to truncate(@merge_request.title, :length => 16), project_merge_request_url(@project, @merge_request) + = link_to truncate(@merge_request.title, :length => 16), project_merge_request_url(@merge_request.project, @merge_request) %td{:style => "font-size: 1px; line-height: 1px;", :width => "21"} %tr %td{:style => "font-size: 1px; line-height: 1px;", :width => "21"} %td{:style => "padding: 15px 0 15px;", :valign => "top"} %p{:style => "color:#767676; font-weight: normal; margin: 0; padding: 0; line-height: 20px; font-size: 12px;font-family: Helvetica, Arial, sans-serif; "} Branches: #{@merge_request.source_branch} → #{@merge_request.target_branch} - %p{:style => "color:#767676; font-weight: normal; margin: 0; padding: 0; line-height: 20px; font-size: 12px;font-family: Helvetica, Arial, sans-serif; "} - Asignee: #{@merge_request.author.name} → #{@merge_request.assignee.name} - + Asignee: #{@merge_request.author_name} → #{@merge_request.assignee_name} %td diff --git a/app/views/notify/note_commit_email.html.haml b/app/views/notify/note_commit_email.html.haml index aee8fe6c5da25e6bd6bcaadb97677265cd84b13c..c834f1ddfd71bd5c953fdd21840f15b2218b94b6 100644 --- a/app/views/notify/note_commit_email.html.haml +++ b/app/views/notify/note_commit_email.html.haml @@ -5,13 +5,13 @@ %td{:align => "left", :style => "padding: 20px 0 0;"} %h2{:style => "color:#646464; font-weight: bold; margin: 0; padding: 0; line-height: 26px; font-size: 18px; font-family: Helvetica, Arial, sans-serif; "} New comment for commit - = link_to truncate(@commit.id.to_s, :length => 16), project_commit_url(@project, :id => @commit.id, :anchor => "note_#{@note.id}") + = link_to truncate(@commit.id.to_s, :length => 16), project_commit_url(@commit.project, :id => @commit.id, :anchor => "note_#{@note.id}") %td{:style => "font-size: 1px; line-height: 1px;", :width => "21"} %tr %td{:style => "font-size: 1px; line-height: 1px;", :width => "21"} %td{:style => "padding: 15px 0 15px;", :valign => "top"} %p{:style => "color:#767676; font-weight: normal; margin: 0; padding: 0; line-height: 20px; font-size: 12px;font-family: Helvetica, Arial, sans-serif; "} - %a{:href => "#", :style => "color: #0eb6ce; text-decoration: none;"} #{@note.author.name} + %a{:href => "#", :style => "color: #0eb6ce; text-decoration: none;"} #{@note.author_name} left next message: %br %table{:border => "0", :cellpadding => "0", :cellspacing => "0", :width => "558"} diff --git a/app/views/notify/note_issue_email.html.haml b/app/views/notify/note_issue_email.html.haml index eb2cbc0c33d1fd6dc9ba94816c63f6e7d547b8aa..17d58bdec737b8dfc1f1788b2e1f9fe38a3324ce 100644 --- a/app/views/notify/note_issue_email.html.haml +++ b/app/views/notify/note_issue_email.html.haml @@ -5,7 +5,7 @@ %td{:align => "left", :style => "padding: 20px 0 0;"} %h2{:style => "color:#646464 !important; font-weight: bold; margin: 0; padding: 0; line-height: 26px; font-size: 18px; font-family: Helvetica, Arial, sans-serif; "} New comment - - = link_to project_issue_url(@project, @issue, :anchor => "note_#{@note.id}") do + = link_to project_issue_url(@issue.project, @issue, :anchor => "note_#{@note.id}") do = "Issue ##{@issue.id.to_s}" = truncate(@issue.title, :length => 35) %td{:style => "font-size: 1px; line-height: 1px;", :width => "21"} @@ -13,7 +13,7 @@ %td{:style => "font-size: 1px; line-height: 1px;", :width => "21"} %td{:style => "padding: 15px 0 15px;", :valign => "top"} %p{:style => "color:#767676; font-weight: normal; margin: 0; padding: 0; line-height: 20px; font-size: 12px;font-family: Helvetica, Arial, sans-serif; "} - %a{:href => "#", :style => "color: #0eb6ce; text-decoration: none;"} #{@note.author.name} + %a{:href => "#", :style => "color: #0eb6ce; text-decoration: none;"} #{@note.author_name} left next message: %br %table{:border => "0", :cellpadding => "0", :cellspacing => "0", :width => "558"} diff --git a/app/views/notify/note_merge_request_email.html.haml b/app/views/notify/note_merge_request_email.html.haml index e2dfec35b7fbe42b004d1400202e0eed9065703e..9c2284a645768ec61ff52825c351d8e1ada6e1c3 100644 --- a/app/views/notify/note_merge_request_email.html.haml +++ b/app/views/notify/note_merge_request_email.html.haml @@ -5,13 +5,13 @@ %td{:align => "left", :style => "padding: 20px 0 0;"} %h2{:style => "color:#646464; font-weight: bold; margin: 0; padding: 0; line-height: 26px; font-size: 18px; font-family: Helvetica, Arial, sans-serif; "} New comment for Merge Request - = link_to truncate(@merge_request.title, :length => 16), project_merge_request_url(@project, @merge_request, :anchor => "note_#{@note.id}") + = link_to truncate(@merge_request.title, :length => 16), project_merge_request_url(@merge_request.project, @merge_request, :anchor => "note_#{@note.id}") %td{:style => "font-size: 1px; line-height: 1px;", :width => "21"} %tr %td{:style => "font-size: 1px; line-height: 1px;", :width => "21"} %td{:style => "padding: 15px 0 15px;", :valign => "top"} %p{:style => "color:#767676; font-weight: normal; margin: 0; padding: 0; line-height: 20px; font-size: 12px;font-family: Helvetica, Arial, sans-serif; "} - %a{:href => "#", :style => "color: #0eb6ce; text-decoration: none;"} #{@note.author.name} + %a{:href => "#", :style => "color: #0eb6ce; text-decoration: none;"} #{@note.author_name} left next message: %br %table{:border => "0", :cellpadding => "0", :cellspacing => "0", :width => "558"} diff --git a/app/views/notify/note_wall_email.html.haml b/app/views/notify/note_wall_email.html.haml index da18f64f937d4293b1cff651abd7024e98233513..f62e1f917cd834c33a4f2488924ec3e253d78604 100644 --- a/app/views/notify/note_wall_email.html.haml +++ b/app/views/notify/note_wall_email.html.haml @@ -5,13 +5,13 @@ %td{:align => "left", :style => "padding: 20px 0 0;"} %h2{:style => "color:#646464; font-weight: bold; margin: 0; padding: 0; line-height: 26px; font-size: 18px; font-family: Helvetica, Arial, sans-serif; "} New message on - = link_to "Project Wall", wall_project_url(@project, :anchor => "note_#{@note.id}") + = link_to "Project Wall", wall_project_url(@note.project, :anchor => "note_#{@note.id}") %td{:style => "font-size: 1px; line-height: 1px;", :width => "21"} %tr %td{:style => "font-size: 1px; line-height: 1px;", :width => "21"} %td{:style => "padding: 15px 0 15px;", :valign => "top"} %p{:style => "color:#767676; font-weight: normal; margin: 0; padding: 0; line-height: 20px; font-size: 12px;font-family: Helvetica, Arial, sans-serif; "} - %a{:href => "#", :style => "color: #0eb6ce; text-decoration: none;"} #{@note.author.name} + %a{:href => "#", :style => "color: #0eb6ce; text-decoration: none;"} #{@note.author_name} left next message: %br %table{:border => "0", :cellpadding => "0", :cellspacing => "0", :width => "558"} diff --git a/app/views/notify/changed_issue_email.html.haml b/app/views/notify/reassigned_issue_email.html.haml similarity index 90% rename from app/views/notify/changed_issue_email.html.haml rename to app/views/notify/reassigned_issue_email.html.haml index fe046e408a11207c7b20b35c2215ef55279aaf51..43579b274d3bbe49d4599fea950f5d62eb59299e 100644 --- a/app/views/notify/changed_issue_email.html.haml +++ b/app/views/notify/reassigned_issue_email.html.haml @@ -5,12 +5,12 @@ %td{:align => "left", :style => "padding: 20px 0 0;"} %h2{:style => "color:#646464; font-weight: bold; margin: 0; padding: 0; line-height: 26px; font-size: 18px; font-family: Helvetica, Arial, sans-serif; "} Reassigned Issue - = link_to truncate(@issue.title, :length => 16), project_issue_url(@project, @issue) + = link_to truncate(@issue.title, :length => 16), project_issue_url(@issue.project, @issue) %td{:style => "font-size: 1px; line-height: 1px;", :width => "21"} %tr %td{:style => "font-size: 1px; line-height: 1px;", :width => "21"} %td{:style => "padding: 15px 0 15px;", :valign => "top"} %p{:style => "color:#767676; font-weight: normal; margin: 0; padding: 0; line-height: 20px; font-size: 12px;font-family: Helvetica, Arial, sans-serif; "} - Assignee changed from #{@assignee_was.name} to #{@issue.assignee.name} + Assignee changed from #{@previous_assignee.name} to #{@issue.assignee_name} %td diff --git a/app/views/notify/changed_merge_request_email.html.haml b/app/views/notify/reassigned_merge_request_email.html.haml similarity index 87% rename from app/views/notify/changed_merge_request_email.html.haml rename to app/views/notify/reassigned_merge_request_email.html.haml index 403d51232a913a5326944e611d94fdcfec4a880b..30b1f4fec4239f9fa370d762fa031e2182c4473f 100644 --- a/app/views/notify/changed_merge_request_email.html.haml +++ b/app/views/notify/reassigned_merge_request_email.html.haml @@ -5,12 +5,12 @@ %td{:align => "left", :style => "padding: 20px 0 0;"} %h2{:style => "color:#646464; font-weight: bold; margin: 0; padding: 0; line-height: 26px; font-size: 18px; font-family: Helvetica, Arial, sans-serif; "} Reassigned Merge Request - = link_to truncate(@merge_request.title, :length => 16), project_merge_request_url(@project, @merge_request) + = link_to truncate(@merge_request.title, :length => 16), project_merge_request_url(@merge_request.project, @merge_request) %td{:style => "font-size: 1px; line-height: 1px;", :width => "21"} %tr %td{:style => "font-size: 1px; line-height: 1px;", :width => "21"} %td{:style => "padding: 15px 0 15px;", :valign => "top"} %p{:style => "color:#767676; font-weight: normal; margin: 0; padding: 0; line-height: 20px; font-size: 12px;font-family: Helvetica, Arial, sans-serif; "} - Assignee changed from #{@assignee_was.name} to #{@merge_request.assignee.name} + Assignee changed from #{@previous_assignee.name} to #{@merge_request.assignee_name} %td diff --git a/spec/mailers/notify_spec.rb b/spec/mailers/notify_spec.rb new file mode 100644 index 0000000000000000000000000000000000000000..065094e964ec85e42cce008e28e5286d172db8df --- /dev/null +++ b/spec/mailers/notify_spec.rb @@ -0,0 +1,249 @@ +require 'spec_helper' + +describe Notify do + include EmailSpec::Helpers + include EmailSpec::Matchers + + before :all do + default_url_options[:host] = 'example.com' + end + + let(:recipient) { Factory.create(:user, :email => 'recipient@example.com') } + let(:project) { Factory.create(:project) } + + shared_examples 'a multiple recipients email' do + it 'is sent to the given recipient' do + should deliver_to recipient.email + end + end + + describe 'for new users, the email' do + let(:example_site_url) { root_url } + let(:new_user) { Factory.create(:user, :email => 'newguy@example.com') } + + subject { Notify.new_user_email(new_user.id, new_user.password) } + + it 'is sent to the new user' do + should deliver_to new_user.email + end + + it 'has the correct subject' do + should have_subject /Account was created for you/ + end + + it 'contains the new user\'s login name' do + should have_body_text /#{new_user.email}/ + end + + it 'contains the new user\'s password' do + should have_body_text /#{new_user.password}/ + end + + it 'includes a link to the site' do + should have_body_text /#{example_site_url}/ + end + end + + context 'for a project' do + describe 'items that are assignable, the email' do + let(:assignee) { Factory.create(:user, :email => 'assignee@example.com') } + let(:previous_assignee) { Factory.create(:user, :name => 'Previous Assignee') } + + shared_examples 'an assignee email' do + it 'is sent to the assignee' do + should deliver_to assignee.email + end + end + + context 'for issues' do + let(:issue) { Factory.create(:issue, :assignee => assignee, :project => project ) } + + describe 'that are new' do + subject { Notify.new_issue_email(issue.id) } + + it_behaves_like 'an assignee email' + + it 'has the correct subject' do + should have_subject /New Issue was created/ + end + + it 'contains a link to the new issue' do + should have_body_text /#{project_issue_url project, issue}/ + end + end + + describe 'that have been reassigned' do + before(:each) { issue.stub(:assignee_id_was).and_return(previous_assignee.id) } + + subject { Notify.reassigned_issue_email(recipient.id, issue.id, previous_assignee.id) } + + it_behaves_like 'a multiple recipients email' + + it 'has the correct subject' do + should have_subject /changed issue/ + end + + it 'contains the name of the previous assignee' do + should have_body_text /#{previous_assignee.name}/ + end + + it 'contains the name of the new assignee' do + should have_body_text /#{assignee.name}/ + end + + it 'contains a link to the issue' do + should have_body_text /#{project_issue_url project, issue}/ + end + end + end + + context 'for merge requests' do + let(:merge_request) { Factory.create(:merge_request, :assignee => assignee, :project => project) } + + describe 'that are new' do + subject { Notify.new_merge_request_email(merge_request.id) } + + it_behaves_like 'an assignee email' + + it 'has the correct subject' do + should have_subject /new merge request/ + end + + it 'contains a link to the new merge request' do + should have_body_text /#{project_merge_request_url(project, merge_request)}/ + end + + it 'contains the source branch for the merge request' do + should have_body_text /#{merge_request.source_branch}/ + end + + it 'contains the target branch for the merge request' do + should have_body_text /#{merge_request.target_branch}/ + end + end + + describe 'that are reassigned' do + before(:each) { merge_request.stub(:assignee_id_was).and_return(previous_assignee.id) } + + subject { Notify.reassigned_merge_request_email(recipient.id, merge_request.id, previous_assignee.id) } + + it_behaves_like 'a multiple recipients email' + + it 'has the correct subject' do + should have_subject /merge request changed/ + end + + it 'contains the name of the previous assignee' do + should have_body_text /#{previous_assignee.name}/ + end + + it 'contains the name of the new assignee' do + should have_body_text /#{assignee.name}/ + end + + it 'contains a link to the merge request' do + should have_body_text /#{project_merge_request_url project, merge_request}/ + end + + end + end + end + + context 'items that are noteable, the email for a note' do + let(:note_author) { Factory.create(:user, :name => 'author_name') } + let(:note) { Factory.create(:note, :project => project, :author => note_author) } + + before :each do + Note.stub(:find).with(note.id).and_return(note) + end + + shared_examples 'a note email' do + it 'is sent to the given recipient' do + should deliver_to recipient.email + end + + it 'contains the name of the note\'s author' do + should have_body_text /#{note_author.name}/ + end + + it 'contains the message from the note' do + should have_body_text /#{note.note}/ + end + end + + describe 'on a project wall' do + let(:note_on_the_wall_url) { wall_project_url(project, :anchor => "note_#{note.id}") } + + subject { Notify.note_wall_email(recipient.id, note.id) } + + it_behaves_like 'a note email' + + it 'has the correct subject' do + should have_subject /#{project.name}/ + end + + it 'contains a link to the wall note' do + should have_body_text /#{note_on_the_wall_url}/ + end + end + + describe 'on a commit' do + let(:commit) do + mock(:commit).tap do |commit| + commit.stub(:id).and_return('fauxsha1') + commit.stub(:project).and_return(project) + end + end + before(:each) { note.stub(:target).and_return(commit) } + + subject { Notify.note_commit_email(recipient.id, note.id) } + + it_behaves_like 'a note email' + + it 'has the correct subject' do + should have_subject /note for commit/ + end + + it 'contains a link to the commit' do + should have_body_text /fauxsha1/ + end + end + + describe 'on a merge request' do + let(:merge_request) { Factory.create(:merge_request, :project => project) } + let(:note_on_merge_request_url) { project_merge_request_url(project, merge_request, :anchor => "note_#{note.id}") } + before(:each) { note.stub(:noteable).and_return(merge_request) } + + subject { Notify.note_merge_request_email(recipient.id, note.id) } + + it_behaves_like 'a note email' + + it 'has the correct subject' do + should have_subject /note for merge request/ + end + + it 'contains a link to the merge request note' do + should have_body_text /#{note_on_merge_request_url}/ + end + end + + describe 'on an issue' do + let(:issue) { Factory.create(:issue, :project => project) } + let(:note_on_issue_url) { project_issue_url(project, issue, :anchor => "note_#{note.id}") } + before(:each) { note.stub(:noteable).and_return(issue) } + + subject { Notify.note_issue_email(recipient.id, note.id) } + + it_behaves_like 'a note email' + + it 'has the correct subject' do + should have_subject /note for issue #{issue.id}/ + end + + it 'contains a link to the issue note' do + should have_body_text /#{note_on_issue_url}/ + end + end + end + end +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index f24496ec92c3629f64b4602883f58ee954d61e5e..18b7854d102d53b2a87182037dc4222c2d9589a9 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -11,6 +11,7 @@ require 'capybara/dsl' require 'webmock/rspec' require 'factories' require 'monkeypatch' +require 'email_spec' # Requires supporting ruby files with custom matchers and macros, etc, # in spec/support/ and its subdirectories.