summaryrefslogtreecommitdiff
path: root/spec/features/issues/notes_on_issues_spec.rb
blob: 4e98062e8b2df43d913d62d44a02860e88b49a70 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe 'Create notes on issues', :js do
  let(:user) { create(:user) }

  def submit_comment(text)
    fill_in 'note[note]', with: text
    click_button 'Comment'
    wait_for_requests
  end

  shared_examples 'notes with reference' do
    let(:issue) { create(:issue, project: project) }
    let(:note_text) { "Check #{mention.to_reference}" }

    before do
      project.add_developer(user)
      sign_in(user)
      visit project_issue_path(project, issue)

      submit_comment(note_text)
    end

    it 'creates a note with reference and cross references the issue', :sidekiq_might_not_need_inline do
      page.within('div#notes li.note div.note-text') do
        expect(page).to have_content(note_text)
        expect(page.find('a')).to have_content(mention.to_reference)
      end

      find('div#notes li.note div.note-text a').click

      page.within('div#notes li.note .system-note-message') do
        expect(page).to have_content('mentioned in issue')
        expect(page.find('a')).to have_content(issue.to_reference)
      end
    end
  end

  context 'mentioning issue on a private project' do
    it_behaves_like 'notes with reference' do
      let(:project) { create(:project, :private) }
      let(:mention) { create(:issue, project: project) }
    end
  end

  context 'mentioning issue on an internal project' do
    it_behaves_like 'notes with reference' do
      let(:project) { create(:project, :internal) }
      let(:mention) { create(:issue, project: project) }
    end
  end

  context 'mentioning issue on a public project' do
    it_behaves_like 'notes with reference' do
      let(:project) { create(:project, :public) }
      let(:mention) { create(:issue, project: project) }
    end
  end

  context 'mentioning merge request on a private project' do
    it_behaves_like 'notes with reference' do
      let(:project) { create(:project, :private, :repository) }
      let(:mention) { create(:merge_request, source_project: project) }
    end
  end

  context 'mentioning merge request on an internal project' do
    it_behaves_like 'notes with reference' do
      let(:project) { create(:project, :internal, :repository) }
      let(:mention) { create(:merge_request, source_project: project) }
    end
  end

  context 'mentioning merge request on a public project' do
    it_behaves_like 'notes with reference' do
      let(:project) { create(:project, :public, :repository) }
      let(:mention) { create(:merge_request, source_project: project) }
    end
  end

  it 'highlights the current user in a comment' do
    project = create(:project)
    issue = create(:issue, project: project)
    project.add_developer(user)
    sign_in(user)

    visit project_issue_path(project, issue)
    submit_comment("@#{user.username} note to self")

    expect(page).to have_selector '.gfm-project_member.current-user', text: user.username
  end

  shared_examples "when reference belongs to a private project" do
    let(:project) { create(:project, :private, :repository) }
    let(:issue) { create(:issue, project: project) }

    before do
      sign_in(user)
    end

    context 'when the user does not have permission to see the reference' do
      before do
        project.add_guest(user)
      end

      it 'does not show the user the reference' do
        visit project_issue_path(project, issue)

        expect(page).not_to have_content('closed via')
      end
    end

    context 'when the user has permission to see the reference' do
      before do
        project.add_developer(user)
      end

      it 'shows the user the reference' do
        visit project_issue_path(project, issue)

        page.within('div#notes li.note .system-note-message') do
          expect(page).to have_content('closed via')
          expect(page.find('a')).to have_content(reference_content)
        end
      end
    end
  end

  context 'when the issue is closed via a merge request' do
    it_behaves_like "when reference belongs to a private project" do
      let(:reference) { create(:merge_request, source_project: project) }
      let(:reference_content) { reference.to_reference }

      before do
        create(:resource_state_event, issue: issue, state: :closed, created_at: '2020-02-05', source_merge_request: reference)
      end
    end
  end

  context 'when the issue is closed via a commit' do
    it_behaves_like "when reference belongs to a private project" do
      let(:reference) { create(:commit, project: project) }
      let(:reference_content) { reference.short_sha }

      before do
        create(:resource_state_event, issue: issue, state: :closed, created_at: '2020-02-05', source_commit: reference.id)
      end
    end
  end
end