Commit 461731f0 authored by Valery Sizov's avatar Valery Sizov

fix notification_service specs

parent 7f214cee
...@@ -148,7 +148,6 @@ class NotificationService ...@@ -148,7 +148,6 @@ class NotificationService
# build notify method like 'note_commit_email' # build notify method like 'note_commit_email'
notify_method = "note_#{note.noteable_type.underscore}_email".to_sym notify_method = "note_#{note.noteable_type.underscore}_email".to_sym
recipients.each do |recipient| recipients.each do |recipient|
mailer.send(notify_method, recipient.id, note.id).deliver_later mailer.send(notify_method, recipient.id, note.id).deliver_later
end end
...@@ -371,7 +370,7 @@ class NotificationService ...@@ -371,7 +370,7 @@ class NotificationService
recipients = build_recipients(target, project, current_user) recipients = build_recipients(target, project, current_user)
recipients.each do |recipient| recipients.each do |recipient|
mailer.send(method, recipient.id, target.id, current_user.id).deliver mailer.send(method, recipient.id, target.id, current_user.id).deliver_later
end end
end end
...@@ -396,7 +395,7 @@ class NotificationService ...@@ -396,7 +395,7 @@ class NotificationService
recipients = build_recipients(target, project, current_user) recipients = build_recipients(target, project, current_user)
recipients.each do |recipient| recipients.each do |recipient|
mailer.send(method, recipient.id, target.id, status, current_user.id).deliver mailer.send(method, recipient.id, target.id, status, current_user.id).deliver_later
end end
end end
......
...@@ -32,4 +32,6 @@ Rails.application.configure do ...@@ -32,4 +32,6 @@ Rails.application.configure do
config.eager_load = false config.eager_load = false
config.cache_store = :null_store config.cache_store = :null_store
config.active_job.queue_adapter = :test
end end
...@@ -3,6 +3,12 @@ require 'spec_helper' ...@@ -3,6 +3,12 @@ require 'spec_helper'
describe NotificationService do describe NotificationService do
let(:notification) { NotificationService.new } let(:notification) { NotificationService.new }
around(:each) do |example|
perform_enqueued_jobs do
example.run
end
end
describe 'Keys' do describe 'Keys' do
describe :new_key do describe :new_key do
let!(:key) { create(:personal_key) } let!(:key) { create(:personal_key) }
...@@ -10,8 +16,7 @@ describe NotificationService do ...@@ -10,8 +16,7 @@ describe NotificationService do
it { expect(notification.new_key(key)).to be_truthy } it { expect(notification.new_key(key)).to be_truthy }
it 'should sent email to key owner' do it 'should sent email to key owner' do
expect(Notify).to receive(:new_ssh_key_email).with(key.id) expect{ notification.new_key(key) }.to change{ ActionMailer::Base.deliveries.size }.by(1)
notification.new_key(key)
end end
end end
end end
...@@ -23,8 +28,7 @@ describe NotificationService do ...@@ -23,8 +28,7 @@ describe NotificationService do
it { expect(notification.new_email(email)).to be_truthy } it { expect(notification.new_email(email)).to be_truthy }
it 'should send email to email owner' do it 'should send email to email owner' do
expect(Notify).to receive(:new_email_email).with(email.id) expect{ notification.new_email(email) }.to change{ ActionMailer::Base.deliveries.size }.by(1)
notification.new_email(email)
end end
end end
end end
...@@ -47,18 +51,20 @@ describe NotificationService do ...@@ -47,18 +51,20 @@ describe NotificationService do
it do it do
add_users_with_subscription(note.project, issue) add_users_with_subscription(note.project, issue)
should_email(@u_watcher.id) ActionMailer::Base.deliveries.clear
should_email(note.noteable.author_id)
should_email(note.noteable.assignee_id)
should_email(@u_mentioned.id)
should_email(@subscriber.id)
should_not_email(note.author_id)
should_not_email(@u_participating.id)
should_not_email(@u_disabled.id)
should_not_email(@unsubscriber.id)
should_not_email(@u_outsider_mentioned)
notification.new_note(note) notification.new_note(note)
should_email(@u_watcher)
should_email(note.noteable.author)
should_email(note.noteable.assignee)
should_email(@u_mentioned)
should_email(@subscriber)
should_not_email(note.author)
should_not_email(@u_participating)
should_not_email(@u_disabled)
should_not_email(@unsubscriber)
should_not_email(@u_outsider_mentioned)
end end
it 'filters out "mentioned in" notes' do it 'filters out "mentioned in" notes' do
...@@ -82,26 +88,20 @@ describe NotificationService do ...@@ -82,26 +88,20 @@ describe NotificationService do
group_member = note.project.group.group_members.find_by_user_id(@u_watcher.id) group_member = note.project.group.group_members.find_by_user_id(@u_watcher.id)
group_member.notification_level = Notification::N_GLOBAL group_member.notification_level = Notification::N_GLOBAL
group_member.save group_member.save
ActionMailer::Base.deliveries.clear
end end
it do it do
should_email(note.noteable.author_id)
should_email(note.noteable.assignee_id)
should_email(@u_mentioned.id)
should_not_email(@u_watcher.id)
should_not_email(note.author_id)
should_not_email(@u_participating.id)
should_not_email(@u_disabled.id)
notification.new_note(note) notification.new_note(note)
end
end
def should_email(user_id) should_email(note.noteable.author)
expect(Notify).to receive(:note_issue_email).with(user_id, note.id) should_email(note.noteable.assignee)
should_email(@u_mentioned)
should_not_email(@u_watcher)
should_not_email(note.author)
should_not_email(@u_participating)
should_not_email(@u_disabled)
end end
def should_not_email(user_id)
expect(Notify).not_to receive(:note_issue_email).with(user_id, note.id)
end end
end end
...@@ -113,24 +113,26 @@ describe NotificationService do ...@@ -113,24 +113,26 @@ describe NotificationService do
before do before do
build_team(note.project) build_team(note.project)
ActionMailer::Base.deliveries.clear
end end
describe :new_note do describe :new_note do
it do it do
notification.new_note(note)
# Notify all team members # Notify all team members
note.project.team.members.each do |member| note.project.team.members.each do |member|
# User with disabled notification should not be notified # User with disabled notification should not be notified
next if member.id == @u_disabled.id next if member.id == @u_disabled.id
should_email(member.id) should_email(member)
end end
should_email(note.noteable.author_id)
should_email(note.noteable.assignee_id)
should_not_email(note.author_id) should_email(note.noteable.author)
should_not_email(@u_mentioned.id) should_email(note.noteable.assignee)
should_not_email(@u_disabled.id) should_not_email(note.author)
should_not_email(@u_not_mentioned.id) should_email(@u_mentioned)
notification.new_note(note) should_not_email(@u_disabled)
should_email(@u_not_mentioned)
end end
it 'filters out "mentioned in" notes' do it 'filters out "mentioned in" notes' do
...@@ -140,14 +142,6 @@ describe NotificationService do ...@@ -140,14 +142,6 @@ describe NotificationService do
notification.new_note(mentioned_note) notification.new_note(mentioned_note)
end end
end end
def should_email(user_id)
expect(Notify).to receive(:note_issue_email).with(user_id, note.id)
end
def should_not_email(user_id)
expect(Notify).not_to receive(:note_issue_email).with(user_id, note.id)
end
end end
context 'commit note' do context 'commit note' do
...@@ -156,43 +150,38 @@ describe NotificationService do ...@@ -156,43 +150,38 @@ describe NotificationService do
before do before do
build_team(note.project) build_team(note.project)
ActionMailer::Base.deliveries.clear
allow_any_instance_of(Commit).to receive(:author).and_return(@u_committer) allow_any_instance_of(Commit).to receive(:author).and_return(@u_committer)
end end
describe :new_note do describe :new_note, :perform_enqueued_jobs do
it do it do
should_email(@u_committer.id, note)
should_email(@u_watcher.id, note)
should_not_email(@u_mentioned.id, note)
should_not_email(note.author_id, note)
should_not_email(@u_participating.id, note)
should_not_email(@u_disabled.id, note)
notification.new_note(note) notification.new_note(note)
should_email(@u_committer)
should_email(@u_watcher)
should_not_email(@u_mentioned)
should_not_email(note.author)
should_not_email(@u_participating)
should_not_email(@u_disabled)
end end
it do it do
note.update_attribute(:note, '@mention referenced') note.update_attribute(:note, '@mention referenced')
should_email(@u_committer.id, note)
should_email(@u_watcher.id, note)
should_email(@u_mentioned.id, note)
should_not_email(note.author_id, note)
should_not_email(@u_participating.id, note)
should_not_email(@u_disabled.id, note)
notification.new_note(note) notification.new_note(note)
should_email(@u_committer)
should_email(@u_watcher)
should_email(@u_mentioned)
should_not_email(note.author)
should_not_email(@u_participating)
should_not_email(@u_disabled)
end end
it do it do
@u_committer.update_attributes(notification_level: Notification::N_MENTION) @u_committer.update_attributes(notification_level: Notification::N_MENTION)
should_not_email(@u_committer.id, note)
notification.new_note(note) notification.new_note(note)
end should_not_email(@u_committer)
def should_email(user_id, n)
expect(Notify).to receive(:note_commit_email).with(user_id, n.id)
end
def should_not_email(user_id, n)
expect(Notify).not_to receive(:note_commit_email).with(user_id, n.id)
end end
end end
end end
...@@ -205,99 +194,69 @@ describe NotificationService do ...@@ -205,99 +194,69 @@ describe NotificationService do
before do before do
build_team(issue.project) build_team(issue.project)
add_users_with_subscription(issue.project, issue) add_users_with_subscription(issue.project, issue)
ActionMailer::Base.deliveries.clear
end end
describe :new_issue do describe :new_issue do
it do it do
should_email(issue.assignee_id)
should_email(@u_watcher.id)
should_email(@u_participant_mentioned.id)
should_not_email(@u_mentioned.id)
should_not_email(@u_participating.id)
should_not_email(@u_disabled.id)
notification.new_issue(issue, @u_disabled) notification.new_issue(issue, @u_disabled)
should_email(issue.assignee)
should_email(@u_watcher)
should_email(@u_participant_mentioned)
should_not_email(@u_mentioned)
should_not_email(@u_participating)
should_not_email(@u_disabled)
end end
it do it do
issue.assignee.update_attributes(notification_level: Notification::N_MENTION) issue.assignee.update_attributes(notification_level: Notification::N_MENTION)
should_not_email(issue.assignee_id)
notification.new_issue(issue, @u_disabled) notification.new_issue(issue, @u_disabled)
end
def should_email(user_id)
expect(Notify).to receive(:new_issue_email).with(user_id, issue.id)
end
def should_not_email(user_id) should_not_email(issue.assignee)
expect(Notify).not_to receive(:new_issue_email).with(user_id, issue.id)
end end
end end
describe :reassigned_issue do describe :reassigned_issue do
it 'should email new assignee' do it 'should email new assignee' do
should_email(issue.assignee_id)
should_email(@u_watcher.id)
should_email(@u_participant_mentioned.id)
should_email(@subscriber.id)
should_not_email(@unsubscriber.id)
should_not_email(@u_participating.id)
should_not_email(@u_disabled.id)
notification.reassigned_issue(issue, @u_disabled) notification.reassigned_issue(issue, @u_disabled)
end
def should_email(user_id)
expect(Notify).to receive(:reassigned_issue_email).with(user_id, issue.id, nil, @u_disabled.id)
end
def should_not_email(user_id) should_email(issue.assignee)
expect(Notify).not_to receive(:reassigned_issue_email).with(user_id, issue.id, issue.assignee_id, @u_disabled.id) should_email(@u_watcher)
should_email(@u_participant_mentioned)
should_email(@subscriber)
should_not_email(@unsubscriber)
should_not_email(@u_participating)
should_not_email(@u_disabled)
end end
end end
describe :close_issue do describe :close_issue do
it 'should sent email to issue assignee and issue author' do it 'should sent email to issue assignee and issue author' do
should_email(issue.assignee_id)
should_email(issue.author_id)
should_email(@u_watcher.id)
should_email(@u_participant_mentioned.id)
should_email(@subscriber.id)
should_not_email(@unsubscriber.id)
should_not_email(@u_participating.id)
should_not_email(@u_disabled.id)
notification.close_issue(issue, @u_disabled) notification.close_issue(issue, @u_disabled)
end
def should_email(user_id)
expect(Notify).to receive(:closed_issue_email).with(user_id, issue.id, @u_disabled.id)
end
def should_not_email(user_id) should_email(issue.assignee)
expect(Notify).not_to receive(:closed_issue_email).with(user_id, issue.id, @u_disabled.id) should_email(issue.author)
should_email(@u_watcher)
should_email(@u_participant_mentioned)
should_email(@subscriber)
should_not_email(@unsubscriber)
should_not_email(@u_participating)
should_not_email(@u_disabled)
end end
end end
describe :reopen_issue do describe :reopen_issue do
it 'should send email to issue assignee and issue author' do it 'should send email to issue assignee and issue author' do
should_email(issue.assignee_id)
should_email(issue.author_id)
should_email(@u_watcher.id)
should_email(@u_participant_mentioned.id)
should_email(@subscriber.id)
should_not_email(@unsubscriber.id)
should_not_email(@u_participating.id)
should_not_email(@u_disabled.id)
notification.reopen_issue(issue, @u_disabled) notification.reopen_issue(issue, @u_disabled)
end
def should_email(user_id)
expect(Notify).to receive(:issue_status_changed_email).with(user_id, issue.id, 'reopened', @u_disabled.id)
end
def should_not_email(user_id) should_email(issue.assignee)
expect(Notify).not_to receive(:issue_status_changed_email).with(user_id, issue.id, 'reopened', @u_disabled.id) should_email(issue.author)
should_email(@u_watcher)
should_email(@u_participant_mentioned)
should_email(@subscriber)
should_not_email(@unsubscriber)
should_not_email(@u_participating)
end end
end end
end end
...@@ -309,108 +268,74 @@ describe NotificationService do ...@@ -309,108 +268,74 @@ describe NotificationService do
before do before do
build_team(merge_request.target_project) build_team(merge_request.target_project)
add_users_with_subscription(merge_request.target_project, merge_request) add_users_with_subscription(merge_request.target_project, merge_request)
ActionMailer::Base.deliveries.clear
end end
describe :new_merge_request do describe :new_merge_request do
it do it do
should_email(merge_request.assignee_id)
should_email(@u_watcher.id)
should_email(@u_participant_mentioned.id)
should_not_email(@u_participating.id)
should_not_email(@u_disabled.id)
notification.new_merge_request(merge_request, @u_disabled) notification.new_merge_request(merge_request, @u_disabled)
end
def should_email(user_id) should_email(merge_request.assignee)
expect(Notify).to receive(:new_merge_request_email).with(user_id, merge_request.id) should_email(@u_watcher)
end should_email(@u_participant_mentioned)
should_not_email(@u_participating)
def should_not_email(user_id) should_not_email(@u_disabled)
expect(Notify).not_to receive(:new_merge_request_email).with(user_id, merge_request.id)
end end
end end
describe :reassigned_merge_request do describe :reassigned_merge_request do
it do it do
should_email(merge_request.assignee_id)
should_email(@u_watcher.id)
should_email(@u_participant_mentioned.id)
should_email(@subscriber.id)
should_not_email(@unsubscriber.id)
should_not_email(@u_participating.id)
should_not_email(@u_disabled.id)
notification.reassigned_merge_request(merge_request, merge_request.author) notification.reassigned_merge_request(merge_request, merge_request.author)
end
def should_email(user_id)
expect(Notify).to receive(:reassigned_merge_request_email).with(user_id, merge_request.id, nil, merge_request.author_id)
end
def should_not_email(user_id) should_email(merge_request.assignee)
expect(Notify).not_to receive(:reassigned_merge_request_email).with(user_id, merge_request.id, merge_request.assignee_id, merge_request.author_id) should_email(@u_watcher)
should_email(@u_participant_mentioned)
should_email(@subscriber)
should_not_email(@unsubscriber)
should_not_email(@u_participating)
should_not_email(@u_disabled)
end end
end end
describe :closed_merge_request do describe :closed_merge_request do
it do it do
should_email(merge_request.assignee_id)
should_email(@u_watcher.id)
should_email(@u_participant_mentioned.id)
should_email(@subscriber.id)
should_not_email(@unsubscriber.id)
should_not_email(@u_participating.id)
should_not_email(@u_disabled.id)
notification.close_mr(merge_request, @u_disabled) notification.close_mr(merge_request, @u_disabled)
end
def should_email(user_id)
expect(Notify).to receive(:closed_merge_request_email).with(user_id, merge_request.id, @u_disabled.id)
end
def should_not_email(user_id) should_email(merge_request.assignee)
expect(Notify).not_to receive(:closed_merge_request_email).with(user_id, merge_request.id, @u_disabled.id) should_email(@u_watcher)
should_email(@u_participant_mentioned)
should_email(@subscriber)
should_not_email(@unsubscriber)
should_not_email(@u_participating)
should_not_email(@u_disabled)
end end
end end
describe :merged_merge_request do describe :merged_merge_request do
it do it do
should_email(merge_request.assignee_id)
should_email(@u_watcher.id)
should_email(@u_participant_mentioned.id)
should_email(@subscriber.id)
should_not_email(@unsubscriber.id)
should_not_email(@u_participating.id)
should_not_email(@u_disabled.id)
notification.merge_mr(merge_request, @u_disabled) notification.merge_mr(merge_request, @u_disabled)
end
def should_email(user_id)
expect(Notify).to receive(:merged_merge_request_email).with(user_id, merge_request.id, @u_disabled.id)
end
def should_not_email(user_id) should_email(merge_request.assignee)
expect(Notify).not_to receive(:merged_merge_request_email).with(user_id, merge_request.id, @u_disabled.id) should_email(@u_watcher)
should_email(@u_participant_mentioned)
should_email(@subscriber)
should_not_email(@unsubscriber)
should_not_email(@u_participating)
should_not_email(@u_disabled)
end end
end end
describe :reopen_merge_request do describe :reopen_merge_request do
it do it do
should_email(merge_request.assignee_id)
should_email(@u_watcher.id)
should_email(@u_participant_mentioned.id)
should_email(@subscriber.id)
should_not_email(@unsubscriber.id)
should_not_email(@u_participating.id)
should_not_email(@u_disabled.id)
notification.reopen_mr(merge_request, @u_disabled) notification.reopen_mr(merge_request, @u_disabled)
end
def should_email(user_id) should_email(merge_request.assignee)
expect(Notify).to receive(:merge_request_status_email).with(user_id, merge_request.id, 'reopened', @u_disabled.id) should_email(@u_watcher)
end should_email(@u_participant_mentioned)
should_email(@subscriber)
def should_not_email(user_id) should_not_email(@unsubscriber)
expect(Notify).not_to receive(:merge_request_status_email).with(user_id, merge_request.id, 'reopened', @u_disabled.id) should_not_email(@u_participating)
should_not_email(@u_disabled)
end end
end end
end end
...@@ -420,22 +345,16 @@ describe NotificationService do ...@@ -420,22 +345,16 @@ describe NotificationService do
before do before do
build_team(project) build_team(project)
ActionMailer::Base.deliveries.clear
end end
describe :project_was_moved do describe :project_was_moved do
it do it do
should_email(@u_watcher.id)
should_email(@u_participating.id)
should_not_email(@u_disabled.id)
notification.project_was_moved(project, "gitlab/gitlab") notification.project_was_moved(project, "gitlab/gitlab")
end
def should_email(user_id)
expect(Notify).to receive(:project_was_moved_email).with(project.id, user_id, "gitlab/gitlab")
end
def should_not_email(user_id) should_email(@u_watcher)
expect(Notify).not_to receive(:project_was_moved_email).with(project.id, user_id, "gitlab/gitlab") should_email(@u_participating)
should_not_email(@u_disabled)
end end
end end
end end
...@@ -469,4 +388,18 @@ describe NotificationService do ...@@ -469,4 +388,18 @@ describe NotificationService do
issuable.subscriptions.create(user: @subscriber, subscribed: true) issuable.subscriptions.create(user: @subscriber, subscribed: true)
issuable.subscriptions.create(user: @unsubscriber, subscribed: false) issuable.subscriptions.create(user: @unsubscriber, subscribed: false)
end end
def sent_to_user?(user)
ActionMailer::Base.deliveries.any? do |message|
message.to.include?(user.email)
end
end
def should_email(user)
expect(sent_to_user?(user)).to be_truthy
end
def should_not_email(user)
expect(sent_to_user?(user)).to be_falsey
end
end end
...@@ -31,6 +31,7 @@ RSpec.configure do |config| ...@@ -31,6 +31,7 @@ RSpec.configure do |config|
config.include StubConfiguration config.include StubConfiguration
config.include RelativeUrl, type: feature config.include RelativeUrl, type: feature
config.include TestEnv config.include TestEnv
config.include ActiveJob::TestHelper
config.include StubGitlabCalls config.include StubGitlabCalls
config.include StubGitlabData config.include StubGitlabData
config.include BenchmarkMatchers, benchmark: true config.include BenchmarkMatchers, benchmark: true
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment