forked from sendgrid/sendgrid-ruby
-
Notifications
You must be signed in to change notification settings - Fork 0
/
eventwebhook_spec.rb
105 lines (94 loc) · 3.05 KB
/
eventwebhook_spec.rb
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
require 'spec_helper'
require './spec/fixtures/event_webhook'
describe SendGrid::EventWebhook do
describe '.verify_signature' do
it 'verifies a valid signature' do
unless skip_jruby
expect(verify(
Fixtures::EventWebhook::PUBLIC_KEY,
Fixtures::EventWebhook::PAYLOAD,
Fixtures::EventWebhook::SIGNATURE,
Fixtures::EventWebhook::TIMESTAMP
)).to be true
end
end
it 'rejects a bad key' do
unless skip_jruby
expect(verify(
Fixtures::EventWebhook::FAILING_PUBLIC_KEY,
Fixtures::EventWebhook::PAYLOAD,
Fixtures::EventWebhook::SIGNATURE,
Fixtures::EventWebhook::TIMESTAMP
)).to be false
end
end
it 'rejects a bad payload' do
unless skip_jruby
expect(verify(
Fixtures::EventWebhook::PUBLIC_KEY,
'payload',
Fixtures::EventWebhook::SIGNATURE,
Fixtures::EventWebhook::TIMESTAMP
)).to be false
end
end
it 'rejects a bad signature' do
unless skip_jruby
expect(verify(
Fixtures::EventWebhook::PUBLIC_KEY,
Fixtures::EventWebhook::PAYLOAD,
Fixtures::EventWebhook::FAILING_SIGNATURE,
Fixtures::EventWebhook::TIMESTAMP
)).to be false
end
end
it 'rejects a bad timestamp' do
unless skip_jruby
expect(verify(
Fixtures::EventWebhook::PUBLIC_KEY,
Fixtures::EventWebhook::PAYLOAD,
Fixtures::EventWebhook::SIGNATURE,
'timestamp'
)).to be false
end
end
it 'rejects a missing signature' do
unless skip_jruby
expect(verify(
Fixtures::EventWebhook::PUBLIC_KEY,
Fixtures::EventWebhook::PAYLOAD,
nil,
Fixtures::EventWebhook::TIMESTAMP
)).to be false
end
end
it 'throws an error when using jruby' do
if skip_jruby
expect do
verify(
Fixtures::EventWebhook::PUBLIC_KEY,
Fixtures::EventWebhook::PAYLOAD,
Fixtures::EventWebhook::SIGNATURE,
Fixtures::EventWebhook::TIMESTAMP
)
end.to raise_error(SendGrid::EventWebhook::NotSupportedError)
end
end
end
end
describe SendGrid::EventWebhookHeader do
it 'sets the signature header constant' do
expect(SendGrid::EventWebhookHeader::SIGNATURE).to eq("HTTP_X_TWILIO_EMAIL_EVENT_WEBHOOK_SIGNATURE")
end
it 'sets the timestamp header constant' do
expect(SendGrid::EventWebhookHeader::TIMESTAMP).to eq("HTTP_X_TWILIO_EMAIL_EVENT_WEBHOOK_TIMESTAMP")
end
end
def verify(public_key, payload, signature, timestamp)
ew = SendGrid::EventWebhook.new
ec_public_key = ew.convert_public_key_to_ecdsa(public_key)
ew.verify_signature(ec_public_key, payload, signature, timestamp)
end
def skip_jruby
RUBY_PLATFORM == 'java'
end