Skip to content

Commit

Permalink
Adding metadata validation for required fields to model validation (#669
Browse files Browse the repository at this point in the history
)
  • Loading branch information
carolyncole authored May 1, 2024
1 parent 464758d commit cc911df
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 11 deletions.
2 changes: 1 addition & 1 deletion app/models/project_mediaflux.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def self.create!(project:, session_id:, xml_namespace: nil)
when /'asset.create' failed/

# Ensure that the metadata validations are run
if project.valid? && project.metadata_model.valid?
if project.valid?
raise response_error[:message] # something strange went wrong
else
raise TigerData::MissingMetadata.missing_metadata(schema_version: ::TigerdataSchema::SCHEMA_VERSION, errors: project.metadata_model.errors)
Expand Down
6 changes: 3 additions & 3 deletions app/models/project_metadata.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def validate(record)
def initialize(project:, current_user: nil)
@project = project
@current_user = current_user
@params = {}
@params = { }
end

# Generates a Hash of updated Project metadata attributes
Expand Down Expand Up @@ -90,8 +90,8 @@ def attributes
directory: params[:directory],
title: params[:title],
description: params[:description],
status: params[:status],
project_id: project.metadata[:project_id],
status: params[:status] || project.metadata[:status],
project_id: project.metadata[:project_id] || '', # allow validation to pass until doi can be generated
storage_capacity: project.metadata[:storage_capacity],
storage_performance_expectations: project.metadata[:storage_performance_expectations],
project_purpose: project.metadata[:project_purpose],
Expand Down
5 changes: 5 additions & 0 deletions app/models/project_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ def validate(project)
# Validate if present
project.metadata[:data_user_read_only]&.each { |read_only| validate_role(project:, netid: read_only, role: "Data User Read Only")}
project.metadata[:data_user_read_write]&.each { |read_write| validate_role(project:, netid: read_write, role: "Data User Read Write")}

# validate all required fields
if !project.metadata_model.valid?
project.errors.add :base, "Invalid Project Metadata it does not match the schema #{TigerdataSchema::SCHEMA_VERSION}\n #{project.metadata_model.errors.to_a.join(", ")}"
end
end

private
Expand Down
6 changes: 4 additions & 2 deletions spec/factories/project.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@
title { FFaker::Movie.title }
created_on { Time.current.in_time_zone("America/New_York").iso8601 }
updated_on { Time.current.in_time_zone("America/New_York").iso8601 }
project_id { nil }
project_id { "" }
status { "pending" }
storage_capacity { { size: { requested: 500 }, unit: { requested: "GB" } } }
storage_performance { { requested: "standard" } }
project_purpose { "research" }
directory { "big-data" }
schema_version { "0.6.1" }
end
mediaflux_id { nil }
metadata do
Expand All @@ -36,7 +37,8 @@
status: status,
storage_capacity: storage_capacity,
storage_performance_expectations: storage_performance,
project_purpose: project_purpose
project_purpose: project_purpose,
schema_version: schema_version
}
end
factory :project_with_dynamic_directory, class: "Project" do
Expand Down
2 changes: 1 addition & 1 deletion spec/models/project_metadata_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
project_metadata = described_class.new(current_user: current_user, project: Project.new)
update = project_metadata.create(params: {})
expect(datacite_stub).not_to have_received(:draft_doi)
expect(update).to be_nil
expect(update).to be_blank
end
end
end
Expand Down
20 changes: 16 additions & 4 deletions spec/models/project_validator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,28 @@
context "with valid roles" do
it "finds no errors" do
sponsor = FactoryBot.create(:project_sponsor)
project = FactoryBot.create(:project, data_sponsor: sponsor.uid, data_manager: sponsor.uid)
project = FactoryBot.create(:project, data_sponsor: sponsor.uid, data_manager: sponsor.uid, project_id: "abc123")
expect(project).to be_valid
end
end

context "with missing schema version" do
it "finds the error" do
sponsor = FactoryBot.create(:project_sponsor)
project = FactoryBot.build(:project, data_sponsor: nil, data_manager: sponsor.uid, project_id: "abc123", schema_version: nil)
expect(project).not_to be_valid
expect(project.errors.map(&:full_message)).to eq(["Mising netid for role Data Sponsor",
"Invalid Project Metadata it does not match the schema 0.6.1\n Data sponsor Missing metadata value for data_sponsor"])
end
end

context "with missing data sponsor" do
it "finds the error" do
sponsor = FactoryBot.create(:project_sponsor)
project = FactoryBot.build(:project, data_sponsor: nil, data_manager: sponsor.uid)
project = FactoryBot.build(:project, data_sponsor: nil, data_manager: sponsor.uid, project_id: "abc123")
expect(project).not_to be_valid
expect(project.errors.map(&:full_message)).to eq(["Mising netid for role Data Sponsor"])
expect(project.errors.map(&:full_message)).to eq(["Mising netid for role Data Sponsor",
"Invalid Project Metadata it does not match the schema 0.6.1\n Data sponsor Missing metadata value for data_sponsor"])
end
end

Expand All @@ -25,7 +36,8 @@
sponsor = FactoryBot.create(:project_sponsor)
project = FactoryBot.build(:project, data_sponsor: sponsor.uid, data_manager: nil)
expect(project).not_to be_valid
expect(project.errors.map(&:full_message)).to eq(["Mising netid for role Data Manager"])
expect(project.errors.map(&:full_message)).to eq(["Mising netid for role Data Manager",
"Invalid Project Metadata it does not match the schema 0.6.1\n Data manager Missing metadata value for data_manager"])
end
end

Expand Down
1 change: 1 addition & 0 deletions spec/requests/projects_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

context "when the client is authenticated" do
let(:user) { FactoryBot.create(:user, uid: "pul123") }
let(:title) { "a title" }
let(:response_body) do
filename = Rails.root.join("spec", "fixtures", "files", "version_response.xml")
File.new(filename).read
Expand Down

0 comments on commit cc911df

Please sign in to comment.