domain_blocks_spec.rb 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. describe 'blocking domains through the moderation interface' do
  4. before do
  5. allow(DomainBlockWorker).to receive(:perform_async).and_return(true)
  6. sign_in Fabricate(:user, role: UserRole.find_by(name: 'Admin')), scope: :user
  7. end
  8. context 'when silencing a new domain' do
  9. it 'adds a new domain block' do
  10. visit new_admin_domain_block_path
  11. fill_in 'domain_block_domain', with: 'example.com'
  12. select I18n.t('admin.domain_blocks.new.severity.silence'), from: 'domain_block_severity'
  13. click_button I18n.t('admin.domain_blocks.new.create')
  14. expect(DomainBlock.exists?(domain: 'example.com', severity: 'silence')).to be true
  15. expect(DomainBlockWorker).to have_received(:perform_async)
  16. end
  17. end
  18. context 'when suspending a new domain' do
  19. it 'presents a confirmation screen before suspending the domain' do
  20. visit new_admin_domain_block_path
  21. fill_in 'domain_block_domain', with: 'example.com'
  22. select I18n.t('admin.domain_blocks.new.severity.suspend'), from: 'domain_block_severity'
  23. click_button I18n.t('admin.domain_blocks.new.create')
  24. # It doesn't immediately block but presents a confirmation screen
  25. expect(page).to have_title(I18n.t('admin.domain_blocks.confirm_suspension.title', domain: 'example.com'))
  26. expect(DomainBlockWorker).to_not have_received(:perform_async)
  27. # Confirming creates a block
  28. click_button I18n.t('admin.domain_blocks.confirm_suspension.confirm')
  29. expect(DomainBlock.exists?(domain: 'example.com', severity: 'suspend')).to be true
  30. expect(DomainBlockWorker).to have_received(:perform_async)
  31. end
  32. end
  33. context 'when suspending a domain that is already silenced' do
  34. it 'presents a confirmation screen before suspending the domain' do
  35. domain_block = Fabricate(:domain_block, domain: 'example.com', severity: 'silence')
  36. visit new_admin_domain_block_path
  37. fill_in 'domain_block_domain', with: 'example.com'
  38. select I18n.t('admin.domain_blocks.new.severity.suspend'), from: 'domain_block_severity'
  39. click_button I18n.t('admin.domain_blocks.new.create')
  40. # It doesn't immediately block but presents a confirmation screen
  41. expect(page).to have_title(I18n.t('admin.domain_blocks.confirm_suspension.title', domain: 'example.com'))
  42. expect(DomainBlockWorker).to_not have_received(:perform_async)
  43. # Confirming updates the block
  44. click_button I18n.t('admin.domain_blocks.confirm_suspension.confirm')
  45. expect(domain_block.reload.severity).to eq 'suspend'
  46. expect(DomainBlockWorker).to have_received(:perform_async)
  47. end
  48. end
  49. context 'when suspending a subdomain of an already-silenced domain' do
  50. it 'presents a confirmation screen before suspending the domain' do
  51. domain_block = Fabricate(:domain_block, domain: 'example.com', severity: 'silence')
  52. visit new_admin_domain_block_path
  53. fill_in 'domain_block_domain', with: 'subdomain.example.com'
  54. select I18n.t('admin.domain_blocks.new.severity.suspend'), from: 'domain_block_severity'
  55. click_button I18n.t('admin.domain_blocks.new.create')
  56. # It doesn't immediately block but presents a confirmation screen
  57. expect(page).to have_title(I18n.t('admin.domain_blocks.confirm_suspension.title', domain: 'subdomain.example.com'))
  58. expect(DomainBlockWorker).to_not have_received(:perform_async)
  59. # Confirming creates the block
  60. click_button I18n.t('admin.domain_blocks.confirm_suspension.confirm')
  61. expect(DomainBlock.where(domain: 'subdomain.example.com', severity: 'suspend')).to exist
  62. expect(DomainBlockWorker).to have_received(:perform_async)
  63. # And leaves the previous block alone
  64. expect(domain_block.reload.severity).to eq 'silence'
  65. expect(domain_block.reload.domain).to eq 'example.com'
  66. end
  67. end
  68. context 'when editing a domain block' do
  69. it 'presents a confirmation screen before suspending the domain' do
  70. domain_block = Fabricate(:domain_block, domain: 'example.com', severity: 'silence')
  71. visit edit_admin_domain_block_path(domain_block)
  72. select I18n.t('admin.domain_blocks.new.severity.suspend'), from: 'domain_block_severity'
  73. click_button I18n.t('generic.save_changes')
  74. # It doesn't immediately block but presents a confirmation screen
  75. expect(page).to have_title(I18n.t('admin.domain_blocks.confirm_suspension.title', domain: 'example.com'))
  76. expect(DomainBlockWorker).to_not have_received(:perform_async)
  77. # Confirming updates the block
  78. click_button I18n.t('admin.domain_blocks.confirm_suspension.confirm')
  79. expect(DomainBlockWorker).to have_received(:perform_async)
  80. expect(domain_block.reload.severity).to eq 'suspend'
  81. end
  82. end
  83. end