123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409 |
- # frozen_string_literal: true
- require 'rails_helper'
- RSpec.describe 'Accounts' do
- let(:role) { UserRole.find_by(name: 'Admin') }
- let(:user) { Fabricate(:user, role: role) }
- let(:scopes) { 'admin:read:accounts admin:write:accounts' }
- let(:token) { Fabricate(:accessible_access_token, resource_owner_id: user.id, scopes: scopes) }
- let(:headers) { { 'Authorization' => "Bearer #{token.token}" } }
- describe 'GET /api/v1/admin/accounts' do
- subject do
- get '/api/v1/admin/accounts', headers: headers, params: params
- end
- shared_examples 'a successful request' do
- it 'returns the correct accounts', :aggregate_failures do
- subject
- expect(response).to have_http_status(200)
- expect(body_as_json.pluck(:id)).to match_array(expected_results.map { |a| a.id.to_s })
- end
- end
- let!(:remote_account) { Fabricate(:account, domain: 'example.org') }
- let!(:suspended_account) { Fabricate(:account, suspended: true) }
- let!(:disabled_account) { Fabricate(:user, disabled: true).account }
- let!(:pending_account) { Fabricate(:user, approved: false).account }
- let!(:admin_account) { user.account }
- let(:params) { {} }
- it_behaves_like 'forbidden for wrong scope', 'read read:accounts admin:write admin:write:accounts'
- it_behaves_like 'forbidden for wrong role', ''
- context 'when requesting active local staff accounts' do
- let(:expected_results) { [admin_account] }
- let(:params) { { active: 'true', local: 'true', staff: 'true' } }
- it_behaves_like 'a successful request'
- end
- context 'when requesting remote accounts from a specified domain' do
- let(:expected_results) { [remote_account] }
- let(:params) { { by_domain: 'example.org', remote: 'true' } }
- before do
- Fabricate(:account, domain: 'foo.bar')
- end
- it_behaves_like 'a successful request'
- end
- context 'when requesting suspended accounts' do
- let(:expected_results) { [suspended_account] }
- let(:params) { { suspended: 'true' } }
- before do
- Fabricate(:account, domain: 'foo.bar', suspended: true)
- end
- it_behaves_like 'a successful request'
- end
- context 'when requesting disabled accounts' do
- let(:expected_results) { [disabled_account] }
- let(:params) { { disabled: 'true' } }
- it_behaves_like 'a successful request'
- end
- context 'when requesting pending accounts' do
- let(:expected_results) { [pending_account] }
- let(:params) { { pending: 'true' } }
- before do
- pending_account.user.update(approved: false)
- end
- it_behaves_like 'a successful request'
- end
- context 'when no parameter is given' do
- let(:expected_results) { [disabled_account, pending_account, admin_account] }
- it_behaves_like 'a successful request'
- end
- context 'with limit param' do
- let(:params) { { limit: 2 } }
- it 'returns only the requested number of accounts', :aggregate_failures do
- subject
- expect(response).to have_http_status(200)
- expect(body_as_json.size).to eq(params[:limit])
- end
- end
- end
- describe 'GET /api/v1/admin/accounts/:id' do
- subject do
- get "/api/v1/admin/accounts/#{account.id}", headers: headers
- end
- let(:account) { Fabricate(:account) }
- it_behaves_like 'forbidden for wrong scope', 'read read:accounts admin:write admin:write:accounts'
- it_behaves_like 'forbidden for wrong role', ''
- it 'returns the requested account successfully', :aggregate_failures do
- subject
- expect(response).to have_http_status(200)
- expect(body_as_json).to match(
- a_hash_including(id: account.id.to_s, username: account.username, email: account.user.email)
- )
- end
- context 'when the account is not found' do
- it 'returns http not found' do
- get '/api/v1/admin/accounts/-1', headers: headers
- expect(response).to have_http_status(404)
- end
- end
- end
- describe 'POST /api/v1/admin/accounts/:id/approve' do
- subject do
- post "/api/v1/admin/accounts/#{account.id}/approve", headers: headers
- end
- let(:account) { Fabricate(:account) }
- context 'when the account is pending' do
- before do
- account.user.update(approved: false)
- end
- it_behaves_like 'forbidden for wrong scope', 'write write:accounts read admin:read'
- it_behaves_like 'forbidden for wrong role', ''
- it 'approves the user successfully', :aggregate_failures do
- subject
- expect(response).to have_http_status(200)
- expect(account.reload.user_approved?).to be(true)
- end
- it 'logs action', :aggregate_failures do
- subject
- expect(latest_admin_action_log)
- .to be_present
- .and have_attributes(
- action: eq(:approve),
- account_id: eq(user.account_id),
- target_id: eq(account.user.id)
- )
- end
- end
- context 'when the account is already approved' do
- it 'returns http forbidden' do
- subject
- expect(response).to have_http_status(403)
- end
- end
- context 'when the account is not found' do
- it 'returns http not found' do
- post '/api/v1/admin/accounts/-1/approve', headers: headers
- expect(response).to have_http_status(404)
- end
- end
- end
- describe 'POST /api/v1/admin/accounts/:id/reject' do
- subject do
- post "/api/v1/admin/accounts/#{account.id}/reject", headers: headers
- end
- let(:account) { Fabricate(:account) }
- context 'when the account is pending' do
- before do
- account.user.update(approved: false)
- end
- it_behaves_like 'forbidden for wrong scope', 'write write:accounts read admin:read'
- it_behaves_like 'forbidden for wrong role', ''
- it 'removes the user successfully', :aggregate_failures do
- subject
- expect(response).to have_http_status(200)
- expect(User.where(id: account.user.id)).to_not exist
- end
- it 'logs action', :aggregate_failures do
- subject
- expect(latest_admin_action_log)
- .to be_present
- .and have_attributes(
- action: eq(:reject),
- account_id: eq(user.account_id),
- target_id: eq(account.user.id)
- )
- end
- end
- context 'when account is already approved' do
- it 'returns http forbidden' do
- subject
- expect(response).to have_http_status(403)
- end
- end
- context 'when the account is not found' do
- it 'returns http not found' do
- post '/api/v1/admin/accounts/-1/reject', headers: headers
- expect(response).to have_http_status(404)
- end
- end
- end
- describe 'POST /api/v1/admin/accounts/:id/enable' do
- subject do
- post "/api/v1/admin/accounts/#{account.id}/enable", headers: headers
- end
- let(:account) { Fabricate(:account) }
- before do
- account.user.update(disabled: true)
- end
- it_behaves_like 'forbidden for wrong scope', 'write write:accounts read admin:read'
- it_behaves_like 'forbidden for wrong role', ''
- it 'enables the user successfully', :aggregate_failures do
- subject
- expect(response).to have_http_status(200)
- expect(account.reload.user_disabled?).to be false
- end
- context 'when the account is not found' do
- it 'returns http not found' do
- post '/api/v1/admin/accounts/-1/enable', headers: headers
- expect(response).to have_http_status(404)
- end
- end
- end
- describe 'POST /api/v1/admin/accounts/:id/unsuspend' do
- subject do
- post "/api/v1/admin/accounts/#{account.id}/unsuspend", headers: headers
- end
- let(:account) { Fabricate(:account) }
- context 'when the account is suspended' do
- before do
- account.suspend!
- end
- it_behaves_like 'forbidden for wrong scope', 'write write:accounts read admin:read'
- it_behaves_like 'forbidden for wrong role', ''
- it 'unsuspends the account successfully', :aggregate_failures do
- subject
- expect(response).to have_http_status(200)
- expect(account.reload.suspended?).to be false
- end
- end
- context 'when the account is not suspended' do
- it 'returns http forbidden' do
- subject
- expect(response).to have_http_status(403)
- end
- end
- context 'when the account is not found' do
- it 'returns http not found' do
- post '/api/v1/admin/accounts/-1/unsuspend', headers: headers
- expect(response).to have_http_status(404)
- end
- end
- end
- describe 'POST /api/v1/admin/accounts/:id/unsensitive' do
- subject do
- post "/api/v1/admin/accounts/#{account.id}/unsensitive", headers: headers
- end
- let(:account) { Fabricate(:account) }
- before do
- account.update(sensitized_at: 10.days.ago)
- end
- it_behaves_like 'forbidden for wrong scope', 'write write:accounts read admin:read'
- it_behaves_like 'forbidden for wrong role', ''
- it 'unsensitizes the account successfully', :aggregate_failures do
- subject
- expect(response).to have_http_status(200)
- expect(account.reload.sensitized?).to be false
- end
- context 'when the account is not found' do
- it 'returns http not found' do
- post '/api/v1/admin/accounts/-1/unsensitive', headers: headers
- expect(response).to have_http_status(404)
- end
- end
- end
- describe 'POST /api/v1/admin/accounts/:id/unsilence' do
- subject do
- post "/api/v1/admin/accounts/#{account.id}/unsilence", headers: headers
- end
- let(:account) { Fabricate(:account) }
- before do
- account.update(silenced_at: 3.days.ago)
- end
- it_behaves_like 'forbidden for wrong scope', 'write write:accounts read admin:read'
- it_behaves_like 'forbidden for wrong role', ''
- it 'unsilences the account successfully', :aggregate_failures do
- subject
- expect(response).to have_http_status(200)
- expect(account.reload.silenced?).to be false
- end
- context 'when the account is not found' do
- it 'returns http not found' do
- post '/api/v1/admin/accounts/-1/unsilence', headers: headers
- expect(response).to have_http_status(404)
- end
- end
- end
- describe 'DELETE /api/v1/admin/accounts/:id' do
- subject do
- delete "/api/v1/admin/accounts/#{account.id}", headers: headers
- end
- let(:account) { Fabricate(:account) }
- context 'when account is suspended' do
- before do
- account.suspend!
- end
- it_behaves_like 'forbidden for wrong scope', 'write write:accounts read admin:read'
- it_behaves_like 'forbidden for wrong role', ''
- it 'deletes the account successfully', :aggregate_failures do
- allow(Admin::AccountDeletionWorker).to receive(:perform_async)
- subject
- expect(response).to have_http_status(200)
- expect(Admin::AccountDeletionWorker).to have_received(:perform_async).with(account.id).once
- end
- end
- context 'when account is not suspended' do
- it 'returns http forbidden' do
- subject
- expect(response).to have_http_status(403)
- end
- end
- context 'when the account is not found' do
- it 'returns http not found' do
- delete '/api/v1/admin/accounts/-1', headers: headers
- expect(response).to have_http_status(404)
- end
- end
- end
- private
- def latest_admin_action_log
- Admin::ActionLog.last
- end
- end
|