note_data_builder_spec.rb 3.72 KB
Newer Older
1 2
require 'spec_helper'

Douwe Maan's avatar
Douwe Maan committed
3
describe 'Gitlab::NoteDataBuilder', lib: true do
4 5 6 7 8 9 10 11
  let(:project) { create(:project) }
  let(:user) { create(:user) }
  let(:data) { Gitlab::NoteDataBuilder.build(note, user) }
  let(:fixed_time) { Time.at(1425600000) } # Avoid time precision errors

  before(:each) do
    expect(data).to have_key(:object_attributes)
    expect(data[:object_attributes]).to have_key(:url)
12
    expect(data[:object_attributes][:url]).to eq(Gitlab::UrlBuilder.build(note))
13 14 15 16 17
    expect(data[:object_kind]).to eq('note')
    expect(data[:user]).to eq(user.hook_attrs)
  end

  describe 'When asking for a note on commit' do
18
    let(:note) { create(:note_on_commit, project: project) }
19 20 21 22

    it 'returns the note and commit-specific data' do
      expect(data).to have_key(:commit)
    end
23 24 25

    include_examples 'project hook data'
    include_examples 'deprecated repository hook data'
26 27 28
  end

  describe 'When asking for a note on commit diff' do
29
    let(:note) { create(:note_on_commit_diff, project: project) }
30 31 32 33

    it 'returns the note and commit-specific data' do
      expect(data).to have_key(:commit)
    end
34 35 36

    include_examples 'project hook data'
    include_examples 'deprecated repository hook data'
37 38 39 40
  end

  describe 'When asking for a note on issue' do
    let(:issue) { create(:issue, created_at: fixed_time, updated_at: fixed_time) }
41
    let(:note) { create(:note_on_issue, noteable_id: issue.id, project: project) }
42 43 44

    it 'returns the note and issue-specific data' do
      expect(data).to have_key(:issue)
Rubén Dávila's avatar
Rubén Dávila committed
45 46
      expect(data[:issue].except('updated_at')).to eq(issue.hook_attrs.except('updated_at'))
      expect(data[:issue]['updated_at']).to be > issue.hook_attrs['updated_at']
47
    end
48 49 50

    include_examples 'project hook data'
    include_examples 'deprecated repository hook data'
51 52 53 54
  end

  describe 'When asking for a note on merge request' do
    let(:merge_request) { create(:merge_request, created_at: fixed_time, updated_at: fixed_time) }
55
    let(:note) { create(:note_on_merge_request, noteable_id: merge_request.id, project: project) }
56 57 58

    it 'returns the note and merge request data' do
      expect(data).to have_key(:merge_request)
Rubén Dávila's avatar
Rubén Dávila committed
59 60
      expect(data[:merge_request].except('updated_at')).to eq(merge_request.hook_attrs.except('updated_at'))
      expect(data[:merge_request]['updated_at']).to be > merge_request.hook_attrs['updated_at']
61
    end
62 63 64

    include_examples 'project hook data'
    include_examples 'deprecated repository hook data'
65 66 67 68
  end

  describe 'When asking for a note on merge request diff' do
    let(:merge_request) { create(:merge_request, created_at: fixed_time, updated_at: fixed_time) }
69
    let(:note) { create(:note_on_merge_request_diff, noteable_id: merge_request.id, project: project) }
70 71 72

    it 'returns the note and merge request diff data' do
      expect(data).to have_key(:merge_request)
Rubén Dávila's avatar
Rubén Dávila committed
73 74
      expect(data[:merge_request].except('updated_at')).to eq(merge_request.hook_attrs.except('updated_at'))
      expect(data[:merge_request]['updated_at']).to be > merge_request.hook_attrs['updated_at']
75
    end
76 77 78

    include_examples 'project hook data'
    include_examples 'deprecated repository hook data'
79 80 81 82
  end

  describe 'When asking for a note on project snippet' do
    let!(:snippet) { create(:project_snippet, created_at: fixed_time, updated_at: fixed_time) }
83
    let!(:note) { create(:note_on_project_snippet, noteable_id: snippet.id, project: project) }
84 85 86

    it 'returns the note and project snippet data' do
      expect(data).to have_key(:snippet)
Rubén Dávila's avatar
Rubén Dávila committed
87 88
      expect(data[:snippet].except('updated_at')).to eq(snippet.hook_attrs.except('updated_at'))
      expect(data[:snippet]['updated_at']).to be > snippet.hook_attrs['updated_at']
89
    end
90 91 92

    include_examples 'project hook data'
    include_examples 'deprecated repository hook data'
93 94
  end
end