1
0

instances_controller_spec.rb 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe Admin::InstancesController do
  4. render_views
  5. let(:current_user) { Fabricate(:user, role: UserRole.find_by(name: 'Admin')) }
  6. let!(:account_popular_main) { Fabricate(:account, domain: 'popular') }
  7. before do
  8. _account_less_popular = Fabricate(:account, domain: 'less.popular')
  9. _account_popular_other = Fabricate(:account, domain: 'popular')
  10. sign_in current_user, scope: :user
  11. end
  12. describe 'GET #index' do
  13. around do |example|
  14. default_per_page = Instance.default_per_page
  15. Instance.paginates_per 1
  16. example.run
  17. Instance.paginates_per default_per_page
  18. end
  19. it 'renders instances' do
  20. get :index, params: { page: 2 }
  21. instances = assigns(:instances).to_a
  22. expect(instances.size).to eq 1
  23. expect(instances[0].domain).to eq 'less.popular'
  24. expect(response).to have_http_status(200)
  25. end
  26. end
  27. describe 'GET #show' do
  28. it 'shows an instance page' do
  29. get :show, params: { id: account_popular_main.domain }
  30. expect(response).to have_http_status(200)
  31. end
  32. end
  33. describe 'POST #clear_delivery_errors' do
  34. let(:tracker) { instance_double(DeliveryFailureTracker, clear_failures!: true) }
  35. before { allow(DeliveryFailureTracker).to receive(:new).and_return(tracker) }
  36. it 'clears instance delivery errors' do
  37. post :clear_delivery_errors, params: { id: account_popular_main.domain }
  38. expect(response).to redirect_to(admin_instance_path(account_popular_main.domain))
  39. expect(tracker).to have_received(:clear_failures!)
  40. end
  41. end
  42. describe 'POST #restart_delivery' do
  43. let(:tracker) { instance_double(DeliveryFailureTracker, track_success!: true) }
  44. before { allow(DeliveryFailureTracker).to receive(:new).and_return(tracker) }
  45. context 'with an unavailable instance' do
  46. before { Fabricate(:unavailable_domain, domain: account_popular_main.domain) }
  47. it 'tracks success on the instance' do
  48. post :restart_delivery, params: { id: account_popular_main.domain }
  49. expect(response).to redirect_to(admin_instance_path(account_popular_main.domain))
  50. expect(tracker).to have_received(:track_success!)
  51. end
  52. end
  53. context 'with an available instance' do
  54. it 'does not track success on the instance' do
  55. post :restart_delivery, params: { id: account_popular_main.domain }
  56. expect(response).to redirect_to(admin_instance_path(account_popular_main.domain))
  57. expect(tracker).to_not have_received(:track_success!)
  58. end
  59. end
  60. end
  61. describe 'POST #stop_delivery' do
  62. it 'clears instance delivery errors' do
  63. expect do
  64. post :stop_delivery, params: { id: account_popular_main.domain }
  65. end.to change(UnavailableDomain, :count).by(1)
  66. expect(response).to redirect_to(admin_instance_path(account_popular_main.domain))
  67. end
  68. end
  69. describe 'DELETE #destroy' do
  70. subject { delete :destroy, params: { id: Instance.first.id } }
  71. let(:current_user) { Fabricate(:user, role: role) }
  72. let(:account) { Fabricate(:account) }
  73. context 'when user is admin' do
  74. let(:role) { UserRole.find_by(name: 'Admin') }
  75. it 'succeeds in purging instance' do
  76. expect(subject).to redirect_to admin_instances_path
  77. end
  78. end
  79. context 'when user is not admin' do
  80. let(:role) { nil }
  81. it 'fails to purge instance' do
  82. expect(subject).to have_http_status 403
  83. end
  84. end
  85. end
  86. end