accounts_spec.rb 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe 'Accounts' do
  4. let(:role) { UserRole.find_by(name: 'Admin') }
  5. let(:user) { Fabricate(:user, role: role) }
  6. let(:scopes) { 'admin:read:accounts 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. describe 'GET /api/v1/admin/accounts' do
  10. subject do
  11. get '/api/v1/admin/accounts', headers: headers, params: params
  12. end
  13. shared_examples 'a successful request' do
  14. it 'returns the correct accounts', :aggregate_failures do
  15. subject
  16. expect(response).to have_http_status(200)
  17. expect(body_as_json.pluck(:id)).to match_array(expected_results.map { |a| a.id.to_s })
  18. end
  19. end
  20. let!(:remote_account) { Fabricate(:account, domain: 'example.org') }
  21. let!(:suspended_account) { Fabricate(:account, suspended: true) }
  22. let!(:disabled_account) { Fabricate(:user, disabled: true).account }
  23. let!(:pending_account) { Fabricate(:user, approved: false).account }
  24. let!(:admin_account) { user.account }
  25. let(:params) { {} }
  26. it_behaves_like 'forbidden for wrong scope', 'read read:accounts admin:write admin:write:accounts'
  27. it_behaves_like 'forbidden for wrong role', ''
  28. context 'when requesting active local staff accounts' do
  29. let(:expected_results) { [admin_account] }
  30. let(:params) { { active: 'true', local: 'true', staff: 'true' } }
  31. it_behaves_like 'a successful request'
  32. end
  33. context 'when requesting remote accounts from a specified domain' do
  34. let(:expected_results) { [remote_account] }
  35. let(:params) { { by_domain: 'example.org', remote: 'true' } }
  36. before do
  37. Fabricate(:account, domain: 'foo.bar')
  38. end
  39. it_behaves_like 'a successful request'
  40. end
  41. context 'when requesting suspended accounts' do
  42. let(:expected_results) { [suspended_account] }
  43. let(:params) { { suspended: 'true' } }
  44. before do
  45. Fabricate(:account, domain: 'foo.bar', suspended: true)
  46. end
  47. it_behaves_like 'a successful request'
  48. end
  49. context 'when requesting disabled accounts' do
  50. let(:expected_results) { [disabled_account] }
  51. let(:params) { { disabled: 'true' } }
  52. it_behaves_like 'a successful request'
  53. end
  54. context 'when requesting pending accounts' do
  55. let(:expected_results) { [pending_account] }
  56. let(:params) { { pending: 'true' } }
  57. before do
  58. pending_account.user.update(approved: false)
  59. end
  60. it_behaves_like 'a successful request'
  61. end
  62. context 'when no parameter is given' do
  63. let(:expected_results) { [disabled_account, pending_account, admin_account] }
  64. it_behaves_like 'a successful request'
  65. end
  66. context 'with limit param' do
  67. let(:params) { { limit: 2 } }
  68. it 'returns only the requested number of accounts', :aggregate_failures do
  69. subject
  70. expect(response).to have_http_status(200)
  71. expect(body_as_json.size).to eq(params[:limit])
  72. end
  73. end
  74. end
  75. describe 'GET /api/v1/admin/accounts/:id' do
  76. subject do
  77. get "/api/v1/admin/accounts/#{account.id}", headers: headers
  78. end
  79. let(:account) { Fabricate(:account) }
  80. it_behaves_like 'forbidden for wrong scope', 'read read:accounts admin:write admin:write:accounts'
  81. it_behaves_like 'forbidden for wrong role', ''
  82. it 'returns the requested account successfully', :aggregate_failures do
  83. subject
  84. expect(response).to have_http_status(200)
  85. expect(body_as_json).to match(
  86. a_hash_including(id: account.id.to_s, username: account.username, email: account.user.email)
  87. )
  88. end
  89. context 'when the account is not found' do
  90. it 'returns http not found' do
  91. get '/api/v1/admin/accounts/-1', headers: headers
  92. expect(response).to have_http_status(404)
  93. end
  94. end
  95. end
  96. describe 'POST /api/v1/admin/accounts/:id/approve' do
  97. subject do
  98. post "/api/v1/admin/accounts/#{account.id}/approve", headers: headers
  99. end
  100. let(:account) { Fabricate(:account) }
  101. context 'when the account is pending' do
  102. before do
  103. account.user.update(approved: false)
  104. end
  105. it_behaves_like 'forbidden for wrong scope', 'write write:accounts read admin:read'
  106. it_behaves_like 'forbidden for wrong role', ''
  107. it 'approves the user successfully', :aggregate_failures do
  108. subject
  109. expect(response).to have_http_status(200)
  110. expect(account.reload.user_approved?).to be(true)
  111. end
  112. it 'logs action', :aggregate_failures do
  113. subject
  114. expect(latest_admin_action_log)
  115. .to be_present
  116. .and have_attributes(
  117. action: eq(:approve),
  118. account_id: eq(user.account_id),
  119. target_id: eq(account.user.id)
  120. )
  121. end
  122. end
  123. context 'when the account is already approved' do
  124. it 'returns http forbidden' do
  125. subject
  126. expect(response).to have_http_status(403)
  127. end
  128. end
  129. context 'when the account is not found' do
  130. it 'returns http not found' do
  131. post '/api/v1/admin/accounts/-1/approve', headers: headers
  132. expect(response).to have_http_status(404)
  133. end
  134. end
  135. end
  136. describe 'POST /api/v1/admin/accounts/:id/reject' do
  137. subject do
  138. post "/api/v1/admin/accounts/#{account.id}/reject", headers: headers
  139. end
  140. let(:account) { Fabricate(:account) }
  141. context 'when the account is pending' do
  142. before do
  143. account.user.update(approved: false)
  144. end
  145. it_behaves_like 'forbidden for wrong scope', 'write write:accounts read admin:read'
  146. it_behaves_like 'forbidden for wrong role', ''
  147. it 'removes the user successfully', :aggregate_failures do
  148. subject
  149. expect(response).to have_http_status(200)
  150. expect(User.where(id: account.user.id)).to_not exist
  151. end
  152. it 'logs action', :aggregate_failures do
  153. subject
  154. expect(latest_admin_action_log)
  155. .to be_present
  156. .and have_attributes(
  157. action: eq(:reject),
  158. account_id: eq(user.account_id),
  159. target_id: eq(account.user.id)
  160. )
  161. end
  162. end
  163. context 'when account is already approved' do
  164. it 'returns http forbidden' do
  165. subject
  166. expect(response).to have_http_status(403)
  167. end
  168. end
  169. context 'when the account is not found' do
  170. it 'returns http not found' do
  171. post '/api/v1/admin/accounts/-1/reject', headers: headers
  172. expect(response).to have_http_status(404)
  173. end
  174. end
  175. end
  176. describe 'POST /api/v1/admin/accounts/:id/enable' do
  177. subject do
  178. post "/api/v1/admin/accounts/#{account.id}/enable", headers: headers
  179. end
  180. let(:account) { Fabricate(:account) }
  181. before do
  182. account.user.update(disabled: true)
  183. end
  184. it_behaves_like 'forbidden for wrong scope', 'write write:accounts read admin:read'
  185. it_behaves_like 'forbidden for wrong role', ''
  186. it 'enables the user successfully', :aggregate_failures do
  187. subject
  188. expect(response).to have_http_status(200)
  189. expect(account.reload.user_disabled?).to be false
  190. end
  191. context 'when the account is not found' do
  192. it 'returns http not found' do
  193. post '/api/v1/admin/accounts/-1/enable', headers: headers
  194. expect(response).to have_http_status(404)
  195. end
  196. end
  197. end
  198. describe 'POST /api/v1/admin/accounts/:id/unsuspend' do
  199. subject do
  200. post "/api/v1/admin/accounts/#{account.id}/unsuspend", headers: headers
  201. end
  202. let(:account) { Fabricate(:account) }
  203. context 'when the account is suspended' do
  204. before do
  205. account.suspend!
  206. end
  207. it_behaves_like 'forbidden for wrong scope', 'write write:accounts read admin:read'
  208. it_behaves_like 'forbidden for wrong role', ''
  209. it 'unsuspends the account successfully', :aggregate_failures do
  210. subject
  211. expect(response).to have_http_status(200)
  212. expect(account.reload.suspended?).to be false
  213. end
  214. end
  215. context 'when the account is not suspended' do
  216. it 'returns http forbidden' do
  217. subject
  218. expect(response).to have_http_status(403)
  219. end
  220. end
  221. context 'when the account is not found' do
  222. it 'returns http not found' do
  223. post '/api/v1/admin/accounts/-1/unsuspend', headers: headers
  224. expect(response).to have_http_status(404)
  225. end
  226. end
  227. end
  228. describe 'POST /api/v1/admin/accounts/:id/unsensitive' do
  229. subject do
  230. post "/api/v1/admin/accounts/#{account.id}/unsensitive", headers: headers
  231. end
  232. let(:account) { Fabricate(:account) }
  233. before do
  234. account.update(sensitized_at: 10.days.ago)
  235. end
  236. it_behaves_like 'forbidden for wrong scope', 'write write:accounts read admin:read'
  237. it_behaves_like 'forbidden for wrong role', ''
  238. it 'unsensitizes the account successfully', :aggregate_failures do
  239. subject
  240. expect(response).to have_http_status(200)
  241. expect(account.reload.sensitized?).to be false
  242. end
  243. context 'when the account is not found' do
  244. it 'returns http not found' do
  245. post '/api/v1/admin/accounts/-1/unsensitive', headers: headers
  246. expect(response).to have_http_status(404)
  247. end
  248. end
  249. end
  250. describe 'POST /api/v1/admin/accounts/:id/unsilence' do
  251. subject do
  252. post "/api/v1/admin/accounts/#{account.id}/unsilence", headers: headers
  253. end
  254. let(:account) { Fabricate(:account) }
  255. before do
  256. account.update(silenced_at: 3.days.ago)
  257. end
  258. it_behaves_like 'forbidden for wrong scope', 'write write:accounts read admin:read'
  259. it_behaves_like 'forbidden for wrong role', ''
  260. it 'unsilences the account successfully', :aggregate_failures do
  261. subject
  262. expect(response).to have_http_status(200)
  263. expect(account.reload.silenced?).to be false
  264. end
  265. context 'when the account is not found' do
  266. it 'returns http not found' do
  267. post '/api/v1/admin/accounts/-1/unsilence', headers: headers
  268. expect(response).to have_http_status(404)
  269. end
  270. end
  271. end
  272. describe 'DELETE /api/v1/admin/accounts/:id' do
  273. subject do
  274. delete "/api/v1/admin/accounts/#{account.id}", headers: headers
  275. end
  276. let(:account) { Fabricate(:account) }
  277. context 'when account is suspended' do
  278. before do
  279. account.suspend!
  280. end
  281. it_behaves_like 'forbidden for wrong scope', 'write write:accounts read admin:read'
  282. it_behaves_like 'forbidden for wrong role', ''
  283. it 'deletes the account successfully', :aggregate_failures do
  284. allow(Admin::AccountDeletionWorker).to receive(:perform_async)
  285. subject
  286. expect(response).to have_http_status(200)
  287. expect(Admin::AccountDeletionWorker).to have_received(:perform_async).with(account.id).once
  288. end
  289. end
  290. context 'when account is not suspended' do
  291. it 'returns http forbidden' do
  292. subject
  293. expect(response).to have_http_status(403)
  294. end
  295. end
  296. context 'when the account is not found' do
  297. it 'returns http not found' do
  298. delete '/api/v1/admin/accounts/-1', headers: headers
  299. expect(response).to have_http_status(404)
  300. end
  301. end
  302. end
  303. private
  304. def latest_admin_action_log
  305. Admin::ActionLog.last
  306. end
  307. end