delete_account_service_spec.rb 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe DeleteAccountService 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 and target records and target notifications' do
  24. subject
  25. expect_deletion_of_associated_owned_records
  26. expect_deletion_of_associated_target_records
  27. expect_deletion_of_associated_target_notifications
  28. end
  29. def expect_deletion_of_associated_owned_records
  30. expect { status.reload }.to raise_error(ActiveRecord::RecordNotFound)
  31. expect { status_with_mention.reload }.to raise_error(ActiveRecord::RecordNotFound)
  32. expect { mention.reload }.to raise_error(ActiveRecord::RecordNotFound)
  33. expect { media_attachment.reload }.to raise_error(ActiveRecord::RecordNotFound)
  34. expect { notification.reload }.to raise_error(ActiveRecord::RecordNotFound)
  35. expect { favourite.reload }.to raise_error(ActiveRecord::RecordNotFound)
  36. expect { active_relationship.reload }.to raise_error(ActiveRecord::RecordNotFound)
  37. expect { passive_relationship.reload }.to raise_error(ActiveRecord::RecordNotFound)
  38. expect { poll.reload }.to raise_error(ActiveRecord::RecordNotFound)
  39. expect { poll_vote.reload }.to raise_error(ActiveRecord::RecordNotFound)
  40. expect { account_note.reload }.to raise_error(ActiveRecord::RecordNotFound)
  41. end
  42. def expect_deletion_of_associated_target_records
  43. expect { endorsement.reload }.to raise_error(ActiveRecord::RecordNotFound)
  44. end
  45. def expect_deletion_of_associated_target_notifications
  46. expect { favourite_notification.reload }.to raise_error(ActiveRecord::RecordNotFound)
  47. expect { follow_notification.reload }.to raise_error(ActiveRecord::RecordNotFound)
  48. expect { mention_notification.reload }.to raise_error(ActiveRecord::RecordNotFound)
  49. expect { poll_notification.reload }.to raise_error(ActiveRecord::RecordNotFound)
  50. expect { status_notification.reload }.to raise_error(ActiveRecord::RecordNotFound)
  51. end
  52. end
  53. describe '#call on local account', :inline_jobs do
  54. before do
  55. stub_request(:post, remote_alice.inbox_url).to_return(status: 201)
  56. stub_request(:post, remote_bob.inbox_url).to_return(status: 201)
  57. end
  58. let!(:remote_alice) { Fabricate(:account, inbox_url: 'https://alice.com/inbox', domain: 'alice.com', protocol: :activitypub) }
  59. let!(:remote_bob) { Fabricate(:account, inbox_url: 'https://bob.com/inbox', domain: 'bob.com', protocol: :activitypub) }
  60. include_examples 'common behavior' do
  61. let(:account) { Fabricate(:account) }
  62. let(:local_follower) { Fabricate(:account) }
  63. it 'sends a delete actor activity to all known inboxes' do
  64. subject
  65. expect(a_request(:post, remote_alice.inbox_url)).to have_been_made.once
  66. expect(a_request(:post, remote_bob.inbox_url)).to have_been_made.once
  67. end
  68. end
  69. end
  70. describe '#call on remote account', :inline_jobs do
  71. before do
  72. stub_request(:post, account.inbox_url).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(post_to_inbox_with_reject).to have_been_made.once
  80. expect(post_to_inbox_with_undo).to have_been_made.once
  81. end
  82. def post_to_inbox_with_undo
  83. a_request(:post, account.inbox_url).with(
  84. body: hash_including({
  85. 'type' => 'Undo',
  86. 'object' => hash_including({
  87. 'type' => 'Follow',
  88. 'actor' => ActivityPub::TagManager.instance.uri_for(local_follower),
  89. 'object' => account.uri,
  90. }),
  91. })
  92. )
  93. end
  94. def post_to_inbox_with_reject
  95. a_request(:post, account.inbox_url).with(
  96. body: hash_including({
  97. 'type' => 'Reject',
  98. 'object' => hash_including({
  99. 'type' => 'Follow',
  100. 'actor' => account.uri,
  101. 'object' => ActivityPub::TagManager.instance.uri_for(local_follower),
  102. }),
  103. })
  104. )
  105. end
  106. end
  107. end
  108. end