domain_blocks_controller_spec.rb 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe Api::V1::DomainBlocksController, type: :controller do
  4. render_views
  5. let(:user) { Fabricate(:user) }
  6. let(:token) { Fabricate(:accessible_access_token, resource_owner_id: user.id, scopes: scopes) }
  7. before do
  8. user.account.block_domain!('example.com')
  9. allow(controller).to receive(:doorkeeper_token) { token }
  10. end
  11. shared_examples 'forbidden for wrong scope' do |wrong_scope|
  12. let(:scopes) { wrong_scope }
  13. it 'returns http forbidden' do
  14. expect(response).to have_http_status(403)
  15. end
  16. end
  17. describe 'GET #show' do
  18. let(:scopes) { 'read:blocks' }
  19. before do
  20. get :show, params: { limit: 1 }
  21. end
  22. it 'returns http success' do
  23. expect(response).to have_http_status(200)
  24. end
  25. it 'returns blocked domains' do
  26. expect(body_as_json.first).to eq 'example.com'
  27. end
  28. it_behaves_like 'forbidden for wrong scope', 'write:statuses'
  29. end
  30. describe 'POST #create' do
  31. let(:scopes) { 'write:blocks' }
  32. before do
  33. post :create, params: { domain: 'example.org' }
  34. end
  35. it 'returns http success' do
  36. expect(response).to have_http_status(200)
  37. end
  38. it 'creates a domain block' do
  39. expect(user.account.domain_blocking?('example.org')).to be true
  40. end
  41. it_behaves_like 'forbidden for wrong scope', 'write:statuses'
  42. end
  43. describe 'DELETE #destroy' do
  44. let(:scopes) { 'write:blocks' }
  45. before do
  46. delete :destroy, params: { domain: 'example.com' }
  47. end
  48. it 'returns http success' do
  49. expect(response).to have_http_status(200)
  50. end
  51. it 'deletes a domain block' do
  52. expect(user.account.domain_blocking?('example.com')).to be false
  53. end
  54. it_behaves_like 'forbidden for wrong scope', 'write:statuses'
  55. end
  56. end