diff --git a/app/controllers/repp/v1/base_controller.rb b/app/controllers/repp/v1/base_controller.rb index b3aa786d1..c46c837f6 100644 --- a/app/controllers/repp/v1/base_controller.rb +++ b/app/controllers/repp/v1/base_controller.rb @@ -76,7 +76,8 @@ module Repp def basic_token pattern = /^Basic / header = request.headers['Authorization'] - header.gsub(pattern, '') if header&.match(pattern) + header = header.gsub(pattern, '') if header&.match(pattern) + header.strip end def authenticate_user @@ -85,9 +86,12 @@ module Repp return if @current_user - render(json: { errors: [{ base: ['Not authorized'] }] }, status: :unauthorized) - rescue NoMethodError - render(json: { errors: [{ base: ['Not authorized'] }] }, status: :unauthorized) + raise(ArgumentError) + rescue NoMethodError, ArgumentError + render( + json: { code: 2202, message: 'Invalid authorization information' }, + status: :unauthorized + ) end def check_ip_restriction diff --git a/test/integration/repp/auctions_test.rb b/test/integration/repp/v1/auctions_test.rb similarity index 100% rename from test/integration/repp/auctions_test.rb rename to test/integration/repp/v1/auctions_test.rb diff --git a/test/integration/repp/v1/base_test.rb b/test/integration/repp/v1/base_test.rb new file mode 100644 index 000000000..931ad094c --- /dev/null +++ b/test/integration/repp/v1/base_test.rb @@ -0,0 +1,26 @@ +require 'test_helper' + +class ReppV1BaseTest < ActionDispatch::IntegrationTest + def setup + @registrant = users(:api_bestnames) + token = Base64.encode64("#{@registrant.username}:#{@registrant.plain_text_password}") + token = "Basic #{token}" + + @auth_headers = { 'Authorization' => token } + end + + def test_unauthorized_user_has_no_access + get repp_v1_contacts_path + response_json = JSON.parse(response.body, symbolize_names: true) + + assert_response :unauthorized + assert_equal 'Invalid authorization information', response_json[:message] + end + + def test_authenticates_valid_user + get repp_v1_contacts_path, headers: @auth_headers + response_json = JSON.parse(response.body, symbolize_names: true) + + assert_response :ok + end +end diff --git a/test/integration/repp/v1/contacts_test.rb b/test/integration/repp/v1/contacts_test.rb new file mode 100644 index 000000000..92c254dc3 --- /dev/null +++ b/test/integration/repp/v1/contacts_test.rb @@ -0,0 +1,24 @@ +require 'test_helper' + +class ReppV1ContactsTest < ActionDispatch::IntegrationTest + def setup + @auction = auctions(:one) + @auction.update!(uuid: '1b3ee442-e8fe-4922-9492-8fcb9dccc69c', + domain: 'auction.test', + status: Auction.statuses[:started]) + end + + def test_get_index + get repp_v1_contacts_path + response_json = JSON.parse(response.body, symbolize_names: true) + + puts response_json + + assert response_json[:count] == 1 + + expected_response = [{ domain_name: @auction.domain, + punycode_domain_name: @auction.domain }] + + assert_equal expected_response, response_json[:auctions] + end +end diff --git a/test/integration/repp/retained_domains_test.rb b/test/integration/repp/v1/retained_domains_test.rb similarity index 100% rename from test/integration/repp/retained_domains_test.rb rename to test/integration/repp/v1/retained_domains_test.rb