accounts_controller_spec.rb 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe Admin::AccountsController do
  4. render_views
  5. before { sign_in current_user, scope: :user }
  6. describe 'GET #index' do
  7. let(:current_user) { Fabricate(:user, role: UserRole.find_by(name: 'Admin')) }
  8. let(:params) do
  9. {
  10. origin: 'local',
  11. by_domain: 'domain',
  12. status: 'active',
  13. username: 'username',
  14. display_name: 'display name',
  15. email: 'local-part@domain',
  16. ip: '0.0.0.42',
  17. }
  18. end
  19. around do |example|
  20. default_per_page = Account.default_per_page
  21. Account.paginates_per 1
  22. example.run
  23. Account.paginates_per default_per_page
  24. end
  25. before do
  26. Fabricate(:account)
  27. account_filter = instance_double(AccountFilter, results: Account.all)
  28. allow(AccountFilter).to receive(:new).and_return(account_filter)
  29. end
  30. it 'returns success and paginates and filters with parameters' do
  31. get :index, params: params.merge(page: 2)
  32. expect(response)
  33. .to have_http_status(200)
  34. expect(accounts_table_rows.size)
  35. .to eq(1)
  36. expect(AccountFilter)
  37. .to have_received(:new)
  38. .with(hash_including(params))
  39. end
  40. def accounts_table_rows
  41. response.parsed_body.css('table.accounts-table tr')
  42. end
  43. end
  44. describe 'GET #show' do
  45. let(:current_user) { Fabricate(:user, role: UserRole.find_by(name: 'Admin')) }
  46. describe 'account moderation notes' do
  47. let(:account) { Fabricate(:account) }
  48. it 'includes moderation notes' do
  49. note1 = Fabricate(:account_moderation_note, target_account: account)
  50. note2 = Fabricate(:account_moderation_note, target_account: account)
  51. get :show, params: { id: account.id }
  52. expect(response).to have_http_status(200)
  53. moderation_notes = assigns(:moderation_notes).to_a
  54. expect(moderation_notes.size).to be 2
  55. expect(moderation_notes).to eq [note1, note2]
  56. end
  57. end
  58. context 'with a remote account' do
  59. let(:account) { Fabricate(:account, domain: 'example.com') }
  60. it 'returns http success' do
  61. get :show, params: { id: account.id }
  62. expect(response).to have_http_status(200)
  63. end
  64. end
  65. context 'with a local account' do
  66. let(:account) { Fabricate(:account, domain: nil) }
  67. it 'returns http success' do
  68. get :show, params: { id: account.id }
  69. expect(response).to have_http_status(200)
  70. end
  71. end
  72. context 'with a local deleted account' do
  73. let(:account) { Fabricate(:account, domain: nil, user: nil) }
  74. it 'returns http success' do
  75. get :show, params: { id: account.id }
  76. expect(response).to have_http_status(200)
  77. end
  78. end
  79. end
  80. describe 'POST #memorialize' do
  81. subject { post :memorialize, params: { id: account.id } }
  82. let(:current_user) { Fabricate(:user, role: current_role) }
  83. let(:account) { user.account }
  84. let(:user) { Fabricate(:user, role: target_role) }
  85. context 'when user is admin' do
  86. let(:current_role) { UserRole.find_by(name: 'Admin') }
  87. context 'when target user is admin' do
  88. let(:target_role) { UserRole.find_by(name: 'Admin') }
  89. it 'fails to memorialize account' do
  90. expect(subject).to have_http_status 403
  91. expect(account.reload).to_not be_memorial
  92. end
  93. end
  94. context 'when target user is not admin' do
  95. let(:target_role) { UserRole.find_by(name: 'Moderator') }
  96. it 'succeeds in memorializing account' do
  97. expect(subject).to redirect_to admin_account_path(account.id)
  98. expect(account.reload).to be_memorial
  99. end
  100. end
  101. end
  102. context 'when user is not admin' do
  103. let(:current_role) { UserRole.find_by(name: 'Moderator') }
  104. context 'when target user is admin' do
  105. let(:target_role) { UserRole.find_by(name: 'Admin') }
  106. it 'fails to memorialize account' do
  107. expect(subject).to have_http_status 403
  108. expect(account.reload).to_not be_memorial
  109. end
  110. end
  111. context 'when target user is not admin' do
  112. let(:target_role) { UserRole.find_by(name: 'Moderator') }
  113. it 'fails to memorialize account' do
  114. expect(subject).to have_http_status 403
  115. expect(account.reload).to_not be_memorial
  116. end
  117. end
  118. end
  119. end
  120. describe 'POST #enable' do
  121. subject { post :enable, params: { id: account.id } }
  122. let(:current_user) { Fabricate(:user, role: role) }
  123. let(:account) { user.account }
  124. let(:user) { Fabricate(:user, disabled: true) }
  125. context 'when user is admin' do
  126. let(:role) { UserRole.find_by(name: 'Admin') }
  127. it 'succeeds in enabling account' do
  128. expect(subject).to redirect_to admin_account_path(account.id)
  129. expect(user.reload).to_not be_disabled
  130. end
  131. end
  132. context 'when user is not admin' do
  133. let(:role) { UserRole.everyone }
  134. it 'fails to enable account' do
  135. expect(subject).to have_http_status 403
  136. expect(user.reload).to be_disabled
  137. end
  138. end
  139. end
  140. describe 'POST #approve' do
  141. subject { post :approve, params: { id: account.id } }
  142. let(:current_user) { Fabricate(:user, role: role) }
  143. let(:account) { user.account }
  144. let(:user) { Fabricate(:user) }
  145. before do
  146. account.user.update(approved: false)
  147. end
  148. context 'when user is admin' do
  149. let(:role) { UserRole.find_by(name: 'Admin') }
  150. it 'succeeds in approving account and logs action' do
  151. expect(subject).to redirect_to admin_accounts_path(status: 'pending')
  152. expect(user.reload).to be_approved
  153. expect(latest_admin_action_log)
  154. .to be_present
  155. .and have_attributes(
  156. action: eq(:approve),
  157. account_id: eq(current_user.account_id),
  158. target_id: eq(account.user.id)
  159. )
  160. end
  161. end
  162. context 'when user is not admin' do
  163. let(:role) { UserRole.everyone }
  164. it 'fails to approve account' do
  165. expect(subject).to have_http_status 403
  166. expect(user.reload).to_not be_approved
  167. end
  168. end
  169. end
  170. describe 'POST #reject' do
  171. subject { post :reject, params: { id: account.id } }
  172. let(:current_user) { Fabricate(:user, role: role) }
  173. let(:account) { user.account }
  174. let(:user) { Fabricate(:user) }
  175. before do
  176. account.user.update(approved: false)
  177. end
  178. context 'when user is admin' do
  179. let(:role) { UserRole.find_by(name: 'Admin') }
  180. it 'succeeds in rejecting account and logs action' do
  181. expect(subject).to redirect_to admin_accounts_path(status: 'pending')
  182. expect(latest_admin_action_log)
  183. .to be_present
  184. .and have_attributes(
  185. action: eq(:reject),
  186. account_id: eq(current_user.account_id),
  187. target_id: eq(account.user.id)
  188. )
  189. end
  190. end
  191. context 'when user is not admin' do
  192. let(:role) { UserRole.everyone }
  193. it 'fails to reject account' do
  194. expect(subject).to have_http_status 403
  195. expect(user.reload).to_not be_approved
  196. end
  197. end
  198. end
  199. describe 'POST #redownload' do
  200. subject { post :redownload, params: { id: account.id } }
  201. let(:current_user) { Fabricate(:user, role: role) }
  202. let(:account) { Fabricate(:account, domain: 'example.com') }
  203. before do
  204. service = instance_double(ResolveAccountService, call: nil)
  205. allow(ResolveAccountService).to receive(:new).and_return(service)
  206. end
  207. context 'when user is admin' do
  208. let(:role) { UserRole.find_by(name: 'Admin') }
  209. it 'succeeds in redownloading' do
  210. expect(subject).to redirect_to admin_account_path(account.id)
  211. end
  212. end
  213. context 'when user is not admin' do
  214. let(:role) { UserRole.everyone }
  215. it 'fails to redownload' do
  216. expect(subject).to have_http_status 403
  217. end
  218. end
  219. end
  220. describe 'POST #remove_avatar' do
  221. subject { post :remove_avatar, params: { id: account.id } }
  222. let(:current_user) { Fabricate(:user, role: role) }
  223. let(:account) { Fabricate(:account) }
  224. context 'when user is admin' do
  225. let(:role) { UserRole.find_by(name: 'Admin') }
  226. it 'succeeds in removing avatar' do
  227. expect(subject).to redirect_to admin_account_path(account.id)
  228. end
  229. end
  230. context 'when user is not admin' do
  231. let(:role) { UserRole.everyone }
  232. it 'fails to remove avatar' do
  233. expect(subject).to have_http_status 403
  234. end
  235. end
  236. end
  237. describe 'POST #unblock_email' do
  238. subject { post :unblock_email, params: { id: account.id } }
  239. let(:current_user) { Fabricate(:user, role: role) }
  240. let(:account) { Fabricate(:account, suspended: true) }
  241. before do
  242. _email_block = Fabricate(:canonical_email_block, reference_account: account)
  243. end
  244. context 'when user is admin' do
  245. let(:role) { UserRole.find_by(name: 'Admin') }
  246. it 'succeeds in removing email blocks and redirects to admin account path' do
  247. expect { subject }.to change { CanonicalEmailBlock.where(reference_account: account).count }.from(1).to(0)
  248. expect(response).to redirect_to admin_account_path(account.id)
  249. end
  250. end
  251. context 'when user is not admin' do
  252. let(:role) { UserRole.everyone }
  253. it 'fails to remove avatar' do
  254. subject
  255. expect(response).to have_http_status 403
  256. end
  257. end
  258. end
  259. describe 'POST #unsensitive' do
  260. subject { post :unsensitive, params: { id: account.id } }
  261. let(:current_user) { Fabricate(:user, role: role) }
  262. let(:account) { Fabricate(:account, sensitized_at: 1.year.ago) }
  263. context 'when user is admin' do
  264. let(:role) { UserRole.find_by(name: 'Admin') }
  265. it 'marks accounts not sensitized' do
  266. subject
  267. expect(account.reload).to_not be_sensitized
  268. expect(response).to redirect_to admin_account_path(account.id)
  269. end
  270. end
  271. context 'when user is not admin' do
  272. let(:role) { UserRole.everyone }
  273. it 'fails to change account' do
  274. subject
  275. expect(response).to have_http_status 403
  276. end
  277. end
  278. end
  279. describe 'POST #unsilence' do
  280. subject { post :unsilence, params: { id: account.id } }
  281. let(:current_user) { Fabricate(:user, role: role) }
  282. let(:account) { Fabricate(:account, silenced_at: 1.year.ago) }
  283. context 'when user is admin' do
  284. let(:role) { UserRole.find_by(name: 'Admin') }
  285. it 'marks accounts not silenced' do
  286. subject
  287. expect(account.reload).to_not be_silenced
  288. expect(response).to redirect_to admin_account_path(account.id)
  289. end
  290. end
  291. context 'when user is not admin' do
  292. let(:role) { UserRole.everyone }
  293. it 'fails to change account' do
  294. subject
  295. expect(response).to have_http_status 403
  296. end
  297. end
  298. end
  299. describe 'POST #unsuspend' do
  300. subject { post :unsuspend, params: { id: account.id } }
  301. let(:current_user) { Fabricate(:user, role: role) }
  302. let(:account) { Fabricate(:account) }
  303. before do
  304. account.suspend!
  305. end
  306. context 'when user is admin' do
  307. let(:role) { UserRole.find_by(name: 'Admin') }
  308. it 'marks accounts not suspended' do
  309. subject
  310. expect(account.reload).to_not be_suspended
  311. expect(response).to redirect_to admin_account_path(account.id)
  312. end
  313. end
  314. context 'when user is not admin' do
  315. let(:role) { UserRole.everyone }
  316. it 'fails to change account' do
  317. subject
  318. expect(response).to have_http_status 403
  319. end
  320. end
  321. end
  322. describe 'POST #destroy' do
  323. subject { post :destroy, params: { id: account.id } }
  324. let(:current_user) { Fabricate(:user, role: role) }
  325. let(:account) { Fabricate(:account) }
  326. before do
  327. account.suspend!
  328. end
  329. context 'when user is admin' do
  330. let(:role) { UserRole.find_by(name: 'Admin') }
  331. before do
  332. allow(Admin::AccountDeletionWorker).to receive(:perform_async).with(account.id)
  333. end
  334. it 'destroys the account' do
  335. subject
  336. expect(Admin::AccountDeletionWorker).to have_received(:perform_async).with(account.id)
  337. expect(response).to redirect_to admin_account_path(account.id)
  338. end
  339. end
  340. context 'when user is not admin' do
  341. let(:role) { UserRole.everyone }
  342. it 'fails to change account' do
  343. subject
  344. expect(response).to have_http_status 403
  345. end
  346. end
  347. end
  348. private
  349. def latest_admin_action_log
  350. Admin::ActionLog.last
  351. end
  352. end