process_account_service_spec.rb 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe ActivityPub::ProcessAccountService, type: :service do
  4. subject { described_class.new }
  5. context 'with property values' do
  6. let(:payload) do
  7. {
  8. id: 'https://foo.test',
  9. type: 'Actor',
  10. inbox: 'https://foo.test/inbox',
  11. attachment: [
  12. { type: 'PropertyValue', name: 'Pronouns', value: 'They/them' },
  13. { type: 'PropertyValue', name: 'Occupation', value: 'Unit test' },
  14. { type: 'PropertyValue', name: 'non-string', value: %w(foo bar) },
  15. ],
  16. }.with_indifferent_access
  17. end
  18. it 'parses out of attachment' do
  19. account = subject.call('alice', 'example.com', payload)
  20. expect(account.fields).to be_a Array
  21. expect(account.fields.size).to eq 2
  22. expect(account.fields[0]).to be_a Account::Field
  23. expect(account.fields[0].name).to eq 'Pronouns'
  24. expect(account.fields[0].value).to eq 'They/them'
  25. expect(account.fields[1]).to be_a Account::Field
  26. expect(account.fields[1].name).to eq 'Occupation'
  27. expect(account.fields[1].value).to eq 'Unit test'
  28. end
  29. end
  30. context 'when account is not suspended' do
  31. subject { described_class.new.call('alice', 'example.com', payload) }
  32. let!(:account) { Fabricate(:account, username: 'alice', domain: 'example.com') }
  33. let(:payload) do
  34. {
  35. id: 'https://foo.test',
  36. type: 'Actor',
  37. inbox: 'https://foo.test/inbox',
  38. suspended: true,
  39. }.with_indifferent_access
  40. end
  41. before do
  42. allow(Admin::SuspensionWorker).to receive(:perform_async)
  43. end
  44. it 'suspends account remotely' do
  45. expect(subject.suspended?).to be true
  46. expect(subject.suspension_origin_remote?).to be true
  47. end
  48. it 'queues suspension worker' do
  49. subject
  50. expect(Admin::SuspensionWorker).to have_received(:perform_async)
  51. end
  52. end
  53. context 'when account is suspended' do
  54. subject { described_class.new.call('alice', 'example.com', payload) }
  55. let!(:account) { Fabricate(:account, username: 'alice', domain: 'example.com', display_name: '') }
  56. let(:payload) do
  57. {
  58. id: 'https://foo.test',
  59. type: 'Actor',
  60. inbox: 'https://foo.test/inbox',
  61. suspended: false,
  62. name: 'Hoge',
  63. }.with_indifferent_access
  64. end
  65. before do
  66. allow(Admin::UnsuspensionWorker).to receive(:perform_async)
  67. account.suspend!(origin: suspension_origin)
  68. end
  69. context 'when locally' do
  70. let(:suspension_origin) { :local }
  71. it 'does not unsuspend it' do
  72. expect(subject.suspended?).to be true
  73. end
  74. it 'does not update any attributes' do
  75. expect(subject.display_name).to_not eq 'Hoge'
  76. end
  77. end
  78. context 'when remotely' do
  79. let(:suspension_origin) { :remote }
  80. it 'unsuspends it' do
  81. expect(subject.suspended?).to be false
  82. end
  83. it 'queues unsuspension worker' do
  84. subject
  85. expect(Admin::UnsuspensionWorker).to have_received(:perform_async)
  86. end
  87. it 'updates attributes' do
  88. expect(subject.display_name).to eq 'Hoge'
  89. end
  90. end
  91. end
  92. context 'when discovering many subdomains in a short timeframe' do
  93. subject do
  94. 8.times do |i|
  95. domain = "test#{i}.testdomain.com"
  96. json = {
  97. id: "https://#{domain}/users/1",
  98. type: 'Actor',
  99. inbox: "https://#{domain}/inbox",
  100. }.with_indifferent_access
  101. described_class.new.call('alice', domain, json)
  102. end
  103. end
  104. before do
  105. stub_const 'ActivityPub::ProcessAccountService::SUBDOMAINS_RATELIMIT', 5
  106. end
  107. it 'creates at least some accounts' do
  108. expect { subject }.to change { Account.remote.count }.by_at_least(2)
  109. end
  110. it 'creates no more account than the limit allows' do
  111. expect { subject }.to change { Account.remote.count }.by_at_most(5)
  112. end
  113. end
  114. context 'when Accounts referencing other accounts' do
  115. let(:payload) do
  116. {
  117. '@context': ['https://www.w3.org/ns/activitystreams'],
  118. id: 'https://foo.test/users/1',
  119. type: 'Person',
  120. inbox: 'https://foo.test/inbox',
  121. featured: 'https://foo.test/users/1/featured',
  122. preferredUsername: 'user1',
  123. }.with_indifferent_access
  124. end
  125. before do
  126. stub_const 'ActivityPub::ProcessAccountService::DISCOVERIES_PER_REQUEST', 5
  127. 8.times do |i|
  128. actor_json = {
  129. '@context': ['https://www.w3.org/ns/activitystreams'],
  130. id: "https://foo.test/users/#{i}",
  131. type: 'Person',
  132. inbox: 'https://foo.test/inbox',
  133. featured: "https://foo.test/users/#{i}/featured",
  134. preferredUsername: "user#{i}",
  135. }.with_indifferent_access
  136. status_json = {
  137. '@context': ['https://www.w3.org/ns/activitystreams'],
  138. id: "https://foo.test/users/#{i}/status",
  139. attributedTo: "https://foo.test/users/#{i}",
  140. type: 'Note',
  141. content: "@user#{i + 1} test",
  142. tag: [
  143. {
  144. type: 'Mention',
  145. href: "https://foo.test/users/#{i + 1}",
  146. name: "@user#{i + 1}",
  147. },
  148. ],
  149. to: ['as:Public', "https://foo.test/users/#{i + 1}"],
  150. }.with_indifferent_access
  151. featured_json = {
  152. '@context': ['https://www.w3.org/ns/activitystreams'],
  153. id: "https://foo.test/users/#{i}/featured",
  154. type: 'OrderedCollection',
  155. totalItems: 1,
  156. orderedItems: [status_json],
  157. }.with_indifferent_access
  158. webfinger = {
  159. subject: "acct:user#{i}@foo.test",
  160. links: [{ rel: 'self', href: "https://foo.test/users/#{i}" }],
  161. }.with_indifferent_access
  162. stub_request(:get, "https://foo.test/users/#{i}").to_return(status: 200, body: actor_json.to_json, headers: { 'Content-Type': 'application/activity+json' })
  163. stub_request(:get, "https://foo.test/users/#{i}/featured").to_return(status: 200, body: featured_json.to_json, headers: { 'Content-Type': 'application/activity+json' })
  164. stub_request(:get, "https://foo.test/users/#{i}/status").to_return(status: 200, body: status_json.to_json, headers: { 'Content-Type': 'application/activity+json' })
  165. stub_request(:get, "https://foo.test/.well-known/webfinger?resource=acct:user#{i}@foo.test").to_return(body: webfinger.to_json, headers: { 'Content-Type': 'application/jrd+json' })
  166. end
  167. end
  168. it 'creates at least some accounts' do
  169. expect { subject.call('user1', 'foo.test', payload) }.to change { Account.remote.count }.by_at_least(2)
  170. end
  171. it 'creates no more account than the limit allows' do
  172. expect { subject.call('user1', 'foo.test', payload) }.to change { Account.remote.count }.by_at_most(5)
  173. end
  174. end
  175. end