Skip to content

Commit

Permalink
show all prefixes by consortium. datacite/datacite#964
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Fenner committed Feb 28, 2020
1 parent fdad144 commit 3468ef7
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 6 deletions.
11 changes: 6 additions & 5 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ class ApplicationController < ActionController::API
include Authenticable
include CanCan::ControllerAdditions
include ErrorSerializable
require 'facets/string/snakecase'

require "facets/string/snakecase"

# include helper module for caching infrequently changing resources
include Cacheable
Expand All @@ -30,7 +31,7 @@ class ApplicationController < ActionController::API
def set_jsonp_format
if params[:callback] && request.get?
self.response_body = "#{params[:callback]}(#{response.body})"
headers["Content-Type"] = 'application/javascript'
headers["Content-Type"] = "application/javascript"
end
end

Expand All @@ -43,9 +44,9 @@ def detect_crawler

def set_consumer_header
if current_user
response.headers['X-Credential-Username'] = current_user.uid
response.headers["X-Credential-Username"] = current_user.uid
else
response.headers['X-Anonymous-Consumer'] = true
response.headers["X-Anonymous-Consumer"] = true
end
end

Expand All @@ -64,7 +65,7 @@ def authenticate_user_with_basic_auth!
@user = authenticate_user!

if !@user
request_http_basic_authentication(realm = ENV['REALM'])
request_http_basic_authentication(realm = ENV["REALM"])
end

@user
Expand Down
3 changes: 3 additions & 0 deletions app/controllers/provider_prefixes_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ def index
collection = ProviderPrefix.where(id: params[:id])
elsif params[:provider_id].present? && params[:prefix_id].present?
collection = ProviderPrefix.joins(:provider, :prefix).where('allocator.symbol = ?', params[:provider_id]).where('prefix.prefix = ?', params[:prefix_id])
elsif params[:consortium_id].present?
providers = Provider.where('allocator.consortium_id = ?', params[:consortium_id])
collection = providers.present? ? ProviderPrefix.joins(:provider, :prefix).where('allocator.consortium_id = ?', params[:consortium_id]) : ProviderPrefix.none
elsif params[:provider_id].present?
provider = Provider.where('allocator.symbol = ?', params[:provider_id]).first
collection = provider.present? ? provider.provider_prefixes.joins(:prefix) : ProviderPrefix.none
Expand Down
23 changes: 22 additions & 1 deletion spec/requests/provider_prefixes_spec.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,32 @@
require 'rails_helper'

describe "Provider Prefixes", type: :request do
let!(:provider_prefixes) { create_list(:provider_prefix, 5) }
let(:consortium) { create(:provider, role_name: "ROLE_CONSORTIUM") }
let(:provider) { create(:provider, consortium: consortium, role_name: "ROLE_CONSORTIUM_ORGANIZATION", password_input: "12345") }
let!(:provider_prefixes) { create_list(:provider_prefix, 3, provider: provider) }
let!(:provider_prefixes2) { create_list(:provider_prefix, 2) }
let(:provider_prefix) { create(:provider_prefix) }
let(:bearer) { User.generate_token(role_id: "staff_admin") }
let(:headers) { {'HTTP_ACCEPT'=>'application/vnd.api+json', 'HTTP_AUTHORIZATION' => 'Bearer ' + bearer }}

describe "GET /provider-prefixes by consortium" do
it "returns provider-prefixes" do
get "/provider-prefixes?consortium-id=#{consortium.symbol.downcase}", nil, headers
puts last_response.body
expect(last_response.status).to eq(200)
expect(json["data"].size).to eq(3)
end
end

describe "GET /provider-prefixes by provider" do
it "returns provider-prefixes" do
get "/provider-prefixes?provider-id=#{provider.symbol.downcase}", nil, headers

expect(last_response.status).to eq(200)
expect(json["data"].size).to eq(3)
end
end

describe 'GET /provider-prefixes' do
it 'returns provider-prefixes' do
get '/provider-prefixes', nil, headers
Expand Down

0 comments on commit 3468ef7

Please sign in to comment.