account_actions_spec.rb 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe 'Account actions' do
  4. let(:role) { UserRole.find_by(name: 'Admin') }
  5. let(:user) { Fabricate(:user, role: role) }
  6. let(:scopes) { 'admin:write admin:write:accounts' }
  7. let(:token) { Fabricate(:accessible_access_token, resource_owner_id: user.id, scopes: scopes) }
  8. let(:headers) { { 'Authorization' => "Bearer #{token.token}" } }
  9. shared_examples 'a successful notification delivery' do
  10. it 'notifies the user about the action taken', :sidekiq_inline do
  11. emails = capture_emails { subject }
  12. expect(emails.size)
  13. .to eq(1)
  14. expect(emails.first)
  15. .to have_attributes(
  16. to: contain_exactly(target_account.user.email)
  17. )
  18. end
  19. end
  20. shared_examples 'a successful logged action' do |action_type, target_type|
  21. it 'logs action' do
  22. subject
  23. expect(latest_admin_action_log)
  24. .to be_present
  25. .and have_attributes(
  26. action: eq(action_type),
  27. account_id: eq(user.account_id),
  28. target_id: eq(target_type == :user ? target_account.user.id : target_account.id)
  29. )
  30. end
  31. private
  32. def latest_admin_action_log
  33. Admin::ActionLog.last
  34. end
  35. end
  36. describe 'POST /api/v1/admin/accounts/:id/action' do
  37. subject do
  38. post "/api/v1/admin/accounts/#{target_account.id}/action", headers: headers, params: params
  39. end
  40. let(:target_account) { Fabricate(:account) }
  41. context 'with type of disable' do
  42. let(:params) { { type: 'disable' } }
  43. it_behaves_like 'forbidden for wrong scope', 'admin:read admin:read:accounts'
  44. it_behaves_like 'forbidden for wrong role', ''
  45. it_behaves_like 'a successful notification delivery'
  46. it_behaves_like 'a successful logged action', :disable, :user
  47. it 'disables the target account' do
  48. expect { subject }.to change { target_account.reload.user_disabled? }.from(false).to(true)
  49. expect(response).to have_http_status(200)
  50. end
  51. end
  52. context 'with type of sensitive' do
  53. let(:params) { { type: 'sensitive' } }
  54. it_behaves_like 'forbidden for wrong scope', 'admin:read admin:read:accounts'
  55. it_behaves_like 'forbidden for wrong role', ''
  56. it_behaves_like 'a successful notification delivery'
  57. it_behaves_like 'a successful logged action', :sensitive, :account
  58. it 'marks the target account as sensitive' do
  59. expect { subject }.to change { target_account.reload.sensitized? }.from(false).to(true)
  60. expect(response).to have_http_status(200)
  61. end
  62. end
  63. context 'with type of silence' do
  64. let(:params) { { type: 'silence' } }
  65. it_behaves_like 'forbidden for wrong scope', 'admin:read admin:read:accounts'
  66. it_behaves_like 'forbidden for wrong role', ''
  67. it_behaves_like 'a successful notification delivery'
  68. it_behaves_like 'a successful logged action', :silence, :account
  69. it 'marks the target account as silenced' do
  70. expect { subject }.to change { target_account.reload.silenced? }.from(false).to(true)
  71. expect(response).to have_http_status(200)
  72. end
  73. end
  74. context 'with type of suspend' do
  75. let(:params) { { type: 'suspend' } }
  76. it_behaves_like 'forbidden for wrong scope', 'admin:read admin:read:accounts'
  77. it_behaves_like 'forbidden for wrong role', ''
  78. it_behaves_like 'a successful notification delivery'
  79. it_behaves_like 'a successful logged action', :suspend, :account
  80. it 'marks the target account as suspended' do
  81. expect { subject }.to change { target_account.reload.suspended? }.from(false).to(true)
  82. expect(response).to have_http_status(200)
  83. end
  84. end
  85. context 'with type of none' do
  86. let(:params) { { type: 'none' } }
  87. it_behaves_like 'a successful notification delivery'
  88. it 'returns http success' do
  89. subject
  90. expect(response).to have_http_status(200)
  91. end
  92. end
  93. context 'with no type' do
  94. let(:params) { {} }
  95. it 'returns http unprocessable entity' do
  96. subject
  97. expect(response).to have_http_status(422)
  98. end
  99. end
  100. context 'with invalid type' do
  101. let(:params) { { type: 'invalid' } }
  102. it 'returns http unprocessable entity' do
  103. subject
  104. expect(response).to have_http_status(422)
  105. end
  106. end
  107. end
  108. end