Skip to content

Commit

Permalink
Bugfix: Ensure test directories are valid paths (#732)
Browse files Browse the repository at this point in the history
* Bugfix: Ensure test directories are valid paths

* Fix

* Remove un-needed reload

* Remove blank line
  • Loading branch information
bess authored May 23, 2024
1 parent 1b03418 commit e75ccda
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 9 deletions.
14 changes: 11 additions & 3 deletions app/models/project.rb
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,14 @@ def file_list_to_file(session_id:, filename:)
Mediaflux::Http::IteratorDestroyRequest.new(session_token: session_id, iterator: iterator_id).resolve
end

# Ensure that the project directory is a valid path
# @example
# Project.safe_name("My Project") # => "My-Project"
def self.safe_name(name)
# only alphanumeric characters
name.strip.gsub(/[^A-Za-z\d]/, "-")
end

private

def files_from_iterator(iterator_resp)
Expand All @@ -246,9 +254,9 @@ def project_directory_pathname
Pathname.new(@original_directory)
end
end


# Ensure that the project directory is a valid path
def safe_name(name)
# only alphanumeric characters
name.strip.gsub(/[^A-Za-z\d]/, "-")
Project.safe_name(name)
end
end
5 changes: 3 additions & 2 deletions app/models/project_metadata.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def initialize(project:, current_user: nil)
@params = {}
end

# Generates a Hash of updated Project metadata attributes
# Generate a Hash of updated Project metadata attributes
# @param params [Hash] the updated Project metadata attributes
# @return [Hash]
def update_metadata(params:)
Expand Down Expand Up @@ -36,8 +36,9 @@ def activate_project(collection_id:, current_user:)
project.provenance_events.create(event_type: ProvenanceEvent::STATUS_UPDATE_EVENT_TYPE, event_person: current_user.uid, event_details: "The Status of this project has been set to active")
end

# Approve a project by recording the mediaflux id & setting the status to 'approved'
# @param params [Hash] the updated Project metadata attributes
def approve_project(params:)
# approve a project by recording the mediaflux id & setting the status to 'approved'
project.mediaflux_id = params[:mediaflux_id]
project.metadata_json["status"] = Project::APPROVED_STATUS
project.metadata_json["project_directory"] = "#{params[:project_directory_prefix]}/#{params[:project_directory]}"
Expand Down
2 changes: 1 addition & 1 deletion spec/factories/project.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
factory :project_with_dynamic_directory, class: "Project" do
transient do
sequence :project_directory do |n|
"#{FFaker::Food.fruit}#{n}"
Project.safe_name("#{FFaker::Food.fruit}#{n}")
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions spec/models/project_mediaflux_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
require "rails_helper"

RSpec.describe ProjectMediaflux, type: :model do
let(:collection_metadata) { { id: "abc", name: "test", path: "td-demo-001/rc/test-ns/test", description: "description", namespace: "td-demo-001/rc/test-ns" } }
let(:collection_metadata) { { id: "abc", name: "test", path: "/td-demo-001/rc/test-ns/test", description: "description", namespace: "/td-demo-001/rc/test-ns" } }
let(:project) { FactoryBot.build :project_with_doi }
let(:current_user) { FactoryBot.create(:user, uid: "jh1234") }

Expand Down Expand Up @@ -130,7 +130,7 @@
#raise a metadata error & log what specific required fields are missing when writing a project to mediaflux
expect {
incomplete_project.metadata_json["project_id"] = nil # we can no longer save the project without an id, so we have to reset it here to cause the error
collection_id = ProjectMediaflux.create!(project: incomplete_project, session_id: session_token)
ProjectMediaflux.create!(project: incomplete_project, session_id: session_token)
}.to raise_error do |error|
expect(error).to be_a(TigerData::MetadataError)
expect(error.message).to include("Project creation failed with metadata schema version 0.6.1 due to the missing fields:")
Expand Down
1 change: 0 additions & 1 deletion spec/models/project_metadata_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,6 @@
event_note_message: "Message filler"
}
project_metadata.approve_project(params:)

# activation should do nothing because the project_id (DOI) will not match
project_metadata.activate_project(collection_id: "112233", current_user:)

Expand Down

0 comments on commit e75ccda

Please sign in to comment.