-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
File import controller enhancements (#714)
* starting to create the group metadata file import controller * adding translations * trying to move action into the new file import concern
- Loading branch information
Showing
9 changed files
with
187 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# frozen_string_literal: true | ||
|
||
# Common file import actions | ||
module FileImportActions | ||
extend ActiveSupport::Concern | ||
|
||
included do | ||
before_action proc { namespace } | ||
end | ||
|
||
def create | ||
authorize! @namespace, to: :update_sample_metadata? | ||
@imported_metadata = ::Samples::Metadata::FileImportService.new(@namespace, current_user, | ||
file_import_params).execute | ||
if @namespace.errors.empty? | ||
render status: :ok, locals: { type: :success, message: t('.success') } | ||
elsif @namespace.errors.include?(:sample) | ||
errors = @namespace.errors.messages_for(:sample) | ||
render status: :partial_content, locals: { type: :alert, message: t('.error'), errors: } | ||
else | ||
error = @namespace.errors.full_messages_for(:base).first | ||
render status: :unprocessable_entity, locals: { type: :danger, message: error } | ||
end | ||
end | ||
|
||
private | ||
|
||
def file_import_params | ||
params.require(:file_import).permit(:file, :sample_id_column, :ignore_empty_values) | ||
end | ||
end |
24 changes: 24 additions & 0 deletions
24
app/controllers/groups/samples/metadata/file_imports_controller.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# frozen_string_literal: true | ||
|
||
module Groups | ||
module Samples | ||
module Metadata | ||
# Controller actions for Group Samples Metadata File Import Controller | ||
class FileImportsController < Groups::ApplicationController | ||
include FileImportActions | ||
|
||
respond_to :turbo_stream | ||
|
||
private | ||
|
||
def namespace | ||
@namespace = group | ||
end | ||
|
||
def group | ||
@group = Group.find_by_full_path(params[:group_id]) # rubocop:disable Rails/DynamicFindBy | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 113 additions & 0 deletions
113
test/controllers/groups/samples/metadata/file_import_controller_test.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'test_helper' | ||
|
||
module Groups | ||
module Samples | ||
module Metadata | ||
class FileImportControllerTest < ActionDispatch::IntegrationTest | ||
setup do | ||
sign_in users(:john_doe) | ||
@group = groups(:group_one) | ||
@csv = fixture_file_upload('test/fixtures/files/metadata/valid.csv') | ||
end | ||
|
||
test 'import sample metadata with permission' do | ||
post group_samples_file_import_path(@group, format: :turbo_stream), | ||
params: { | ||
file_import: { | ||
file: @csv, | ||
sample_id_column: 'sample_name' | ||
} | ||
} | ||
|
||
assert_response :success | ||
end | ||
|
||
test 'import sample metadata without permission' do | ||
login_as users(:micha_doe) | ||
|
||
post group_samples_file_import_path(@group, format: :turbo_stream), | ||
params: { | ||
file_import: { | ||
file: @csv, | ||
sample_id_column: 'sample_name' | ||
} | ||
} | ||
|
||
assert_response :unauthorized | ||
end | ||
|
||
test 'import sample metadata with no file' do | ||
post group_samples_file_import_path(@group, format: :turbo_stream), | ||
params: { | ||
file_import: { sample_id_column: 'sample_name' } | ||
} | ||
assert_response :unprocessable_entity | ||
end | ||
|
||
test 'import sample metadata with no sample_id_column' do | ||
csv = File.new('test/fixtures/files/metadata/missing_sample_id_column.csv', 'r') | ||
post group_samples_file_import_path(@group, format: :turbo_stream), | ||
params: { | ||
file_import: { file: csv, sample_id_column: 'sample_name' } | ||
} | ||
assert_response :unprocessable_entity | ||
end | ||
|
||
test 'import sample metadata with duplicate column names' do | ||
csv = File.new('test/fixtures/files/metadata/duplicate_headers.csv', 'r') | ||
post group_samples_file_import_path(@group, format: :turbo_stream), | ||
params: { | ||
file_import: { file: csv, sample_id_column: 'sample_name' } | ||
} | ||
assert_response :unprocessable_entity | ||
end | ||
|
||
test 'import sample metadata with no metadata columns' do | ||
csv = File.new('test/fixtures/files/metadata/missing_metadata_columns.csv', 'r') | ||
post group_samples_file_import_path(@group, format: :turbo_stream), | ||
params: { | ||
file_import: { file: csv, sample_id_column: 'sample_name' } | ||
} | ||
assert_response :unprocessable_entity | ||
end | ||
|
||
test 'import sample metadata with no metadata rows' do | ||
csv = File.new('test/fixtures/files/metadata/missing_metadata_rows.csv', 'r') | ||
post group_samples_file_import_path(@group, format: :turbo_stream), | ||
params: { | ||
file_import: { file: csv, sample_id_column: 'sample_name' } | ||
} | ||
assert_response :unprocessable_entity | ||
end | ||
|
||
test 'import sample metadata with invalid file' do | ||
other = fixture_file_upload('test/fixtures/files/metadata/invalid.txt') | ||
post group_samples_file_import_path(@group, format: :turbo_stream), | ||
params: { | ||
file_import: { | ||
file: other, | ||
sample_id_column: 'sample_name' | ||
} | ||
} | ||
|
||
assert_response :unprocessable_entity | ||
end | ||
|
||
test 'import sample metadata with a sample that does not belong to group' do | ||
csv = fixture_file_upload('test/fixtures/files/metadata/mixed_project_samples.csv') | ||
post group_samples_file_import_path(@group, format: :turbo_stream), | ||
params: { | ||
file_import: { | ||
file: csv, | ||
sample_id_column: 'sample_name' | ||
} | ||
} | ||
|
||
assert_response :partial_content | ||
end | ||
end | ||
end | ||
end | ||
end |