reblog_service_spec.rb 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe ReblogService, type: :service do
  4. let(:alice) { Fabricate(:account, username: 'alice') }
  5. context 'when creates a reblog with appropriate visibility' do
  6. subject { described_class.new }
  7. let(:visibility) { :public }
  8. let(:reblog_visibility) { :public }
  9. let(:status) { Fabricate(:status, account: alice, visibility: visibility) }
  10. before do
  11. subject.call(alice, status, visibility: reblog_visibility)
  12. end
  13. describe 'boosting privately' do
  14. let(:reblog_visibility) { :private }
  15. it 'reblogs privately' do
  16. expect(status.reblogs.first.visibility).to eq 'private'
  17. end
  18. end
  19. describe 'public reblogs of private toots should remain private' do
  20. let(:visibility) { :private }
  21. let(:reblog_visibility) { :public }
  22. it 'reblogs privately' do
  23. expect(status.reblogs.first.visibility).to eq 'private'
  24. end
  25. end
  26. end
  27. context 'when the reblogged status is discarded in the meantime' do
  28. let(:status) { Fabricate(:status, account: alice, visibility: :public, text: 'discard-status-text') }
  29. # Add a callback to discard the status being reblogged after the
  30. # validations pass but before the database commit is executed.
  31. before do
  32. Status.class_eval do
  33. before_save :discard_status
  34. def discard_status
  35. Status
  36. .where(id: reblog_of_id)
  37. .where(text: 'discard-status-text')
  38. .update_all(deleted_at: Time.now.utc) # rubocop:disable Rails/SkipsModelValidations
  39. end
  40. end
  41. end
  42. # Remove race condition simulating `discard_status` callback.
  43. after do
  44. Status._save_callbacks.delete(:discard_status)
  45. end
  46. it 'raises an exception' do
  47. expect { subject.call(alice, status) }.to raise_error ActiveRecord::ActiveRecordError
  48. end
  49. end
  50. context 'with ActivityPub' do
  51. subject { described_class.new }
  52. let(:bob) { Fabricate(:account, username: 'bob', protocol: :activitypub, domain: 'example.com', inbox_url: 'http://example.com/inbox') }
  53. let(:status) { Fabricate(:status, account: bob) }
  54. before do
  55. stub_request(:post, bob.inbox_url)
  56. allow(ActivityPub::DistributionWorker).to receive(:perform_async)
  57. subject.call(alice, status)
  58. end
  59. it 'creates a reblog' do
  60. expect(status.reblogs.count).to eq 1
  61. end
  62. describe 'after_create_commit :store_uri' do
  63. it 'keeps consistent reblog count' do
  64. expect(status.reblogs.count).to eq 1
  65. end
  66. end
  67. it 'distributes to followers' do
  68. expect(ActivityPub::DistributionWorker).to have_received(:perform_async)
  69. end
  70. it 'sends an announce activity to the author' do
  71. expect(a_request(:post, bob.inbox_url)).to have_been_made.once
  72. end
  73. end
  74. end