notes_helper_spec.rb 11.5 KB
Newer Older
1 2 3
require "spec_helper"

describe NotesHelper do
4 5
  include RepoHelpers

6 7
  let(:owner) { create(:owner) }
  let(:group) { create(:group) }
8
  let(:project) { create(:project, namespace: group) }
9
  let(:maintainer) { create(:user) }
10 11
  let(:reporter) { create(:user) }
  let(:guest) { create(:user) }
12

13
  let(:owner_note) { create(:note, author: owner, project: project) }
14
  let(:maintainer_note) { create(:note, author: maintainer, project: project) }
15
  let(:reporter_note) { create(:note, author: reporter, project: project) }
16
  let!(:notes) { [owner_note, maintainer_note, reporter_note] }
17

18 19
  before do
    group.add_owner(owner)
20
    project.add_maintainer(maintainer)
21 22
    project.add_reporter(reporter)
    project.add_guest(guest)
23 24 25
  end

  describe "#notes_max_access_for_users" do
26 27
    it 'returns access levels' do
      expect(helper.note_max_access_for_user(owner_note)).to eq(Gitlab::Access::OWNER)
28
      expect(helper.note_max_access_for_user(maintainer_note)).to eq(Gitlab::Access::MAINTAINER)
29
      expect(helper.note_max_access_for_user(reporter_note)).to eq(Gitlab::Access::REPORTER)
30
    end
31 32

    it 'handles access in different projects' do
33
      second_project = create(:project)
34 35
      second_project.add_reporter(maintainer)
      other_note = create(:note, author: maintainer, project: second_project)
36

37
      expect(helper.note_max_access_for_user(maintainer_note)).to eq(Gitlab::Access::MAINTAINER)
38
      expect(helper.note_max_access_for_user(other_note)).to eq(Gitlab::Access::REPORTER)
39 40 41
    end
  end

42
  describe '#discussion_path' do
43
    let(:project) { create(:project, :repository) }
44
    let(:anchor) { discussion.line_code }
45 46 47 48 49 50 51 52 53 54 55 56

    context 'for a merge request discusion' do
      let(:merge_request) { create(:merge_request, source_project: project, target_project: project, importing: true) }
      let!(:merge_request_diff1) { merge_request.merge_request_diffs.create(head_commit_sha: '6f6d7e7ed97bb5f0054f2b1df789b39ca89b6ff9') }
      let!(:merge_request_diff2) { merge_request.merge_request_diffs.create(head_commit_sha: nil) }
      let!(:merge_request_diff3) { merge_request.merge_request_diffs.create(head_commit_sha: '5937ac0a7beb003549fc5fd26fc247adbce4a52e') }

      context 'for a diff discussion' do
        context 'when the discussion is active' do
          let(:discussion) { create(:diff_note_on_merge_request, noteable: merge_request, project: project).to_discussion }

          it 'returns the diff path with the line code' do
57
            expect(helper.discussion_path(discussion)).to eq(diffs_project_merge_request_path(project, merge_request, anchor: discussion.line_code))
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
          end
        end

        context 'when the discussion is on an older merge request version' do
          let(:position) do
            Gitlab::Diff::Position.new(
              old_path: ".gitmodules",
              new_path: ".gitmodules",
              old_line: nil,
              new_line: 4,
              diff_refs: merge_request_diff1.diff_refs
            )
          end

          let(:diff_note) { create(:diff_note_on_merge_request, noteable: merge_request, project: project, position: position) }
          let(:discussion) { diff_note.to_discussion }

          before do
            diff_note.position = diff_note.original_position
            diff_note.save!
          end

          it 'returns the diff version path with the line code' do
81
            expect(helper.discussion_path(discussion)).to eq(diffs_project_merge_request_path(project, merge_request, diff_id: merge_request_diff1, anchor: discussion.line_code))
82 83 84 85 86 87 88 89 90 91 92 93 94 95
          end
        end

        context 'when the discussion is on a comparison between merge request versions' do
          let(:position) do
            Gitlab::Diff::Position.new(
              old_path: ".gitmodules",
              new_path: ".gitmodules",
              old_line: 4,
              new_line: 4,
              diff_refs: merge_request_diff3.compare_with(merge_request_diff1.head_commit_sha).diff_refs
            )
          end

Douwe Maan's avatar
Douwe Maan committed
96 97 98 99 100 101 102
          let(:diff_note) { create(:diff_note_on_merge_request, noteable: merge_request, project: project, position: position) }
          let(:discussion) { diff_note.to_discussion }

          before do
            diff_note.position = diff_note.original_position
            diff_note.save!
          end
103 104

          it 'returns the diff version comparison path with the line code' do
105
            expect(helper.discussion_path(discussion)).to eq(diffs_project_merge_request_path(project, merge_request, diff_id: merge_request_diff3, start_sha: merge_request_diff1.head_commit_sha, anchor: discussion.line_code))
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
          end
        end

        context 'when the discussion does not have a merge request version' do
          let(:outdated_diff_note) { create(:diff_note_on_merge_request, noteable: merge_request, project: project, diff_refs: project.commit(sample_commit.id).diff_refs) }
          let(:discussion) { outdated_diff_note.to_discussion }

          before do
            outdated_diff_note.position = outdated_diff_note.original_position
            outdated_diff_note.save!
          end

          it 'returns nil' do
            expect(helper.discussion_path(discussion)).to be_nil
          end
        end
      end

      context 'for a legacy diff discussion' do
        let(:discussion) { create(:legacy_diff_note_on_merge_request, noteable: merge_request, project: project).to_discussion }

        context 'when the discussion is active' do
          before do
            allow(discussion).to receive(:active?).and_return(true)
          end

          it 'returns the diff path with the line code' do
133
            expect(helper.discussion_path(discussion)).to eq(diffs_project_merge_request_path(project, merge_request, anchor: discussion.line_code))
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
          end
        end

        context 'when the discussion is outdated' do
          before do
            allow(discussion).to receive(:active?).and_return(false)
          end

          it 'returns nil' do
            expect(helper.discussion_path(discussion)).to be_nil
          end
        end
      end

      context 'for a non-diff discussion' do
        let(:discussion) { create(:discussion_note_on_merge_request, noteable: merge_request, project: project).to_discussion }

        it 'returns nil' do
          expect(helper.discussion_path(discussion)).to be_nil
        end
      end
155 156 157 158 159 160 161 162 163

      context 'for a contextual commit discussion' do
        let(:commit) { merge_request.commits.last }
        let(:discussion) { create(:diff_note_on_merge_request, noteable: merge_request, project: project, commit_id: commit.id).to_discussion }

        it 'returns the merge request diff discussion scoped in the commit' do
          expect(helper.discussion_path(discussion)).to eq(diffs_project_merge_request_path(project, merge_request, commit_id: commit.id, anchor: anchor))
        end
      end
164 165 166 167 168 169 170 171 172
    end

    context 'for a commit discussion' do
      let(:commit) { discussion.noteable }

      context 'for a diff discussion' do
        let(:discussion) { create(:diff_note_on_commit, project: project).to_discussion }

        it 'returns the commit path with the line code' do
173
          expect(helper.discussion_path(discussion)).to eq(project_commit_path(project, commit, anchor: anchor))
174 175 176 177 178 179 180
        end
      end

      context 'for a legacy diff discussion' do
        let(:discussion) { create(:legacy_diff_note_on_commit, project: project).to_discussion }

        it 'returns the commit path with the line code' do
181
          expect(helper.discussion_path(discussion)).to eq(project_commit_path(project, commit, anchor: anchor))
182 183 184 185 186 187
        end
      end

      context 'for a non-diff discussion' do
        let(:discussion) { create(:discussion_note_on_commit, project: project).to_discussion }

188 189
        it 'returns the commit path with the note anchor' do
          expect(helper.discussion_path(discussion)).to eq(project_commit_path(project, commit, anchor: "note_#{discussion.first_note.id}"))
190 191 192 193
        end
      end
    end
  end
194 195 196 197 198 199 200 201 202 203

  describe '#notes_url' do
    it 'return snippet notes path for personal snippet' do
      @snippet = create(:personal_snippet)

      expect(helper.notes_url).to eq("/snippets/#{@snippet.id}/notes")
    end

    it 'return project notes path for project snippet' do
      namespace = create(:namespace, path: 'nm')
204
      @project = create(:project, path: 'test', namespace: namespace)
205 206 207 208 209 210 211 212
      @snippet = create(:project_snippet, project: @project)
      @noteable = @snippet

      expect(helper.notes_url).to eq("/nm/test/noteable/project_snippet/#{@noteable.id}/notes")
    end

    it 'return project notes path for other noteables' do
      namespace = create(:namespace, path: 'nm')
213
      @project = create(:project, path: 'test', namespace: namespace)
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
      @noteable = create(:issue, project: @project)

      expect(helper.notes_url).to eq("/nm/test/noteable/issue/#{@noteable.id}/notes")
    end
  end

  describe '#note_url' do
    it 'return snippet notes path for personal snippet' do
      note = create(:note_on_personal_snippet)

      expect(helper.note_url(note)).to eq("/snippets/#{note.noteable.id}/notes/#{note.id}")
    end

    it 'return project notes path for project snippet' do
      namespace = create(:namespace, path: 'nm')
229
      @project = create(:project, path: 'test', namespace: namespace)
230 231 232 233 234 235 236
      note = create(:note_on_project_snippet, project: @project)

      expect(helper.note_url(note)).to eq("/nm/test/notes/#{note.id}")
    end

    it 'return project notes path for other noteables' do
      namespace = create(:namespace, path: 'nm')
237
      @project = create(:project, path: 'test', namespace: namespace)
238 239 240 241 242 243
      note = create(:note_on_issue, project: @project)

      expect(helper.note_url(note)).to eq("/nm/test/notes/#{note.id}")
    end
  end

244
  describe '#form_resources' do
245 246 247 248 249 250 251 252 253
    it 'returns note for personal snippet' do
      @snippet = create(:personal_snippet)
      @note = create(:note_on_personal_snippet)

      expect(helper.form_resources).to eq([@note])
    end

    it 'returns namespace, project and note for project snippet' do
      namespace = create(:namespace, path: 'nm')
254
      @project = create(:project, path: 'test', namespace: namespace)
255 256 257 258 259 260 261 262
      @snippet = create(:project_snippet, project: @project)
      @note = create(:note_on_personal_snippet)

      expect(helper.form_resources).to eq([@project.namespace, @project, @note])
    end

    it 'returns namespace, project and note path for other noteables' do
      namespace = create(:namespace, path: 'nm')
263
      @project = create(:project, path: 'test', namespace: namespace)
264 265 266 267 268
      @note = create(:note_on_issue, project: @project)

      expect(helper.form_resources).to eq([@project.namespace, @project, @note])
    end
  end
269 270

  describe '#noteable_note_url' do
271
    let(:project) { create(:project) }
272 273 274 275 276 277 278
    let(:issue) { create(:issue, project: project) }
    let(:note) { create(:note_on_issue, noteable: issue, project: project) }

    it 'returns the noteable url with an anchor to the note' do
      expect(noteable_note_url(note)).to match("/#{project.namespace.path}/#{project.path}/issues/#{issue.iid}##{dom_id(note)}")
    end
  end
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296

  describe '#discussion_resolved_intro' do
    context 'when the discussion was resolved by a push' do
      let(:discussion) { double(:discussion, resolved_by_push?: true) }

      it 'returns "Automatically resolved"' do
        expect(discussion_resolved_intro(discussion)).to eq('Automatically resolved')
      end
    end

    context 'when the discussion was not resolved by a push' do
      let(:discussion) { double(:discussion, resolved_by_push?: false) }

      it 'returns "Resolved"' do
        expect(discussion_resolved_intro(discussion)).to eq('Resolved')
      end
    end
  end
297
end