note_spec.rb 4.23 KB
Newer Older
1 2
require 'spec_helper'

3
describe Gitlab::DataBuilder::Note do
4
  let(:project) { create(:project, :repository) }
5
  let(:user) { create(:user) }
6
  let(:data) { described_class.build(note, user) }
7 8
  let(:fixed_time) { Time.at(1425600000) } # Avoid time precision errors

9
  before do
10 11
    expect(data).to have_key(:object_attributes)
    expect(data[:object_attributes]).to have_key(:url)
12 13
    expect(data[:object_attributes][:url])
      .to eq(Gitlab::UrlBuilder.build(note))
14 15 16 17 18
    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
19
    let(:note) { create(:note_on_commit, project: project) }
20 21 22 23

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

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

  describe 'When asking for a note on commit diff' do
Douwe Maan's avatar
Douwe Maan committed
30
    let(:note) { create(:diff_note_on_commit, project: project) }
31 32 33 34

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

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

  describe 'When asking for a note on issue' do
41 42 43 44 45 46
    let(:issue) do
      create(:issue, created_at: fixed_time, updated_at: fixed_time,
                     project: project)
    end

    let(:note) do
Grzegorz Bizon's avatar
Grzegorz Bizon committed
47
      create(:note_on_issue, noteable: issue, project: project)
48
    end
49 50 51

    it 'returns the note and issue-specific data' do
      expect(data).to have_key(:issue)
52
      expect(data[:issue].except('updated_at'))
Grzegorz Bizon's avatar
Grzegorz Bizon committed
53
        .to eq(issue.reload.hook_attrs.except('updated_at'))
54 55
      expect(data[:issue]['updated_at'])
        .to be > issue.hook_attrs['updated_at']
56
    end
57 58 59

    include_examples 'project hook data'
    include_examples 'deprecated repository hook data'
60 61 62
  end

  describe 'When asking for a note on merge request' do
63 64 65 66 67 68 69
    let(:merge_request) do
      create(:merge_request, created_at: fixed_time,
                             updated_at: fixed_time,
                             source_project: project)
    end

    let(:note) do
Grzegorz Bizon's avatar
Grzegorz Bizon committed
70
      create(:note_on_merge_request, noteable: merge_request,
71 72
                                     project: project)
    end
73 74 75

    it 'returns the note and merge request data' do
      expect(data).to have_key(:merge_request)
76
      expect(data[:merge_request].except('updated_at'))
Grzegorz Bizon's avatar
Grzegorz Bizon committed
77
        .to eq(merge_request.reload.hook_attrs.except('updated_at'))
78 79
      expect(data[:merge_request]['updated_at'])
        .to be > merge_request.hook_attrs['updated_at']
80
    end
81 82 83

    include_examples 'project hook data'
    include_examples 'deprecated repository hook data'
84 85 86
  end

  describe 'When asking for a note on merge request diff' do
87 88 89 90 91 92
    let(:merge_request) do
      create(:merge_request, created_at: fixed_time, updated_at: fixed_time,
                             source_project: project)
    end

    let(:note) do
Douwe Maan's avatar
Douwe Maan committed
93
      create(:diff_note_on_merge_request, noteable: merge_request,
94 95
                                          project: project)
    end
96 97 98

    it 'returns the note and merge request diff data' do
      expect(data).to have_key(:merge_request)
99
      expect(data[:merge_request].except('updated_at'))
Grzegorz Bizon's avatar
Grzegorz Bizon committed
100
        .to eq(merge_request.reload.hook_attrs.except('updated_at'))
101 102
      expect(data[:merge_request]['updated_at'])
        .to be > merge_request.hook_attrs['updated_at']
103
    end
104 105 106

    include_examples 'project hook data'
    include_examples 'deprecated repository hook data'
107 108 109
  end

  describe 'When asking for a note on project snippet' do
110 111 112 113 114 115
    let!(:snippet) do
      create(:project_snippet, created_at: fixed_time, updated_at: fixed_time,
                               project: project)
    end

    let!(:note) do
Grzegorz Bizon's avatar
Grzegorz Bizon committed
116
      create(:note_on_project_snippet, noteable: snippet,
117 118
                                       project: project)
    end
119 120 121

    it 'returns the note and project snippet data' do
      expect(data).to have_key(:snippet)
122
      expect(data[:snippet].except('updated_at'))
Grzegorz Bizon's avatar
Grzegorz Bizon committed
123
        .to eq(snippet.reload.hook_attrs.except('updated_at'))
124 125
      expect(data[:snippet]['updated_at'])
        .to be > snippet.hook_attrs['updated_at']
126
    end
127 128 129

    include_examples 'project hook data'
    include_examples 'deprecated repository hook data'
130 131
  end
end