process_account_service_spec.rb 6.7 KB

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