domain_blocks_controller_spec.rb 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. require 'rails_helper'
  2. RSpec.describe Api::V1::Admin::DomainBlocksController, type: :controller do
  3. render_views
  4. let(:role) { UserRole.find_by(name: 'Admin') }
  5. let(:user) { Fabricate(:user, role: role) }
  6. let(:scopes) { 'admin:read admin:write' }
  7. let(:token) { Fabricate(:accessible_access_token, resource_owner_id: user.id, scopes: scopes) }
  8. before do
  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. shared_examples 'forbidden for wrong role' do |wrong_role|
  18. let(:role) { UserRole.find_by(name: wrong_role) }
  19. it 'returns http forbidden' do
  20. expect(response).to have_http_status(403)
  21. end
  22. end
  23. describe 'GET #index' do
  24. let!(:block) { Fabricate(:domain_block) }
  25. before do
  26. get :index
  27. end
  28. it_behaves_like 'forbidden for wrong scope', 'write:statuses'
  29. it_behaves_like 'forbidden for wrong role', ''
  30. it_behaves_like 'forbidden for wrong role', 'Moderator'
  31. it 'returns http success' do
  32. expect(response).to have_http_status(200)
  33. end
  34. it 'returns the expected domain blocks' do
  35. json = body_as_json
  36. expect(json.length).to eq 1
  37. expect(json[0][:id].to_i).to eq block.id
  38. end
  39. end
  40. describe 'GET #show' do
  41. let!(:block) { Fabricate(:domain_block) }
  42. before do
  43. get :show, params: { id: block.id }
  44. end
  45. it_behaves_like 'forbidden for wrong scope', 'write:statuses'
  46. it_behaves_like 'forbidden for wrong role', ''
  47. it_behaves_like 'forbidden for wrong role', 'Moderator'
  48. it 'returns http success' do
  49. expect(response).to have_http_status(200)
  50. end
  51. it 'returns expected domain name' do
  52. json = body_as_json
  53. expect(json[:domain]).to eq block.domain
  54. end
  55. end
  56. describe 'PUT #update' do
  57. let!(:remote_account) { Fabricate(:account, domain: 'example.com') }
  58. let(:domain_block) { Fabricate(:domain_block, domain: 'example.com', severity: original_severity) }
  59. before do
  60. BlockDomainService.new.call(domain_block)
  61. end
  62. let(:subject) do
  63. post :update, params: { id: domain_block.id, domain: 'example.com', severity: new_severity }
  64. end
  65. context 'downgrading a domain suspension to silence' do
  66. let(:original_severity) { 'suspend' }
  67. let(:new_severity) { 'silence' }
  68. it 'changes the block severity' do
  69. expect { subject }.to change { domain_block.reload.severity }.from('suspend').to('silence')
  70. end
  71. it 'undoes individual suspensions' do
  72. expect { subject }.to change { remote_account.reload.suspended? }.from(true).to(false)
  73. end
  74. it 'performs individual silences' do
  75. expect { subject }.to change { remote_account.reload.silenced? }.from(false).to(true)
  76. end
  77. end
  78. context 'upgrading a domain silence to suspend' do
  79. let(:original_severity) { 'silence' }
  80. let(:new_severity) { 'suspend' }
  81. it 'changes the block severity' do
  82. expect { subject }.to change { domain_block.reload.severity }.from('silence').to('suspend')
  83. end
  84. it 'undoes individual silences' do
  85. expect { subject }.to change { remote_account.reload.silenced? }.from(true).to(false)
  86. end
  87. it 'performs individual suspends' do
  88. expect { subject }.to change { remote_account.reload.suspended? }.from(false).to(true)
  89. end
  90. end
  91. end
  92. describe 'DELETE #destroy' do
  93. let!(:block) { Fabricate(:domain_block) }
  94. before do
  95. delete :destroy, params: { id: block.id }
  96. end
  97. it_behaves_like 'forbidden for wrong scope', 'write:statuses'
  98. it_behaves_like 'forbidden for wrong role', ''
  99. it_behaves_like 'forbidden for wrong role', 'Moderator'
  100. it 'returns http success' do
  101. expect(response).to have_http_status(200)
  102. end
  103. it 'deletes the block' do
  104. expect(DomainBlock.find_by(id: block.id)).to be_nil
  105. end
  106. end
  107. describe 'POST #create' do
  108. let(:existing_block_domain) { 'example.com' }
  109. let!(:block) { Fabricate(:domain_block, domain: existing_block_domain, severity: :suspend) }
  110. before do
  111. post :create, params: { domain: 'foo.bar.com', severity: :silence }
  112. end
  113. it_behaves_like 'forbidden for wrong scope', 'write:statuses'
  114. it_behaves_like 'forbidden for wrong role', ''
  115. it_behaves_like 'forbidden for wrong role', 'Moderator'
  116. it 'returns http success' do
  117. expect(response).to have_http_status(200)
  118. end
  119. it 'returns expected domain name' do
  120. json = body_as_json
  121. expect(json[:domain]).to eq 'foo.bar.com'
  122. end
  123. it 'creates a domain block' do
  124. expect(DomainBlock.find_by(domain: 'foo.bar.com')).to_not be_nil
  125. end
  126. context 'when a stricter domain block already exists' do
  127. let(:existing_block_domain) { 'bar.com' }
  128. it 'returns http unprocessable entity' do
  129. expect(response).to have_http_status(422)
  130. end
  131. it 'renders existing domain block in error' do
  132. json = body_as_json
  133. expect(json[:existing_domain_block][:domain]).to eq existing_block_domain
  134. end
  135. end
  136. end
  137. end