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.

239 lines
6.2 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[:active]).to eq '1'
  21. expect(h[:silenced]).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. active: '1',
  34. silenced: '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 #redownload' do
  149. subject { post :redownload, params: { id: account.id } }
  150. let(:current_user) { Fabricate(:user, admin: admin) }
  151. let(:account) { Fabricate(:account) }
  152. context 'when user is admin' do
  153. let(:admin) { true }
  154. it 'succeeds in redownloadin' do
  155. is_expected.to redirect_to admin_account_path(account.id)
  156. end
  157. end
  158. context 'when user is not admin' do
  159. let(:admin) { false }
  160. it 'fails to redownload' do
  161. is_expected.to have_http_status :forbidden
  162. end
  163. end
  164. end
  165. describe 'POST #remove_avatar' do
  166. subject { post :remove_avatar, params: { id: account.id } }
  167. let(:current_user) { Fabricate(:user, admin: admin) }
  168. let(:account) { Fabricate(:account) }
  169. context 'when user is admin' do
  170. let(:admin) { true }
  171. it 'succeeds in removing avatar' do
  172. is_expected.to redirect_to admin_account_path(account.id)
  173. end
  174. end
  175. context 'when user is not admin' do
  176. let(:admin) { false }
  177. it 'fails to remove avatar' do
  178. is_expected.to have_http_status :forbidden
  179. end
  180. end
  181. end
  182. end