resolve_account_service_spec.rb 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. require 'rails_helper'
  2. RSpec.describe ResolveAccountService, type: :service do
  3. subject { described_class.new }
  4. before do
  5. stub_request(:get, "https://quitter.no/.well-known/host-meta").to_return(request_fixture('.host-meta.txt'))
  6. stub_request(:get, "https://example.com/.well-known/webfinger?resource=acct:catsrgr8@example.com").to_return(status: 404)
  7. stub_request(:get, "https://example.com/.well-known/host-meta").to_return(status: 404)
  8. stub_request(:get, "https://quitter.no/avatar/7477-300-20160211190340.png").to_return(request_fixture('avatar.txt'))
  9. stub_request(:get, "https://quitter.no/.well-known/webfinger?resource=acct:catsrgr8@quitter.no").to_return(status: 404)
  10. stub_request(:get, "https://ap.example.com/.well-known/webfinger?resource=acct:foo@ap.example.com").to_return(request_fixture('activitypub-webfinger.txt'))
  11. stub_request(:get, "https://ap.example.com/users/foo").to_return(request_fixture('activitypub-actor.txt'))
  12. stub_request(:get, "https://ap.example.com/users/foo.atom").to_return(request_fixture('activitypub-feed.txt'))
  13. stub_request(:get, %r{https://ap.example.com/users/foo/\w+}).to_return(status: 404)
  14. end
  15. it 'raises error if no such user can be resolved via webfinger' do
  16. expect(subject.call('catsrgr8@quitter.no')).to be_nil
  17. end
  18. it 'raises error if the domain does not have webfinger' do
  19. expect(subject.call('catsrgr8@example.com')).to be_nil
  20. end
  21. context 'with an ActivityPub account' do
  22. it 'returns new remote account' do
  23. account = subject.call('foo@ap.example.com')
  24. expect(account.activitypub?).to eq true
  25. expect(account.domain).to eq 'ap.example.com'
  26. expect(account.inbox_url).to eq 'https://ap.example.com/users/foo/inbox'
  27. end
  28. context 'with multiple types' do
  29. before do
  30. stub_request(:get, "https://ap.example.com/users/foo").to_return(request_fixture('activitypub-actor-individual.txt'))
  31. end
  32. it 'returns new remote account' do
  33. account = subject.call('foo@ap.example.com')
  34. expect(account.activitypub?).to eq true
  35. expect(account.domain).to eq 'ap.example.com'
  36. expect(account.inbox_url).to eq 'https://ap.example.com/users/foo/inbox'
  37. expect(account.actor_type).to eq 'Person'
  38. end
  39. end
  40. end
  41. it 'processes one remote account at a time using locks' do
  42. wait_for_start = true
  43. fail_occurred = false
  44. return_values = Concurrent::Array.new
  45. # Preload classes that throw circular dependency errors in threads
  46. Account
  47. TagManager
  48. DomainBlock
  49. threads = Array.new(5) do
  50. Thread.new do
  51. true while wait_for_start
  52. begin
  53. return_values << described_class.new.call('foo@ap.example.com')
  54. rescue ActiveRecord::RecordNotUnique
  55. fail_occurred = true
  56. end
  57. end
  58. end
  59. wait_for_start = false
  60. threads.each(&:join)
  61. expect(fail_occurred).to be false
  62. expect(return_values).to_not include(nil)
  63. end
  64. end