diff --git a/app/controllers/works_controller.rb b/app/controllers/works_controller.rb index 32a227517..f27444a20 100644 --- a/app/controllers/works_controller.rb +++ b/app/controllers/works_controller.rb @@ -119,11 +119,9 @@ def show protected def set_doi - @doi = Doi.where(doi: params[:id]).first + response = Doi.find_by_id(params[:id]) + @doi = response.records.first fail ActiveRecord::RecordNotFound unless @doi.present? - - # capture username and password for reuse in the handle system - @doi.current_user = current_user end def set_include diff --git a/spec/fixtures/vcr_cassettes/dois/GET_/dois/DOI/get-url/it_works/returns_url.yml b/spec/fixtures/vcr_cassettes/dois/GET_/dois/DOI/get-url/it_works/returns_url.yml new file mode 100644 index 000000000..74814b1ea --- /dev/null +++ b/spec/fixtures/vcr_cassettes/dois/GET_/dois/DOI/get-url/it_works/returns_url.yml @@ -0,0 +1,32 @@ +--- +http_interactions: +- request: + method: get + uri: https://handle.test.datacite.org/api/handles/10.5438/FJ3W-0SHD?index=1 + body: + encoding: US-ASCII + string: '' + headers: + User-Agent: + - Mozilla/5.0 (compatible; Maremma/4.2.6; mailto:info@datacite.org) + Accept: + - text/html,application/json,application/xml;q=0.9, text/plain;q=0.8,image/png,*/*;q=0.5 + response: + status: + code: 200 + message: OK + headers: + Date: + - Fri, 12 Jul 2019 07:29:01 GMT + Content-Type: + - application/json;charset=UTF-8 + Connection: + - keep-alive + Vary: + - Accept-Encoding + body: + encoding: ASCII-8BIT + string: '{"responseCode":1,"handle":"10.5438/FJ3W-0SHD","values":[{"index":1,"type":"URL","data":{"format":"string","value":"https://blog.datacite.org/data-driven-development/"},"ttl":86400,"timestamp":"2016-12-19T15:06:36Z"}]}' + http_version: + recorded_at: Fri, 12 Jul 2019 07:29:01 GMT +recorded_with: VCR 3.0.3 diff --git a/spec/fixtures/vcr_cassettes/dois/GET_/dois/DOI/get-url/not_found/returns_not_found.yml b/spec/fixtures/vcr_cassettes/dois/GET_/dois/DOI/get-url/not_found/returns_not_found.yml new file mode 100644 index 000000000..d4f718d4e --- /dev/null +++ b/spec/fixtures/vcr_cassettes/dois/GET_/dois/DOI/get-url/not_found/returns_not_found.yml @@ -0,0 +1,32 @@ +--- +http_interactions: +- request: + method: get + uri: https://handle.test.datacite.org/api/handles/10.14454/61Y1-E521?index=1 + body: + encoding: US-ASCII + string: '' + headers: + User-Agent: + - Mozilla/5.0 (compatible; Maremma/4.2.6; mailto:info@datacite.org) + Accept: + - text/html,application/json,application/xml;q=0.9, text/plain;q=0.8,image/png,*/*;q=0.5 + response: + status: + code: 404 + message: Not Found + headers: + Date: + - Fri, 12 Jul 2019 07:29:09 GMT + Content-Type: + - application/json;charset=UTF-8 + Content-Length: + - '50' + Connection: + - keep-alive + body: + encoding: ASCII-8BIT + string: '{"responseCode":100,"handle":"10.14454/61Y1-E521"}' + http_version: + recorded_at: Fri, 12 Jul 2019 07:29:09 GMT +recorded_with: VCR 3.0.3 diff --git a/spec/requests/dois_spec.rb b/spec/requests/dois_spec.rb index 706d29602..64cd8bbae 100644 --- a/spec/requests/dois_spec.rb +++ b/spec/requests/dois_spec.rb @@ -31,7 +31,14 @@ end end - describe 'GET /dois/:id' do + describe 'GET /dois/:id', elasticsearch: true do + let!(:doi) { create(:doi, client: client) } + + before do + Doi.import + sleep 1 + end + context 'when the record exists' do it 'returns the Doi' do get "/dois/#{doi.doi}", nil, headers @@ -94,7 +101,14 @@ let(:bearer) { User.generate_token(role_id: "client_admin", client_id: client.symbol.downcase) } let(:headers) { {'HTTP_ACCEPT'=>'application/vnd.api+json', 'HTTP_AUTHORIZATION' => 'Bearer ' + bearer}} - context 'initial state draft' do + context 'initial state draft', elasticsearch: true do + let!(:doi) { create(:doi, client: client) } + + before do + Doi.import + sleep 1 + end + it 'fetches the record' do get "/dois/#{doi.doi}", nil, headers @@ -2175,10 +2189,10 @@ end end - context 'remove series_information via xml' do + context 'remove series_information via xml', elasticsearch: true do let(:xml) { Base64.strict_encode64(File.read(file_fixture('datacite_series_information.xml'))) } let(:xml_new) { Base64.strict_encode64(File.read(file_fixture('datacite_no_series_information.xml'))) } - let(:doi) { create(:doi, client: client, doi: "10.14454/05mb-q396", event: "publish") } + let!(:doi) { create(:doi, client: client, doi: "10.14454/05mb-q396", event: "publish") } let(:update_attributes) do { "data" => { @@ -2200,6 +2214,11 @@ } end + before do + Doi.import + sleep 1 + end + it 'updates the Doi' do get "/dois/#{doi.doi}", nil, headers @@ -2398,7 +2417,7 @@ end end - describe 'GET /dois/ linkcheck results' do + describe 'GET /dois/ linkcheck results', elasticsearch: true do let(:landing_page) { { "checked" => Time.zone.now.utc.iso8601, "status" => 200, @@ -2416,7 +2435,7 @@ } } # Setup an initial DOI with results will check permissions against. - let(:doi) { create(:doi, doi: "10.24425/2210181332", + let!(:doi) { create(:doi, doi: "10.24425/2210181332", client: client, state: "findable", event: 'publish', @@ -2431,6 +2450,11 @@ event: 'publish', landing_page: landing_page) } + before do + Doi.import + sleep 1 + end + context 'anonymous get' do let(:headers) { { 'HTTP_ACCEPT'=>'application/vnd.api+json' } } @@ -2501,82 +2525,114 @@ end end - describe 'GET /dois/DOI/get-url', vcr: true do - let(:doi) { create(:doi, client: client, doi: "10.5438/fj3w-0shd", url: "https://blog.datacite.org/data-driven-development/", event: "publish") } + describe 'GET /dois/DOI/get-url', vcr: true, elasticsearch: true do + context 'it works' do + let!(:doi) { create(:doi, client: client, doi: "10.5438/fj3w-0shd", url: "https://blog.datacite.org/data-driven-development/", event: "publish") } - it 'returns url' do - get "/dois/#{doi.doi}/get-url", nil, headers + before do + Doi.import + sleep 1 + end - expect(json["url"]).to eq("https://blog.datacite.org/data-driven-development/") - expect(last_response.status).to eq(200) + it 'returns url' do + get "/dois/#{doi.doi}/get-url", nil, headers + + expect(json["url"]).to eq("https://blog.datacite.org/data-driven-development/") + expect(last_response.status).to eq(200) + end end - end - describe 'GET /dois/DOI/get-url no password', vcr: true do - let(:doi) { create(:doi, client: client, doi: "10.14454/05mb-q396", event: "publish") } + context 'no password' do + let!(:doi) { create(:doi, client: client, doi: "10.14454/05mb-q396", event: "publish") } - it 'returns error' do - get "/dois/#{doi.doi}/get-url", nil, { 'HTTP_ACCEPT'=>'application/vnd.api+json' } + before do + Doi.import + sleep 1 + end - expect(last_response.status).to eq(401) - expect(json['errors']).to eq([{"status"=>"401", "title"=>"Bad credentials."}]) + it 'returns error' do + get "/dois/#{doi.doi}/get-url", nil, { 'HTTP_ACCEPT'=>'application/vnd.api+json' } + + expect(last_response.status).to eq(401) + expect(json['errors']).to eq([{"status"=>"401", "title"=>"Bad credentials."}]) + end end - end - describe 'GET /dois/DOI/get-url wrong password', vcr: true do - let(:doi) { create(:doi, client: client, doi: "10.14454/05mb-q396", event: "publish") } - let(:credentials) { client.encode_auth_param(username: client.symbol.downcase, password: "12345") } + context 'wrong password' do + let!(:doi) { create(:doi, client: client, doi: "10.14454/05mb-q396", event: "publish") } + let(:credentials) { client.encode_auth_param(username: client.symbol.downcase, password: "12345") } - it 'returns error' do - get "/dois/#{doi.doi}/get-url", nil, { 'HTTP_ACCEPT'=>'application/vnd.api+json', 'HTTP_AUTHORIZATION' => 'Basic ' + credentials } + before do + Doi.import + sleep 1 + end - expect(last_response.status).to eq(401) - expect(json['errors']).to eq([{"status"=>"401", "title"=>"Bad credentials."}]) + it 'returns error' do + get "/dois/#{doi.doi}/get-url", nil, { 'HTTP_ACCEPT'=>'application/vnd.api+json', 'HTTP_AUTHORIZATION' => 'Basic ' + credentials } + + expect(last_response.status).to eq(401) + expect(json['errors']).to eq([{"status"=>"401", "title"=>"Bad credentials."}]) + end end - end - describe 'GET /dois/DOI/get-url no permission', vcr: true do - let(:other_client) { create(:client, provider: provider) } - let(:doi) { create(:doi, client: other_client, doi: "10.14454/8syz-ym47", event: "publish") } + context 'no permission' do + let(:other_client) { create(:client, provider: provider) } + let!(:doi) { create(:doi, client: other_client, doi: "10.14454/8syz-ym47", event: "publish") } + + before do + Doi.import + sleep 1 + end - it 'returns error' do - get "/dois/#{doi.doi}/get-url", nil, headers + it 'returns error' do + get "/dois/#{doi.doi}/get-url", nil, headers - expect(last_response.status).to eq(403) - expect(json['errors']).to eq([{"status"=>"403", "title"=>"You are not authorized to access this resource."}]) + expect(last_response.status).to eq(403) + expect(json['errors']).to eq([{"status"=>"403", "title"=>"You are not authorized to access this resource."}]) + end end - end - describe 'GET /dois/DOI/get-url not found', vcr: true do - let(:doi) { create(:doi, client: client, doi: "10.14454/61y1-e521", event: "publish") } + context 'not found' do + let!(:doi) { create(:doi, client: client, doi: "10.14454/61y1-e521", event: "publish") } - it 'returns not found' do - get "/dois/#{doi.doi}/get-url", nil, headers + before do + Doi.import + sleep 1 + end - expect(last_response.status).to eq(404) - expect(json['errors']).to eq([{"status"=>404, "title"=>"Not found"}]) + it 'returns not found' do + get "/dois/#{doi.doi}/get-url", nil, headers + + expect(last_response.status).to eq(404) + expect(json['errors']).to eq([{"status"=>404, "title"=>"Not found"}]) + end end - end - describe 'GET /dois/DOI/get-url draft doi', vcr: true do - let(:doi) { create(:doi, client: client, doi: "10.14454/61y1-e521") } + context 'draft doi' do + let!(:doi) { create(:doi, client: client, doi: "10.14454/61y1-e521") } + + before do + Doi.import + sleep 1 + end - it 'returns not found' do - get "/dois/#{doi.doi}/get-url", nil, headers + it 'returns not found' do + get "/dois/#{doi.doi}/get-url", nil, headers - expect(last_response.status).to eq(200) - expect(json['url']).to eq(doi.url) + expect(last_response.status).to eq(200) + expect(json['url']).to eq(doi.url) + end end - end - describe 'GET /dois/DOI/get-url not DataCite DOI', vcr: true do - let(:doi) { create(:doi, client: client, doi: "10.1371/journal.pbio.2001414", event: "publish") } + context 'not DataCite DOI' do + let(:doi) { create(:doi, client: client, doi: "10.1371/journal.pbio.2001414", event: "publish") } - it 'returns nil' do - get "/dois/#{doi.doi}/get-url", nil, headers + it 'returns nil' do + get "/dois/#{doi.doi}/get-url", nil, headers - expect(last_response.status).to eq(403) - expect(json['url']).to be_nil + expect(last_response.status).to eq(404) + expect(json['url']).to be_nil + end end end @@ -2602,11 +2658,16 @@ end end - describe "content_negotation", type: :request do + describe "content_negotation", type: :request, elasticsearch: true do let(:provider) { create(:provider, symbol: "DATACITE") } 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") } + let!(:doi) { create(:doi, client: client, aasm_state: "findable") } + + before do + Doi.import + sleep 1 + end context "no permission" do let(:doi) { create(:doi) } diff --git a/spec/requests/works_spec.rb b/spec/requests/works_spec.rb index c15b33f00..9abec94e9 100644 --- a/spec/requests/works_spec.rb +++ b/spec/requests/works_spec.rb @@ -9,12 +9,12 @@ 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!(:dois) { create_list(:doi, 3, client: client, event: "publish") } - let(:doi) { create(:doi, client: client, event: "publish") } 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) { { 'HTTP_ACCEPT'=>'application/vnd.api+json', 'HTTP_AUTHORIZATION' => 'Bearer ' + bearer }} describe 'GET /works', elasticsearch: true do + let!(:dois) { create_list(:doi, 3, client: client, event: "publish") } + before do Doi.import sleep 1 @@ -29,7 +29,14 @@ end end - describe 'GET /works/:id' do + describe 'GET /works/:id', elasticsearch: true do + let!(:doi) { create(:doi, client: client, event: "publish") } + + before do + Doi.import + sleep 1 + end + context 'when the record exists' do it 'returns the Doi' do get "/works/#{doi.doi}", nil, headers