1
0

accounts_controller_spec.rb 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe Api::V1::Admin::AccountsController do
  4. render_views
  5. let(:role) { UserRole.find_by(name: 'Moderator') }
  6. let(:user) { Fabricate(:user, role: role) }
  7. let(:scopes) { 'admin:read admin:write' }
  8. let(:token) { Fabricate(:accessible_access_token, resource_owner_id: user.id, scopes: scopes) }
  9. let(:account) { Fabricate(:account) }
  10. before do
  11. allow(controller).to receive(:doorkeeper_token) { token }
  12. end
  13. describe 'GET #index' do
  14. let!(:remote_account) { Fabricate(:account, domain: 'example.org') }
  15. let!(:other_remote_account) { Fabricate(:account, domain: 'foo.bar') }
  16. let!(:suspended_account) { Fabricate(:account, suspended: true) }
  17. let!(:suspended_remote) { Fabricate(:account, domain: 'foo.bar', suspended: true) }
  18. let!(:disabled_account) { Fabricate(:user, disabled: true).account }
  19. let!(:pending_account) { Fabricate(:user, approved: false).account }
  20. let!(:admin_account) { user.account }
  21. let(:params) { {} }
  22. before do
  23. pending_account.user.update(approved: false)
  24. get :index, params: params
  25. end
  26. it_behaves_like 'forbidden for wrong scope', 'write:statuses'
  27. it_behaves_like 'forbidden for wrong role', ''
  28. [
  29. [{ active: 'true', local: 'true', staff: 'true' }, [:admin_account]],
  30. [{ by_domain: 'example.org', remote: 'true' }, [:remote_account]],
  31. [{ suspended: 'true' }, [:suspended_account]],
  32. [{ disabled: 'true' }, [:disabled_account]],
  33. [{ pending: 'true' }, [:pending_account]],
  34. ].each do |params, expected_results|
  35. context "when called with #{params.inspect}" do
  36. let(:params) { params }
  37. it 'returns http success' do
  38. expect(response).to have_http_status(200)
  39. end
  40. it "returns the correct accounts (#{expected_results.inspect})" do
  41. json = body_as_json
  42. expect(json.map { |a| a[:id].to_i }).to eq(expected_results.map { |symbol| send(symbol).id })
  43. end
  44. end
  45. end
  46. end
  47. describe 'GET #show' do
  48. before do
  49. get :show, params: { id: account.id }
  50. end
  51. it_behaves_like 'forbidden for wrong scope', 'write:statuses'
  52. it_behaves_like 'forbidden for wrong role', ''
  53. it 'returns http success' do
  54. expect(response).to have_http_status(200)
  55. end
  56. end
  57. describe 'POST #approve' do
  58. before do
  59. account.user.update(approved: false)
  60. post :approve, params: { id: account.id }
  61. end
  62. it_behaves_like 'forbidden for wrong scope', 'write:statuses'
  63. it_behaves_like 'forbidden for wrong role', ''
  64. it 'returns http success' do
  65. expect(response).to have_http_status(200)
  66. end
  67. it 'approves user' do
  68. expect(account.reload.user_approved?).to be true
  69. end
  70. it 'logs action' do
  71. log_item = Admin::ActionLog.last
  72. expect(log_item).to_not be_nil
  73. expect(log_item.action).to eq :approve
  74. expect(log_item.account_id).to eq user.account_id
  75. expect(log_item.target_id).to eq account.user.id
  76. end
  77. end
  78. describe 'POST #reject' do
  79. before do
  80. account.user.update(approved: false)
  81. post :reject, params: { id: account.id }
  82. end
  83. it_behaves_like 'forbidden for wrong scope', 'write:statuses'
  84. it_behaves_like 'forbidden for wrong role', ''
  85. it 'returns http success' do
  86. expect(response).to have_http_status(200)
  87. end
  88. it 'removes user' do
  89. expect(User.where(id: account.user.id).count).to eq 0
  90. end
  91. it 'logs action' do
  92. log_item = Admin::ActionLog.last
  93. expect(log_item).to_not be_nil
  94. expect(log_item.action).to eq :reject
  95. expect(log_item.account_id).to eq user.account_id
  96. expect(log_item.target_id).to eq account.user.id
  97. end
  98. end
  99. describe 'POST #enable' do
  100. before do
  101. account.user.update(disabled: true)
  102. post :enable, params: { id: account.id }
  103. end
  104. it_behaves_like 'forbidden for wrong scope', 'write:statuses'
  105. it_behaves_like 'forbidden for wrong role', ''
  106. it 'returns http success' do
  107. expect(response).to have_http_status(200)
  108. end
  109. it 'enables user' do
  110. expect(account.reload.user_disabled?).to be false
  111. end
  112. end
  113. describe 'POST #unsuspend' do
  114. before do
  115. account.suspend!
  116. post :unsuspend, params: { id: account.id }
  117. end
  118. it_behaves_like 'forbidden for wrong scope', 'write:statuses'
  119. it_behaves_like 'forbidden for wrong role', ''
  120. it 'returns http success' do
  121. expect(response).to have_http_status(200)
  122. end
  123. it 'unsuspends account' do
  124. expect(account.reload.suspended?).to be false
  125. end
  126. end
  127. describe 'POST #unsensitive' do
  128. before do
  129. account.touch(:sensitized_at)
  130. post :unsensitive, params: { id: account.id }
  131. end
  132. it_behaves_like 'forbidden for wrong scope', 'write:statuses'
  133. it_behaves_like 'forbidden for wrong role', ''
  134. it 'returns http success' do
  135. expect(response).to have_http_status(200)
  136. end
  137. it 'unsensitizes account' do
  138. expect(account.reload.sensitized?).to be false
  139. end
  140. end
  141. describe 'POST #unsilence' do
  142. before do
  143. account.touch(:silenced_at)
  144. post :unsilence, params: { id: account.id }
  145. end
  146. it_behaves_like 'forbidden for wrong scope', 'write:statuses'
  147. it_behaves_like 'forbidden for wrong role', ''
  148. it 'returns http success' do
  149. expect(response).to have_http_status(200)
  150. end
  151. it 'unsilences account' do
  152. expect(account.reload.silenced?).to be false
  153. end
  154. end
  155. end