update_status_service_spec.rb 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. require 'rails_helper'
  2. RSpec.describe UpdateStatusService, type: :service do
  3. subject { described_class.new }
  4. context 'when nothing changes' do
  5. let!(:status) { Fabricate(:status, text: 'Foo', language: 'en') }
  6. before do
  7. allow(ActivityPub::DistributionWorker).to receive(:perform_async)
  8. subject.call(status, status.account_id, text: 'Foo')
  9. end
  10. it 'does not create an edit' do
  11. expect(status.reload.edits).to be_empty
  12. end
  13. it 'does not notify anyone' do
  14. expect(ActivityPub::DistributionWorker).to_not have_received(:perform_async)
  15. end
  16. end
  17. context 'when text changes' do
  18. let!(:status) { Fabricate(:status, text: 'Foo') }
  19. let(:preview_card) { Fabricate(:preview_card) }
  20. before do
  21. status.preview_cards << preview_card
  22. subject.call(status, status.account_id, text: 'Bar')
  23. end
  24. it 'updates text' do
  25. expect(status.reload.text).to eq 'Bar'
  26. end
  27. it 'resets preview card' do
  28. expect(status.reload.preview_card).to be_nil
  29. end
  30. it 'saves edit history' do
  31. expect(status.edits.pluck(:text)).to eq %w(Foo Bar)
  32. end
  33. end
  34. context 'when content warning changes' do
  35. let!(:status) { Fabricate(:status, text: 'Foo', spoiler_text: '') }
  36. let(:preview_card) { Fabricate(:preview_card) }
  37. before do
  38. status.preview_cards << preview_card
  39. subject.call(status, status.account_id, text: 'Foo', spoiler_text: 'Bar')
  40. end
  41. it 'updates content warning' do
  42. expect(status.reload.spoiler_text).to eq 'Bar'
  43. end
  44. it 'saves edit history' do
  45. expect(status.edits.pluck(:text, :spoiler_text)).to eq [['Foo', ''], ['Foo', 'Bar']]
  46. end
  47. end
  48. context 'when media attachments change' do
  49. let!(:status) { Fabricate(:status, text: 'Foo') }
  50. let!(:detached_media_attachment) { Fabricate(:media_attachment, account: status.account) }
  51. let!(:attached_media_attachment) { Fabricate(:media_attachment, account: status.account) }
  52. before do
  53. status.media_attachments << detached_media_attachment
  54. subject.call(status, status.account_id, text: 'Foo', media_ids: [attached_media_attachment.id])
  55. end
  56. it 'updates media attachments' do
  57. expect(status.ordered_media_attachments).to eq [attached_media_attachment]
  58. end
  59. it 'does not detach detached media attachments' do
  60. expect(detached_media_attachment.reload.status_id).to eq status.id
  61. end
  62. it 'attaches attached media attachments' do
  63. expect(attached_media_attachment.reload.status_id).to eq status.id
  64. end
  65. it 'saves edit history' do
  66. expect(status.edits.pluck(:ordered_media_attachment_ids)).to eq [[detached_media_attachment.id], [attached_media_attachment.id]]
  67. end
  68. end
  69. context 'when poll changes' do
  70. let(:account) { Fabricate(:account) }
  71. let!(:status) { Fabricate(:status, text: 'Foo', account: account, poll_attributes: {options: %w(Foo Bar), account: account, multiple: false, hide_totals: false, expires_at: 7.days.from_now }) }
  72. let!(:poll) { status.poll }
  73. let!(:voter) { Fabricate(:account) }
  74. before do
  75. status.update(poll: poll)
  76. VoteService.new.call(voter, poll, [0])
  77. subject.call(status, status.account_id, text: 'Foo', poll: { options: %w(Bar Baz Foo), expires_in: 5.days.to_i })
  78. end
  79. it 'updates poll' do
  80. poll = status.poll.reload
  81. expect(poll.options).to eq %w(Bar Baz Foo)
  82. end
  83. it 'resets votes' do
  84. poll = status.poll.reload
  85. expect(poll.votes_count).to eq 0
  86. expect(poll.votes.count).to eq 0
  87. expect(poll.cached_tallies).to eq [0, 0, 0]
  88. end
  89. it 'saves edit history' do
  90. expect(status.edits.pluck(:poll_options)).to eq [%w(Foo Bar), %w(Bar Baz Foo)]
  91. end
  92. end
  93. context 'when mentions in text change' do
  94. let!(:account) { Fabricate(:account) }
  95. let!(:alice) { Fabricate(:account, username: 'alice') }
  96. let!(:bob) { Fabricate(:account, username: 'bob') }
  97. let!(:status) { PostStatusService.new.call(account, text: 'Hello @alice') }
  98. before do
  99. subject.call(status, status.account_id, text: 'Hello @bob')
  100. end
  101. it 'changes mentions' do
  102. expect(status.active_mentions.pluck(:account_id)).to eq [bob.id]
  103. end
  104. it 'keeps old mentions as silent mentions' do
  105. expect(status.mentions.pluck(:account_id)).to match_array([alice.id, bob.id])
  106. end
  107. end
  108. context 'when hashtags in text change' do
  109. let!(:account) { Fabricate(:account) }
  110. let!(:status) { PostStatusService.new.call(account, text: 'Hello #foo') }
  111. before do
  112. subject.call(status, status.account_id, text: 'Hello #bar')
  113. end
  114. it 'changes tags' do
  115. expect(status.tags.pluck(:name)).to eq %w(bar)
  116. end
  117. end
  118. it 'notifies ActivityPub about the update' do
  119. status = Fabricate(:status, text: 'Foo')
  120. allow(ActivityPub::DistributionWorker).to receive(:perform_async)
  121. subject.call(status, status.account_id, text: 'Bar')
  122. expect(ActivityPub::DistributionWorker).to have_received(:perform_async)
  123. end
  124. end