Skip to content

Commit

Permalink
use elasticsearch for fetching single doi
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Fenner committed Jul 12, 2019
1 parent 268a418 commit e0d94ee
Show file tree
Hide file tree
Showing 5 changed files with 195 additions and 65 deletions.
6 changes: 2 additions & 4 deletions app/controllers/works_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

177 changes: 119 additions & 58 deletions spec/requests/dois_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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" => {
Expand All @@ -2200,6 +2214,11 @@
}
end

before do
Doi.import
sleep 1
end

it 'updates the Doi' do
get "/dois/#{doi.doi}", nil, headers

Expand Down Expand Up @@ -2398,7 +2417,7 @@
end
end

describe 'GET /dois/<doi> linkcheck results' do
describe 'GET /dois/<doi> linkcheck results', elasticsearch: true do
let(:landing_page) { {
"checked" => Time.zone.now.utc.iso8601,
"status" => 200,
Expand All @@ -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',
Expand All @@ -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' } }

Expand Down Expand Up @@ -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

Expand All @@ -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) }
Expand Down
13 changes: 10 additions & 3 deletions spec/requests/works_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit e0d94ee

Please sign in to comment.