Commit cec29428 authored by Eugenia Grieff's avatar Eugenia Grieff

Fix issuable import csv base service

- Raise CSV error if files have no lines
- Add specs for empty file case
parent 4d8c5880
......@@ -38,7 +38,7 @@ module Issuable
def with_csv_lines
csv_data = @csv_io.open(&:read).force_encoding(Encoding::UTF_8)
verify_headers!(csv_data)
validate_headers_presence!(csv_data.lines.first)
csv_parsing_params = {
col_sep: detect_col_sep(csv_data.lines.first),
......@@ -49,9 +49,9 @@ module Issuable
CSV.new(csv_data, csv_parsing_params).each.with_index(2)
end
def verify_headers!(data)
headers = data.lines.first.downcase
return if headers.include?('title') && headers.include?('description')
def validate_headers_presence!(headers)
headers.downcase! if headers
return if headers && headers.include?('title') && headers.include?('description')
raise CSV::MalformedCSVError
end
......
---
title: Fix error in Issuable::ImportCsv::BaseService when CSV file is empty
merge_request: 47918
author:
type: fixed
......@@ -26,29 +26,33 @@ RSpec.shared_examples 'issuable import csv service' do |issuable_type|
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' do
context 'invalid file extension' do
let(:file) { fixture_file_upload('spec/fixtures/banana_sample.gif') }
it 'returns invalid file error' do
expect(subject[:success]).to eq(0)
expect(subject[:parse_error]).to eq(true)
end
it_behaves_like 'invalid file'
end
it_behaves_like 'importer with email notification'
it_behaves_like 'an issuable importer'
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 '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'
it_behaves_like 'invalid file'
end
context 'with a file generated by Gitlab CSV export' do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment