resolve_account_service_spec.rb 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe ResolveAccountService, type: :service do
  4. subject { described_class.new }
  5. before do
  6. stub_request(:get, 'https://example.com/.well-known/host-meta').to_return(status: 404)
  7. stub_request(:get, 'https://quitter.no/avatar/7477-300-20160211190340.png').to_return(request_fixture('avatar.txt'))
  8. stub_request(:get, 'https://ap.example.com/.well-known/webfinger?resource=acct:foo@ap.example.com').to_return(request_fixture('activitypub-webfinger.txt'))
  9. stub_request(:get, 'https://ap.example.com/users/foo').to_return(request_fixture('activitypub-actor.txt'))
  10. stub_request(:get, 'https://ap.example.com/users/foo.atom').to_return(request_fixture('activitypub-feed.txt'))
  11. stub_request(:get, %r{https://ap\.example\.com/users/foo/\w+}).to_return(status: 404)
  12. stub_request(:get, 'https://example.com/.well-known/webfinger?resource=acct:hoge@example.com').to_return(status: 410)
  13. end
  14. context 'when using skip_webfinger' do
  15. context 'when account is known' do
  16. let!(:remote_account) { Fabricate(:account, username: 'foo', domain: 'ap.example.com', protocol: 'activitypub') }
  17. context 'when domain is banned' do
  18. let!(:domain_block) { Fabricate(:domain_block, domain: 'ap.example.com', severity: :suspend) }
  19. it 'does not return an account' do
  20. expect(subject.call('foo@ap.example.com', skip_webfinger: true)).to be_nil
  21. end
  22. it 'does not make a webfinger query' do
  23. subject.call('foo@ap.example.com', skip_webfinger: true)
  24. expect(a_request(:get, 'https://ap.example.com/.well-known/webfinger?resource=acct:foo@ap.example.com')).to_not have_been_made
  25. end
  26. end
  27. context 'when domain is not banned' do
  28. it 'returns the expected account' do
  29. expect(subject.call('foo@ap.example.com', skip_webfinger: true)).to eq remote_account
  30. end
  31. it 'does not make a webfinger query' do
  32. subject.call('foo@ap.example.com', skip_webfinger: true)
  33. expect(a_request(:get, 'https://ap.example.com/.well-known/webfinger?resource=acct:foo@ap.example.com')).to_not have_been_made
  34. end
  35. end
  36. end
  37. context 'when account is not known' do
  38. it 'does not return an account' do
  39. expect(subject.call('foo@ap.example.com', skip_webfinger: true)).to be_nil
  40. end
  41. it 'does not make a webfinger query' do
  42. subject.call('foo@ap.example.com', skip_webfinger: true)
  43. expect(a_request(:get, 'https://ap.example.com/.well-known/webfinger?resource=acct:foo@ap.example.com')).to_not have_been_made
  44. end
  45. end
  46. end
  47. context 'when there is an LRDD endpoint but no resolvable account' do
  48. before do
  49. stub_request(:get, 'https://quitter.no/.well-known/host-meta').to_return(request_fixture('.host-meta.txt'))
  50. stub_request(:get, 'https://quitter.no/.well-known/webfinger?resource=acct:catsrgr8@quitter.no').to_return(status: 404)
  51. end
  52. it 'returns nil' do
  53. expect(subject.call('catsrgr8@quitter.no')).to be_nil
  54. end
  55. end
  56. context 'when there is no LRDD endpoint nor resolvable account' do
  57. before do
  58. stub_request(:get, 'https://example.com/.well-known/webfinger?resource=acct:catsrgr8@example.com').to_return(status: 404)
  59. end
  60. it 'returns nil' do
  61. expect(subject.call('catsrgr8@example.com')).to be_nil
  62. end
  63. end
  64. context 'when webfinger returns http gone' do
  65. context 'with a previously known account' do
  66. before do
  67. Fabricate(:account, username: 'hoge', domain: 'example.com', last_webfingered_at: nil)
  68. allow(AccountDeletionWorker).to receive(:perform_async)
  69. end
  70. it 'returns nil' do
  71. expect(subject.call('hoge@example.com')).to be_nil
  72. end
  73. it 'queues account deletion worker' do
  74. subject.call('hoge@example.com')
  75. expect(AccountDeletionWorker).to have_received(:perform_async)
  76. end
  77. end
  78. context 'with a previously unknown account' do
  79. it 'returns nil' do
  80. expect(subject.call('hoge@example.com')).to be_nil
  81. end
  82. end
  83. end
  84. context 'with a legitimate webfinger redirection' do
  85. before do
  86. webfinger = { subject: 'acct:foo@ap.example.com', links: [{ rel: 'self', href: 'https://ap.example.com/users/foo', type: 'application/activity+json' }] }
  87. stub_request(:get, 'https://redirected.example.com/.well-known/webfinger?resource=acct:Foo@redirected.example.com').to_return(body: Oj.dump(webfinger), headers: { 'Content-Type': 'application/jrd+json' })
  88. end
  89. it 'returns new remote account' do
  90. account = subject.call('Foo@redirected.example.com')
  91. expect(account.activitypub?).to be true
  92. expect(account.acct).to eq 'foo@ap.example.com'
  93. expect(account.inbox_url).to eq 'https://ap.example.com/users/foo/inbox'
  94. end
  95. end
  96. context 'with a misconfigured redirection' do
  97. before do
  98. webfinger = { subject: 'acct:Foo@redirected.example.com', links: [{ rel: 'self', href: 'https://ap.example.com/users/foo', type: 'application/activity+json' }] }
  99. stub_request(:get, 'https://redirected.example.com/.well-known/webfinger?resource=acct:Foo@redirected.example.com').to_return(body: Oj.dump(webfinger), headers: { 'Content-Type': 'application/jrd+json' })
  100. end
  101. it 'returns new remote account' do
  102. account = subject.call('Foo@redirected.example.com')
  103. expect(account.activitypub?).to be true
  104. expect(account.acct).to eq 'foo@ap.example.com'
  105. expect(account.inbox_url).to eq 'https://ap.example.com/users/foo/inbox'
  106. end
  107. end
  108. context 'with too many webfinger redirections' do
  109. before do
  110. webfinger = { subject: 'acct:foo@evil.example.com', links: [{ rel: 'self', href: 'https://ap.example.com/users/foo', type: 'application/activity+json' }] }
  111. stub_request(:get, 'https://redirected.example.com/.well-known/webfinger?resource=acct:Foo@redirected.example.com').to_return(body: Oj.dump(webfinger), headers: { 'Content-Type': 'application/jrd+json' })
  112. webfinger2 = { subject: 'acct:foo@ap.example.com', links: [{ rel: 'self', href: 'https://ap.example.com/users/foo', type: 'application/activity+json' }] }
  113. stub_request(:get, 'https://evil.example.com/.well-known/webfinger?resource=acct:foo@evil.example.com').to_return(body: Oj.dump(webfinger2), headers: { 'Content-Type': 'application/jrd+json' })
  114. end
  115. it 'does not return a new remote account' do
  116. expect(subject.call('Foo@redirected.example.com')).to be_nil
  117. end
  118. end
  119. context 'with an ActivityPub account' do
  120. it 'returns new remote account' do
  121. account = subject.call('foo@ap.example.com')
  122. expect(account.activitypub?).to be true
  123. expect(account.domain).to eq 'ap.example.com'
  124. expect(account.inbox_url).to eq 'https://ap.example.com/users/foo/inbox'
  125. end
  126. context 'with multiple types' do
  127. before do
  128. stub_request(:get, 'https://ap.example.com/users/foo').to_return(request_fixture('activitypub-actor-individual.txt'))
  129. end
  130. it 'returns new remote account' do
  131. account = subject.call('foo@ap.example.com')
  132. expect(account.activitypub?).to be true
  133. expect(account.domain).to eq 'ap.example.com'
  134. expect(account.inbox_url).to eq 'https://ap.example.com/users/foo/inbox'
  135. expect(account.actor_type).to eq 'Person'
  136. end
  137. end
  138. end
  139. context 'with an already-known actor changing acct: URI' do
  140. let!(:duplicate) { Fabricate(:account, username: 'foo', domain: 'old.example.com', uri: 'https://ap.example.com/users/foo') }
  141. let!(:status) { Fabricate(:status, account: duplicate, text: 'foo') }
  142. it 'returns new remote account' do
  143. account = subject.call('foo@ap.example.com')
  144. expect(account.activitypub?).to be true
  145. expect(account.domain).to eq 'ap.example.com'
  146. expect(account.inbox_url).to eq 'https://ap.example.com/users/foo/inbox'
  147. expect(account.uri).to eq 'https://ap.example.com/users/foo'
  148. end
  149. it 'merges accounts' do
  150. account = subject.call('foo@ap.example.com')
  151. expect(status.reload.account_id).to eq account.id
  152. expect(Account.where(uri: account.uri).count).to eq 1
  153. end
  154. end
  155. context 'with an already-known acct: URI changing ActivityPub id' do
  156. let!(:old_account) { Fabricate(:account, username: 'foo', domain: 'ap.example.com', uri: 'https://old.example.com/users/foo', last_webfingered_at: nil) }
  157. let!(:status) { Fabricate(:status, account: old_account, text: 'foo') }
  158. it 'returns new remote account' do
  159. account = subject.call('foo@ap.example.com')
  160. expect(account.activitypub?).to be true
  161. expect(account.domain).to eq 'ap.example.com'
  162. expect(account.inbox_url).to eq 'https://ap.example.com/users/foo/inbox'
  163. expect(account.uri).to eq 'https://ap.example.com/users/foo'
  164. end
  165. end
  166. it 'processes one remote account at a time using locks' do
  167. wait_for_start = true
  168. fail_occurred = false
  169. return_values = Concurrent::Array.new
  170. threads = Array.new(5) do
  171. Thread.new do
  172. true while wait_for_start
  173. begin
  174. return_values << described_class.new.call('foo@ap.example.com')
  175. rescue ActiveRecord::RecordNotUnique
  176. fail_occurred = true
  177. ensure
  178. RedisConfiguration.pool.checkin if Thread.current[:redis]
  179. end
  180. end
  181. end
  182. wait_for_start = false
  183. threads.each(&:join)
  184. expect(fail_occurred).to be false
  185. expect(return_values).to_not include(nil)
  186. end
  187. end