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

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

Also fix spec that wasn’t testing anything.

diff --git a/plugins/poll/jobs/regular/close_poll.rb b/plugins/poll/jobs/regular/close_poll.rb
index ccfac10..6e75241 100644
--- a/plugins/poll/jobs/regular/close_poll.rb
+++ b/plugins/poll/jobs/regular/close_poll.rb
@@ -3,7 +3,19 @@ module Jobs
   class ClosePoll < Jobs::Base
 
     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
diff --git a/plugins/poll/plugin.rb b/plugins/poll/plugin.rb
index cbf7ab4..9d62323 100644
--- a/plugins/poll/plugin.rb
+++ b/plugins/poll/plugin.rb
@@ -221,10 +221,15 @@ after_initialize do
 
       def schedule_jobs(post)
         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
-            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
diff --git a/plugins/poll/spec/controllers/posts_controller_spec.rb b/plugins/poll/spec/controllers/posts_controller_spec.rb
index 912e58c..30fcad9 100644
--- a/plugins/poll/spec/controllers/posts_controller_spec.rb
+++ b/plugins/poll/spec/controllers/posts_controller_spec.rb
@@ -35,17 +35,29 @@ describe PostsController do
     end
 
     it "schedules auto-close job" do
+      freeze_time
       name = "auto_close"
       close_date = 1.month.from_now
 
-      post :create, params: {
-        title: title, raw: "[poll name=#{name} close=#{close_date.iso8601}]\n- A\n- B\n[/poll]"
-      }, format: :json
+      expect do
+        post :create, params: {
+          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)
       json = ::JSON.parse(response.body)
-      expect(Poll.find_by(post_id: json["id"]).close_at).to be
-      expect(Jobs.scheduled_for(:close_poll, post_id: json["id"], poll_name: name)).to be
+      post_id = json["id"]
+
+      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
 
     it "should have different options" do
diff --git a/plugins/poll/spec/jobs/regular/close_poll_spec.rb b/plugins/poll/spec/jobs/regular/close_poll_spec.rb
index 3dfe218..8e5c2b2 100644
--- a/plugins/poll/spec/jobs/regular/close_poll_spec.rb
+++ b/plugins/poll/spec/jobs/regular/close_poll_spec.rb
@@ -1,10 +1,21 @@
 require "rails_helper"
 
 describe Jobs::ClosePoll do
+  let(:post) { Fabricate(:post, raw: "[poll]\n- A\n- B\n[/poll]") }
 
-  it "automatically closes a poll" do
-    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
     expect(post.polls.first.closed?).to eq(false)
 
     Jobs::ClosePoll.new.execute(post_id: post.id, poll_name: "poll")

GitHub
sha: 71ce9ced