account_search_service_spec.rb 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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.size).to eq 2
  35. expect(results).to eq([exact, remote]).or eq([exact, remote_too])
  36. end
  37. end
  38. context 'when there is a domain but no exact match' do
  39. it 'follows the remote account when resolve is true' do
  40. service = double(call: nil)
  41. allow(ResolveAccountService).to receive(:new).and_return(service)
  42. results = subject.call('newuser@remote.com', nil, limit: 10, resolve: true)
  43. expect(service).to have_received(:call).with('newuser@remote.com')
  44. end
  45. it 'does not follow the remote account when resolve is false' do
  46. service = double(call: nil)
  47. allow(ResolveAccountService).to receive(:new).and_return(service)
  48. results = subject.call('newuser@remote.com', nil, limit: 10, resolve: false)
  49. expect(service).not_to have_received(:call)
  50. end
  51. end
  52. it 'returns the fuzzy match first, and does not return suspended exacts' do
  53. partial = Fabricate(:account, username: 'exactness')
  54. exact = Fabricate(:account, username: 'exact', suspended: true)
  55. results = subject.call('exact', nil, limit: 10)
  56. expect(results.size).to eq 1
  57. expect(results).to eq [partial]
  58. end
  59. it "does not return suspended remote accounts" do
  60. remote = Fabricate(:account, username: 'a', domain: 'remote', display_name: 'e', suspended: true)
  61. results = subject.call('a@example.com', nil, limit: 2)
  62. expect(results.size).to eq 0
  63. expect(results).to eq []
  64. end
  65. end
  66. end