account_search_service_spec.rb 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. describe AccountSearchService, type: :service do
  4. describe '#call' do
  5. context 'with a query to ignore' do
  6. it 'returns empty array for missing query' do
  7. results = subject.call('', nil, limit: 10)
  8. expect(results).to eq []
  9. end
  10. it 'returns empty array for limit zero' do
  11. Fabricate(:account, username: 'match')
  12. results = subject.call('match', nil, limit: 0)
  13. expect(results).to eq []
  14. end
  15. end
  16. context 'when searching for a simple term that is not an exact match' do
  17. it 'does not return a nil entry in the array for the exact match' do
  18. account = Fabricate(:account, username: 'matchingusername')
  19. results = subject.call('match', nil, limit: 5)
  20. expect(results).to eq [account]
  21. end
  22. end
  23. context 'when there is a local domain' do
  24. around do |example|
  25. before = Rails.configuration.x.local_domain
  26. example.run
  27. Rails.configuration.x.local_domain = before
  28. end
  29. it 'returns exact match first' do
  30. remote = Fabricate(:account, username: 'a', domain: 'remote', display_name: 'e')
  31. remote_too = Fabricate(:account, username: 'b', domain: 'remote', display_name: 'e')
  32. exact = Fabricate(:account, username: 'e')
  33. Rails.configuration.x.local_domain = 'example.com'
  34. results = subject.call('e@example.com', nil, limit: 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 = instance_double(ResolveAccountService, call: nil)
  41. allow(ResolveAccountService).to receive(:new).and_return(service)
  42. 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 = instance_double(ResolveAccountService, call: nil)
  47. allow(ResolveAccountService).to receive(:new).and_return(service)
  48. subject.call('newuser@remote.com', nil, limit: 10, resolve: false)
  49. expect(service).to_not 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. 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. 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