update_status_service_spec.rb 6.1 KB

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