docbrown/spec/workers/slack/messengers/worker_spec.rb
Daniel Uber 4b4d8a7234
Ensure arguments to perform_async are json safe (#16285)
* Convert symbol hash keys to strings when calling .perform_async

Fixes a warning from Sidekiq 6.4.0+ about perform_async arguments
which are not equal when passed to perform (`JSON.parse(JSON.dump(arg))`
should equal arg).

This is a safety measure to prevent passing objects (like classes, or
model instances) rather than their representations (like a class name,
or a model's attributes hash).

This warning will be an error in sidekiq 7

* Turn warning into an error in non-production environments

* Use string keys for reaction notification and article fetched

Missed these two on the first pass

* Update example argument hashes for #enqueues_on_correct_queue

Since this calls perform_async under the hood we need to pass json
safe hashes in the test cases as well.

https://github.com/forem/forem/blob/main/spec/workers/shared_examples/enqueues_on_correct_queue.rb

* Convert keys from FollowData#to_h to string before perform_async

I'm not sure enough where else (outside of notification) to_h is being
called, so I'm converting here when building args, rather than in
FollowData#to_h, which might be my next step.

* Let FollowData#to_h return a hash with string keys

Update spec to use string keys as well.

* Make to_h return string keys for ReactionData

Like FollowData, the #to_h method is only used to call
notifications (this is used to enqueue sidekiq jobs).

* Remove a key that was in the hash

Since reaction_data calls to_h, it gets string and not symbol,
keys. Call Hash#except with a key that was actually there.
2022-01-25 18:07:40 -06:00

48 lines
1.1 KiB
Ruby

require "rails_helper"
RSpec.describe Slack::Messengers::Worker, type: :worker do
let(:worker) { subject }
let(:params) do
{
message: "hello",
channel: "#help",
username: "sloan_watch_bot",
icon_emoji: ":sloan:"
}
end
include_examples "#enqueues_on_correct_queue", "default", [
{ "message" => "hello", "channel" => "#help", "username" => "sloan_watch_bot", "icon_emoji" => ":sloan:" },
]
describe "#perform_now" do
it "sends a message to Slack" do
allow(Slack::Announcer).to receive(:call)
worker.perform(params)
expect(Slack::Announcer).to have_received(:call).with(params)
end
it "does nothing if there is missing data" do
allow(SlackClient).to receive(:ping)
worker.perform(
message: nil,
channel: nil,
username: nil,
icon_emoji: nil,
)
expect(SlackClient).not_to have_received(:ping)
end
it "works with keys as Strings" do
allow(Slack::Announcer).to receive(:call)
worker.perform(params.stringify_keys)
expect(Slack::Announcer).to have_received(:call).with(params)
end
end
end