Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return JSON response from 404 for Auction API #1608

Merged
merged 1 commit into from
Jun 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions app/controllers/api/v1/base_controller.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
module Api
module V1
class BaseController < ActionController::API
rescue_from ActiveRecord::RecordNotFound, with: :not_found_error

private

def authenticate
ip_allowed = allowed_ips.include?(request.remote_ip)
head :unauthorized unless ip_allowed
end

def not_found_error
uuid = params['uuid']
json = { error: 'Not Found', uuid: uuid, message: 'Record not found' }
render json: json, status: :not_found
end

def allowed_ips
ENV['auction_api_allowed_ips'].split(',').map(&:strip)
end
Expand Down
11 changes: 7 additions & 4 deletions test/integration/api/v1/auctions/details_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,11 @@ def test_returns_auction_details
end

def test_auction_not_found
assert_raises ActiveRecord::RecordNotFound do
get api_v1_auction_path('non-existing-uuid'), as: :json
end
expected_uuid = 'not-a-real-path'
get api_v1_auction_path(expected_uuid), as: :json
assert_response :not_found
json = JSON.parse(response.body, symbolize_names: true)
assert_equal expected_uuid, json[:uuid]
assert_equal 'Not Found', json[:error]
end
end
end
14 changes: 9 additions & 5 deletions test/integration/api/v1/auctions/update_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,14 @@ def test_inaccessible_when_ip_address_is_not_allowed
end

def test_auction_not_found
assert_raises ActiveRecord::RecordNotFound do
patch api_v1_auction_path('non-existing-uuid'),
params: { status: Auction.statuses[:no_bids] },
as: :json
end
expected_uuid = 'non-existing-uuid'
patch api_v1_auction_path(expected_uuid),
params: { status: Auction.statuses[:no_bids] },
as: :json

assert_response :not_found
json = JSON.parse(response.body, symbolize_names: true)
assert_equal expected_uuid, json[:uuid]
assert_equal 'Not Found', json[:error]
end
end