BigW Consortium Gitlab

comment_formatter_spec.rb 2.28 KB
Newer Older
1 2
require 'spec_helper'

3
describe Gitlab::GithubImport::CommentFormatter, lib: true do
4
  let(:project) { create(:project) }
5
  let(:octocat) { double(id: 123456, login: 'octocat') }
6 7
  let(:created_at) { DateTime.strptime('2013-04-10T20:09:31Z') }
  let(:updated_at) { DateTime.strptime('2014-03-03T18:58:10Z') }
8
  let(:base) do
9 10 11
    {
      body: "I'm having a problem with this.",
      user: octocat,
12 13
      commit_id: nil,
      diff_hunk: nil,
14 15 16 17 18
      created_at: created_at,
      updated_at: updated_at
    }
  end

19
  subject(:comment) { described_class.new(project, raw)}
20 21 22

  describe '#attributes' do
    context 'when do not reference a portion of the diff' do
23
      let(:raw) { double(base) }
24 25 26 27 28 29 30 31

      it 'returns formatted attributes' do
        expected = {
          project: project,
          note: "*Created by: octocat*\n\nI'm having a problem with this.",
          commit_id: nil,
          line_code: nil,
          author_id: project.creator_id,
32
          type: nil,
33 34 35 36 37 38 39 40 41
          created_at: created_at,
          updated_at: updated_at
        }

        expect(comment.attributes).to eq(expected)
      end
    end

    context 'when on a portion of the diff' do
42
      let(:diff) do
43 44 45
        {
          body: 'Great stuff',
          commit_id: '6dcb09b5b57875f334f61aebed695e2e4193db5e',
46 47
          diff_hunk: "@@ -1,5 +1,9 @@\n class User\n   def name\n-    'John Doe'\n+    'Jane Doe'",
          path: 'file1.txt'
48 49 50
        }
      end

51
      let(:raw) { double(base.merge(diff)) }
52 53 54 55 56 57

      it 'returns formatted attributes' do
        expected = {
          project: project,
          note: "*Created by: octocat*\n\nGreat stuff",
          commit_id: '6dcb09b5b57875f334f61aebed695e2e4193db5e',
58
          line_code: 'ce1be0ff4065a6e9415095c95f25f47a633cef2b_4_3',
59
          author_id: project.creator_id,
60
          type: 'LegacyDiffNote',
61 62 63 64 65 66 67 68 69
          created_at: created_at,
          updated_at: updated_at
        }

        expect(comment.attributes).to eq(expected)
      end
    end

    context 'when author is a GitLab user' do
70
      let(:raw) { double(base.merge(user: octocat)) }
71

72
      it 'returns GitLab user id as author_id' do
73 74 75 76 77 78
        gl_user = create(:omniauth_user, extern_uid: octocat.id, provider: 'github')
        expect(comment.attributes.fetch(:author_id)).to eq gl_user.id
      end
    end
  end
end