Commit 237d3446 authored by Eugenia Grieff's avatar Eugenia Grieff

Verify if headers are present in csv file

- Add tests to very that an error is raised if the
headers title and description are not present
parent 0c5e65ae
......@@ -20,21 +20,13 @@ module Issuable
private
def process_csv
csv_data = @csv_io.open(&:read).force_encoding(Encoding::UTF_8)
csv_parsing_params = {
col_sep: detect_col_sep(csv_data.lines.first),
headers: true,
header_converters: :symbol
}
CSV.new(csv_data, csv_parsing_params).each.with_index(2) do |row, line_no|
with_csv_lines.each do |row, line_no|
issuable_attributes = {
title: row[:title],
description: row[:description]
}
if issuable(issuable_attributes).persisted?
if create_issuable(issuable_attributes).persisted?
@results[:success] += 1
else
@results[:error_lines].push(line_no)
......@@ -44,6 +36,26 @@ module Issuable
@results[:parse_error] = true
end
def with_csv_lines
csv_data = @csv_io.open(&:read).force_encoding(Encoding::UTF_8)
verify_headers!(csv_data)
csv_parsing_params = {
col_sep: detect_col_sep(csv_data.lines.first),
headers: true,
header_converters: :symbol
}
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')
raise CSV::MalformedCSVError
end
def detect_col_sep(header)
if header.include?(",")
","
......@@ -56,12 +68,12 @@ module Issuable
end
end
def email_results_to_user
# defined in ImportCsvService
def create_issuable(attributes)
create_issuable_class.new(@project, @user, attributes).execute
end
def issuable(attributes)
create_issuable_class.new(@project, @user, attributes).execute
def email_results_to_user
# defined in ImportCsvService
end
def create_issuable_class
......
......@@ -3,19 +3,19 @@
require 'spec_helper'
RSpec.describe RequirementsManagement::ImportCsvService do
let(:project) { create(:project) }
let(:user) { create(:user) }
let_it_be_with_refind(:project) { create(:project) }
let_it_be(:user) { create(:user) }
let(:service) do
uploader = FileUploader.new(project)
uploader.store!(file)
described_class.new(user, project, uploader).execute
described_class.new(user, project, uploader)
end
shared_examples 'resource not available' do
it 'raises an error' do
expect { service }.to raise_error(Gitlab::Access::AccessDeniedError)
expect { service.execute }.to raise_error(Gitlab::Access::AccessDeniedError)
end
end
......
Issue in 中文,Test description
"Hello","World"
"Title with quote""",Description
......@@ -9,7 +9,7 @@ RSpec.describe Issues::ImportCsvService do
uploader = FileUploader.new(project)
uploader.store!(file)
described_class.new(user, project, uploader).execute
described_class.new(user, project, uploader)
end
include_examples 'issuable import csv service', 'issue' do
......
......@@ -3,14 +3,14 @@
require 'spec_helper'
RSpec.shared_examples 'issuable import csv service' do |issuable_type|
let(:project) { create(:project) }
let(:user) { create(:user) }
let_it_be_with_refind(:project) { create(:project) }
let_it_be(:user) { create(:user) }
subject { service }
subject { service.execute }
shared_examples_for 'an issuable importer' do
it 'records the import attempt if resource is an issue' do
if issuable_type == 'issue'
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
......@@ -19,8 +19,8 @@ RSpec.shared_examples 'issuable import csv service' do |issuable_type|
end
shared_examples_for 'importer with email notification' do
it 'notifies user of import result' do
if issuable_type == 'issue'
if issuable_type == 'issue'
it 'notifies user of import result' do
expect(Notify).to receive_message_chain(email_method, :deliver_later)
subject
......@@ -41,6 +41,18 @@ RSpec.shared_examples 'issuable import csv service' do |issuable_type|
it_behaves_like 'an issuable importer'
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'
end
context 'with a file generated by Gitlab CSV export' do
let(:file) { fixture_file_upload('spec/fixtures/csv_gitlab_export.csv') }
......
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