闭社主体 forked from https://github.com/tootsuite/mastodon
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

54 lines
1.2 KiB

  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. class FakeService; end
  4. describe Api::BaseController do
  5. controller do
  6. def success
  7. head 200
  8. end
  9. def error
  10. FakeService.new
  11. end
  12. end
  13. describe 'Forgery protection' do
  14. before do
  15. routes.draw { post 'success' => 'api/base#success' }
  16. end
  17. it 'does not protect from forgery' do
  18. ActionController::Base.allow_forgery_protection = true
  19. post 'success'
  20. expect(response).to have_http_status(:success)
  21. end
  22. end
  23. describe 'Error handling' do
  24. ERRORS_WITH_CODES = {
  25. ActiveRecord::RecordInvalid => 422,
  26. Mastodon::ValidationError => 422,
  27. ActiveRecord::RecordNotFound => 404,
  28. Mastodon::UnexpectedResponseError => 503,
  29. HTTP::Error => 503,
  30. OpenSSL::SSL::SSLError => 503,
  31. Mastodon::NotPermittedError => 403,
  32. }
  33. before do
  34. routes.draw { get 'error' => 'api/base#error' }
  35. end
  36. ERRORS_WITH_CODES.each do |error, code|
  37. it "Handles error class of #{error}" do
  38. expect(FakeService).to receive(:new).and_raise(error)
  39. get 'error'
  40. expect(response).to have_http_status(code)
  41. end
  42. end
  43. end
  44. end