2019-07-07 11:18:12 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-10-15 14:42:47 +05:30
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe MailScheduler::NotificationServiceWorker do
|
|
|
|
let(:worker) { described_class.new }
|
|
|
|
let(:method) { 'new_key' }
|
2020-01-01 13:55:28 +05:30
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
let_it_be(:key) { create(:personal_key) }
|
2018-10-15 14:42:47 +05:30
|
|
|
|
|
|
|
def serialize(*args)
|
|
|
|
ActiveJob::Arguments.serialize(args)
|
|
|
|
end
|
|
|
|
|
2019-03-02 22:35:43 +05:30
|
|
|
def deserialize(args)
|
|
|
|
ActiveJob::Arguments.deserialize(args)
|
|
|
|
end
|
|
|
|
|
2018-10-15 14:42:47 +05:30
|
|
|
describe '#perform' do
|
|
|
|
it 'deserializes arguments from global IDs' do
|
|
|
|
expect(worker.notification_service).to receive(method).with(key)
|
|
|
|
|
|
|
|
worker.perform(method, *serialize(key))
|
|
|
|
end
|
|
|
|
|
2019-01-03 12:48:30 +05:30
|
|
|
context 'when the arguments cannot be deserialized' do
|
2019-02-15 15:39:39 +05:30
|
|
|
context 'when the arguments are not deserializeable' do
|
|
|
|
it 'raises exception' do
|
|
|
|
expect(worker.notification_service).not_to receive(method)
|
|
|
|
expect { worker.perform(method, key.to_global_id.to_s.succ) }.to raise_exception(ArgumentError)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the arguments are deserializeable' do
|
|
|
|
it 'does nothing' do
|
|
|
|
serialized_arguments = *serialize(key)
|
|
|
|
key.destroy!
|
2018-10-15 14:42:47 +05:30
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
expect(worker.notification_service).not_to receive(method)
|
|
|
|
expect { worker.perform(method, serialized_arguments) }.not_to raise_exception
|
|
|
|
end
|
2018-10-15 14:42:47 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the method is not a public method' do
|
|
|
|
it 'raises NoMethodError' do
|
|
|
|
expect { worker.perform('notifiable?', *serialize(key)) }.to raise_error(NoMethodError)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
describe '.perform_async' do
|
2019-03-02 22:35:43 +05:30
|
|
|
around do |example|
|
|
|
|
Sidekiq::Testing.fake! { example.run }
|
|
|
|
end
|
|
|
|
|
2018-10-15 14:42:47 +05:30
|
|
|
it 'serializes arguments as global IDs when scheduling' do
|
2019-03-02 22:35:43 +05:30
|
|
|
described_class.perform_async(method, key)
|
|
|
|
|
|
|
|
expect(described_class.jobs.count).to eq(1)
|
|
|
|
expect(described_class.jobs.first).to include('args' => [method, *serialize(key)])
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with ActiveController::Parameters' do
|
|
|
|
let(:parameters) { ActionController::Parameters.new(hash) }
|
|
|
|
|
|
|
|
let(:hash) do
|
|
|
|
{
|
|
|
|
"nested" => {
|
|
|
|
"hash" => true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when permitted' do
|
|
|
|
before do
|
|
|
|
parameters.permit!
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'serializes as a serializable Hash' do
|
|
|
|
described_class.perform_async(method, parameters)
|
2018-10-15 14:42:47 +05:30
|
|
|
|
2019-03-02 22:35:43 +05:30
|
|
|
expect(described_class.jobs.count).to eq(1)
|
|
|
|
expect(deserialize(described_class.jobs.first['args']))
|
|
|
|
.to eq([method, hash])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when not permitted' do
|
|
|
|
it 'fails to serialize' do
|
|
|
|
expect { described_class.perform_async(method, parameters) }
|
|
|
|
.to raise_error(ActionController::UnfilteredParameters)
|
|
|
|
end
|
2018-10-15 14:42:47 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|