Skip to content

A platform for community discussion. Free, open, simple.

1.1 KB
Raw Blame
# frozen_string_literal: true

RSpec.describe Jobs::ClosePoll do
  let(:post) { Fabricate(:post, raw: "[poll]\n- A\n- B\n[/poll]") }

  describe "missing arguments" do
    it "raises InvalidParameters for missing poll_name or post_id" do
      expect do Jobs::ClosePoll.new.execute(post_id: post.id) end.to raise_error(
        Discourse::InvalidParameters,
        "poll_name",
      )

      expect do Jobs::ClosePoll.new.execute(poll_name: "poll") end.to raise_error(
        Discourse::InvalidParameters,
        "post_id",
      )
    end
  end

  it "automatically closes a poll" do
    expect(post.polls.first.closed?).to eq(false)

    Jobs::ClosePoll.new.execute(post_id: post.id, poll_name: "poll")

    poll = post.polls.first
    expect(poll.closed?).to eq(true)
    expect(poll.closed_by_id).to eq(Discourse.system_user.id)
  end

  it "logs the automatic close as the system user" do
    Jobs::ClosePoll.new.execute(post_id: post.id, poll_name: "poll")

    log = UserHistory.last
    expect(log.custom_type).to eq("poll_closed")
    expect(log.acting_user_id).to eq(Discourse.system_user.id)
  end
end