account_search_service_spec.rb 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. require 'rails_helper'
  2. describe AccountSearchService, type: :service do
  3. describe '#call' do
  4. context 'with a query to ignore' do
  5. it 'returns empty array for missing query' do
  6. results = subject.call('', nil, limit: 10)
  7. expect(results).to eq []
  8. end
  9. it 'returns empty array for limit zero' do
  10. Fabricate(:account, username: 'match')
  11. results = subject.call('match', nil, limit: 0)
  12. expect(results).to eq []
  13. end
  14. end
  15. context 'searching for a simple term that is not an exact match' do
  16. it 'does not return a nil entry in the array for the exact match' do
  17. account = Fabricate(:account, username: 'matchingusername')
  18. results = subject.call('match', nil, limit: 5)
  19. expect(results).to eq [account]
  20. end
  21. end
  22. context 'when there is a local domain' do
  23. around do |example|
  24. before = Rails.configuration.x.local_domain
  25. example.run
  26. Rails.configuration.x.local_domain = before
  27. end
  28. it 'returns exact match first' do
  29. remote = Fabricate(:account, username: 'a', domain: 'remote', display_name: 'e')
  30. remote_too = Fabricate(:account, username: 'b', domain: 'remote', display_name: 'e')
  31. exact = Fabricate(:account, username: 'e')
  32. Rails.configuration.x.local_domain = 'example.com'
  33. results = subject.call('e@example.com', nil, limit: 2)
  34. expect(results).to eq([exact, remote]).or eq([exact, remote_too])
  35. end
  36. end
  37. context 'when there is a domain but no exact match' do
  38. it 'follows the remote account when resolve is true' do
  39. service = double(call: nil)
  40. allow(ResolveAccountService).to receive(:new).and_return(service)
  41. results = subject.call('newuser@remote.com', nil, limit: 10, resolve: true)
  42. expect(service).to have_received(:call).with('newuser@remote.com')
  43. end
  44. it 'does not follow the remote account when resolve is false' do
  45. service = double(call: nil)
  46. allow(ResolveAccountService).to receive(:new).and_return(service)
  47. results = subject.call('newuser@remote.com', nil, limit: 10, resolve: false)
  48. expect(service).not_to have_received(:call)
  49. end
  50. end
  51. it 'returns the fuzzy match first, and does not return suspended exacts' do
  52. partial = Fabricate(:account, username: 'exactness')
  53. exact = Fabricate(:account, username: 'exact', suspended: true)
  54. results = subject.call('exact', nil, limit: 10)
  55. expect(results.size).to eq 1
  56. expect(results).to eq [partial]
  57. end
  58. it "does not return suspended remote accounts" do
  59. remote = Fabricate(:account, username: 'a', domain: 'remote', display_name: 'e', suspended: true)
  60. results = subject.call('a@example.com', nil, limit: 2)
  61. expect(results.size).to eq 0
  62. expect(results).to eq []
  63. end
  64. end
  65. end