BigW Consortium Gitlab

notes_helper_spec.rb 10.3 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 10 11
  let(:master) { create(:user) }
  let(:reporter) { create(:user) }
  let(:guest) { create(:user) }
12

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

18 19 20 21 22 23 24 25
  before do
    group.add_owner(owner)
    project.team << [master, :master]
    project.team << [reporter, :reporter]
    project.team << [guest, :guest]
  end

  describe "#notes_max_access_for_users" do
26
    it 'returns human access levels' do
27 28 29 30
      expect(helper.note_max_access_for_user(owner_note)).to eq('Owner')
      expect(helper.note_max_access_for_user(master_note)).to eq('Master')
      expect(helper.note_max_access_for_user(reporter_note)).to eq('Reporter')
    end
31 32

    it 'handles access in different projects' do
33
      second_project = create(:project)
34 35 36 37 38 39
      second_project.team << [master, :reporter]
      other_note = create(:note, author: master, project: second_project)

      expect(helper.note_max_access_for_user(master_note)).to eq('Master')
      expect(helper.note_max_access_for_user(other_note)).to eq('Reporter')
    end
40
  end
41 42

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

    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
56
            expect(helper.discussion_path(discussion)).to eq(diffs_project_merge_request_path(project, merge_request, anchor: discussion.line_code))
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
          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
80
            expect(helper.discussion_path(discussion)).to eq(diffs_project_merge_request_path(project, merge_request, diff_id: merge_request_diff1, anchor: discussion.line_code))
81 82 83 84 85 86 87 88 89 90 91 92 93 94
          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 committed
95 96 97 98 99 100 101
          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
102 103

          it 'returns the diff version comparison path with the line code' do
104
            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))
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
          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
132
            expect(helper.discussion_path(discussion)).to eq(diffs_project_merge_request_path(project, merge_request, anchor: discussion.line_code))
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
          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
    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
163
          expect(helper.discussion_path(discussion)).to eq(project_commit_path(project, commit, anchor: discussion.line_code))
164 165 166 167 168 169 170
        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
171
          expect(helper.discussion_path(discussion)).to eq(project_commit_path(project, commit, anchor: discussion.line_code))
172 173 174 175 176 177 178
        end
      end

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

        it 'returns the commit path' do
179
          expect(helper.discussion_path(discussion)).to eq(project_commit_path(project, commit))
180 181 182 183
        end
      end
    end
  end
184 185 186 187 188 189 190 191 192 193

  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')
194
      @project = create(:project, path: 'test', namespace: namespace)
195 196 197 198 199 200 201 202
      @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')
203
      @project = create(:project, path: 'test', namespace: namespace)
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
      @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')
219
      @project = create(:project, path: 'test', namespace: namespace)
220 221 222 223 224 225 226
      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')
227
      @project = create(:project, path: 'test', namespace: namespace)
228 229 230 231 232 233 234 235 236 237 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

  describe '#form_resurces' do
    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')
244
      @project = create(:project, path: 'test', namespace: namespace)
245 246 247 248 249 250 251 252
      @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')
253
      @project = create(:project, path: 'test', namespace: namespace)
254 255 256 257 258
      @note = create(:note_on_issue, project: @project)

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

  describe '#noteable_note_url' do
261
    let(:project) { create(:project) }
262 263 264 265 266 267 268
    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
269
end