diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index de1b82025..7fa5bb300 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/app/controllers/provider_prefixes_controller.rb b/app/controllers/provider_prefixes_controller.rb index c8f4d6026..4ede5d688 100644 --- a/app/controllers/provider_prefixes_controller.rb +++ b/app/controllers/provider_prefixes_controller.rb @@ -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 diff --git a/spec/requests/provider_prefixes_spec.rb b/spec/requests/provider_prefixes_spec.rb index c2cb62546..d9593976c 100644 --- a/spec/requests/provider_prefixes_spec.rb +++ b/spec/requests/provider_prefixes_spec.rb @@ -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