directories_controller_spec.rb 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. describe Api::V1::DirectoriesController do
  4. render_views
  5. let(:user) { Fabricate(:user, confirmed_at: nil) }
  6. let(:token) { Fabricate(:accessible_access_token, resource_owner_id: user.id, scopes: 'read:follows') }
  7. before do
  8. allow(controller).to receive(:doorkeeper_token) { token }
  9. end
  10. describe 'GET #show' do
  11. context 'with no params' do
  12. before do
  13. local_unconfirmed_account = Fabricate(
  14. :account,
  15. domain: nil,
  16. user: Fabricate(:user, confirmed_at: nil, approved: true),
  17. username: 'local_unconfirmed'
  18. )
  19. local_unconfirmed_account.create_account_stat!
  20. local_unapproved_account = Fabricate(
  21. :account,
  22. domain: nil,
  23. user: Fabricate(:user, confirmed_at: 10.days.ago),
  24. username: 'local_unapproved'
  25. )
  26. local_unapproved_account.create_account_stat!
  27. local_unapproved_account.user.update(approved: false)
  28. local_undiscoverable_account = Fabricate(
  29. :account,
  30. domain: nil,
  31. user: Fabricate(:user, confirmed_at: 10.days.ago, approved: true),
  32. discoverable: false,
  33. username: 'local_undiscoverable'
  34. )
  35. local_undiscoverable_account.create_account_stat!
  36. excluded_from_timeline_account = Fabricate(
  37. :account,
  38. domain: 'host.example',
  39. discoverable: true,
  40. username: 'remote_excluded_from_timeline'
  41. )
  42. excluded_from_timeline_account.create_account_stat!
  43. Fabricate(:block, account: user.account, target_account: excluded_from_timeline_account)
  44. domain_blocked_account = Fabricate(
  45. :account,
  46. domain: 'test.example',
  47. discoverable: true,
  48. username: 'remote_domain_blocked'
  49. )
  50. domain_blocked_account.create_account_stat!
  51. Fabricate(:account_domain_block, account: user.account, domain: 'test.example')
  52. end
  53. it 'returns the local discoverable account and the remote discoverable account' do
  54. local_discoverable_account = Fabricate(
  55. :account,
  56. domain: nil,
  57. user: Fabricate(:user, confirmed_at: 10.days.ago, approved: true),
  58. discoverable: true,
  59. username: 'local_discoverable'
  60. )
  61. local_discoverable_account.create_account_stat!
  62. eligible_remote_account = Fabricate(
  63. :account,
  64. domain: 'host.example',
  65. discoverable: true,
  66. username: 'eligible_remote'
  67. )
  68. eligible_remote_account.create_account_stat!
  69. get :show
  70. expect(response).to have_http_status(200)
  71. expect(body_as_json.size).to eq(2)
  72. expect(body_as_json.pluck(:id)).to contain_exactly(eligible_remote_account.id.to_s, local_discoverable_account.id.to_s)
  73. end
  74. end
  75. context 'when asking for local accounts only' do
  76. it 'returns only the local accounts' do
  77. user = Fabricate(:user, confirmed_at: 10.days.ago, approved: true)
  78. local_account = Fabricate(:account, domain: nil, user: user)
  79. remote_account = Fabricate(:account, domain: 'host.example')
  80. local_account.create_account_stat!
  81. remote_account.create_account_stat!
  82. get :show, params: { local: '1' }
  83. expect(response).to have_http_status(200)
  84. expect(body_as_json.size).to eq(1)
  85. expect(body_as_json.first[:id]).to include(local_account.id.to_s)
  86. expect(response.body).to_not include(remote_account.id.to_s)
  87. end
  88. end
  89. context 'when ordered by active' do
  90. it 'returns accounts in order of most recent status activity' do
  91. old_stat = Fabricate(:account_stat, last_status_at: 1.day.ago)
  92. new_stat = Fabricate(:account_stat, last_status_at: 1.minute.ago)
  93. get :show, params: { order: 'active' }
  94. expect(response).to have_http_status(200)
  95. expect(body_as_json.size).to eq(2)
  96. expect(body_as_json.first[:id]).to include(new_stat.account_id.to_s)
  97. expect(body_as_json.second[:id]).to include(old_stat.account_id.to_s)
  98. end
  99. end
  100. context 'when ordered by new' do
  101. it 'returns accounts in order of creation' do
  102. account_old = Fabricate(:account_stat).account
  103. travel_to 10.seconds.from_now
  104. account_new = Fabricate(:account_stat).account
  105. get :show, params: { order: 'new' }
  106. expect(response).to have_http_status(200)
  107. expect(body_as_json.size).to eq(2)
  108. expect(body_as_json.first[:id]).to include(account_new.id.to_s)
  109. expect(body_as_json.second[:id]).to include(account_old.id.to_s)
  110. end
  111. end
  112. end
  113. end