accounts_controller_spec.rb 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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.touch(:suspended_at)
  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 #unsilence' do
  101. before do
  102. account.touch(:silenced_at)
  103. post :unsilence, 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 'unsilences account' do
  111. expect(account.reload.silenced?).to be false
  112. end
  113. end
  114. end