instances_controller_spec.rb 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. require 'rails_helper'
  2. RSpec.describe Admin::InstancesController, type: :controller do
  3. render_views
  4. let(:current_user) { Fabricate(:user, role: UserRole.find_by(name: 'Admin')) }
  5. let!(:account) { Fabricate(:account, domain: 'popular') }
  6. let!(:account2) { Fabricate(:account, domain: 'popular') }
  7. let!(:account3) { Fabricate(:account, domain: 'less.popular') }
  8. before do
  9. sign_in current_user, scope: :user
  10. end
  11. describe 'GET #index' do
  12. around do |example|
  13. default_per_page = Instance.default_per_page
  14. Instance.paginates_per 1
  15. example.run
  16. Instance.paginates_per default_per_page
  17. end
  18. it 'renders instances' do
  19. get :index, params: { page: 2 }
  20. instances = assigns(:instances).to_a
  21. expect(instances.size).to eq 1
  22. expect(instances[0].domain).to eq 'less.popular'
  23. expect(response).to have_http_status(200)
  24. end
  25. end
  26. describe 'DELETE #destroy' do
  27. subject { delete :destroy, params: { id: Instance.first.id } }
  28. let(:current_user) { Fabricate(:user, role: role) }
  29. let(:account) { Fabricate(:account) }
  30. context 'when user is admin' do
  31. let(:role) { UserRole.find_by(name: 'Admin') }
  32. it 'succeeds in purging instance' do
  33. is_expected.to redirect_to admin_instances_path
  34. end
  35. end
  36. context 'when user is not admin' do
  37. let(:role) { nil }
  38. it 'fails to purge instance' do
  39. is_expected.to have_http_status :forbidden
  40. end
  41. end
  42. end
  43. end