accounts_controller_spec.rb 5.9 KB

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