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.

165 lines
4.1 KiB

  1. require 'rails_helper'
  2. RSpec.describe Api::V1::Admin::AccountsController, type: :controller do
  3. render_views
  4. let(:role) { 'moderator' }
  5. let(:user) { Fabricate(:user, role: role, account: Fabricate(:account, username: 'alice')) }
  6. let(:scopes) { 'admin:read admin:write' }
  7. let(:token) { Fabricate(:accessible_access_token, resource_owner_id: user.id, scopes: scopes) }
  8. let(:account) { Fabricate(:user).account }
  9. before do
  10. allow(controller).to receive(:doorkeeper_token) { token }
  11. end
  12. shared_examples 'forbidden for wrong scope' do |wrong_scope|
  13. let(:scopes) { wrong_scope }
  14. it 'returns http forbidden' do
  15. expect(response).to have_http_status(403)
  16. end
  17. end
  18. shared_examples 'forbidden for wrong role' do |wrong_role|
  19. let(:role) { wrong_role }
  20. it 'returns http forbidden' do
  21. expect(response).to have_http_status(403)
  22. end
  23. end
  24. describe 'GET #index' do
  25. before do
  26. get :index
  27. end
  28. it_behaves_like 'forbidden for wrong scope', 'write:statuses'
  29. it_behaves_like 'forbidden for wrong role', 'user'
  30. it 'returns http success' do
  31. expect(response).to have_http_status(200)
  32. end
  33. end
  34. describe 'GET #show' do
  35. before do
  36. get :show, params: { id: account.id }
  37. end
  38. it_behaves_like 'forbidden for wrong scope', 'write:statuses'
  39. it_behaves_like 'forbidden for wrong role', 'user'
  40. it 'returns http success' do
  41. expect(response).to have_http_status(200)
  42. end
  43. end
  44. describe 'POST #approve' do
  45. before do
  46. account.user.update(approved: false)
  47. post :approve, params: { id: account.id }
  48. end
  49. it_behaves_like 'forbidden for wrong scope', 'write:statuses'
  50. it_behaves_like 'forbidden for wrong role', 'user'
  51. it 'returns http success' do
  52. expect(response).to have_http_status(200)
  53. end
  54. it 'approves user' do
  55. expect(account.reload.user_approved?).to be true
  56. end
  57. end
  58. describe 'POST #reject' do
  59. before do
  60. account.user.update(approved: false)
  61. post :reject, params: { id: account.id }
  62. end
  63. it_behaves_like 'forbidden for wrong scope', 'write:statuses'
  64. it_behaves_like 'forbidden for wrong role', 'user'
  65. it 'returns http success' do
  66. expect(response).to have_http_status(200)
  67. end
  68. it 'removes user' do
  69. expect(User.where(id: account.user.id).count).to eq 0
  70. end
  71. end
  72. describe 'POST #enable' do
  73. before do
  74. account.user.update(disabled: true)
  75. post :enable, params: { id: account.id }
  76. end
  77. it_behaves_like 'forbidden for wrong scope', 'write:statuses'
  78. it_behaves_like 'forbidden for wrong role', 'user'
  79. it 'returns http success' do
  80. expect(response).to have_http_status(200)
  81. end
  82. it 'enables user' do
  83. expect(account.reload.user_disabled?).to be false
  84. end
  85. end
  86. describe 'POST #unsuspend' do
  87. before do
  88. account.suspend!
  89. post :unsuspend, params: { id: account.id }
  90. end
  91. it_behaves_like 'forbidden for wrong scope', 'write:statuses'
  92. it_behaves_like 'forbidden for wrong role', 'user'
  93. it 'returns http success' do
  94. expect(response).to have_http_status(200)
  95. end
  96. it 'unsuspends account' do
  97. expect(account.reload.suspended?).to be false
  98. end
  99. end
  100. describe 'POST #unsensitive' do
  101. before do
  102. account.touch(:sensitized_at)
  103. post :unsensitive, params: { id: account.id }
  104. end
  105. it_behaves_like 'forbidden for wrong scope', 'write:statuses'
  106. it_behaves_like 'forbidden for wrong role', 'user'
  107. it 'returns http success' do
  108. expect(response).to have_http_status(200)
  109. end
  110. it 'unsensitives account' do
  111. expect(account.reload.sensitized?).to be false
  112. end
  113. end
  114. describe 'POST #unsilence' do
  115. before do
  116. account.touch(:silenced_at)
  117. post :unsilence, params: { id: account.id }
  118. end
  119. it_behaves_like 'forbidden for wrong scope', 'write:statuses'
  120. it_behaves_like 'forbidden for wrong role', 'user'
  121. it 'returns http success' do
  122. expect(response).to have_http_status(200)
  123. end
  124. it 'unsilences account' do
  125. expect(account.reload.silenced?).to be false
  126. end
  127. end
  128. end