2022-03-03 02:49:36 -06:00
|
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
2022-07-27 21:27:38 -05:00
|
|
|
|
RSpec.describe PostActionDestroyer do
|
2023-11-09 16:47:59 -06:00
|
|
|
|
fab!(:admin)
|
2023-12-13 03:18:42 -06:00
|
|
|
|
fab!(:user) { Fabricate(:user, refresh_auto_groups: true) }
|
2023-11-09 16:47:59 -06:00
|
|
|
|
fab!(:post)
|
2022-03-03 02:49:36 -06:00
|
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
|
describe "#perform" do
|
|
|
|
|
context "with like" do
|
|
|
|
|
context "when post action exists" do
|
|
|
|
|
before { PostActionCreator.new(user, post, PostActionType.types[:like]).perform }
|
2022-03-03 02:49:36 -06:00
|
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
|
it "destroys the post action" do
|
|
|
|
|
expect { PostActionDestroyer.destroy(user, post, :like) }.to change {
|
|
|
|
|
PostAction.count
|
|
|
|
|
}.by(-1)
|
2022-03-03 02:49:36 -06:00
|
|
|
|
end
|
|
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
|
it "notifies subscribers" do
|
2022-03-03 02:49:36 -06:00
|
|
|
|
expect(post.reload.like_count).to eq(1)
|
|
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
|
messages = MessageBus.track_publish { PostActionDestroyer.destroy(user, post, :like) }
|
2022-03-03 02:49:36 -06:00
|
|
|
|
|
2022-06-27 16:21:05 -05:00
|
|
|
|
message = messages.find { |msg| msg.data[:type] === :unliked }.data
|
|
|
|
|
expect(message).to be_present
|
2022-05-09 15:23:39 -05:00
|
|
|
|
expect(message[:type]).to eq(:unliked)
|
2022-03-03 02:49:36 -06:00
|
|
|
|
expect(message[:likes_count]).to eq(0)
|
2022-05-09 15:23:39 -05:00
|
|
|
|
expect(message[:user_id]).to eq(user.id)
|
2022-03-03 02:49:36 -06:00
|
|
|
|
end
|
2022-06-27 16:21:05 -05:00
|
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
|
it "notifies updated topic stats to subscribers" do
|
2022-06-27 16:21:05 -05:00
|
|
|
|
topic = Fabricate(:topic)
|
|
|
|
|
post = Fabricate(:post, topic: topic)
|
|
|
|
|
PostActionCreator.new(user, post, PostActionType.types[:like]).perform
|
|
|
|
|
|
|
|
|
|
expect(post.reload.like_count).to eq(1)
|
|
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
|
messages =
|
|
|
|
|
MessageBus.track_publish("/topic/#{topic.id}") do
|
|
|
|
|
PostActionDestroyer.destroy(user, post, :like)
|
|
|
|
|
end
|
2022-06-27 16:21:05 -05:00
|
|
|
|
|
|
|
|
|
stats_message = messages.select { |msg| msg.data[:type] == :stats }.first
|
|
|
|
|
expect(stats_message).to be_present
|
|
|
|
|
expect(stats_message.data[:like_count]).to eq(0)
|
|
|
|
|
end
|
2023-08-09 12:55:00 -05:00
|
|
|
|
|
|
|
|
|
it "triggers the right flag events" do
|
|
|
|
|
PostActionCreator.new(user, post, PostActionType.types[:inappropriate]).perform
|
|
|
|
|
events =
|
|
|
|
|
DiscourseEvent.track_events { PostActionDestroyer.destroy(user, post, :inappropriate) }
|
|
|
|
|
event_names = events.map { |event| event[:event_name] }
|
|
|
|
|
expect(event_names).to include(:flag_destroyed)
|
|
|
|
|
expect(event_names).not_to include(:like_destroyed)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it "triggers the right like events" do
|
|
|
|
|
events = DiscourseEvent.track_events { PostActionDestroyer.destroy(user, post, :like) }
|
|
|
|
|
event_names = events.map { |event| event[:event_name] }
|
|
|
|
|
expect(event_names).to include(:like_destroyed)
|
|
|
|
|
expect(event_names).not_to include(:flag_destroyed)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it "sends the right event arguments" do
|
|
|
|
|
events = DiscourseEvent.track_events { PostActionDestroyer.destroy(user, post, :like) }
|
|
|
|
|
event = events.find { |e| e[:event_name] == :like_destroyed }
|
|
|
|
|
expect(event.present?).to eq(true)
|
|
|
|
|
expect(event[:params].first).to be_instance_of(PostAction)
|
|
|
|
|
expect(event[:params].second).to be_instance_of(PostActionDestroyer)
|
|
|
|
|
end
|
2022-03-03 02:49:36 -06:00
|
|
|
|
end
|
|
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
|
context "when post action doesn’t exist" do
|
|
|
|
|
it "fails" do
|
2022-07-27 11:14:14 -05:00
|
|
|
|
result = PostActionDestroyer.destroy(user, post, :like)
|
|
|
|
|
expect(result.success).to eq(false)
|
|
|
|
|
expect(result.not_found).to eq(true)
|
2022-03-03 02:49:36 -06:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
|
context "with any other notifiable type" do
|
|
|
|
|
before { PostActionCreator.new(user, post, PostActionType.types[:spam]).perform }
|
2022-03-03 02:49:36 -06:00
|
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
|
it "destroys the post action" do
|
|
|
|
|
expect { PostActionDestroyer.destroy(user, post, :spam) }.to change { PostAction.count }.by(
|
|
|
|
|
-1,
|
|
|
|
|
)
|
2022-03-03 02:49:36 -06:00
|
|
|
|
end
|
|
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
|
it "notifies subscribers" do
|
|
|
|
|
messages = MessageBus.track_publish { PostActionDestroyer.destroy(user, post, :spam) }
|
2022-03-03 02:49:36 -06:00
|
|
|
|
|
|
|
|
|
expect(messages.last.data[:type]).to eq(:acted)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|