This repository has been archived on 2024-07-22. You can view files and clone it, but cannot push or open issues or pull requests.
2017-06-07 11:09:25 -07:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'rails_helper'
|
|
|
|
|
|
|
|
class FakeService; end
|
|
|
|
|
|
|
|
describe Api::BaseController do
|
|
|
|
controller do
|
|
|
|
def success
|
|
|
|
head 200
|
|
|
|
end
|
|
|
|
|
|
|
|
def error
|
|
|
|
FakeService.new
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'Forgery protection' do
|
|
|
|
before do
|
|
|
|
routes.draw { post 'success' => 'api/base#success' }
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not protect from forgery' do
|
|
|
|
ActionController::Base.allow_forgery_protection = true
|
|
|
|
post 'success'
|
|
|
|
expect(response).to have_http_status(:success)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'Error handling' do
|
|
|
|
ERRORS_WITH_CODES = {
|
|
|
|
ActiveRecord::RecordInvalid => 422,
|
|
|
|
Mastodon::ValidationError => 422,
|
|
|
|
ActiveRecord::RecordNotFound => 404,
|
2017-07-19 16:59:07 -07:00
|
|
|
Mastodon::UnexpectedResponseError => 503,
|
2017-06-07 11:09:25 -07:00
|
|
|
HTTP::Error => 503,
|
|
|
|
OpenSSL::SSL::SSLError => 503,
|
|
|
|
Mastodon::NotPermittedError => 403,
|
|
|
|
}
|
|
|
|
|
|
|
|
before do
|
|
|
|
routes.draw { get 'error' => 'api/base#error' }
|
|
|
|
end
|
|
|
|
|
|
|
|
ERRORS_WITH_CODES.each do |error, code|
|
|
|
|
it "Handles error class of #{error}" do
|
|
|
|
expect(FakeService).to receive(:new).and_raise(error)
|
|
|
|
|
|
|
|
get 'error'
|
|
|
|
expect(response).to have_http_status(code)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|