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