summaryrefslogtreecommitdiff
path: root/spec/support/services/issuable_import_csv_service_shared_examples.rb
blob: f68750bec325c8e7f0bed23c6cefbe294f236f66 (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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# frozen_string_literal: true

require 'spec_helper'

RSpec.shared_examples 'issuable import csv service' do |issuable_type|
  let_it_be_with_refind(:project) { create(:project) }
  let_it_be(:user) { create(:user) }

  subject { service.execute }

  shared_examples_for 'an issuable importer' do
    if issuable_type == 'issue'
      it 'records the import attempt if resource is an issue' do
        expect { subject }
          .to change { Issues::CsvImport.where(project: project, user: user).count }
          .by 1
      end
    end
  end

  shared_examples_for 'importer with email notification' do
    it 'notifies user of import result' do
      expect(Notify).to receive_message_chain(email_method, :deliver_later)

      subject
    end
  end

  shared_examples_for 'invalid file' do
    it 'returns invalid file error' do
      expect(subject[:success]).to eq(0)
      expect(subject[:parse_error]).to eq(true)
    end

    it_behaves_like 'importer with email notification'
    it_behaves_like 'an issuable importer'
  end

  describe '#execute' do
    context 'invalid file extension' do
      let(:file) { fixture_file_upload('spec/fixtures/banana_sample.gif') }

      it_behaves_like 'invalid file'
    end

    context 'empty file' do
      let(:file) { fixture_file_upload('spec/fixtures/csv_empty.csv') }

      it_behaves_like 'invalid file'
    end

    context 'file without headers' do
      let(:file) { fixture_file_upload('spec/fixtures/csv_no_headers.csv') }

      it_behaves_like 'invalid file'
    end

    context 'with a file generated by Gitlab CSV export' do
      let(:file) { fixture_file_upload('spec/fixtures/csv_gitlab_export.csv') }

      it 'imports the CSV without errors' do
        expect(subject[:success]).to eq(4)
        expect(subject[:error_lines]).to eq([])
        expect(subject[:parse_error]).to eq(false)
      end

      it 'correctly sets the issuable attributes' do
        expect { subject }.to change { issuables.count }.by 4

        expect(issuables.reload.last).to have_attributes(
          title: 'Test Title',
          description: 'Test Description'
        )
      end

      it_behaves_like 'importer with email notification'
      it_behaves_like 'an issuable importer'
    end

    context 'comma delimited file' do
      let(:file) { fixture_file_upload('spec/fixtures/csv_comma.csv') }

      it 'imports CSV without errors' do
        expect(subject[:success]).to eq(3)
        expect(subject[:error_lines]).to eq([])
        expect(subject[:parse_error]).to eq(false)
      end

      it 'correctly sets the issuable attributes' do
        expect { subject }.to change { issuables.count }.by 3

        expect(issuables.reload.last).to have_attributes(
          title: 'Title with quote"',
          description: 'Description'
        )
      end

      it_behaves_like 'importer with email notification'
      it_behaves_like 'an issuable importer'
    end

    context 'tab delimited file with error row' do
      let(:file) { fixture_file_upload('spec/fixtures/csv_tab.csv') }

      it 'imports CSV with some error rows' do
        expect(subject[:success]).to eq(2)
        expect(subject[:error_lines]).to eq([3])
        expect(subject[:parse_error]).to eq(false)
      end

      it 'correctly sets the issuable attributes' do
        expect { subject }.to change { issuables.count }.by 2

        expect(issuables.reload.last).to have_attributes(
          title: 'Hello',
          description: 'World'
        )
      end

      it_behaves_like 'importer with email notification'
      it_behaves_like 'an issuable importer'
    end

    context 'semicolon delimited file with CRLF' do
      let(:file) { fixture_file_upload('spec/fixtures/csv_semicolon.csv') }

      it 'imports CSV with a blank row' do
        expect(subject[:success]).to eq(3)
        expect(subject[:error_lines]).to eq([4])
        expect(subject[:parse_error]).to eq(false)
      end

      it 'correctly sets the issuable attributes' do
        expect { subject }.to change { issuables.count }.by 3

        expect(issuables.reload.last).to have_attributes(
          title: 'Hello',
          description: 'World'
        )
      end

      it_behaves_like 'importer with email notification'
      it_behaves_like 'an issuable importer'
    end
  end
end