1
0

account_search_service_spec.rb 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. require 'rails_helper'
  2. describe AccountSearchService do
  3. describe '.call' do
  4. describe 'with a query to ignore' do
  5. it 'returns empty array for missing query' do
  6. results = subject.call('', 10)
  7. expect(results).to eq []
  8. end
  9. it 'returns empty array for hashtag query' do
  10. results = subject.call('#tag', 10)
  11. expect(results).to eq []
  12. end
  13. it 'returns empty array for limit zero' do
  14. Fabricate(:account, username: 'match')
  15. results = subject.call('match', 0)
  16. expect(results).to eq []
  17. end
  18. end
  19. describe 'searching for a simple term that is not an exact match' do
  20. it 'does not return a nil entry in the array for the exact match' do
  21. match = Fabricate(:account, username: 'matchingusername')
  22. results = subject.call('match', 5)
  23. expect(results).to eq [match]
  24. end
  25. end
  26. describe 'searching local and remote users' do
  27. describe "when only '@'" do
  28. before do
  29. allow(Account).to receive(:find_local)
  30. allow(Account).to receive(:search_for)
  31. subject.call('@', 10)
  32. end
  33. it 'uses find_local with empty query to look for local accounts' do
  34. expect(Account).to have_received(:find_local).with('')
  35. end
  36. end
  37. describe 'when no domain' do
  38. before do
  39. allow(Account).to receive(:find_local)
  40. allow(Account).to receive(:search_for)
  41. subject.call('one', 10)
  42. end
  43. it 'uses find_local to look for local accounts' do
  44. expect(Account).to have_received(:find_local).with('one')
  45. end
  46. it 'uses search_for to find matches' do
  47. expect(Account).to have_received(:search_for).with('one', 10)
  48. end
  49. end
  50. describe 'when there is a domain' do
  51. before do
  52. allow(Account).to receive(:find_remote)
  53. end
  54. it 'uses find_remote to look for remote accounts' do
  55. subject.call('two@example.com', 10)
  56. expect(Account).to have_received(:find_remote).with('two', 'example.com')
  57. end
  58. describe 'and there is no account provided' do
  59. it 'uses search_for to find matches' do
  60. allow(Account).to receive(:search_for)
  61. subject.call('two@example.com', 10, false, nil)
  62. expect(Account).to have_received(:search_for).with('two example.com', 10)
  63. end
  64. end
  65. describe 'and there is an account provided' do
  66. it 'uses advanced_search_for to find matches' do
  67. account = Fabricate(:account)
  68. allow(Account).to receive(:advanced_search_for)
  69. subject.call('two@example.com', 10, false, account)
  70. expect(Account).to have_received(:advanced_search_for).with('two example.com', account, 10)
  71. end
  72. end
  73. end
  74. end
  75. describe 'with an exact match' do
  76. it 'returns exact match first, and does not return duplicates' do
  77. partial = Fabricate(:account, username: 'exactness')
  78. exact = Fabricate(:account, username: 'exact')
  79. results = subject.call('exact', 10)
  80. expect(results.size).to eq 2
  81. expect(results).to eq [exact, partial]
  82. end
  83. end
  84. describe 'when there is a local domain' do
  85. around do |example|
  86. before = Rails.configuration.x.local_domain
  87. example.run
  88. Rails.configuration.x.local_domain = before
  89. end
  90. it 'returns exact match first' do
  91. remote = Fabricate(:account, username: 'a', domain: 'remote', display_name: 'e')
  92. remote_too = Fabricate(:account, username: 'b', domain: 'remote', display_name: 'e')
  93. exact = Fabricate(:account, username: 'e')
  94. Rails.configuration.x.local_domain = 'example.com'
  95. results = subject.call('e@example.com', 2)
  96. expect(results.size).to eq 2
  97. expect(results).to eq([exact, remote]).or eq([exact, remote_too])
  98. end
  99. end
  100. describe 'when there is a domain but no exact match' do
  101. it 'follows the remote account when resolve is true' do
  102. service = double(call: nil)
  103. allow(ResolveRemoteAccountService).to receive(:new).and_return(service)
  104. results = subject.call('newuser@remote.com', 10, true)
  105. expect(service).to have_received(:call).with('newuser@remote.com')
  106. end
  107. it 'does not follow the remote account when resolve is false' do
  108. service = double(call: nil)
  109. allow(ResolveRemoteAccountService).to receive(:new).and_return(service)
  110. results = subject.call('newuser@remote.com', 10, false)
  111. expect(service).not_to have_received(:call)
  112. end
  113. end
  114. end
  115. end