You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

233 lines
9.4 KiB

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