Welcome to mirror list, hosted at ThFree Co, Russian Federation.

issue_notes_importer_spec.rb « importers « bitbucket_import « gitlab « lib « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 1a2a43d68777cd568222e2999b776769a308834d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::BitbucketImport::Importers::IssueNotesImporter, :clean_gitlab_redis_cache, feature_category: :importers do
  let_it_be(:project) do
    create(:project, :import_started, import_source: 'namespace/repo',
      import_data_attributes: {
        credentials: { 'base_uri' => 'http://bitbucket.org/', 'user' => 'bitbucket', 'password' => 'password' }
      }
    )
  end

  let_it_be(:bitbucket_user) { create(:user) }
  let_it_be(:identity) { create(:identity, user: bitbucket_user, extern_uid: 'bitbucket_user', provider: :bitbucket) }
  let_it_be(:issue) { create(:issue, project: project) }
  let(:hash) { { iid: issue.iid } }
  let(:note_body) { 'body' }
  let(:client) { Bitbucket::Client.new({}) }

  subject(:importer) { described_class.new(project, hash) }

  describe '#execute' do
    let(:issue_comments_response) do
      [
        Bitbucket::Representation::Comment.new({
          'user' => { 'nickname' => 'bitbucket_user' },
          'content' => { 'raw' => note_body },
          'created_on' => Date.today,
          'updated_on' => Date.today
        })
      ]
    end

    before do
      allow(Bitbucket::Client).to receive(:new).and_return(client)
      allow(client).to receive(:issue_comments).and_return(issue_comments_response)
    end

    it 'creates a new note with the correct attributes' do
      expect { importer.execute }.to change { issue.notes.count }.from(0).to(1)

      note = issue.notes.first

      expect(note.project).to eq(project)
      expect(note.note).to eq(note_body)
      expect(note.author).to eq(bitbucket_user)
      expect(note.created_at).to eq(Date.today)
      expect(note.updated_at).to eq(Date.today)
    end

    context 'when the author does not have a bitbucket identity' do
      before do
        identity.update!(provider: :github)
      end

      it 'sets the author to the project creator and adds the author to the note' do
        importer.execute

        note = issue.notes.first

        expect(note.author).to eq(project.creator)
        expect(note.note).to eq("*Created by: bitbucket_user*\n\nbody")
      end
    end

    it 'calls RefConverter to convert Bitbucket refs to Gitlab refs' do
      expect(importer.instance_values['ref_converter']).to receive(:convert_note).once

      importer.execute
    end

    context 'when an error is raised' do
      before do
        allow(client).to receive(:issue_comments).and_raise(StandardError)
      end

      it 'tracks the failure and does not fail' do
        expect(Gitlab::Import::ImportFailureService).to receive(:track).once

        importer.execute
      end
    end
  end
end