process_account_service_spec.rb 6.7 KB

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