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

148 lines
4.4 KiB
Ruby
Raw Normal View History

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
2015-12-23 02:04:40 +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
it 'excludes JIRA references' do
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') }
2015-09-11 14:41:01 +05:30
let!(:issue) { create(:issue, description: "#{user.to_reference} mentioned") }
2015-04-26 12:48:37 +05:30
subject { issue.mentioned_users }
it { is_expected.to include(user) }
it { is_expected.not_to include(user2) }
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
let(:project) { create(:empty_project, :public) }
let(:issue) { create(:issue, project: project) }
let(:public_issue) { create(:issue, project: project) }
let(:private_project) { create(:empty_project, :private) }
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
before { private_project.team << [user, Gitlab::Access::DEVELOPER] }
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
let(:project) { create(:project) }
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
create(:issue, project: project, description: commit.to_reference)
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
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
project.team << [author, Gitlab::Access::DEVELOPER]
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)
issue.update_attributes(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.update_attributes(description: issues[1].to_reference)
issue.create_new_cross_references!
end
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