1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
|
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe MailScheduler::NotificationServiceWorker do
let(:worker) { described_class.new }
let(:method) { 'new_key' }
let_it_be(:key) { create(:personal_key) }
def serialize(*args)
ActiveJob::Arguments.serialize(args)
end
def deserialize(args)
ActiveJob::Arguments.deserialize(args)
end
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
context 'when the arguments cannot be deserialized' do
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!
expect(worker.notification_service).not_to receive(method)
expect { worker.perform(method, serialized_arguments) }.not_to raise_exception
end
end
end
context 'when the method is allowed' do
it 'calls the method on NotificationService' do
NotificationService.permitted_actions.each do |action|
expect(worker.notification_service).to receive(action).with(key)
worker.perform(action, *serialize(key))
end
end
end
context 'when the method is not allowed' do
context 'when verify_mail_scheduler_notification_service_worker_method_names is enabled' do
it 'raises ArgumentError' do
expect(worker.notification_service).not_to receive(:async)
expect(worker.notification_service).not_to receive(:foo)
expect { worker.perform('async', *serialize(key)) }
.to raise_error(ArgumentError, 'async not allowed for MailScheduler::NotificationServiceWorker')
expect { worker.perform('foo', *serialize(key)) }
.to raise_error(ArgumentError, 'foo not allowed for MailScheduler::NotificationServiceWorker')
end
end
context 'when verify_mail_scheduler_notification_service_worker_method_names is disabled' do
before do
stub_feature_flags(verify_mail_scheduler_notification_service_worker_method_names: false)
end
it 'forwards the argument to the service' do
expect(worker.notification_service).to receive(:async)
expect(worker.notification_service).to receive(:foo)
worker.perform('async', *serialize(key))
worker.perform('foo', *serialize(key))
end
end
end
end
describe '.perform_async' do
around do |example|
Sidekiq::Testing.fake! { example.run }
end
it 'serializes arguments as global IDs when scheduling' do
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)
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
end
end
end
end
|