debian-mirror-gitlab/spec/models/concerns/mentionable_spec.rb

225 lines
6.8 KiB
Ruby
Raw Normal View History

2019-07-07 11:18:12 +05:30
# frozen_string_literal: true
2015-04-26 12:48:37 +05:30
require 'spec_helper'
2015-12-23 02:04:40 +05:30
describe Mentionable do
2016-11-03 12:29:30 +05:30
class Example
include Mentionable
2015-12-23 02:04:40 +05:30
2016-11-03 12:29:30 +05:30
attr_accessor :project, :message
attr_mentionable :message
def author
nil
end
end
2016-08-24 12:49:21 +05:30
describe 'references' do
2017-09-10 17:25:29 +05:30
let(:project) { create(:project) }
2016-11-03 12:29:30 +05:30
let(:mentionable) { Example.new }
2015-12-23 02:04:40 +05:30
2019-09-30 21:07:59 +05:30
it 'excludes Jira references' do
2015-12-23 02:04:40 +05:30
allow(project).to receive_messages(jira_tracker?: true)
2016-11-03 12:29:30 +05:30
mentionable.project = project
mentionable.message = 'JIRA-123'
expect(mentionable.referenced_mentionables).to be_empty
2015-12-23 02:04:40 +05:30
end
end
end
2015-04-26 12:48:37 +05:30
describe Issue, "Mentionable" do
2015-09-11 14:41:01 +05:30
describe '#mentioned_users' do
2015-04-26 12:48:37 +05:30
let!(:user) { create(:user, username: 'stranger') }
let!(:user2) { create(:user, username: 'john') }
2017-08-17 22:00:37 +05:30
let!(:user3) { create(:user, username: 'jim') }
let(:issue) { create(:issue, description: "#{user.to_reference} mentioned") }
2015-04-26 12:48:37 +05:30
subject { issue.mentioned_users }
2017-08-17 22:00:37 +05:30
it { expect(subject).to contain_exactly(user) }
context 'when a note on personal snippet' do
let!(:note) { create(:note_on_personal_snippet, note: "#{user.to_reference} mentioned #{user3.to_reference}") }
subject { note.mentioned_users }
it { expect(subject).to contain_exactly(user, user3) }
end
2015-04-26 12:48:37 +05:30
end
2015-09-11 14:41:01 +05:30
2016-08-24 12:49:21 +05:30
describe '#referenced_mentionables' do
context 'with an issue on a private project' do
2017-09-10 17:25:29 +05:30
let(:project) { create(:project, :public) }
2016-08-24 12:49:21 +05:30
let(:issue) { create(:issue, project: project) }
let(:public_issue) { create(:issue, project: project) }
2017-09-10 17:25:29 +05:30
let(:private_project) { create(:project, :private) }
2016-08-24 12:49:21 +05:30
let(:private_issue) { create(:issue, project: private_project) }
let(:user) { create(:user) }
def referenced_issues(current_user)
2016-11-03 12:29:30 +05:30
issue.title = "#{private_issue.to_reference(project)} and #{public_issue.to_reference}"
issue.referenced_mentionables(current_user)
2016-08-24 12:49:21 +05:30
end
context 'when the current user can see the issue' do
2017-09-10 17:25:29 +05:30
before do
2018-03-17 18:26:18 +05:30
private_project.add_developer(user)
2017-09-10 17:25:29 +05:30
end
2016-08-24 12:49:21 +05:30
it 'includes the reference' do
expect(referenced_issues(user)).to contain_exactly(private_issue, public_issue)
end
end
context 'when the current user cannot see the issue' do
it 'does not include the reference' do
expect(referenced_issues(user)).to contain_exactly(public_issue)
end
end
context 'when there is no current user' do
it 'does not include the reference' do
expect(referenced_issues(nil)).to contain_exactly(public_issue)
end
end
end
end
2015-09-11 14:41:01 +05:30
describe '#create_cross_references!' do
2017-08-17 22:00:37 +05:30
let(:project) { create(:project, :repository) }
2016-09-13 17:45:13 +05:30
let(:author) { build(:user) }
2015-09-11 14:41:01 +05:30
let(:commit) { project.commit }
let(:commit2) { project.commit }
let!(:issue) do
2017-08-17 22:00:37 +05:30
create(:issue, project: project, description: "See #{commit.to_reference}")
2015-09-11 14:41:01 +05:30
end
it 'correctly removes already-mentioned Commits' do
expect(SystemNoteService).not_to receive(:cross_reference)
2015-10-24 18:46:33 +05:30
issue.create_cross_references!(author, [commit2])
2015-09-11 14:41:01 +05:30
end
end
describe '#create_new_cross_references!' do
2017-09-10 17:25:29 +05:30
let(:project) { create(:project) }
2016-06-02 11:05:42 +05:30
let(:author) { create(:author) }
let(:issues) { create_list(:issue, 2, project: project, author: author) }
2015-09-11 14:41:01 +05:30
2016-09-13 17:45:13 +05:30
before do
2018-03-17 18:26:18 +05:30
project.add_developer(author)
2016-09-13 17:45:13 +05:30
end
2015-09-11 14:41:01 +05:30
context 'before changes are persisted' do
it 'ignores pre-existing references' do
issue = create_issue(description: issues[0].to_reference)
expect(SystemNoteService).not_to receive(:cross_reference)
issue.description = 'New description'
issue.create_new_cross_references!
end
it 'notifies new references' do
issue = create_issue(description: issues[0].to_reference)
expect(SystemNoteService).to receive(:cross_reference).with(issues[1], any_args)
issue.description = issues[1].to_reference
issue.create_new_cross_references!
end
end
context 'after changes are persisted' do
it 'ignores pre-existing references' do
issue = create_issue(description: issues[0].to_reference)
expect(SystemNoteService).not_to receive(:cross_reference)
2018-11-18 11:00:15 +05:30
issue.update(description: 'New description')
2015-09-11 14:41:01 +05:30
issue.create_new_cross_references!
end
it 'notifies new references' do
issue = create_issue(description: issues[0].to_reference)
expect(SystemNoteService).to receive(:cross_reference).with(issues[1], any_args)
2018-11-18 11:00:15 +05:30
issue.update(description: issues[1].to_reference)
2015-09-11 14:41:01 +05:30
issue.create_new_cross_references!
end
2017-08-17 22:00:37 +05:30
it 'notifies new references from project snippet note' do
snippet = create(:snippet, project: project)
note = create(:note, note: issues[0].to_reference, noteable: snippet, project: project, author: author)
expect(SystemNoteService).to receive(:cross_reference).with(issues[1], any_args)
2018-11-18 11:00:15 +05:30
note.update(note: issues[1].to_reference)
2017-08-17 22:00:37 +05:30
note.create_new_cross_references!
end
2015-09-11 14:41:01 +05:30
end
def create_issue(description:)
2016-06-02 11:05:42 +05:30
create(:issue, project: project, description: description, author: author)
2015-09-11 14:41:01 +05:30
end
end
2015-04-26 12:48:37 +05:30
end
2017-09-10 17:25:29 +05:30
describe Commit, 'Mentionable' do
let(:project) { create(:project, :public, :repository) }
let(:commit) { project.commit }
describe '#matches_cross_reference_regex?' do
it "is false when message doesn't reference anything" do
allow(commit.raw).to receive(:message).and_return "WIP: Do something"
expect(commit.matches_cross_reference_regex?).to be_falsey
end
it 'is true if issue #number mentioned in title' do
allow(commit.raw).to receive(:message).and_return "#1"
expect(commit.matches_cross_reference_regex?).to be_truthy
end
it 'is true if references an MR' do
allow(commit.raw).to receive(:message).and_return "See merge request !12"
expect(commit.matches_cross_reference_regex?).to be_truthy
end
it 'is true if references a commit' do
allow(commit.raw).to receive(:message).and_return "a1b2c3d4"
expect(commit.matches_cross_reference_regex?).to be_truthy
end
it 'is true if issue referenced by url' do
issue = create(:issue, project: project)
allow(commit.raw).to receive(:message).and_return Gitlab::UrlBuilder.build(issue)
expect(commit.matches_cross_reference_regex?).to be_truthy
end
context 'with external issue tracker' do
let(:project) { create(:jira_project, :repository) }
it 'is true if external issues referenced' do
allow(commit.raw).to receive(:message).and_return 'JIRA-123'
expect(commit.matches_cross_reference_regex?).to be_truthy
end
it 'is true if internal issues referenced' do
allow(commit.raw).to receive(:message).and_return '#123'
expect(commit.matches_cross_reference_regex?).to be_truthy
end
end
end
end