delete_account_service_spec.rb 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe DeleteAccountService, type: :service do
  4. shared_examples 'common behavior' do
  5. subject { described_class.new.call(account) }
  6. let!(:status) { Fabricate(:status, account: account) }
  7. let!(:mention) { Fabricate(:mention, account: local_follower) }
  8. let!(:status_with_mention) { Fabricate(:status, account: account, mentions: [mention]) }
  9. let!(:media_attachment) { Fabricate(:media_attachment, account: account) }
  10. let!(:notification) { Fabricate(:notification, account: account) }
  11. let!(:favourite) { Fabricate(:favourite, account: account, status: Fabricate(:status, account: local_follower)) }
  12. let!(:poll) { Fabricate(:poll, account: account) }
  13. let!(:poll_vote) { Fabricate(:poll_vote, account: local_follower, poll: poll) }
  14. let!(:active_relationship) { Fabricate(:follow, account: account, target_account: local_follower) }
  15. let!(:passive_relationship) { Fabricate(:follow, account: local_follower, target_account: account) }
  16. let!(:endorsement) { Fabricate(:account_pin, account: local_follower, target_account: account) }
  17. let!(:mention_notification) { Fabricate(:notification, account: local_follower, activity: mention, type: :mention) }
  18. let!(:status_notification) { Fabricate(:notification, account: local_follower, activity: status, type: :status) }
  19. let!(:poll_notification) { Fabricate(:notification, account: local_follower, activity: poll, type: :poll) }
  20. let!(:favourite_notification) { Fabricate(:notification, account: local_follower, activity: favourite, type: :favourite) }
  21. let!(:follow_notification) { Fabricate(:notification, account: local_follower, activity: active_relationship, type: :follow) }
  22. let!(:account_note) { Fabricate(:account_note, account: account) }
  23. it 'deletes associated owned records' do
  24. expect { subject }.to change {
  25. [
  26. account.statuses,
  27. account.media_attachments,
  28. account.notifications,
  29. account.favourites,
  30. account.active_relationships,
  31. account.passive_relationships,
  32. account.polls,
  33. account.account_notes,
  34. ].map(&:count)
  35. }.from([2, 1, 1, 1, 1, 1, 1, 1]).to([0, 0, 0, 0, 0, 0, 0, 0])
  36. end
  37. it 'deletes associated target records' do
  38. expect { subject }.to change {
  39. [
  40. AccountPin.where(target_account: account),
  41. ].map(&:count)
  42. }.from([1]).to([0])
  43. end
  44. it 'deletes associated target notifications' do
  45. expect { subject }.to change {
  46. %w(
  47. poll favourite status mention follow
  48. ).map { |type| Notification.where(type: type).count }
  49. }.from([1, 1, 1, 1, 1]).to([0, 0, 0, 0, 0])
  50. end
  51. end
  52. describe '#call on local account' do
  53. before do
  54. stub_request(:post, 'https://alice.com/inbox').to_return(status: 201)
  55. stub_request(:post, 'https://bob.com/inbox').to_return(status: 201)
  56. end
  57. let!(:remote_alice) { Fabricate(:account, inbox_url: 'https://alice.com/inbox', domain: 'alice.com', protocol: :activitypub) }
  58. let!(:remote_bob) { Fabricate(:account, inbox_url: 'https://bob.com/inbox', domain: 'bob.com', protocol: :activitypub) }
  59. include_examples 'common behavior' do
  60. let!(:account) { Fabricate(:account) }
  61. let!(:local_follower) { Fabricate(:account) }
  62. it 'sends a delete actor activity to all known inboxes' do
  63. subject
  64. expect(a_request(:post, 'https://alice.com/inbox')).to have_been_made.once
  65. expect(a_request(:post, 'https://bob.com/inbox')).to have_been_made.once
  66. end
  67. end
  68. end
  69. describe '#call on remote account' do
  70. before do
  71. stub_request(:post, 'https://alice.com/inbox').to_return(status: 201)
  72. stub_request(:post, 'https://bob.com/inbox').to_return(status: 201)
  73. end
  74. include_examples 'common behavior' do
  75. let!(:account) { Fabricate(:account, inbox_url: 'https://bob.com/inbox', protocol: :activitypub, domain: 'bob.com') }
  76. let!(:local_follower) { Fabricate(:account) }
  77. it 'sends expected activities to followed and follower inboxes' do
  78. subject
  79. expect(a_request(:post, account.inbox_url).with(
  80. body:
  81. hash_including({
  82. 'type' => 'Reject',
  83. 'object' => hash_including({
  84. 'type' => 'Follow',
  85. 'actor' => account.uri,
  86. 'object' => ActivityPub::TagManager.instance.uri_for(local_follower),
  87. }),
  88. })
  89. )).to have_been_made.once
  90. expect(a_request(:post, account.inbox_url).with(
  91. body: hash_including({
  92. 'type' => 'Undo',
  93. 'object' => hash_including({
  94. 'type' => 'Follow',
  95. 'actor' => ActivityPub::TagManager.instance.uri_for(local_follower),
  96. 'object' => account.uri,
  97. }),
  98. })
  99. )).to have_been_made.once
  100. end
  101. end
  102. end
  103. end