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.

157 lines
4.6 KiB

  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 'identity proofs' do
  30. let(:payload) do
  31. {
  32. id: 'https://foo.test',
  33. type: 'Actor',
  34. inbox: 'https://foo.test/inbox',
  35. attachment: [
  36. { type: 'IdentityProof', name: 'Alice', signatureAlgorithm: 'keybase', signatureValue: 'a' * 66 },
  37. ],
  38. }.with_indifferent_access
  39. end
  40. it 'parses out of attachment' do
  41. allow(ProofProvider::Keybase::Worker).to receive(:perform_async)
  42. account = subject.call('alice', 'example.com', payload)
  43. expect(account.identity_proofs.count).to eq 1
  44. proof = account.identity_proofs.first
  45. expect(proof.provider).to eq 'keybase'
  46. expect(proof.provider_username).to eq 'Alice'
  47. expect(proof.token).to eq 'a' * 66
  48. end
  49. it 'removes no longer present proofs' do
  50. allow(ProofProvider::Keybase::Worker).to receive(:perform_async)
  51. account = Fabricate(:account, username: 'alice', domain: 'example.com')
  52. old_proof = Fabricate(:account_identity_proof, account: account, provider: 'keybase', provider_username: 'Bob', token: 'b' * 66)
  53. subject.call('alice', 'example.com', payload)
  54. expect(account.identity_proofs.count).to eq 1
  55. expect(account.identity_proofs.find_by(id: old_proof.id)).to be_nil
  56. end
  57. it 'queues a validity check on the proof' do
  58. allow(ProofProvider::Keybase::Worker).to receive(:perform_async)
  59. account = subject.call('alice', 'example.com', payload)
  60. expect(ProofProvider::Keybase::Worker).to have_received(:perform_async)
  61. end
  62. end
  63. context 'when account is not suspended' do
  64. let!(:account) { Fabricate(:account, username: 'alice', domain: 'example.com') }
  65. let(:payload) do
  66. {
  67. id: 'https://foo.test',
  68. type: 'Actor',
  69. inbox: 'https://foo.test/inbox',
  70. suspended: true,
  71. }.with_indifferent_access
  72. end
  73. before do
  74. allow(Admin::SuspensionWorker).to receive(:perform_async)
  75. end
  76. subject { described_class.new.call('alice', 'example.com', payload) }
  77. it 'suspends account remotely' do
  78. expect(subject.suspended?).to be true
  79. expect(subject.suspension_origin_remote?).to be true
  80. end
  81. it 'queues suspension worker' do
  82. subject
  83. expect(Admin::SuspensionWorker).to have_received(:perform_async)
  84. end
  85. end
  86. context 'when account is suspended' do
  87. let!(:account) { Fabricate(:account, username: 'alice', domain: 'example.com', display_name: '') }
  88. let(:payload) do
  89. {
  90. id: 'https://foo.test',
  91. type: 'Actor',
  92. inbox: 'https://foo.test/inbox',
  93. suspended: false,
  94. name: 'Hoge',
  95. }.with_indifferent_access
  96. end
  97. before do
  98. allow(Admin::UnsuspensionWorker).to receive(:perform_async)
  99. account.suspend!(origin: suspension_origin)
  100. end
  101. subject { described_class.new.call('alice', 'example.com', payload) }
  102. context 'locally' do
  103. let(:suspension_origin) { :local }
  104. it 'does not unsuspend it' do
  105. expect(subject.suspended?).to be true
  106. end
  107. it 'does not update any attributes' do
  108. expect(subject.display_name).to_not eq 'Hoge'
  109. end
  110. end
  111. context 'remotely' do
  112. let(:suspension_origin) { :remote }
  113. it 'unsuspends it' do
  114. expect(subject.suspended?).to be false
  115. end
  116. it 'queues unsuspension worker' do
  117. subject
  118. expect(Admin::UnsuspensionWorker).to have_received(:perform_async)
  119. end
  120. it 'updates attributes' do
  121. expect(subject.display_name).to eq 'Hoge'
  122. end
  123. end
  124. end
  125. end