accounts_controller_spec.rb 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe Api::V2::Admin::AccountsController do
  4. render_views
  5. let(:role) { UserRole.find_by(name: 'Moderator') }
  6. let(:user) { Fabricate(:user, role: role) }
  7. let(:scopes) { 'admin:read admin:write' }
  8. let(:token) { Fabricate(:accessible_access_token, resource_owner_id: user.id, scopes: scopes) }
  9. let(:account) { Fabricate(:account) }
  10. before do
  11. allow(controller).to receive(:doorkeeper_token) { token }
  12. end
  13. describe 'GET #index' do
  14. let!(:remote_account) { Fabricate(:account, domain: 'example.org') }
  15. let!(:other_remote_account) { Fabricate(:account, domain: 'foo.bar') }
  16. let!(:suspended_account) { Fabricate(:account, suspended: true) }
  17. let!(:suspended_remote) { Fabricate(:account, domain: 'foo.bar', suspended: true) }
  18. let!(:disabled_account) { Fabricate(:user, disabled: true).account }
  19. let!(:pending_account) { Fabricate(:user, approved: false).account }
  20. let!(:admin_account) { user.account }
  21. let(:params) { {} }
  22. before do
  23. pending_account.user.update(approved: false)
  24. get :index, params: params
  25. end
  26. it_behaves_like 'forbidden for wrong scope', 'write:statuses'
  27. it_behaves_like 'forbidden for wrong role', ''
  28. [
  29. [{ status: 'active', origin: 'local', permissions: 'staff' }, [:admin_account]],
  30. [{ by_domain: 'example.org', origin: 'remote' }, [:remote_account]],
  31. [{ status: 'suspended' }, [:suspended_remote, :suspended_account]],
  32. [{ status: 'disabled' }, [:disabled_account]],
  33. [{ status: 'pending' }, [:pending_account]],
  34. ].each do |params, expected_results|
  35. context "when called with #{params.inspect}" do
  36. let(:params) { params }
  37. it 'returns http success' do
  38. expect(response).to have_http_status(200)
  39. end
  40. it "returns the correct accounts (#{expected_results.inspect})" do
  41. json = body_as_json
  42. expect(json.map { |a| a[:id].to_i }).to eq(expected_results.map { |symbol| send(symbol).id })
  43. end
  44. end
  45. end
  46. context 'with limit param' do
  47. let(:params) { { limit: 1 } }
  48. it 'sets the correct pagination headers' do
  49. expect(response.headers['Link'].find_link(%w(rel next)).href).to eq api_v2_admin_accounts_url(limit: 1, max_id: admin_account.id)
  50. end
  51. end
  52. end
  53. end