domain_blocks_spec.rb 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. submit_domain_block('example.com', 'silence')
  12. expect(DomainBlock.exists?(domain: 'example.com', severity: 'silence')).to be true
  13. expect(DomainBlockWorker).to have_received(:perform_async)
  14. end
  15. end
  16. context 'when suspending a new domain' do
  17. it 'presents a confirmation screen before suspending the domain' do
  18. visit new_admin_domain_block_path
  19. submit_domain_block('example.com', 'suspend')
  20. # It doesn't immediately block but presents a confirmation screen
  21. expect(page).to have_title(I18n.t('admin.domain_blocks.confirm_suspension.title', domain: 'example.com'))
  22. expect(DomainBlockWorker).to_not have_received(:perform_async)
  23. # Confirming creates a block
  24. click_on I18n.t('admin.domain_blocks.confirm_suspension.confirm')
  25. expect(DomainBlock.exists?(domain: 'example.com', severity: 'suspend')).to be true
  26. expect(DomainBlockWorker).to have_received(:perform_async)
  27. end
  28. end
  29. context 'when suspending a domain that is already silenced' do
  30. it 'presents a confirmation screen before suspending the domain' do
  31. domain_block = Fabricate(:domain_block, domain: 'example.com', severity: 'silence')
  32. visit new_admin_domain_block_path
  33. submit_domain_block('example.com', 'suspend')
  34. # It doesn't immediately block but presents a confirmation screen
  35. expect(page).to have_title(I18n.t('admin.domain_blocks.confirm_suspension.title', domain: 'example.com'))
  36. expect(DomainBlockWorker).to_not have_received(:perform_async)
  37. # Confirming updates the block
  38. click_on I18n.t('admin.domain_blocks.confirm_suspension.confirm')
  39. expect(domain_block.reload.severity).to eq 'suspend'
  40. expect(DomainBlockWorker).to have_received(:perform_async)
  41. end
  42. end
  43. context 'when suspending a subdomain of an already-silenced domain' do
  44. it 'presents a confirmation screen before suspending the domain' do
  45. domain_block = Fabricate(:domain_block, domain: 'example.com', severity: 'silence')
  46. visit new_admin_domain_block_path
  47. submit_domain_block('subdomain.example.com', 'suspend')
  48. # It doesn't immediately block but presents a confirmation screen
  49. expect(page).to have_title(I18n.t('admin.domain_blocks.confirm_suspension.title', domain: 'subdomain.example.com'))
  50. expect(DomainBlockWorker).to_not have_received(:perform_async)
  51. # Confirming creates the block
  52. click_on I18n.t('admin.domain_blocks.confirm_suspension.confirm')
  53. expect(DomainBlock.where(domain: 'subdomain.example.com', severity: 'suspend')).to exist
  54. expect(DomainBlockWorker).to have_received(:perform_async)
  55. # And leaves the previous block alone
  56. expect(domain_block.reload)
  57. .to have_attributes(
  58. severity: eq('silence'),
  59. domain: eq('example.com')
  60. )
  61. end
  62. end
  63. context 'when editing a domain block' do
  64. it 'presents a confirmation screen before suspending the domain' do
  65. domain_block = Fabricate(:domain_block, domain: 'example.com', severity: 'silence')
  66. visit edit_admin_domain_block_path(domain_block)
  67. select I18n.t('admin.domain_blocks.new.severity.suspend'), from: 'domain_block_severity'
  68. click_on I18n.t('generic.save_changes')
  69. # It doesn't immediately block but presents a confirmation screen
  70. expect(page).to have_title(I18n.t('admin.domain_blocks.confirm_suspension.title', domain: 'example.com'))
  71. expect(DomainBlockWorker).to_not have_received(:perform_async)
  72. # Confirming updates the block
  73. click_on I18n.t('admin.domain_blocks.confirm_suspension.confirm')
  74. expect(DomainBlockWorker).to have_received(:perform_async)
  75. expect(domain_block.reload.severity).to eq 'suspend'
  76. end
  77. end
  78. private
  79. def submit_domain_block(domain, severity)
  80. fill_in 'domain_block_domain', with: domain
  81. select I18n.t("admin.domain_blocks.new.severity.#{severity}"), from: 'domain_block_severity'
  82. click_on I18n.t('admin.domain_blocks.new.create')
  83. end
  84. end