BigW Consortium Gitlab

mentionable_spec.rb 4.19 KB
Newer Older
1 2
require 'spec_helper'

Drew Blessing committed
3 4 5
describe Mentionable do
  include Mentionable

Douwe Maan committed
6 7 8 9
  def author
    nil
  end

10
  describe 'references' do
Drew Blessing committed
11 12 13 14 15 16 17 18 19
    let(:project) { create(:project) }

    it 'excludes JIRA references' do
      allow(project).to receive_messages(jira_tracker?: true)
      expect(referenced_mentionables(project, 'JIRA-123')).to be_empty
    end
  end
end

20
describe Issue, "Mentionable" do
21
  describe '#mentioned_users' do
22 23
    let!(:user) { create(:user, username: 'stranger') }
    let!(:user2) { create(:user, username: 'john') }
24
    let!(:issue) { create(:issue, description: "#{user.to_reference} mentioned") }
25 26 27

    subject { issue.mentioned_users }

28 29
    it { is_expected.to include(user) }
    it { is_expected.not_to include(user2) }
30
  end
31

32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
  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)
        text = "#{private_issue.to_reference(project)} and #{public_issue.to_reference}"

        issue.referenced_mentionables(current_user, text)
      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

69 70
  describe '#create_cross_references!' do
    let(:project) { create(:project) }
71
    let(:author)  { build(:user) }
72 73 74 75 76 77 78 79
    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
80
      expect(SystemNoteService).not_to receive(:cross_reference)
81

82
      issue.create_cross_references!(author, [commit2])
83 84
    end
  end
85

86
  describe '#create_new_cross_references!' do
87
    let(:project) { create(:project) }
88 89
    let(:author)  { create(:author) }
    let(:issues)  { create_list(:issue, 2, project: project, author: author) }
90

91 92 93 94
    before do
      project.team << [author, Gitlab::Access::DEVELOPER]
    end

95 96 97 98 99 100 101
    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'
102
        issue.create_new_cross_references!
103 104 105 106 107 108 109 110
      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
111
        issue.create_new_cross_references!
112 113 114 115 116 117 118 119 120 121
      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')
122
        issue.create_new_cross_references!
123 124 125 126 127 128 129 130
      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)
131
        issue.create_new_cross_references!
132 133 134 135
      end
    end

    def create_issue(description:)
136
      create(:issue, project: project, description: description, author: author)
137 138
    end
  end
139
end