Skip to content

Commit

Permalink
Restrict anonymous role_id user to findable DOIs at /dois endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
codycooperross committed May 9, 2023
1 parent eadb526 commit 504deb8
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 4 deletions.
4 changes: 2 additions & 2 deletions app/controllers/datacite_dois_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ def index
"types.resourceTypeGeneral"
end

# only show findable DOIs to anonymous users and role user
if current_user.nil? || current_user.role_id == "user"
# only show findable DOIs to no user, role user, and role anonymous
if current_user.nil? || current_user.role_id == "user" || current_user.role_id == "anonymous"
params[:state] = "findable"
end

Expand Down
64 changes: 62 additions & 2 deletions spec/requests/datacite_dois_spec.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
# frozen_string_literal: true

require "rails_helper"
include Passwordable

describe DataciteDoisController, type: :request, vcr: true do
let(:admin) { create(:provider, symbol: "ADMIN") }
let(:admin_bearer) { Client.generate_token(role_id: "staff_admin", uid: admin.symbol, password: admin.password) }
let(:admin_headers) { { "HTTP_ACCEPT" => "application/vnd.api+json", "HTTP_AUTHORIZATION" => "Bearer " + admin_bearer } }

let(:provider) { create(:provider, symbol: "DATACITE") }
let(:client) { create(:client, provider: provider, symbol: ENV["MDS_USERNAME"], password: ENV["MDS_PASSWORD"], re3data_id: "10.17616/r3xs37") }
let(:provider) { create(:provider, symbol: "DATACITE", password: encrypt_password_sha256(ENV["MDS_PASSWORD"])) }
let(:client) { create(:client, provider: provider, symbol: ENV["MDS_USERNAME"], password: encrypt_password_sha256(ENV["MDS_PASSWORD"]), re3data_id: "10.17616/r3xs37") }
let!(:prefix) { create(:prefix, uid: "10.14454") }
let!(:client_prefix) { create(:client_prefix, client: client, prefix: prefix) }

Expand Down Expand Up @@ -437,6 +438,65 @@
end
end

describe "GET /dois with authorization headers", elasticsearch: true do
let!(:dois) { create_list(:doi, 10, client: client, aasm_state: "findable") }
let!(:doi_draft) { create(:doi, client: client, aasm_state: "draft") }
let!(:doi_registered) { create(:doi, client: client, aasm_state: "registered") }
let(:anonymous_basic_auth_headers) { { "HTTP_ACCEPT" => "application/vnd.api+json", "HTTP_AUTHORIZATION" => ActionController::HttpAuthentication::Basic.encode_credentials(client.symbol, "") } }
let(:client_basic_auth_headers) { { "HTTP_ACCEPT" => "application/vnd.api+json", "HTTP_AUTHORIZATION" => ActionController::HttpAuthentication::Basic.encode_credentials(client.symbol, ENV["MDS_PASSWORD"]) } }
let(:provider_basic_auth_headers) { { "HTTP_ACCEPT" => "application/vnd.api+json", "HTTP_AUTHORIZATION" => ActionController::HttpAuthentication::Basic.encode_credentials(provider.symbol, ENV["MDS_PASSWORD"]) } }

before do
DataciteDoi.import
sleep 2
end

it "return only findable dois with no authorization" do
get "/dois"

expect(json.dig("meta", "total")).to eq(10)
expect(json.dig("meta", "states", 0, "count")).to eq(10)
expect(json.dig("meta", "states", 1)).to eq(nil)
expect(json.dig("meta", "states", 2)).to eq(nil)
end

it "return only findable dois with anonymous user" do
get "/dois", nil, anonymous_basic_auth_headers

expect(json.dig("meta", "total")).to eq(10)
expect(json.dig("meta", "states", 0, "count")).to eq(10)
expect(json.dig("meta", "states", 1)).to eq(nil)
expect(json.dig("meta", "states", 2)).to eq(nil)
end

it "return dois in all states with authenticated client user" do
get "/dois", nil, client_basic_auth_headers

expect(json.dig("meta", "total")).to eq(12)
expect(json.dig("meta", "states", 0, "count")).to eq(10)
expect(json.dig("meta", "states", 1, "count")).to eq(1)
expect(json.dig("meta", "states", 2, "count")).to eq(1)
end

it "return dois in all states with authenticated provider user" do
get "/dois", nil, provider_basic_auth_headers

expect(json.dig("meta", "total")).to eq(12)
expect(json.dig("meta", "states", 0, "count")).to eq(10)
expect(json.dig("meta", "states", 1, "count")).to eq(1)
expect(json.dig("meta", "states", 2, "count")).to eq(1)
end

it "return dois in all states with authenticated admin user" do
get "/dois", nil, admin_headers

expect(json.dig("meta", "total")).to eq(12)
expect(json.dig("meta", "states", 0, "count")).to eq(10)
expect(json.dig("meta", "states", 1, "count")).to eq(1)
expect(json.dig("meta", "states", 2, "count")).to eq(1)
end
end

describe "GET /dois/:id", elasticsearch: true do
let!(:doi) { create(:doi, client: client) }

Expand Down

0 comments on commit 504deb8

Please sign in to comment.