directories_spec.rb 4.8 KB

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