resolve_remote_account_service_spec.rb 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. require 'rails_helper'
  2. RSpec.describe ResolveRemoteAccountService 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://redirected.com/.well-known/host-meta").to_return(request_fixture('redirected.host-meta.txt'))
  8. stub_request(:get, "https://example.com/.well-known/host-meta").to_return(status: 404)
  9. stub_request(:get, "https://quitter.no/.well-known/webfinger?resource=acct:gargron@quitter.no").to_return(request_fixture('webfinger.txt'))
  10. stub_request(:get, "https://redirected.com/.well-known/webfinger?resource=acct:gargron@redirected.com").to_return(request_fixture('webfinger.txt'))
  11. stub_request(:get, "https://redirected.com/.well-known/webfinger?resource=acct:hacker1@redirected.com").to_return(request_fixture('webfinger-hacker1.txt'))
  12. stub_request(:get, "https://redirected.com/.well-known/webfinger?resource=acct:hacker2@redirected.com").to_return(request_fixture('webfinger-hacker2.txt'))
  13. stub_request(:get, "https://quitter.no/.well-known/webfinger?resource=acct:catsrgr8@quitter.no").to_return(status: 404)
  14. stub_request(:get, "https://quitter.no/api/statuses/user_timeline/7477.atom").to_return(request_fixture('feed.txt'))
  15. stub_request(:get, "https://quitter.no/avatar/7477-300-20160211190340.png").to_return(request_fixture('avatar.txt'))
  16. stub_request(:get, "https://localdomain.com/.well-known/host-meta").to_return(request_fixture('localdomain-hostmeta.txt'))
  17. stub_request(:get, "https://localdomain.com/.well-known/webfinger?resource=acct:foo@localdomain.com").to_return(status: 404)
  18. stub_request(:get, "https://webdomain.com/.well-known/webfinger?resource=acct:foo@localdomain.com").to_return(request_fixture('localdomain-webfinger.txt'))
  19. stub_request(:get, "https://webdomain.com/users/foo.atom").to_return(request_fixture('localdomain-feed.txt'))
  20. end
  21. it 'raises error if no such user can be resolved via webfinger' do
  22. expect(subject.call('catsrgr8@quitter.no')).to be_nil
  23. end
  24. it 'raises error if the domain does not have webfinger' do
  25. expect(subject.call('catsrgr8@example.com')).to be_nil
  26. end
  27. it 'prevents hijacking existing accounts' do
  28. account = subject.call('hacker1@redirected.com')
  29. expect(account.salmon_url).to_not eq 'https://hacker.com/main/salmon/user/7477'
  30. end
  31. it 'prevents hijacking inexisting accounts' do
  32. expect(subject.call('hacker2@redirected.com')).to be_nil
  33. end
  34. context 'with an OStatus account' do
  35. it 'returns an already existing remote account' do
  36. old_account = Fabricate(:account, username: 'gargron', domain: 'quitter.no')
  37. returned_account = subject.call('gargron@quitter.no')
  38. expect(old_account.id).to eq returned_account.id
  39. end
  40. it 'returns a new remote account' do
  41. account = subject.call('gargron@quitter.no')
  42. expect(account.username).to eq 'gargron'
  43. expect(account.domain).to eq 'quitter.no'
  44. expect(account.remote_url).to eq 'https://quitter.no/api/statuses/user_timeline/7477.atom'
  45. end
  46. it 'follows a legitimate account redirection' do
  47. account = subject.call('gargron@redirected.com')
  48. expect(account.username).to eq 'gargron'
  49. expect(account.domain).to eq 'quitter.no'
  50. expect(account.remote_url).to eq 'https://quitter.no/api/statuses/user_timeline/7477.atom'
  51. end
  52. it 'returns a new remote account' do
  53. account = subject.call('foo@localdomain.com')
  54. expect(account.username).to eq 'foo'
  55. expect(account.domain).to eq 'localdomain.com'
  56. expect(account.remote_url).to eq 'https://webdomain.com/users/foo.atom'
  57. end
  58. end
  59. context 'with an ActivityPub account' do
  60. pending
  61. end
  62. it 'processes one remote account at a time using locks' do
  63. wait_for_start = true
  64. fail_occurred = false
  65. return_values = []
  66. threads = Array.new(5) do
  67. Thread.new do
  68. true while wait_for_start
  69. begin
  70. return_values << described_class.new.call('foo@localdomain.com')
  71. rescue ActiveRecord::RecordNotUnique
  72. fail_occurred = true
  73. end
  74. end
  75. end
  76. wait_for_start = false
  77. threads.each(&:join)
  78. expect(fail_occurred).to be false
  79. expect(return_values).to_not include(nil)
  80. end
  81. end