2018-11-20 20:47:30 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
RSpec.describe Gitlab::Email::Handler::UnsubscribeHandler do
|
2023-03-04 22:38:38 +05:30
|
|
|
include_context 'email shared context'
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
before do
|
|
|
|
stub_incoming_email_setting(enabled: true, address: 'reply+%{key}@appmail.adventuretime.ooo')
|
|
|
|
stub_config_setting(host: 'localhost')
|
|
|
|
end
|
|
|
|
|
2023-01-13 00:05:48 +05:30
|
|
|
let(:email_raw) { fixture_file('emails/valid_reply.eml').gsub(mail_key, "#{mail_key}#{Gitlab::Email::Common::UNSUBSCRIBE_SUFFIX}") }
|
2019-02-15 15:39:39 +05:30
|
|
|
let(:project) { create(:project, :public) }
|
|
|
|
let(:user) { create(:user) }
|
|
|
|
let(:noteable) { create(:issue, project: project) }
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
let!(:sent_notification) { SentNotification.record(noteable, user.id, mail_key) }
|
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
context "when email key" do
|
|
|
|
let(:mail) { Mail::Message.new(email_raw) }
|
|
|
|
|
|
|
|
it "matches the new format" do
|
2023-01-13 00:05:48 +05:30
|
|
|
handler = described_class.new(mail, "#{mail_key}#{Gitlab::Email::Common::UNSUBSCRIBE_SUFFIX}")
|
2019-02-15 15:39:39 +05:30
|
|
|
|
|
|
|
expect(handler.can_handle?).to be_truthy
|
|
|
|
end
|
|
|
|
|
|
|
|
it "matches the legacy format" do
|
2023-01-13 00:05:48 +05:30
|
|
|
handler = described_class.new(mail, "#{mail_key}#{Gitlab::Email::Common::UNSUBSCRIBE_SUFFIX_LEGACY}")
|
2019-02-15 15:39:39 +05:30
|
|
|
|
|
|
|
expect(handler.can_handle?).to be_truthy
|
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't match either format" do
|
2023-01-13 00:05:48 +05:30
|
|
|
handler = described_class.new(mail, "+#{mail_key}#{Gitlab::Email::Common::UNSUBSCRIBE_SUFFIX}")
|
2019-02-15 15:39:39 +05:30
|
|
|
|
|
|
|
expect(handler.can_handle?).to be_falsey
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
context 'when notification concerns a commit' do
|
|
|
|
let(:commit) { create(:commit, project: project) }
|
|
|
|
let!(:sent_notification) { SentNotification.record(commit, user.id, mail_key) }
|
|
|
|
|
|
|
|
it 'handler does not raise an error' do
|
|
|
|
expect { receiver.execute }.not_to raise_error
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'user is unsubscribed' do
|
|
|
|
it 'leaves user unsubscribed' do
|
|
|
|
expect { receiver.execute }.not_to change { noteable.subscribed?(user) }.from(false)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'user is subscribed' do
|
|
|
|
before do
|
|
|
|
noteable.subscribe(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'unsubscribes user from notable' do
|
|
|
|
expect { receiver.execute }.to change { noteable.subscribed?(user) }.from(true).to(false)
|
|
|
|
end
|
2019-02-15 15:39:39 +05:30
|
|
|
|
|
|
|
context 'when using old style unsubscribe link' do
|
2023-01-13 00:05:48 +05:30
|
|
|
let(:email_raw) { fixture_file('emails/valid_reply.eml').gsub(mail_key, "#{mail_key}#{Gitlab::Email::Common::UNSUBSCRIBE_SUFFIX_LEGACY}") }
|
2019-02-15 15:39:39 +05:30
|
|
|
|
|
|
|
it 'unsubscribes user from notable' do
|
|
|
|
expect { receiver.execute }.to change { noteable.subscribed?(user) }.from(true).to(false)
|
|
|
|
end
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the noteable could not be found' do
|
|
|
|
before do
|
2021-04-29 21:17:54 +05:30
|
|
|
noteable.destroy!
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it 'raises a NoteableNotFoundError' do
|
|
|
|
expect { receiver.execute }.to raise_error(Gitlab::Email::NoteableNotFoundError)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when no sent notification for the mail key could be found' do
|
|
|
|
let(:email_raw) { fixture_file('emails/wrong_mail_key.eml') }
|
|
|
|
|
|
|
|
it 'raises a SentNotificationNotFoundError' do
|
|
|
|
expect { receiver.execute }.to raise_error(Gitlab::Email::SentNotificationNotFoundError)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|