accounts_controller_spec.rb 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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(assigns(:accounts))
  35. .to have_attributes(
  36. count: eq(1),
  37. klass: be(Account)
  38. )
  39. expect(AccountFilter)
  40. .to have_received(:new)
  41. .with(hash_including(params))
  42. end
  43. end
  44. describe 'GET #show' do
  45. let(:current_user) { Fabricate(:user, role: UserRole.find_by(name: 'Admin')) }
  46. let(:account) { Fabricate(:account) }
  47. it 'returns http success' do
  48. get :show, params: { id: account.id }
  49. expect(response).to have_http_status(200)
  50. end
  51. end
  52. describe 'POST #memorialize' do
  53. subject { post :memorialize, params: { id: account.id } }
  54. let(:current_user) { Fabricate(:user, role: current_role) }
  55. let(:account) { user.account }
  56. let(:user) { Fabricate(:user, role: target_role) }
  57. context 'when user is admin' do
  58. let(:current_role) { UserRole.find_by(name: 'Admin') }
  59. context 'when target user is admin' do
  60. let(:target_role) { UserRole.find_by(name: 'Admin') }
  61. it 'fails to memorialize account' do
  62. expect(subject).to have_http_status 403
  63. expect(account.reload).to_not be_memorial
  64. end
  65. end
  66. context 'when target user is not admin' do
  67. let(:target_role) { UserRole.find_by(name: 'Moderator') }
  68. it 'succeeds in memorializing account' do
  69. expect(subject).to redirect_to admin_account_path(account.id)
  70. expect(account.reload).to be_memorial
  71. end
  72. end
  73. end
  74. context 'when user is not admin' do
  75. let(:current_role) { UserRole.find_by(name: 'Moderator') }
  76. context 'when target user is admin' do
  77. let(:target_role) { UserRole.find_by(name: 'Admin') }
  78. it 'fails to memorialize account' do
  79. expect(subject).to have_http_status 403
  80. expect(account.reload).to_not be_memorial
  81. end
  82. end
  83. context 'when target user is not admin' do
  84. let(:target_role) { UserRole.find_by(name: 'Moderator') }
  85. it 'fails to memorialize account' do
  86. expect(subject).to have_http_status 403
  87. expect(account.reload).to_not be_memorial
  88. end
  89. end
  90. end
  91. end
  92. describe 'POST #enable' do
  93. subject { post :enable, params: { id: account.id } }
  94. let(:current_user) { Fabricate(:user, role: role) }
  95. let(:account) { user.account }
  96. let(:user) { Fabricate(:user, disabled: true) }
  97. context 'when user is admin' do
  98. let(:role) { UserRole.find_by(name: 'Admin') }
  99. it 'succeeds in enabling account' do
  100. expect(subject).to redirect_to admin_account_path(account.id)
  101. expect(user.reload).to_not be_disabled
  102. end
  103. end
  104. context 'when user is not admin' do
  105. let(:role) { UserRole.everyone }
  106. it 'fails to enable account' do
  107. expect(subject).to have_http_status 403
  108. expect(user.reload).to be_disabled
  109. end
  110. end
  111. end
  112. describe 'POST #approve' do
  113. subject { post :approve, params: { id: account.id } }
  114. let(:current_user) { Fabricate(:user, role: role) }
  115. let(:account) { user.account }
  116. let(:user) { Fabricate(:user) }
  117. before do
  118. account.user.update(approved: false)
  119. end
  120. context 'when user is admin' do
  121. let(:role) { UserRole.find_by(name: 'Admin') }
  122. it 'succeeds in approving account and logs action' do
  123. expect(subject).to redirect_to admin_accounts_path(status: 'pending')
  124. expect(user.reload).to be_approved
  125. expect(latest_admin_action_log)
  126. .to be_present
  127. .and have_attributes(
  128. action: eq(:approve),
  129. account_id: eq(current_user.account_id),
  130. target_id: eq(account.user.id)
  131. )
  132. end
  133. end
  134. context 'when user is not admin' do
  135. let(:role) { UserRole.everyone }
  136. it 'fails to approve account' do
  137. expect(subject).to have_http_status 403
  138. expect(user.reload).to_not be_approved
  139. end
  140. end
  141. end
  142. describe 'POST #reject' do
  143. subject { post :reject, params: { id: account.id } }
  144. let(:current_user) { Fabricate(:user, role: role) }
  145. let(:account) { user.account }
  146. let(:user) { Fabricate(:user) }
  147. before do
  148. account.user.update(approved: false)
  149. end
  150. context 'when user is admin' do
  151. let(:role) { UserRole.find_by(name: 'Admin') }
  152. it 'succeeds in rejecting account and logs action' do
  153. expect(subject).to redirect_to admin_accounts_path(status: 'pending')
  154. expect(latest_admin_action_log)
  155. .to be_present
  156. .and have_attributes(
  157. action: eq(:reject),
  158. account_id: eq(current_user.account_id),
  159. target_id: eq(account.user.id)
  160. )
  161. end
  162. end
  163. context 'when user is not admin' do
  164. let(:role) { UserRole.everyone }
  165. it 'fails to reject account' do
  166. expect(subject).to have_http_status 403
  167. expect(user.reload).to_not be_approved
  168. end
  169. end
  170. end
  171. describe 'POST #redownload' do
  172. subject { post :redownload, params: { id: account.id } }
  173. let(:current_user) { Fabricate(:user, role: role) }
  174. let(:account) { Fabricate(:account, domain: 'example.com') }
  175. before do
  176. service = instance_double(ResolveAccountService, call: nil)
  177. allow(ResolveAccountService).to receive(:new).and_return(service)
  178. end
  179. context 'when user is admin' do
  180. let(:role) { UserRole.find_by(name: 'Admin') }
  181. it 'succeeds in redownloading' do
  182. expect(subject).to redirect_to admin_account_path(account.id)
  183. end
  184. end
  185. context 'when user is not admin' do
  186. let(:role) { UserRole.everyone }
  187. it 'fails to redownload' do
  188. expect(subject).to have_http_status 403
  189. end
  190. end
  191. end
  192. describe 'POST #remove_avatar' do
  193. subject { post :remove_avatar, params: { id: account.id } }
  194. let(:current_user) { Fabricate(:user, role: role) }
  195. let(:account) { Fabricate(:account) }
  196. context 'when user is admin' do
  197. let(:role) { UserRole.find_by(name: 'Admin') }
  198. it 'succeeds in removing avatar' do
  199. expect(subject).to redirect_to admin_account_path(account.id)
  200. end
  201. end
  202. context 'when user is not admin' do
  203. let(:role) { UserRole.everyone }
  204. it 'fails to remove avatar' do
  205. expect(subject).to have_http_status 403
  206. end
  207. end
  208. end
  209. describe 'POST #unblock_email' do
  210. subject { post :unblock_email, params: { id: account.id } }
  211. let(:current_user) { Fabricate(:user, role: role) }
  212. let(:account) { Fabricate(:account, suspended: true) }
  213. before do
  214. _email_block = Fabricate(:canonical_email_block, reference_account: account)
  215. end
  216. context 'when user is admin' do
  217. let(:role) { UserRole.find_by(name: 'Admin') }
  218. it 'succeeds in removing email blocks and redirects to admin account path' do
  219. expect { subject }.to change { CanonicalEmailBlock.where(reference_account: account).count }.from(1).to(0)
  220. expect(response).to redirect_to admin_account_path(account.id)
  221. end
  222. end
  223. context 'when user is not admin' do
  224. let(:role) { UserRole.everyone }
  225. it 'fails to remove avatar' do
  226. subject
  227. expect(response).to have_http_status 403
  228. end
  229. end
  230. end
  231. describe 'POST #unsensitive' do
  232. subject { post :unsensitive, params: { id: account.id } }
  233. let(:current_user) { Fabricate(:user, role: role) }
  234. let(:account) { Fabricate(:account, sensitized_at: 1.year.ago) }
  235. context 'when user is admin' do
  236. let(:role) { UserRole.find_by(name: 'Admin') }
  237. it 'marks accounts not sensitized' do
  238. subject
  239. expect(account.reload).to_not be_sensitized
  240. expect(response).to redirect_to admin_account_path(account.id)
  241. end
  242. end
  243. context 'when user is not admin' do
  244. let(:role) { UserRole.everyone }
  245. it 'fails to change account' do
  246. subject
  247. expect(response).to have_http_status 403
  248. end
  249. end
  250. end
  251. describe 'POST #unsilence' do
  252. subject { post :unsilence, params: { id: account.id } }
  253. let(:current_user) { Fabricate(:user, role: role) }
  254. let(:account) { Fabricate(:account, silenced_at: 1.year.ago) }
  255. context 'when user is admin' do
  256. let(:role) { UserRole.find_by(name: 'Admin') }
  257. it 'marks accounts not silenced' do
  258. subject
  259. expect(account.reload).to_not be_silenced
  260. expect(response).to redirect_to admin_account_path(account.id)
  261. end
  262. end
  263. context 'when user is not admin' do
  264. let(:role) { UserRole.everyone }
  265. it 'fails to change account' do
  266. subject
  267. expect(response).to have_http_status 403
  268. end
  269. end
  270. end
  271. describe 'POST #unsuspend' do
  272. subject { post :unsuspend, params: { id: account.id } }
  273. let(:current_user) { Fabricate(:user, role: role) }
  274. let(:account) { Fabricate(:account) }
  275. before do
  276. account.suspend!
  277. end
  278. context 'when user is admin' do
  279. let(:role) { UserRole.find_by(name: 'Admin') }
  280. it 'marks accounts not suspended' do
  281. subject
  282. expect(account.reload).to_not be_suspended
  283. expect(response).to redirect_to admin_account_path(account.id)
  284. end
  285. end
  286. context 'when user is not admin' do
  287. let(:role) { UserRole.everyone }
  288. it 'fails to change account' do
  289. subject
  290. expect(response).to have_http_status 403
  291. end
  292. end
  293. end
  294. describe 'POST #destroy' do
  295. subject { post :destroy, params: { id: account.id } }
  296. let(:current_user) { Fabricate(:user, role: role) }
  297. let(:account) { Fabricate(:account) }
  298. before do
  299. account.suspend!
  300. end
  301. context 'when user is admin' do
  302. let(:role) { UserRole.find_by(name: 'Admin') }
  303. before do
  304. allow(Admin::AccountDeletionWorker).to receive(:perform_async).with(account.id)
  305. end
  306. it 'destroys the account' do
  307. subject
  308. expect(Admin::AccountDeletionWorker).to have_received(:perform_async).with(account.id)
  309. expect(response).to redirect_to admin_account_path(account.id)
  310. end
  311. end
  312. context 'when user is not admin' do
  313. let(:role) { UserRole.everyone }
  314. it 'fails to change account' do
  315. subject
  316. expect(response).to have_http_status 403
  317. end
  318. end
  319. end
  320. private
  321. def latest_admin_action_log
  322. Admin::ActionLog.last
  323. end
  324. end