directories_spec.rb 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.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(response.content_type)
  74. .to start_with('application/json')
  75. expect(response.parsed_body)
  76. .to contain_exactly(
  77. hash_including(id: eligible_remote_account.id.to_s),
  78. hash_including(id: local_discoverable_account.id.to_s)
  79. )
  80. end
  81. end
  82. context 'when asking for local accounts only' do
  83. let(:user) { Fabricate(:user, confirmed_at: 10.days.ago, approved: true) }
  84. let(:local_account) { Fabricate(:account, domain: nil, user: user) }
  85. let(:remote_account) { Fabricate(:account, domain: 'host.example') }
  86. before do
  87. local_account.create_account_stat!
  88. remote_account.create_account_stat!
  89. end
  90. it 'returns only the local accounts' do
  91. get '/api/v1/directory', headers: headers, params: { local: '1' }
  92. expect(response).to have_http_status(200)
  93. expect(response.content_type)
  94. .to start_with('application/json')
  95. expect(response.parsed_body)
  96. .to contain_exactly(
  97. hash_including(id: local_account.id.to_s)
  98. )
  99. .and not_include(remote_account.id.to_s)
  100. end
  101. end
  102. context 'when ordered by active' do
  103. it 'returns accounts in order of most recent status activity' do
  104. old_stat = Fabricate(:account_stat, last_status_at: 1.day.ago)
  105. new_stat = Fabricate(:account_stat, last_status_at: 1.minute.ago)
  106. get '/api/v1/directory', headers: headers, params: { order: 'active' }
  107. expect(response).to have_http_status(200)
  108. expect(response.content_type)
  109. .to start_with('application/json')
  110. expect(response.parsed_body)
  111. .to contain_exactly(
  112. hash_including(id: new_stat.account_id.to_s),
  113. hash_including(id: old_stat.account_id.to_s)
  114. )
  115. end
  116. end
  117. context 'when ordered by new' do
  118. it 'returns accounts in order of creation' do
  119. account_old = Fabricate(:account_stat).account
  120. travel_to 10.seconds.from_now
  121. account_new = Fabricate(:account_stat).account
  122. get '/api/v1/directory', headers: headers, params: { order: 'new' }
  123. expect(response).to have_http_status(200)
  124. expect(response.content_type)
  125. .to start_with('application/json')
  126. expect(response.parsed_body)
  127. .to contain_exactly(
  128. hash_including(id: account_new.id.to_s),
  129. hash_including(id: account_old.id.to_s)
  130. )
  131. end
  132. end
  133. end
  134. end