FIX: Incorrect arguments were being passed to Jobs::ClosePoll.

Also fix spec that wasn't testing anything.
This commit is contained in:
Guo Xiang Tan 2018-12-13 16:39:36 +08:00
parent 939b82ef0c
commit 71ce9ced47
4 changed files with 50 additions and 10 deletions

View File

@ -3,7 +3,19 @@ module Jobs
class ClosePoll < Jobs::Base class ClosePoll < Jobs::Base
def execute(args) def execute(args)
DiscoursePoll::Poll.toggle_status(args[:post_id], args[:poll_name], "closed", Discourse.system_user) %i{
post_id
poll_name
}.each do |key|
raise Discourse::InvalidParameters.new(key) if args[key].blank?
end
DiscoursePoll::Poll.toggle_status(
args[:post_id],
args[:poll_name],
"closed",
Discourse.system_user
)
end end
end end

View File

@ -221,10 +221,15 @@ after_initialize do
def schedule_jobs(post) def schedule_jobs(post)
Poll.where(post: post).find_each do |poll| Poll.where(post: post).find_each do |poll|
Jobs.cancel_scheduled_job(:close_poll, poll_id: poll.id) job_args = {
post_id: post.id,
poll_name: poll.name
}
Jobs.cancel_scheduled_job(:close_poll, job_args)
if poll.open? && poll.close_at && poll.close_at > Time.zone.now if poll.open? && poll.close_at && poll.close_at > Time.zone.now
Jobs.enqueue_at(poll.close_at, :close_poll, poll_id: poll.id) Jobs.enqueue_at(poll.close_at, :close_poll, job_args)
end end
end end
end end

View File

@ -35,17 +35,29 @@ describe PostsController do
end end
it "schedules auto-close job" do it "schedules auto-close job" do
freeze_time
name = "auto_close" name = "auto_close"
close_date = 1.month.from_now close_date = 1.month.from_now
post :create, params: { expect do
title: title, raw: "[poll name=#{name} close=#{close_date.iso8601}]\n- A\n- B\n[/poll]" post :create, params: {
}, format: :json title: title,
raw: "[poll name=#{name} close=#{close_date.iso8601}]\n- A\n- B\n[/poll]"
}, format: :json
end.to change { Jobs::ClosePoll.jobs.size }.by(1) &
change { Poll.count }.by(1)
expect(response.status).to eq(200) expect(response.status).to eq(200)
json = ::JSON.parse(response.body) json = ::JSON.parse(response.body)
expect(Poll.find_by(post_id: json["id"]).close_at).to be post_id = json["id"]
expect(Jobs.scheduled_for(:close_poll, post_id: json["id"], poll_name: name)).to be
expect(Poll.find_by(post_id: post_id).close_at).to eq(1.month.from_now)
job = Jobs::ClosePoll.jobs.first
job_args = job["args"].first
expect(job_args["post_id"]).to eq(post_id)
expect(job_args["poll_name"]).to eq(name)
end end
it "should have different options" do it "should have different options" do

View File

@ -1,10 +1,21 @@
require "rails_helper" require "rails_helper"
describe Jobs::ClosePoll do describe Jobs::ClosePoll do
let(:post) { Fabricate(:post, raw: "[poll]\n- A\n- B\n[/poll]") }
describe 'missing arguments' do
it 'should raise the right error' 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 it "automatically closes a poll" do
post = Fabricate(:post, raw: "[poll]\n- A\n- B\n[/poll]")
expect(post.polls.first.closed?).to eq(false) expect(post.polls.first.closed?).to eq(false)
Jobs::ClosePoll.new.execute(post_id: post.id, poll_name: "poll") Jobs::ClosePoll.new.execute(post_id: post.id, poll_name: "poll")