You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

291 lines
7.6 KiB

  1. require 'rails_helper'
  2. RSpec.describe Admin::AccountsController, type: :controller do
  3. render_views
  4. before { sign_in current_user, scope: :user }
  5. describe 'GET #index' do
  6. let(:current_user) { Fabricate(:user, admin: true) }
  7. around do |example|
  8. default_per_page = Account.default_per_page
  9. Account.paginates_per 1
  10. example.run
  11. Account.paginates_per default_per_page
  12. end
  13. it 'filters with parameters' do
  14. new = AccountFilter.method(:new)
  15. expect(AccountFilter).to receive(:new) do |params|
  16. h = params.to_h
  17. expect(h[:local]).to eq '1'
  18. expect(h[:remote]).to eq '1'
  19. expect(h[:by_domain]).to eq 'domain'
  20. expect(h[:silenced]).to eq '1'
  21. expect(h[:recent]).to eq '1'
  22. expect(h[:suspended]).to eq '1'
  23. expect(h[:username]).to eq 'username'
  24. expect(h[:display_name]).to eq 'display name'
  25. expect(h[:email]).to eq 'local-part@domain'
  26. expect(h[:ip]).to eq '0.0.0.42'
  27. new.call({})
  28. end
  29. get :index, params: {
  30. local: '1',
  31. remote: '1',
  32. by_domain: 'domain',
  33. silenced: '1',
  34. recent: '1',
  35. suspended: '1',
  36. username: 'username',
  37. display_name: 'display name',
  38. email: 'local-part@domain',
  39. ip: '0.0.0.42'
  40. }
  41. end
  42. it 'paginates accounts' do
  43. Fabricate(:account)
  44. get :index, params: { page: 2 }
  45. accounts = assigns(:accounts)
  46. expect(accounts.count).to eq 1
  47. expect(accounts.klass).to be Account
  48. end
  49. it 'returns http success' do
  50. get :index
  51. expect(response).to have_http_status(200)
  52. end
  53. end
  54. describe 'GET #show' do
  55. let(:current_user) { Fabricate(:user, admin: true) }
  56. let(:account) { Fabricate(:account, username: 'bob') }
  57. it 'returns http success' do
  58. get :show, params: { id: account.id }
  59. expect(response).to have_http_status(200)
  60. end
  61. end
  62. describe 'POST #subscribe' do
  63. subject { post :subscribe, params: { id: account.id } }
  64. let(:current_user) { Fabricate(:user, admin: admin) }
  65. let(:account) { Fabricate(:account) }
  66. context 'when user is admin' do
  67. let(:admin) { true }
  68. it { is_expected.to redirect_to admin_account_path(account.id) }
  69. end
  70. context 'when user is not admin' do
  71. let(:admin) { false }
  72. it { is_expected.to have_http_status :forbidden }
  73. end
  74. end
  75. describe 'POST #unsubscribe' do
  76. subject { post :unsubscribe, params: { id: account.id } }
  77. let(:current_user) { Fabricate(:user, admin: admin) }
  78. let(:account) { Fabricate(:account) }
  79. context 'when user is admin' do
  80. let(:admin) { true }
  81. it { is_expected.to redirect_to admin_account_path(account.id) }
  82. end
  83. context 'when user is not admin' do
  84. let(:admin) { false }
  85. it { is_expected.to have_http_status :forbidden }
  86. end
  87. end
  88. describe 'POST #memorialize' do
  89. subject { post :memorialize, params: { id: account.id } }
  90. let(:current_user) { Fabricate(:user, admin: current_user_admin) }
  91. let(:account) { Fabricate(:account, user: user) }
  92. let(:user) { Fabricate(:user, admin: target_user_admin) }
  93. context 'when user is admin' do
  94. let(:current_user_admin) { true }
  95. context 'when target user is admin' do
  96. let(:target_user_admin) { true }
  97. it 'fails to memorialize account' do
  98. is_expected.to have_http_status :forbidden
  99. expect(account.reload).not_to be_memorial
  100. end
  101. end
  102. context 'when target user is not admin' do
  103. let(:target_user_admin) { false }
  104. it 'succeeds in memorializing account' do
  105. is_expected.to redirect_to admin_account_path(account.id)
  106. expect(account.reload).to be_memorial
  107. end
  108. end
  109. end
  110. context 'when user is not admin' do
  111. let(:current_user_admin) { false }
  112. context 'when target user is admin' do
  113. let(:target_user_admin) { true }
  114. it 'fails to memorialize account' do
  115. is_expected.to have_http_status :forbidden
  116. expect(account.reload).not_to be_memorial
  117. end
  118. end
  119. context 'when target user is not admin' do
  120. let(:target_user_admin) { false }
  121. it 'fails to memorialize account' do
  122. is_expected.to have_http_status :forbidden
  123. expect(account.reload).not_to be_memorial
  124. end
  125. end
  126. end
  127. end
  128. describe 'POST #enable' do
  129. subject { post :enable, params: { id: account.id } }
  130. let(:current_user) { Fabricate(:user, admin: admin) }
  131. let(:account) { Fabricate(:account, user: user) }
  132. let(:user) { Fabricate(:user, disabled: true) }
  133. context 'when user is admin' do
  134. let(:admin) { true }
  135. it 'succeeds in enabling account' do
  136. is_expected.to redirect_to admin_account_path(account.id)
  137. expect(user.reload).not_to be_disabled
  138. end
  139. end
  140. context 'when user is not admin' do
  141. let(:admin) { false }
  142. it 'fails to enable account' do
  143. is_expected.to have_http_status :forbidden
  144. expect(user.reload).to be_disabled
  145. end
  146. end
  147. end
  148. describe 'POST #disable' do
  149. subject { post :disable, params: { id: account.id } }
  150. let(:current_user) { Fabricate(:user, admin: current_user_admin) }
  151. let(:account) { Fabricate(:account, user: user) }
  152. let(:user) { Fabricate(:user, disabled: false, admin: target_user_admin) }
  153. context 'when user is admin' do
  154. let(:current_user_admin) { true }
  155. context 'when target user is admin' do
  156. let(:target_user_admin) { true }
  157. it 'fails to disable account' do
  158. is_expected.to have_http_status :forbidden
  159. expect(user.reload).not_to be_disabled
  160. end
  161. end
  162. context 'when target user is not admin' do
  163. let(:target_user_admin) { false }
  164. it 'succeeds in disabling account' do
  165. is_expected.to redirect_to admin_account_path(account.id)
  166. expect(user.reload).to be_disabled
  167. end
  168. end
  169. end
  170. context 'when user is not admin' do
  171. let(:current_user_admin) { false }
  172. context 'when target user is admin' do
  173. let(:target_user_admin) { true }
  174. it 'fails to disable account' do
  175. is_expected.to have_http_status :forbidden
  176. expect(user.reload).not_to be_disabled
  177. end
  178. end
  179. context 'when target user is not admin' do
  180. let(:target_user_admin) { false }
  181. it 'fails to disable account' do
  182. is_expected.to have_http_status :forbidden
  183. expect(user.reload).not_to be_disabled
  184. end
  185. end
  186. end
  187. end
  188. describe 'POST #redownload' do
  189. subject { post :redownload, params: { id: account.id } }
  190. let(:current_user) { Fabricate(:user, admin: admin) }
  191. let(:account) { Fabricate(:account) }
  192. context 'when user is admin' do
  193. let(:admin) { true }
  194. it 'succeeds in redownloadin' do
  195. is_expected.to redirect_to admin_account_path(account.id)
  196. end
  197. end
  198. context 'when user is not admin' do
  199. let(:admin) { false }
  200. it 'fails to redownload' do
  201. is_expected.to have_http_status :forbidden
  202. end
  203. end
  204. end
  205. describe 'POST #remove_avatar' do
  206. subject { post :remove_avatar, params: { id: account.id } }
  207. let(:current_user) { Fabricate(:user, admin: admin) }
  208. let(:account) { Fabricate(:account) }
  209. context 'when user is admin' do
  210. let(:admin) { true }
  211. it 'succeeds in removing avatar' do
  212. is_expected.to redirect_to admin_account_path(account.id)
  213. end
  214. end
  215. context 'when user is not admin' do
  216. let(:admin) { false }
  217. it 'fails to remove avatar' do
  218. is_expected.to have_http_status :forbidden
  219. end
  220. end
  221. end
  222. end