BigW Consortium Gitlab

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

Drew Blessing committed
3
describe Mentionable do
4 5
  class Example
    include Mentionable
Drew Blessing committed
6

7 8 9 10 11 12
    attr_accessor :project, :message
    attr_mentionable :message

    def author
      nil
    end
Douwe Maan committed
13 14
  end

15
  describe 'references' do
16
    let(:project) { create(:empty_project) }
17
    let(:mentionable) { Example.new }
Drew Blessing committed
18 19 20

    it 'excludes JIRA references' do
      allow(project).to receive_messages(jira_tracker?: true)
21 22 23 24

      mentionable.project = project
      mentionable.message = 'JIRA-123'
      expect(mentionable.referenced_mentionables).to be_empty
Drew Blessing committed
25 26 27 28
    end
  end
end

29
describe Issue, "Mentionable" do
30
  describe '#mentioned_users' do
31 32
    let!(:user) { create(:user, username: 'stranger') }
    let!(:user2) { create(:user, username: 'john') }
33 34
    let!(:user3) { create(:user, username: 'jim') }
    let(:issue) { create(:issue, description: "#{user.to_reference} mentioned") }
35 36 37

    subject { issue.mentioned_users }

Jarka Kadlecova committed
38
    it { expect(subject).to contain_exactly(user) }
39 40 41 42 43 44

    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 }

Jarka Kadlecova committed
45
      it { expect(subject).to contain_exactly(user, user3) }
46
    end
47
  end
48

49 50 51 52 53 54 55 56 57 58
  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)
59 60
        issue.title = "#{private_issue.to_reference(project)} and #{public_issue.to_reference}"
        issue.referenced_mentionables(current_user)
61 62 63
      end

      context 'when the current user can see the issue' do
64 65 66
        before do
          private_project.team << [user, Gitlab::Access::DEVELOPER]
        end
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86

        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

87
  describe '#create_cross_references!' do
88
    let(:project) { create(:project, :repository) }
89
    let(:author)  { build(:user) }
90 91 92 93
    let(:commit)  { project.commit }
    let(:commit2) { project.commit }

    let!(:issue) do
94
      create(:issue, project: project, description: "See #{commit.to_reference}")
95 96 97
    end

    it 'correctly removes already-mentioned Commits' do
98
      expect(SystemNoteService).not_to receive(:cross_reference)
99

100
      issue.create_cross_references!(author, [commit2])
101 102
    end
  end
103

104
  describe '#create_new_cross_references!' do
105
    let(:project) { create(:empty_project) }
106 107
    let(:author)  { create(:author) }
    let(:issues)  { create_list(:issue, 2, project: project, author: author) }
108

109 110 111 112
    before do
      project.team << [author, Gitlab::Access::DEVELOPER]
    end

113 114 115 116 117 118 119
    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'
120
        issue.create_new_cross_references!
121 122 123 124 125 126 127 128
      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
129
        issue.create_new_cross_references!
130 131 132 133 134 135 136 137 138 139
      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')
140
        issue.create_new_cross_references!
141 142 143 144 145 146 147 148
      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)
149
        issue.create_new_cross_references!
150
      end
151 152 153 154 155 156 157 158 159 160

      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)

        note.update_attributes(note: issues[1].to_reference)
        note.create_new_cross_references!
      end
161 162 163
    end

    def create_issue(description:)
164
      create(:issue, project: project, description: description, author: author)
165 166
    end
  end
167
end
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216

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 false
    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 true
    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 true
    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 true
    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 true
    end

    context 'with external issue tracker' do
      let(:project) { create(:jira_project) }

      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 true
      end
    end
  end
end