accounts_controller_spec.rb 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 #memorialize' do
  63. subject { post :memorialize, params: { id: account.id } }
  64. let(:current_user) { Fabricate(:user, admin: current_user_admin) }
  65. let(:account) { Fabricate(:account, user: user) }
  66. let(:user) { Fabricate(:user, admin: target_user_admin) }
  67. context 'when user is admin' do
  68. let(:current_user_admin) { true }
  69. context 'when target user is admin' do
  70. let(:target_user_admin) { true }
  71. it 'fails to memorialize account' do
  72. is_expected.to have_http_status :forbidden
  73. expect(account.reload).not_to be_memorial
  74. end
  75. end
  76. context 'when target user is not admin' do
  77. let(:target_user_admin) { false }
  78. it 'succeeds in memorializing account' do
  79. is_expected.to redirect_to admin_account_path(account.id)
  80. expect(account.reload).to be_memorial
  81. end
  82. end
  83. end
  84. context 'when user is not admin' do
  85. let(:current_user_admin) { false }
  86. context 'when target user is admin' do
  87. let(:target_user_admin) { true }
  88. it 'fails to memorialize account' do
  89. is_expected.to have_http_status :forbidden
  90. expect(account.reload).not_to be_memorial
  91. end
  92. end
  93. context 'when target user is not admin' do
  94. let(:target_user_admin) { false }
  95. it 'fails to memorialize account' do
  96. is_expected.to have_http_status :forbidden
  97. expect(account.reload).not_to be_memorial
  98. end
  99. end
  100. end
  101. end
  102. describe 'POST #enable' do
  103. subject { post :enable, params: { id: account.id } }
  104. let(:current_user) { Fabricate(:user, admin: admin) }
  105. let(:account) { Fabricate(:account, user: user) }
  106. let(:user) { Fabricate(:user, disabled: true) }
  107. context 'when user is admin' do
  108. let(:admin) { true }
  109. it 'succeeds in enabling account' do
  110. is_expected.to redirect_to admin_account_path(account.id)
  111. expect(user.reload).not_to be_disabled
  112. end
  113. end
  114. context 'when user is not admin' do
  115. let(:admin) { false }
  116. it 'fails to enable account' do
  117. is_expected.to have_http_status :forbidden
  118. expect(user.reload).to be_disabled
  119. end
  120. end
  121. end
  122. describe 'POST #redownload' do
  123. subject { post :redownload, params: { id: account.id } }
  124. let(:current_user) { Fabricate(:user, admin: admin) }
  125. let(:account) { Fabricate(:account) }
  126. context 'when user is admin' do
  127. let(:admin) { true }
  128. it 'succeeds in redownloadin' do
  129. is_expected.to redirect_to admin_account_path(account.id)
  130. end
  131. end
  132. context 'when user is not admin' do
  133. let(:admin) { false }
  134. it 'fails to redownload' do
  135. is_expected.to have_http_status :forbidden
  136. end
  137. end
  138. end
  139. describe 'POST #remove_avatar' do
  140. subject { post :remove_avatar, params: { id: account.id } }
  141. let(:current_user) { Fabricate(:user, admin: admin) }
  142. let(:account) { Fabricate(:account) }
  143. context 'when user is admin' do
  144. let(:admin) { true }
  145. it 'succeeds in removing avatar' do
  146. is_expected.to redirect_to admin_account_path(account.id)
  147. end
  148. end
  149. context 'when user is not admin' do
  150. let(:admin) { false }
  151. it 'fails to remove avatar' do
  152. is_expected.to have_http_status :forbidden
  153. end
  154. end
  155. end
  156. end