summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/github_import/importer/labels_importer_spec.rb
blob: ad9ef4afddd85f42248d21f4dc92bd25ea7e2b77 (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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::GithubImport::Importer::LabelsImporter, :clean_gitlab_redis_cache, feature_category: :importer do
  let(:project) { create(:project, import_source: 'foo/bar') }
  let(:client) { double(:client) }
  let(:importer) { described_class.new(project, client) }

  describe '#execute' do
    it 'imports the labels in bulk' do
      label_hash = { title: 'bug', color: '#fffaaa' }

      expect(importer).to receive(:build_labels).and_return([[label_hash], []])
      expect(importer).to receive(:bulk_insert).with([label_hash])
      expect(importer).to receive(:build_labels_cache)

      importer.execute
    end
  end

  describe '#build_labels' do
    it 'returns an Array containing label rows' do
      label = { name: 'bug', color: 'ffffff' }

      expect(importer).to receive(:each_label).and_return([label])

      rows, errors = importer.build_labels

      expect(rows.length).to eq(1)
      expect(rows[0][:title]).to eq('bug')
      expect(errors).to be_blank
    end

    it 'does not build labels that already exist' do
      create(:label, project: project, title: 'bug')

      label = { name: 'bug', color: 'ffffff' }

      expect(importer).to receive(:each_label).and_return([label])

      rows, errors = importer.build_labels

      expect(rows).to be_empty
      expect(errors).to be_empty
    end

    it 'does not build labels that are invalid' do
      label = { id: 1, name: 'bug,bug', color: 'ffffff' }

      expect(importer).to receive(:each_label).and_return([label])
      expect(Gitlab::Import::Logger).to receive(:error)
        .with(
          import_type: :github,
          project_id: project.id,
          importer: described_class.name,
          message: ['Title is invalid'],
          github_identifier: 1
        )

      rows, errors = importer.build_labels

      expect(rows).to be_empty
      expect(errors.length).to eq(1)
      expect(errors[0].full_messages).to match_array(['Title is invalid'])
    end
  end

  describe '#build_labels_cache' do
    it 'builds the labels cache' do
      expect_next_instance_of(Gitlab::GithubImport::LabelFinder) do |instance|
        expect(instance).to receive(:build_cache)
      end

      importer.build_labels_cache
    end
  end

  describe '#build_attributes' do
    let(:label_hash) do
      importer.build_attributes({ name: 'bug', color: 'ffffff' })
    end

    it 'returns the attributes of the label as a Hash' do
      expect(label_hash).to be_an_instance_of(Hash)
    end

    context 'the returned Hash' do
      it 'includes the label title' do
        expect(label_hash[:title]).to eq('bug')
      end

      it 'includes the label color' do
        expect(label_hash[:color]).to eq('#ffffff')
      end

      it 'includes the project ID' do
        expect(label_hash[:project_id]).to eq(project.id)
      end

      it 'includes the label type' do
        expect(label_hash[:type]).to eq('ProjectLabel')
      end

      it 'includes the created timestamp' do
        freeze_time do
          expect(label_hash[:created_at]).to eq(Time.zone.now)
        end
      end

      it 'includes the updated timestamp' do
        freeze_time do
          expect(label_hash[:updated_at]).to eq(Time.zone.now)
        end
      end
    end
  end

  describe '#each_label' do
    it 'returns the labels' do
      expect(client)
        .to receive(:labels)
        .with('foo/bar')

      importer.each_label
    end
  end
end