debian-mirror-gitlab/spec/services/notification_service_spec.rb

427 lines
14 KiB
Ruby
Raw Normal View History

2014-09-02 18:07:02 +05:30
require 'spec_helper'
2015-12-23 02:04:40 +05:30
describe NotificationService, services: true do
2014-09-02 18:07:02 +05:30
let(:notification) { NotificationService.new }
2015-12-23 02:04:40 +05:30
around(:each) do |example|
perform_enqueued_jobs do
example.run
end
end
2014-09-02 18:07:02 +05:30
describe 'Keys' do
describe :new_key do
let!(:key) { create(:personal_key) }
2015-04-26 12:48:37 +05:30
it { expect(notification.new_key(key)).to be_truthy }
2014-09-02 18:07:02 +05:30
it 'should sent email to key owner' do
2015-12-23 02:04:40 +05:30
expect{ notification.new_key(key) }.to change{ ActionMailer::Base.deliveries.size }.by(1)
2014-09-02 18:07:02 +05:30
end
end
end
describe 'Email' do
describe :new_email do
let!(:email) { create(:email) }
2015-04-26 12:48:37 +05:30
it { expect(notification.new_email(email)).to be_truthy }
2014-09-02 18:07:02 +05:30
it 'should send email to email owner' do
2015-12-23 02:04:40 +05:30
expect{ notification.new_email(email) }.to change{ ActionMailer::Base.deliveries.size }.by(1)
2014-09-02 18:07:02 +05:30
end
end
end
describe 'Notes' do
context 'issue note' do
2015-09-25 12:07:36 +05:30
let(:project) { create(:empty_project, :private) }
2015-09-11 14:41:01 +05:30
let(:issue) { create(:issue, project: project, assignee: create(:user)) }
2014-09-02 18:07:02 +05:30
let(:mentioned_issue) { create(:issue, assignee: issue.assignee) }
2015-09-25 12:07:36 +05:30
let(:note) { create(:note_on_issue, noteable: issue, project_id: issue.project_id, note: '@mention referenced, @outsider also') }
2014-09-02 18:07:02 +05:30
before do
build_team(note.project)
2015-09-25 12:07:36 +05:30
project.team << [issue.author, :master]
project.team << [issue.assignee, :master]
project.team << [note.author, :master]
2015-12-23 02:04:40 +05:30
create(:note_on_issue, noteable: issue, project_id: issue.project_id, note: '@subscribed_participant cc this guy')
2014-09-02 18:07:02 +05:30
end
describe :new_note do
it do
2015-04-26 12:48:37 +05:30
add_users_with_subscription(note.project, issue)
# Ensure create SentNotification by noteable = issue 6 times, not noteable = note
expect(SentNotification).to receive(:record).with(issue, any_args).exactly(7).times
2015-12-23 02:04:40 +05:30
ActionMailer::Base.deliveries.clear
2015-04-26 12:48:37 +05:30
2014-09-02 18:07:02 +05:30
notification.new_note(note)
2015-12-23 02:04:40 +05:30
should_email(@u_watcher)
should_email(note.noteable.author)
should_email(note.noteable.assignee)
should_email(@u_mentioned)
should_email(@subscriber)
should_email(@watcher_and_subscriber)
2015-12-23 02:04:40 +05:30
should_email(@subscribed_participant)
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)
2014-09-02 18:07:02 +05:30
end
it 'filters out "mentioned in" notes' do
2015-09-11 14:41:01 +05:30
mentioned_note = SystemNoteService.cross_reference(mentioned_issue, issue, issue.author)
2014-09-02 18:07:02 +05:30
2015-04-26 12:48:37 +05:30
expect(Notify).not_to receive(:note_issue_email)
2014-09-02 18:07:02 +05:30
notification.new_note(mentioned_note)
end
end
describe 'new note on issue in project that belongs to a group' do
let(:group) { create(:group) }
before do
note.project.namespace_id = group.id
2015-04-26 12:48:37 +05:30
note.project.group.add_user(@u_watcher, GroupMember::MASTER)
2014-09-02 18:07:02 +05:30
note.project.save
2015-04-26 12:48:37 +05:30
user_project = note.project.project_members.find_by_user_id(@u_watcher.id)
2014-09-02 18:07:02 +05:30
user_project.notification_level = Notification::N_PARTICIPATING
user_project.save
2015-04-26 12:48:37 +05:30
group_member = note.project.group.group_members.find_by_user_id(@u_watcher.id)
group_member.notification_level = Notification::N_GLOBAL
group_member.save
2015-12-23 02:04:40 +05:30
ActionMailer::Base.deliveries.clear
2014-09-02 18:07:02 +05:30
end
it do
notification.new_note(note)
2015-12-23 02:04:40 +05:30
should_email(note.noteable.author)
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
2014-09-02 18:07:02 +05:30
end
end
context 'issue note mention' do
2015-09-11 14:41:01 +05:30
let(:project) { create(:empty_project, :public) }
let(:issue) { create(:issue, project: project, assignee: create(:user)) }
2014-09-02 18:07:02 +05:30
let(:mentioned_issue) { create(:issue, assignee: issue.assignee) }
let(:note) { create(:note_on_issue, noteable: issue, project_id: issue.project_id, note: '@all mentioned') }
before do
build_team(note.project)
note.project.team << [note.author, :master]
2015-12-23 02:04:40 +05:30
ActionMailer::Base.deliveries.clear
2014-09-02 18:07:02 +05:30
end
describe :new_note do
it do
2015-12-23 02:04:40 +05:30
notification.new_note(note)
2014-09-02 18:07:02 +05:30
# Notify all team members
note.project.team.members.each do |member|
# User with disabled notification should not be notified
next if member.id == @u_disabled.id
# Author should not be notified
next if member.id == note.author.id
2015-12-23 02:04:40 +05:30
should_email(member)
2014-09-02 18:07:02 +05:30
end
2015-12-23 02:04:40 +05:30
should_email(note.noteable.author)
should_email(note.noteable.assignee)
should_not_email(note.author)
should_email(@u_mentioned)
should_not_email(@u_disabled)
should_email(@u_not_mentioned)
2014-09-02 18:07:02 +05:30
end
it 'filters out "mentioned in" notes' do
2015-09-11 14:41:01 +05:30
mentioned_note = SystemNoteService.cross_reference(mentioned_issue, issue, issue.author)
2014-09-02 18:07:02 +05:30
2015-04-26 12:48:37 +05:30
expect(Notify).not_to receive(:note_issue_email)
2014-09-02 18:07:02 +05:30
notification.new_note(mentioned_note)
end
end
end
context 'commit note' do
2015-09-11 14:41:01 +05:30
let(:project) { create(:project, :public) }
let(:note) { create(:note_on_commit, project: project) }
2014-09-02 18:07:02 +05:30
before do
build_team(note.project)
2015-12-23 02:04:40 +05:30
ActionMailer::Base.deliveries.clear
2015-09-11 14:41:01 +05:30
allow_any_instance_of(Commit).to receive(:author).and_return(@u_committer)
2014-09-02 18:07:02 +05:30
end
2015-12-23 02:04:40 +05:30
describe :new_note, :perform_enqueued_jobs do
2014-09-02 18:07:02 +05:30
it do
notification.new_note(note)
2015-12-23 02:04:40 +05:30
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)
2014-09-02 18:07:02 +05:30
end
it do
note.update_attribute(:note, '@mention referenced')
notification.new_note(note)
2015-12-23 02:04:40 +05:30
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)
2014-09-02 18:07:02 +05:30
end
2015-04-26 12:48:37 +05:30
it do
@u_committer.update_attributes(notification_level: Notification::N_MENTION)
notification.new_note(note)
2015-12-23 02:04:40 +05:30
should_not_email(@u_committer)
2014-09-02 18:07:02 +05:30
end
end
end
end
describe 'Issues' do
2015-09-11 14:41:01 +05:30
let(:project) { create(:empty_project, :public) }
let(:issue) { create :issue, project: project, assignee: create(:user), description: 'cc @participant' }
2014-09-02 18:07:02 +05:30
before do
build_team(issue.project)
2015-04-26 12:48:37 +05:30
add_users_with_subscription(issue.project, issue)
2015-12-23 02:04:40 +05:30
ActionMailer::Base.deliveries.clear
2014-09-02 18:07:02 +05:30
end
describe :new_issue do
it do
notification.new_issue(issue, @u_disabled)
2015-12-23 02:04:40 +05:30
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)
2014-09-02 18:07:02 +05:30
end
2015-04-26 12:48:37 +05:30
it do
issue.assignee.update_attributes(notification_level: Notification::N_MENTION)
notification.new_issue(issue, @u_disabled)
2014-09-02 18:07:02 +05:30
2015-12-23 02:04:40 +05:30
should_not_email(issue.assignee)
2014-09-02 18:07:02 +05:30
end
end
describe :reassigned_issue do
it 'should email new assignee' do
notification.reassigned_issue(issue, @u_disabled)
2015-12-23 02:04:40 +05:30
should_email(issue.assignee)
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)
2014-09-02 18:07:02 +05:30
end
end
describe :close_issue do
it 'should sent email to issue assignee and issue author' do
notification.close_issue(issue, @u_disabled)
2015-12-23 02:04:40 +05:30
should_email(issue.assignee)
should_email(issue.author)
should_email(@u_watcher)
should_email(@u_participant_mentioned)
should_email(@subscriber)
should_email(@watcher_and_subscriber)
2015-12-23 02:04:40 +05:30
should_not_email(@unsubscriber)
should_not_email(@u_participating)
should_not_email(@u_disabled)
2014-09-02 18:07:02 +05:30
end
end
describe :reopen_issue do
it 'should send email to issue assignee and issue author' do
notification.reopen_issue(issue, @u_disabled)
2015-12-23 02:04:40 +05:30
should_email(issue.assignee)
should_email(issue.author)
should_email(@u_watcher)
should_email(@u_participant_mentioned)
should_email(@subscriber)
should_email(@watcher_and_subscriber)
2015-12-23 02:04:40 +05:30
should_not_email(@unsubscriber)
should_not_email(@u_participating)
2014-09-02 18:07:02 +05:30
end
end
end
describe 'Merge Requests' do
2015-09-11 14:41:01 +05:30
let(:project) { create(:project, :public) }
let(:merge_request) { create :merge_request, source_project: project, assignee: create(:user), description: 'cc @participant' }
2014-09-02 18:07:02 +05:30
before do
build_team(merge_request.target_project)
2015-04-26 12:48:37 +05:30
add_users_with_subscription(merge_request.target_project, merge_request)
2015-12-23 02:04:40 +05:30
ActionMailer::Base.deliveries.clear
2014-09-02 18:07:02 +05:30
end
describe :new_merge_request do
it do
notification.new_merge_request(merge_request, @u_disabled)
2015-12-23 02:04:40 +05:30
should_email(merge_request.assignee)
should_email(@u_watcher)
should_email(@watcher_and_subscriber)
2015-12-23 02:04:40 +05:30
should_email(@u_participant_mentioned)
should_not_email(@u_participating)
should_not_email(@u_disabled)
2014-09-02 18:07:02 +05:30
end
end
describe :reassigned_merge_request do
it do
notification.reassigned_merge_request(merge_request, merge_request.author)
2015-12-23 02:04:40 +05:30
should_email(merge_request.assignee)
should_email(@u_watcher)
should_email(@u_participant_mentioned)
should_email(@subscriber)
should_email(@watcher_and_subscriber)
2015-12-23 02:04:40 +05:30
should_not_email(@unsubscriber)
should_not_email(@u_participating)
should_not_email(@u_disabled)
2014-09-02 18:07:02 +05:30
end
end
describe :closed_merge_request do
it do
notification.close_mr(merge_request, @u_disabled)
2015-12-23 02:04:40 +05:30
should_email(merge_request.assignee)
should_email(@u_watcher)
should_email(@u_participant_mentioned)
should_email(@subscriber)
should_email(@watcher_and_subscriber)
2015-12-23 02:04:40 +05:30
should_not_email(@unsubscriber)
should_not_email(@u_participating)
should_not_email(@u_disabled)
2014-09-02 18:07:02 +05:30
end
end
describe :merged_merge_request do
it do
notification.merge_mr(merge_request, @u_disabled)
2015-12-23 02:04:40 +05:30
should_email(merge_request.assignee)
should_email(@u_watcher)
should_email(@u_participant_mentioned)
should_email(@subscriber)
should_email(@watcher_and_subscriber)
2015-12-23 02:04:40 +05:30
should_not_email(@unsubscriber)
should_not_email(@u_participating)
should_not_email(@u_disabled)
2014-09-02 18:07:02 +05:30
end
end
describe :reopen_merge_request do
it do
notification.reopen_mr(merge_request, @u_disabled)
2015-12-23 02:04:40 +05:30
should_email(merge_request.assignee)
should_email(@u_watcher)
should_email(@u_participant_mentioned)
should_email(@subscriber)
should_email(@watcher_and_subscriber)
2015-12-23 02:04:40 +05:30
should_not_email(@unsubscriber)
should_not_email(@u_participating)
should_not_email(@u_disabled)
2014-09-02 18:07:02 +05:30
end
end
end
describe 'Projects' do
let(:project) { create :project }
before do
build_team(project)
2015-12-23 02:04:40 +05:30
ActionMailer::Base.deliveries.clear
2014-09-02 18:07:02 +05:30
end
describe :project_was_moved do
it do
2015-10-24 18:46:33 +05:30
notification.project_was_moved(project, "gitlab/gitlab")
2014-09-02 18:07:02 +05:30
2015-12-23 02:04:40 +05:30
should_email(@u_watcher)
should_email(@u_participating)
should_not_email(@u_disabled)
2014-09-02 18:07:02 +05:30
end
end
end
def build_team(project)
@u_watcher = create(:user, notification_level: Notification::N_WATCH)
@u_participating = create(:user, notification_level: Notification::N_PARTICIPATING)
2015-04-26 12:48:37 +05:30
@u_participant_mentioned = create(:user, username: 'participant', notification_level: Notification::N_PARTICIPATING)
2014-09-02 18:07:02 +05:30
@u_disabled = create(:user, notification_level: Notification::N_DISABLED)
2015-04-26 12:48:37 +05:30
@u_mentioned = create(:user, username: 'mention', notification_level: Notification::N_MENTION)
2014-09-02 18:07:02 +05:30
@u_committer = create(:user, username: 'committer')
@u_not_mentioned = create(:user, username: 'regular', notification_level: Notification::N_PARTICIPATING)
2015-09-25 12:07:36 +05:30
@u_outsider_mentioned = create(:user, username: 'outsider')
2014-09-02 18:07:02 +05:30
project.team << [@u_watcher, :master]
project.team << [@u_participating, :master]
2015-09-25 12:07:36 +05:30
project.team << [@u_participant_mentioned, :master]
2014-09-02 18:07:02 +05:30
project.team << [@u_disabled, :master]
project.team << [@u_mentioned, :master]
project.team << [@u_committer, :master]
2015-09-25 12:07:36 +05:30
project.team << [@u_not_mentioned, :master]
2014-09-02 18:07:02 +05:30
end
2015-04-26 12:48:37 +05:30
def add_users_with_subscription(project, issuable)
@subscriber = create :user
@unsubscriber = create :user
2015-12-23 02:04:40 +05:30
@subscribed_participant = create(:user, username: 'subscribed_participant', notification_level: Notification::N_PARTICIPATING)
@watcher_and_subscriber = create(:user, notification_level: Notification::N_WATCH)
2015-04-26 12:48:37 +05:30
2015-12-23 02:04:40 +05:30
project.team << [@subscribed_participant, :master]
2015-04-26 12:48:37 +05:30
project.team << [@subscriber, :master]
project.team << [@unsubscriber, :master]
project.team << [@watcher_and_subscriber, :master]
2015-04-26 12:48:37 +05:30
issuable.subscriptions.create(user: @subscriber, subscribed: true)
2015-12-23 02:04:40 +05:30
issuable.subscriptions.create(user: @subscribed_participant, subscribed: true)
2015-04-26 12:48:37 +05:30
issuable.subscriptions.create(user: @unsubscriber, subscribed: false)
# Make the watcher a subscriber to detect dupes
issuable.subscriptions.create(user: @watcher_and_subscriber, subscribed: true)
2015-04-26 12:48:37 +05:30
end
2015-12-23 02:04:40 +05:30
def sent_to_user?(user)
ActionMailer::Base.deliveries.map(&:to).flatten.count(user.email) == 1
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
2014-09-02 18:07:02 +05:30
end