account_actions_controller_spec.rb 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. require 'rails_helper'
  2. RSpec.describe Api::V1::Admin::AccountActionsController, type: :controller do
  3. render_views
  4. let(:role) { UserRole.find_by(name: 'Moderator') }
  5. let(:user) { Fabricate(:user, role: role) }
  6. let(:scopes) { 'admin:read admin:write' }
  7. let(:token) { Fabricate(:accessible_access_token, resource_owner_id: user.id, scopes: scopes) }
  8. let(:account) { Fabricate(:account) }
  9. before do
  10. allow(controller).to receive(:doorkeeper_token) { token }
  11. end
  12. shared_examples 'forbidden for wrong scope' do |wrong_scope|
  13. let(:scopes) { wrong_scope }
  14. it 'returns http forbidden' do
  15. expect(response).to have_http_status(403)
  16. end
  17. end
  18. shared_examples 'forbidden for wrong role' do |wrong_role|
  19. let(:role) { UserRole.find_by(name: wrong_role) }
  20. it 'returns http forbidden' do
  21. expect(response).to have_http_status(403)
  22. end
  23. end
  24. describe 'POST #create' do
  25. context do
  26. before do
  27. post :create, params: { account_id: account.id, type: 'disable' }
  28. end
  29. it_behaves_like 'forbidden for wrong scope', 'write:statuses'
  30. it_behaves_like 'forbidden for wrong role', ''
  31. it 'returns http success' do
  32. expect(response).to have_http_status(200)
  33. end
  34. it 'performs action against account' do
  35. expect(account.reload.user_disabled?).to be true
  36. end
  37. it 'logs action' do
  38. log_item = Admin::ActionLog.last
  39. expect(log_item).to_not be_nil
  40. expect(log_item.action).to eq :disable
  41. expect(log_item.account_id).to eq user.account_id
  42. expect(log_item.target_id).to eq account.user.id
  43. end
  44. end
  45. context 'with no type' do
  46. before do
  47. post :create, params: { account_id: account.id }
  48. end
  49. it 'returns http unprocessable entity' do
  50. expect(response).to have_http_status(422)
  51. end
  52. end
  53. end
  54. end