email_domain_blocks_controller_spec.rb 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe Admin::EmailDomainBlocksController do
  4. render_views
  5. before do
  6. sign_in Fabricate(:user, role: UserRole.find_by(name: 'Admin')), scope: :user
  7. end
  8. describe 'GET #index' do
  9. around do |example|
  10. default_per_page = EmailDomainBlock.default_per_page
  11. EmailDomainBlock.paginates_per 2
  12. example.run
  13. EmailDomainBlock.paginates_per default_per_page
  14. end
  15. it 'returns http success' do
  16. 2.times { Fabricate(:email_domain_block) }
  17. Fabricate(:email_domain_block, allow_with_approval: true)
  18. get :index, params: { page: 2 }
  19. expect(response).to have_http_status(200)
  20. end
  21. end
  22. describe 'GET #new' do
  23. it 'returns http success' do
  24. get :new
  25. expect(response).to have_http_status(200)
  26. end
  27. end
  28. describe 'POST #create' do
  29. context 'when resolve button is pressed' do
  30. before do
  31. resolver = instance_double(Resolv::DNS)
  32. allow(resolver).to receive(:getresources)
  33. .with('example.com', Resolv::DNS::Resource::IN::MX)
  34. .and_return([])
  35. allow(resolver).to receive(:getresources).with('example.com', Resolv::DNS::Resource::IN::A).and_return([])
  36. allow(resolver).to receive(:getresources).with('example.com', Resolv::DNS::Resource::IN::AAAA).and_return([])
  37. allow(resolver).to receive(:timeouts=).and_return(nil)
  38. allow(Resolv::DNS).to receive(:open).and_yield(resolver)
  39. post :create, params: { email_domain_block: { domain: 'example.com' } }
  40. end
  41. it 'renders new template' do
  42. expect(response).to render_template(:new)
  43. end
  44. end
  45. context 'when save button is pressed' do
  46. before do
  47. post :create, params: { email_domain_block: { domain: 'example.com' }, save: '' }
  48. end
  49. it 'blocks the domain' do
  50. expect(EmailDomainBlock.find_by(domain: 'example.com')).to_not be_nil
  51. end
  52. it 'redirects to e-mail domain blocks' do
  53. expect(response).to redirect_to(admin_email_domain_blocks_path)
  54. end
  55. end
  56. end
  57. end