diff --git a/app/serializers/doi_serializer.rb b/app/serializers/doi_serializer.rb index 58369cbc5..7abbf609a 100644 --- a/app/serializers/doi_serializer.rb +++ b/app/serializers/doi_serializer.rb @@ -19,6 +19,15 @@ class DoiSerializer object.doi.downcase end + attribute :creators do |object| + # If we're just a hash then force this over to a list. + if object.creators.class == Hash + object.creators = [object.creators] + end + + object.creators + end + attribute :state do |object| object.aasm_state end diff --git a/spec/requests/dois_spec.rb b/spec/requests/dois_spec.rb index 5a1089048..22d042ebe 100644 --- a/spec/requests/dois_spec.rb +++ b/spec/requests/dois_spec.rb @@ -9,7 +9,7 @@ let(:client) { create(:client, provider: provider, symbol: ENV['MDS_USERNAME'], password: ENV['MDS_PASSWORD']) } let!(:prefix) { create(:prefix, prefix: "10.14454") } let!(:client_prefix) { create(:client_prefix, client: client, prefix: prefix) } - + let(:doi) { create(:doi, client: client) } let(:bearer) { Client.generate_token(role_id: "client_admin", uid: client.symbol, provider_id: provider.symbol.downcase, client_id: client.symbol.downcase, password: client.password) } let(:headers) { { 'ACCEPT'=>'application/vnd.api+json', 'CONTENT_TYPE'=>'application/vnd.api+json', 'Authorization' => 'Bearer ' + bearer }} @@ -87,6 +87,25 @@ expect(response).to have_http_status(401) end end + + context 'creators started as an object not array' do + let(:doi) { create(:doi, client: client, creators: + { + "nameType": "Personal", + "name": "John Doe", + } ) } + + before { get "/dois/#{doi.doi}", headers: headers } + + it 'returns the creators as list' do + expect(json).not_to be_empty + expect(json.dig('data', 'attributes', 'creators')).to eq([doi.creators]) + end + + it 'returns status code 200' do + expect(response).to have_http_status(200) + end + end end describe 'state' do @@ -898,7 +917,7 @@ expect(json.dig('data', 'attributes', 'schemaVersion')).to eq("http://datacite.org/schema/kernel-4") expect(json.dig('data', 'attributes', 'source')).to eq("test") expect(json.dig('data', 'attributes', 'types')).to eq("bibtex"=>"article", "citeproc"=>"article-journal", "resourceType"=>"BlogPosting", "resourceTypeGeneral"=>"Text", "ris"=>"RPRT", "schemaOrg"=>"ScholarlyArticle") - + doc = Nokogiri::XML(Base64.decode64(json.dig('data', 'attributes', 'xml')), nil, 'UTF-8', &:noblanks) expect(doc.at_css("identifier").content).to eq("10.14454/10703") end @@ -1649,7 +1668,7 @@ it 'returns validation error' do expect(json.dig('errors')).to eq([{"source"=>"doi", "title"=>"Is invalid"}]) end - + end context 'validates schema 3' do @@ -1984,7 +2003,7 @@ } end - before { patch "/dois/#{doi.doi}", params: update_attributes.to_json, headers: headers } + before { patch "/dois/#{doi.doi}", params: update_attributes.to_json, headers: headers } it 'updates the Doi' do expect(json.dig('data', 'attributes', 'rightsList')).to eq(rights_list) @@ -2007,7 +2026,7 @@ } end - before { patch "/dois/#{doi.doi}", params: update_attributes.to_json, headers: headers } + before { patch "/dois/#{doi.doi}", params: update_attributes.to_json, headers: headers } it 'updates the Doi' do expect(json.dig('data', 'attributes', 'subjects')).to eq(subjects) @@ -2027,7 +2046,7 @@ } end - before { patch "/dois/#{doi.doi}", params: update_attributes.to_json, headers: headers } + before { patch "/dois/#{doi.doi}", params: update_attributes.to_json, headers: headers } it 'updates the Doi' do expect(json.dig('data', 'attributes', 'contentUrl')).to eq(content_url) @@ -2047,7 +2066,7 @@ } end - before { patch "/dois/#{doi.doi}", params: update_attributes.to_json, headers: headers } + before { patch "/dois/#{doi.doi}", params: update_attributes.to_json, headers: headers } it 'updates the Doi' do expect(json.dig('data', 'attributes', 'creators')).to eq(creators) @@ -2086,7 +2105,7 @@ } end - before { patch "/dois/#{doi.doi}", params: update_attributes.to_json, headers: headers } + before { patch "/dois/#{doi.doi}", params: update_attributes.to_json, headers: headers } it 'updates the Doi' do expect(json.dig('data', 'attributes', 'geoLocations')).to eq(geo_locations) @@ -2133,7 +2152,7 @@ } end - before { patch "/dois/#{doi.doi}", params: update_attributes.to_json, headers: headers } + before { patch "/dois/#{doi.doi}", params: update_attributes.to_json, headers: headers } it 'updates the Doi' do expect(json.dig('data', 'attributes', 'descriptions')).to eq(descriptions) @@ -2166,20 +2185,20 @@ } end - before { get "/dois/#{doi.doi}", headers: headers } + before { get "/dois/#{doi.doi}", headers: headers } it 'updates the Doi' do expect(json.dig('data', 'attributes', 'descriptions')).to eq([{"description"=>"Data from: A new malaria agent in African hominids."}]) expect(json.dig('data', 'attributes', 'container')).to be nil patch "/dois/#{doi.doi}", params: update_attributes.to_json, headers: headers - + expect(json.dig('data', 'attributes', 'descriptions').size).to eq(2) expect(json.dig('data', 'attributes', 'descriptions').last).to eq("description"=>"Keck Institute for Space Studies", "descriptionType"=>"SeriesInformation") expect(json.dig('data', 'attributes', 'container')).to eq("title"=>"Keck Institute for Space Studies", "type"=>"Series") patch "/dois/#{doi.doi}", params: update_attributes_again.to_json, headers: headers - + expect(json.dig('data', 'attributes', 'descriptions').size).to eq(1) expect(json.dig('data', 'attributes', 'container')).to be_nil end @@ -2654,275 +2673,275 @@ let(:client) { create(:client, provider: provider, symbol: ENV['MDS_USERNAME'], password: ENV['MDS_PASSWORD']) } let(:bearer) { Client.generate_token(role_id: "client_admin", uid: client.symbol, provider_id: provider.symbol.downcase, client_id: client.symbol.downcase, password: client.password) } let(:doi) { create(:doi, client: client, aasm_state: "findable") } - + context "no permission" do let(:doi) { create(:doi) } - + before { get "/dois/#{doi.doi}", headers: { "HTTP_ACCEPT" => "application/vnd.jats+xml", 'Authorization' => 'Bearer ' + bearer } } - + it 'returns error message' do expect(json["errors"]).to eq([{"status"=>"403", "title"=>"You are not authorized to access this resource."}]) end - + it 'returns status code 403' do expect(response).to have_http_status(403) end end - + context "no authentication" do - let(:doi) { create(:doi) } + let(:doi) { create(:doi) } before { get "/dois/#{doi.doi}", headers: { "HTTP_ACCEPT" => "application/vnd.jats+xml" } } - + it 'returns error message' do expect(json["errors"]).to eq([{"status"=>"401", "title"=>"Bad credentials."}]) end - + it 'returns status code 401' do expect(response).to have_http_status(401) end end - + context "application/vnd.jats+xml" do before { get "/dois/#{doi.doi}", headers: { "HTTP_ACCEPT" => "application/vnd.jats+xml", 'Authorization' => 'Bearer ' + bearer } } - + it 'returns the Doi' do jats = Maremma.from_xml(response.body).fetch("element_citation", {}) expect(jats.dig("publication_type")).to eq("data") expect(jats.dig("data_title")).to eq("Data from: A new malaria agent in African hominids.") end - + it 'returns status code 200' do expect(response).to have_http_status(200) end end - + context "application/vnd.jats+xml link" do before { get "/dois/application/vnd.jats+xml/#{doi.doi}" } - + it 'returns the Doi' do jats = Maremma.from_xml(response.body).fetch("element_citation", {}) expect(jats.dig("publication_type")).to eq("data") expect(jats.dig("data_title")).to eq("Data from: A new malaria agent in African hominids.") end - + it 'returns status code 200' do expect(response).to have_http_status(200) end end - + context "application/vnd.datacite.datacite+xml" do before { get "/dois/#{doi.doi}", headers: { "HTTP_ACCEPT" => "application/vnd.datacite.datacite+xml", 'Authorization' => 'Bearer ' + bearer } } - + it 'returns the Doi' do data = Maremma.from_xml(response.body).to_h.fetch("resource", {}) expect(data.dig("xmlns")).to eq("http://datacite.org/schema/kernel-4") expect(data.dig("publisher")).to eq("Dryad Digital Repository") expect(data.dig("titles", "title")).to eq("Data from: A new malaria agent in African hominids.") end - + it 'returns status code 200' do expect(response).to have_http_status(200) end end - + context "application/vnd.datacite.datacite+xml link" do before { get "/dois/application/vnd.datacite.datacite+xml/#{doi.doi}" } - + it 'returns the Doi' do data = Maremma.from_xml(response.body).to_h.fetch("resource", {}) expect(data.dig("xmlns")).to eq("http://datacite.org/schema/kernel-4") expect(data.dig("publisher")).to eq("Dryad Digital Repository") expect(data.dig("titles", "title")).to eq("Data from: A new malaria agent in African hominids.") end - + it 'returns status code 200' do expect(response).to have_http_status(200) end end - + context "application/vnd.datacite.datacite+xml schema 3" do let(:xml) { file_fixture('datacite_schema_3.xml').read } let(:doi) { create(:doi, xml: xml, client: client, regenerate: false) } - + before { get "/dois/#{doi.doi}", headers: { "HTTP_ACCEPT" => "application/vnd.datacite.datacite+xml", 'Authorization' => 'Bearer ' + bearer } } - + it 'returns the Doi' do data = Maremma.from_xml(response.body).to_h.fetch("resource", {}) expect(data.dig("xmlns")).to eq("http://datacite.org/schema/kernel-3") expect(data.dig("publisher")).to eq("Dryad Digital Repository") expect(data.dig("titles", "title")).to eq("Data from: A new malaria agent in African hominids.") end - + it 'returns status code 200' do expect(response).to have_http_status(200) end end - + # context "no metadata" do # let(:doi) { create(:doi, xml: nil, client: client) } - + # before { get "/#{doi.doi}", headers: { "HTTP_ACCEPT" => "application/vnd.datacite.datacite+xml", 'Authorization' => 'Bearer ' + bearer } } - + # it 'returns the Doi' do # expect(response.body).to eq('') # end - + # it 'returns status code 200' do # expect(response).to have_http_status(200) # end # end - + context "application/vnd.datacite.datacite+xml not found" do before { get "/dois/xxx", headers: { "HTTP_ACCEPT" => "application/vnd.datacite.datacite+xml", 'Authorization' => 'Bearer ' + bearer } } - + it 'returns error message' do expect(json["errors"]).to eq([{"status"=>"404", "title"=>"The resource you are looking for doesn't exist."}]) end - + it 'returns status code 404' do expect(response).to have_http_status(404) end end - + context "application/vnd.datacite.datacite+json" do before { get "/dois/#{doi.doi}", headers: { "HTTP_ACCEPT" => "application/vnd.datacite.datacite+json", 'Authorization' => 'Bearer ' + bearer } } - + it 'returns the Doi' do expect(json["doi"]).to eq(doi.doi) end - + it 'returns status code 200' do expect(response).to have_http_status(200) end end - + context "application/vnd.datacite.datacite+json link" do before { get "/dois/application/vnd.datacite.datacite+json/#{doi.doi}" } - + it 'returns the Doi' do expect(json["doi"]).to eq(doi.doi) end - + it 'returns status code 200' do expect(response).to have_http_status(200) end end - + context "application/vnd.crosscite.crosscite+json" do before { get "/dois/#{doi.doi}", headers: { "HTTP_ACCEPT" => "application/vnd.crosscite.crosscite+json", 'Authorization' => 'Bearer ' + bearer } } - + it 'returns the Doi' do expect(json["doi"]).to eq(doi.doi) end - + it 'returns status code 200' do expect(response).to have_http_status(200) end end - + context "application/vnd.crosscite.crosscite+json link" do before { get "/dois/application/vnd.crosscite.crosscite+json/#{doi.doi}" } - + it 'returns the Doi' do expect(json["doi"]).to eq(doi.doi) end - + it 'returns status code 200' do expect(response).to have_http_status(200) end end - + context "application/vnd.schemaorg.ld+json" do before { get "/dois/#{doi.doi}", headers: { "HTTP_ACCEPT" => "application/vnd.schemaorg.ld+json", 'Authorization' => 'Bearer ' + bearer } } - + it 'returns the Doi' do expect(json["@type"]).to eq("Dataset") end - + it 'returns status code 200' do expect(response).to have_http_status(200) end end - + context "application/vnd.schemaorg.ld+json link" do before { get "/dois/application/vnd.schemaorg.ld+json/#{doi.doi}" } - + it 'returns the Doi' do expect(json["@type"]).to eq("Dataset") end - + it 'returns status code 200' do expect(response).to have_http_status(200) end end - + context "application/vnd.citationstyles.csl+json" do before { get "/dois/#{doi.doi}", headers: { "HTTP_ACCEPT" => "application/vnd.citationstyles.csl+json", 'Authorization' => 'Bearer ' + bearer } } - + it 'returns the Doi' do expect(json["type"]).to eq("dataset") end - + it 'returns status code 200' do expect(response).to have_http_status(200) end end - + context "application/vnd.citationstyles.csl+json link" do before { get "/dois/application/vnd.citationstyles.csl+json/#{doi.doi}" } - + it 'returns the Doi' do expect(json["type"]).to eq("dataset") end - + it 'returns status code 200' do expect(response).to have_http_status(200) end end - + context "application/x-research-info-systems" do before { get "/dois/#{doi.doi}", headers: { "HTTP_ACCEPT" => "application/x-research-info-systems", 'Authorization' => 'Bearer ' + bearer } } - + it 'returns the Doi' do expect(response.body).to start_with("TY - DATA") end - + it 'returns status code 200' do expect(response).to have_http_status(200) end end - + context "application/x-research-info-systems link" do before { get "/dois/application/x-research-info-systems/#{doi.doi}" } - + it 'returns the Doi' do expect(response.body).to start_with("TY - DATA") end - + it 'returns status code 200' do expect(response).to have_http_status(200) end end - + context "application/x-bibtex" do before { get "/dois/#{doi.doi}", headers: { "HTTP_ACCEPT" => "application/x-bibtex", 'Authorization' => 'Bearer ' + bearer } } - + it 'returns the Doi' do expect(response.body).to start_with("@misc{https://doi.org/#{doi.doi.downcase}") end - + it 'returns status code 200' do expect(response).to have_http_status(200) end end - + context "application/x-bibtex link" do before { get "/dois/application/x-bibtex/#{doi.doi}" } - + it 'returns the Doi' do expect(response.body).to start_with("@misc{https://doi.org/#{doi.doi.downcase}") end - + it 'returns status code 200' do expect(response).to have_http_status(200) end @@ -2930,112 +2949,112 @@ context "text/csv" do before { get "/dois/#{doi.doi}", headers: { "HTTP_ACCEPT" => "text/csv", 'Authorization' => 'Bearer ' + bearer } } - + it 'returns the Doi' do expect(response.body).to include(doi.doi) end - + it 'returns status code 200' do expect(response).to have_http_status(200) end end - + context "text/csv link" do before { get "/dois/text/csv/#{doi.doi}" } - + it 'returns the Doi' do expect(response.body).to include(doi.doi) end - + it 'returns status code 200' do expect(response).to have_http_status(200) end end - + context "text/x-bibliography", vcr: true do context "default style" do before { get "/dois/#{doi.doi}", headers: { "HTTP_ACCEPT" => "text/x-bibliography", 'Authorization' => 'Bearer ' + bearer } } - + it 'returns the Doi' do expect(response.body).to start_with("Ollomo, B.") end - + it 'returns status code 200' do expect(response).to have_http_status(200) end end - + context "default style link" do before { get "/dois/text/x-bibliography/#{doi.doi}" } - + it 'returns the Doi' do expect(response.body).to start_with("Ollomo, B.") end - + it 'returns status code 200' do expect(response).to have_http_status(200) end end - + context "ieee style" do before { get "/dois/#{doi.doi}?style=ieee", headers: { "HTTP_ACCEPT" => "text/x-bibliography", 'Authorization' => 'Bearer ' + bearer } } - + it 'returns the Doi' do expect(response.body).to start_with("B. Ollomo") end - + it 'returns status code 200' do expect(response).to have_http_status(200) end end - - context "ieee style link" do + + context "ieee style link" do before { get "/dois/text/x-bibliography/#{doi.doi}?style=ieee" } - + it 'returns the Doi' do expect(response.body).to start_with("B. Ollomo") end - + it 'returns status code 200' do expect(response).to have_http_status(200) end end - + context "style and locale" do before { get "/dois/#{doi.doi}?style=vancouver&locale=de", headers: { "HTTP_ACCEPT" => "text/x-bibliography", 'Authorization' => 'Bearer ' + bearer } } - + it 'returns the Doi' do expect(response.body).to start_with("Ollomo B") end - + it 'returns status code 200' do expect(response).to have_http_status(200) end end end - + context "unknown content type" do before { get "/dois/#{doi.doi}", headers: { "HTTP_ACCEPT" => "application/vnd.ms-excel", 'Authorization' => 'Bearer ' + bearer } } - + it 'returns the Doi' do expect(json["errors"]).to eq([{"status"=>"406", "title"=>"The content type is not recognized."}]) end - + it 'returns status code 406' do expect(response).to have_http_status(406) end end - + context "missing content type" do before { get "/dois/#{doi.doi}", headers: { 'Authorization' => 'Bearer ' + bearer } } - + it 'returns the Doi' do expect(json.dig('data', 'attributes', 'doi')).to eq(doi.doi.downcase) end - + it 'returns status code 200' do expect(response).to have_http_status(200) end end - end + end end