1
0

instances_controller_spec.rb 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. Instance.refresh
  11. sign_in current_user, scope: :user
  12. end
  13. describe 'GET #index' do
  14. around do |example|
  15. default_per_page = Instance.default_per_page
  16. Instance.paginates_per 1
  17. example.run
  18. Instance.paginates_per default_per_page
  19. end
  20. it 'renders instances' do
  21. get :index, params: { page: 2 }
  22. expect(instance_directory_links.size).to eq(1)
  23. expect(instance_directory_links.first.text.strip).to match('less.popular')
  24. expect(response).to have_http_status(200)
  25. end
  26. def instance_directory_links
  27. response.parsed_body.css('div.directory__tag a')
  28. end
  29. end
  30. describe 'GET #show' do
  31. before do
  32. allow(Admin::ActionLogFilter).to receive(:new).and_call_original
  33. end
  34. it 'shows an instance page' do
  35. get :show, params: { id: account_popular_main.domain }
  36. expect(response).to have_http_status(200)
  37. instance = assigns(:instance)
  38. expect(instance).to_not be_new_record
  39. expect(Admin::ActionLogFilter).to have_received(:new).with(target_domain: account_popular_main.domain)
  40. action_logs = assigns(:action_logs).to_a
  41. expect(action_logs.size).to eq 0
  42. end
  43. context 'with an unknown domain' do
  44. it 'returns http success' do
  45. get :show, params: { id: 'unknown.example' }
  46. expect(response).to have_http_status(200)
  47. instance = assigns(:instance)
  48. expect(instance).to be_new_record
  49. end
  50. end
  51. end
  52. describe 'POST #clear_delivery_errors' do
  53. let(:tracker) { instance_double(DeliveryFailureTracker, clear_failures!: true) }
  54. before { allow(DeliveryFailureTracker).to receive(:new).and_return(tracker) }
  55. it 'clears instance delivery errors' do
  56. post :clear_delivery_errors, params: { id: account_popular_main.domain }
  57. expect(response).to redirect_to(admin_instance_path(account_popular_main.domain))
  58. expect(tracker).to have_received(:clear_failures!)
  59. end
  60. end
  61. describe 'POST #restart_delivery' do
  62. let(:tracker) { instance_double(DeliveryFailureTracker, track_success!: true) }
  63. before { allow(DeliveryFailureTracker).to receive(:new).and_return(tracker) }
  64. context 'with an unavailable instance' do
  65. before { Fabricate(:unavailable_domain, domain: account_popular_main.domain) }
  66. it 'tracks success on the instance' do
  67. post :restart_delivery, params: { id: account_popular_main.domain }
  68. expect(response).to redirect_to(admin_instance_path(account_popular_main.domain))
  69. expect(tracker).to have_received(:track_success!)
  70. end
  71. end
  72. context 'with an available instance' do
  73. it 'does not track success on the instance' do
  74. post :restart_delivery, params: { id: account_popular_main.domain }
  75. expect(response).to redirect_to(admin_instance_path(account_popular_main.domain))
  76. expect(tracker).to_not have_received(:track_success!)
  77. end
  78. end
  79. end
  80. describe 'POST #stop_delivery' do
  81. it 'clears instance delivery errors' do
  82. expect do
  83. post :stop_delivery, params: { id: account_popular_main.domain }
  84. end.to change(UnavailableDomain, :count).by(1)
  85. expect(response).to redirect_to(admin_instance_path(account_popular_main.domain))
  86. end
  87. end
  88. describe 'DELETE #destroy' do
  89. subject { delete :destroy, params: { id: Instance.first.id } }
  90. let(:current_user) { Fabricate(:user, role: role) }
  91. let(:account) { Fabricate(:account) }
  92. context 'when user is admin' do
  93. let(:role) { UserRole.find_by(name: 'Admin') }
  94. it 'succeeds in purging instance' do
  95. expect(subject).to redirect_to admin_instances_path
  96. end
  97. end
  98. context 'when user is not admin' do
  99. let(:role) { nil }
  100. it 'fails to purge instance' do
  101. expect(subject).to have_http_status 403
  102. end
  103. end
  104. end
  105. end