sendgrid-ruby 6.2.1 → 6.3.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (122) hide show
  1. checksums.yaml +4 -4
  2. data/.codeclimate.yml +1 -1
  3. data/.gitignore +1 -1
  4. data/.rubocop.yml +4 -0
  5. data/.rubocop_todo.yml +109 -0
  6. data/.travis.yml +3 -4
  7. data/CHANGELOG.md +69 -9
  8. data/CONTRIBUTING.md +11 -21
  9. data/FIRST_TIMERS.md +79 -0
  10. data/Gemfile +0 -1
  11. data/ISSUE_TEMPLATE.md +5 -1
  12. data/Makefile +4 -2
  13. data/PULL_REQUEST_TEMPLATE.md +1 -1
  14. data/README.md +22 -27
  15. data/Rakefile +2 -3
  16. data/TROUBLESHOOTING.md +5 -5
  17. data/USAGE.md +145 -38
  18. data/examples/accesssettings/accesssettings.rb +9 -12
  19. data/examples/alerts/alerts.rb +8 -11
  20. data/examples/apikeys/apikeys.rb +12 -15
  21. data/examples/asm/asm.rb +27 -30
  22. data/examples/browsers/browsers.rb +0 -3
  23. data/examples/campaigns/campaigns.rb +29 -32
  24. data/examples/categories/categories.rb +0 -3
  25. data/examples/clients/clients.rb +1 -4
  26. data/examples/contactdb/contactdb.rb +63 -66
  27. data/examples/devices/devices.rb +0 -3
  28. data/examples/emailactivity/emailactivity.rb +52 -0
  29. data/examples/geo/geo.rb +0 -3
  30. data/examples/helpers/eventwebhook/example.rb +16 -0
  31. data/examples/helpers/mail/example.rb +19 -13
  32. data/examples/helpers/settings/example.rb +1 -1
  33. data/examples/helpers/stats/example.rb +4 -4
  34. data/examples/ips/ips.rb +19 -22
  35. data/examples/mail/mail.rb +72 -75
  36. data/examples/mailboxproviders/mailboxproviders.rb +0 -3
  37. data/examples/mailsettings/mailsettings.rb +21 -24
  38. data/examples/partnersettings/partnersettings.rb +3 -6
  39. data/examples/scopes/scopes.rb +7 -9
  40. data/examples/senderauthentication/senderauthentication.rb +41 -44
  41. data/examples/senders/senders.rb +28 -31
  42. data/examples/stats/stats.rb +0 -3
  43. data/examples/subusers/subusers.rb +17 -20
  44. data/examples/suppression/suppression.rb +15 -18
  45. data/examples/templates/templates.rb +29 -31
  46. data/examples/trackingsettings/trackingsettings.rb +14 -17
  47. data/examples/user/user.rb +41 -44
  48. data/lib/rack/sendgrid_webhook_verification.rb +53 -0
  49. data/lib/sendgrid-ruby.rb +2 -0
  50. data/lib/sendgrid/base_interface.rb +1 -1
  51. data/lib/sendgrid/helpers/eventwebhook/eventwebhook.rb +50 -0
  52. data/lib/sendgrid/helpers/inbound/README.md +5 -5
  53. data/lib/sendgrid/helpers/inbound/app.rb +1 -1
  54. data/lib/sendgrid/helpers/inbound/public/index.html +1 -1
  55. data/lib/sendgrid/helpers/inbound/send.rb +2 -2
  56. data/lib/sendgrid/helpers/ip_management/ip_management.rb +1 -1
  57. data/lib/sendgrid/helpers/mail/README.md +3 -3
  58. data/lib/sendgrid/helpers/mail/asm.rb +6 -18
  59. data/lib/sendgrid/helpers/mail/attachment.rb +12 -42
  60. data/lib/sendgrid/helpers/mail/bcc_settings.rb +6 -18
  61. data/lib/sendgrid/helpers/mail/bypass_list_management.rb +8 -18
  62. data/lib/sendgrid/helpers/mail/category.rb +2 -2
  63. data/lib/sendgrid/helpers/mail/click_tracking.rb +6 -18
  64. data/lib/sendgrid/helpers/mail/content.rb +4 -3
  65. data/lib/sendgrid/helpers/mail/custom_arg.rb +6 -10
  66. data/lib/sendgrid/helpers/mail/email.rb +5 -4
  67. data/lib/sendgrid/helpers/mail/footer.rb +7 -27
  68. data/lib/sendgrid/helpers/mail/ganalytics.rb +10 -54
  69. data/lib/sendgrid/helpers/mail/header.rb +6 -10
  70. data/lib/sendgrid/helpers/mail/mail.rb +30 -48
  71. data/lib/sendgrid/helpers/mail/mail_settings.rb +9 -25
  72. data/lib/sendgrid/helpers/mail/open_tracking.rb +6 -18
  73. data/lib/sendgrid/helpers/mail/personalization.rb +34 -27
  74. data/lib/sendgrid/helpers/mail/section.rb +6 -10
  75. data/lib/sendgrid/helpers/mail/spam_check.rb +7 -27
  76. data/lib/sendgrid/helpers/mail/subscription_tracking.rb +8 -36
  77. data/lib/sendgrid/helpers/mail/substitution.rb +6 -10
  78. data/lib/sendgrid/helpers/mail/tracking_settings.rb +7 -20
  79. data/lib/sendgrid/helpers/permissions/scope.rb +1 -1
  80. data/lib/sendgrid/helpers/settings/README.md +2 -2
  81. data/lib/sendgrid/helpers/settings/settings.rb +1 -1
  82. data/lib/sendgrid/helpers/settings/tracking_settings_dto.rb +3 -5
  83. data/lib/sendgrid/helpers/stats/metrics.rb +5 -5
  84. data/lib/sendgrid/sendgrid.rb +1 -1
  85. data/lib/sendgrid/twilio_email.rb +1 -1
  86. data/lib/sendgrid/version.rb +1 -1
  87. data/mail_helper_v3.md +12 -12
  88. data/sendgrid-ruby.gemspec +8 -7
  89. data/spec/fixtures/event_webhook.rb +22 -0
  90. data/spec/rack/sendgrid_webhook_verification_spec.rb +116 -0
  91. data/spec/sendgrid/helpers/eventwebhook/eventwebhook_spec.rb +105 -0
  92. data/spec/sendgrid/helpers/settings/mail_settings_dto_spec.rb +1 -1
  93. data/spec/sendgrid/helpers/settings/partner_settings_dto_spec.rb +1 -1
  94. data/spec/sendgrid/helpers/settings/settings_spec.rb +2 -2
  95. data/spec/sendgrid/helpers/settings/tracking_settings_dto_spec.rb +1 -1
  96. data/spec/sendgrid/helpers/settings/user_settings_dto_spec.rb +1 -1
  97. data/spec/sendgrid/helpers/stats/email_stats_spec.rb +22 -23
  98. data/spec/sendgrid/helpers/stats/metrics_spec.rb +19 -20
  99. data/spec/sendgrid/helpers/stats/stats_response_spec.rb +22 -23
  100. data/spec/spec_helper.rb +3 -1
  101. data/static/img/github-fork.png +0 -0
  102. data/static/img/github-sign-up.png +0 -0
  103. data/test/sendgrid/helpers/mail/test_attachment.rb +4 -6
  104. data/test/sendgrid/helpers/mail/test_category.rb +0 -2
  105. data/test/sendgrid/helpers/mail/test_email.rb +9 -11
  106. data/test/sendgrid/helpers/mail/test_mail.rb +101 -102
  107. data/test/sendgrid/helpers/mail/test_personalizations.rb +106 -93
  108. data/test/sendgrid/permissions/test_scopes.rb +0 -2
  109. data/test/sendgrid/test_sendgrid-ruby.rb +1948 -1958
  110. data/twilio_sendgrid_logo.png +0 -0
  111. data/use-cases/README.md +16 -0
  112. data/use-cases/domain-authentication.md +5 -0
  113. data/use-cases/email-statistics.md +52 -0
  114. data/use-cases/legacy-templates.md +98 -0
  115. data/use-cases/sms.md +39 -0
  116. data/use-cases/transactional-templates.md +111 -0
  117. data/use-cases/twilio-email.md +13 -0
  118. data/use-cases/twilio-setup.md +54 -0
  119. metadata +73 -24
  120. data/USE_CASES.md +0 -405
  121. data/docker/Dockerfile +0 -12
  122. data/docker/README.md +0 -30
@@ -1,21 +1,17 @@
1
+ require 'json'
2
+
1
3
  module SendGrid
2
4
  class Substitution
5
+ attr_accessor :substitution
6
+
3
7
  def initialize(key: nil, value: nil)
4
8
  @substitution = {}
5
- (key.nil? || value.nil?) ? @substitution = nil : @substitution[key] = value
6
- end
7
-
8
- def substitution=(substitution)
9
- @substitution = substitution
10
- end
11
-
12
- def substitution
13
- @substitution
9
+ key.nil? || value.nil? ? @substitution = nil : @substitution[key] = value
14
10
  end
15
11
 
16
12
  def to_json(*)
17
13
  {
18
- 'substitution' => self.substitution
14
+ 'substitution' => substitution
19
15
  }.delete_if { |_, value| value.to_s.strip == '' }
20
16
  end
21
17
  end
@@ -1,5 +1,8 @@
1
+ require 'json'
2
+
1
3
  module SendGrid
2
4
  class TrackingSettings
5
+ attr_writer :click_tracking, :open_tracking, :subscription_tracking, :ganalytics
3
6
  def initialize
4
7
  @click_tracking = nil
5
8
  @open_tracking = nil
@@ -7,44 +10,28 @@ module SendGrid
7
10
  @ganalytics = nil
8
11
  end
9
12
 
10
- def click_tracking=(click_tracking)
11
- @click_tracking = click_tracking
12
- end
13
-
14
13
  def click_tracking
15
14
  @click_tracking.nil? ? nil : @click_tracking.to_json
16
15
  end
17
16
 
18
- def open_tracking=(open_tracking)
19
- @open_tracking = open_tracking
20
- end
21
-
22
17
  def open_tracking
23
18
  @open_tracking.nil? ? nil : @open_tracking.to_json
24
19
  end
25
20
 
26
- def subscription_tracking=(subscription_tracking)
27
- @subscription_tracking = subscription_tracking
28
- end
29
-
30
21
  def subscription_tracking
31
22
  @subscription_tracking.nil? ? nil : @subscription_tracking.to_json
32
23
  end
33
24
 
34
- def ganalytics=(ganalytics)
35
- @ganalytics = ganalytics
36
- end
37
-
38
25
  def ganalytics
39
26
  @ganalytics.nil? ? nil : @ganalytics.to_json
40
27
  end
41
28
 
42
29
  def to_json(*)
43
30
  {
44
- 'click_tracking' => self.click_tracking,
45
- 'open_tracking' => self.open_tracking,
46
- 'subscription_tracking' => self.subscription_tracking,
47
- 'ganalytics' => self.ganalytics
31
+ 'click_tracking' => click_tracking,
32
+ 'open_tracking' => open_tracking,
33
+ 'subscription_tracking' => subscription_tracking,
34
+ 'ganalytics' => ganalytics
48
35
  }.delete_if { |_, value| value.to_s.strip == '' }
49
36
  end
50
37
  end
@@ -4,7 +4,7 @@ require 'yaml'
4
4
  module SendGrid
5
5
  class Scope
6
6
  SCOPES = YAML.load_file(File.dirname(__FILE__) + '/scopes.yml').freeze
7
-
7
+
8
8
  class << self
9
9
  def admin_permissions
10
10
  SCOPES.values.map(&:values).flatten
@@ -2,7 +2,7 @@
2
2
 
3
3
  # Quick Start
4
4
 
5
- Run the [example](https://github.com/sendgrid/sendgrid-ruby/tree/master/examples/helpers/settings) (make sure you have set your environment variable to include your SENDGRID_API_KEY).
5
+ Run the [example](../../../../examples/helpers/settings) (make sure you have set your environment variable to include your SENDGRID_API_KEY).
6
6
 
7
7
  ```bash
8
8
  ruby examples/helpers/settings/example.rb
@@ -10,5 +10,5 @@ ruby examples/helpers/settings/example.rb
10
10
 
11
11
  ## Usage
12
12
 
13
- - See the [example](https://github.com/sendgrid/sendgrid-ruby/tree/master/examples/helpers/settings) for a complete working example.
13
+ - See the [example](../../../../examples/helpers/settings) for a complete working example.
14
14
  - [Documentation](https://sendgrid.com/docs/API_Reference/Web_API_v3/Settings/index.html)
@@ -8,7 +8,7 @@ module SendGrid
8
8
  attr_accessor :sendgrid_client
9
9
 
10
10
  SETTING_TYPES = [SendGrid::MailSettingsDto, SendGrid::TrackingSettingsDto,
11
- SendGrid::PartnerSettingsDto, SendGrid::UserSettingsDto]
11
+ SendGrid::PartnerSettingsDto, SendGrid::UserSettingsDto].freeze
12
12
 
13
13
  def initialize(sendgrid_client:)
14
14
  @sendgrid_client = sendgrid_client
@@ -1,9 +1,9 @@
1
1
  module SendGrid
2
2
  class TrackingSettingsDto
3
3
  attr_reader :open, :click, :google_analytics, :subscription
4
- alias :click_tracking :click
5
- alias :open_tracking :open
6
- alias :subscription_tracking :subscription
4
+ alias click_tracking click
5
+ alias open_tracking open
6
+ alias subscription_tracking subscription
7
7
 
8
8
  def self.fetch(sendgrid_client:, name:, query_params:)
9
9
  name = scrub_alias_names(name.to_s)
@@ -15,8 +15,6 @@ module SendGrid
15
15
  sendgrid_client.tracking_settings.public_send(name).patch(request_body: request_body)
16
16
  end
17
17
 
18
- private
19
-
20
18
  def self.scrub_alias_names(name)
21
19
  name.gsub(/_tracking/, '')
22
20
  end
@@ -1,12 +1,12 @@
1
1
  module SendGrid
2
2
  class Metrics
3
3
  attr_reader :blocks, :bounce_drops,
4
- :bounces, :clicks, :deferred, :delivered,
5
- :invalid_emails, :opens, :processed, :requests,
6
- :spam_report_drops, :spam_reports, :unique_clicks,
7
- :unique_opens, :unsubscribe_drops, :unsubscribes
4
+ :bounces, :clicks, :deferred, :delivered,
5
+ :invalid_emails, :opens, :processed, :requests,
6
+ :spam_report_drops, :spam_reports, :unique_clicks,
7
+ :unique_opens, :unsubscribe_drops, :unsubscribes
8
8
 
9
- def initialize(args={})
9
+ def initialize(args = {})
10
10
  @date = args['date']
11
11
  @blocks = args['blocks']
12
12
  @bounce_drops = args['bounce_drops']
@@ -12,7 +12,7 @@ module SendGrid
12
12
  #
13
13
  def initialize(api_key:, host: nil, request_headers: nil, version: nil, impersonate_subuser: nil)
14
14
  auth = "Bearer #{api_key}"
15
- host = 'https://api.sendgrid.com' unless host
15
+ host ||= 'https://api.sendgrid.com'
16
16
 
17
17
  super(auth: auth, host: host, request_headers: request_headers, version: version, impersonate_subuser: impersonate_subuser)
18
18
  end
@@ -13,7 +13,7 @@ module TwilioEmail
13
13
  #
14
14
  def initialize(username:, password:, host: nil, request_headers: nil, version: nil, impersonate_subuser: nil)
15
15
  auth = "Basic #{Base64.strict_encode64("#{username}:#{password}")}"
16
- host = 'https://email.twilio.com' unless host
16
+ host ||= 'https://email.twilio.com'
17
17
 
18
18
  super(auth: auth, host: host, request_headers: request_headers, version: version, impersonate_subuser: impersonate_subuser)
19
19
  end
@@ -1,3 +1,3 @@
1
1
  module SendGrid
2
- VERSION = '6.2.1'
2
+ VERSION = '6.3.5'.freeze
3
3
  end
@@ -2,11 +2,11 @@ Hello!
2
2
 
3
3
  It is now time to implement the final piece of our v2 to v3 migration. Before we dig into writing the code, we would love to get feedback on the following proposed interfaces.
4
4
 
5
- We are starting with the use cases below for the first iteration. (we have completed this work on [our C# library](https://github.com/sendgrid/sendgrid-csharp/blob/master/USE_CASES.md), you can check that out for a sneak peek of where we are heading).
5
+ We are starting with the use cases below for the first iteration. (we have completed this work on [our C# library](https://github.com/sendgrid/sendgrid-csharp/blob/HEAD/USE_CASES.md), you can check that out for a sneak peek of where we are heading).
6
6
 
7
7
  # Send a Single Email to a Single Recipient
8
8
 
9
- The following code assumes you are storing the API key in an [environment variable (recommended)](https://github.com/sendgrid/sendgrid-ruby/blob/master/TROUBLESHOOTING.md#environment-variables-and-your-sendgrid-api-key). If you don't have your key stored in an environment variable, you can assign it directly to `api_key` for testing purposes.
9
+ The following code assumes you are storing the API key in an [environment variable (recommended)](TROUBLESHOOTING.md#environment-variables-and-your-sendgrid-api-key). If you don't have your key stored in an environment variable, you can assign it directly to `api_key` for testing purposes.
10
10
 
11
11
  ```ruby
12
12
  require 'sendgrid-ruby'
@@ -37,7 +37,7 @@ puts response.headers
37
37
 
38
38
  # Send a Single Email to Multiple Recipients
39
39
 
40
- The following code assumes you are storing the API key in an [environment variable (recommended)](https://github.com/sendgrid/sendgrid-ruby/blob/master/TROUBLESHOOTING.md#environment-variables-and-your-sendgrid-api-key). If you don't have your key stored in an environment variable, you can assign it directly to `api_key` for testing purposes.
40
+ The following code assumes you are storing the API key in an [environment variable (recommended)](TROUBLESHOOTING.md#environment-variables-and-your-sendgrid-api-key). If you don't have your key stored in an environment variable, you can assign it directly to `api_key` for testing purposes.
41
41
 
42
42
  ```ruby
43
43
  require 'sendgrid-ruby'
@@ -72,7 +72,7 @@ puts response.headers
72
72
 
73
73
  # Send Multiple Emails to Multiple Recipients
74
74
 
75
- The following code assumes you are storing the API key in an [environment variable (recommended)](https://github.com/sendgrid/sendgrid-ruby/blob/master/TROUBLESHOOTING.md#environment-variables-and-your-sendgrid-api-key). If you don't have your key stored in an environment variable, you can assign it directly to `api_key` for testing purposes.
75
+ The following code assumes you are storing the API key in an [environment variable (recommended)](TROUBLESHOOTING.md#environment-variables-and-your-sendgrid-api-key). If you don't have your key stored in an environment variable, you can assign it directly to `api_key` for testing purposes.
76
76
 
77
77
  ```ruby
78
78
  require 'sendgrid-ruby'
@@ -119,7 +119,7 @@ puts response.headers
119
119
 
120
120
  # Kitchen Sink - an example with all settings used
121
121
 
122
- The following code assumes you are storing the API key in an [environment variable (recommended)](https://github.com/sendgrid/sendgrid-ruby/blob/master/TROUBLESHOOTING.md#environment-variables-and-your-sendgrid-api-key). If you don't have your key stored in an environment variable, you can assign it directly to `api_key` for testing purposes.
122
+ The following code assumes you are storing the API key in an [environment variable (recommended)](TROUBLESHOOTING.md#environment-variables-and-your-sendgrid-api-key). If you don't have your key stored in an environment variable, you can assign it directly to `api_key` for testing purposes.
123
123
 
124
124
  ```ruby
125
125
  client = SendGrid::Client.new(api_key: ENV['SENDGRID_API_KEY'])
@@ -233,7 +233,7 @@ msg.set_send_at(1461775052, 1)
233
233
 
234
234
  msg.set_subject('this subject overrides the Global Subject on the second Personalization', 1)
235
235
 
236
- # The values below this comment are global to entire message
236
+ # The values below this comment are global to the entire message
237
237
 
238
238
  msg.set_from(SendGrid::Email.new('test0@example.com', 'Example User0'))
239
239
 
@@ -263,7 +263,7 @@ attachments = [
263
263
  'base64 encoded content',
264
264
  'image/png',
265
265
  'inline',
266
- 'Banner 2'),
266
+ 'Banner 2'),
267
267
  ]
268
268
  msg.add_attachments(attachments)
269
269
 
@@ -279,7 +279,7 @@ msg.set_global_headers(global_headers)
279
279
  msg.add_section('%section1%', 'Substitution for Section 1 Tag')
280
280
  sections = [
281
281
  '%section2%' => 'Substitution for Section 2 Tag',
282
- '%section3%' => 'Substitution for Section 3 Tag'
282
+ '%section3%' => 'Substitution for Section 3 Tag'
283
283
  ]
284
284
  msg.add_sections(sections)
285
285
 
@@ -295,7 +295,7 @@ puts response.headers
295
295
 
296
296
  # Attachments
297
297
 
298
- The following code assumes you are storing the API key in an [environment variable (recommended)](https://github.com/sendgrid/sendgrid-ruby/blob/master/TROUBLESHOOTING.md#environment-variables-and-your-sendgrid-api-key). If you don't have your key stored in an environment variable, you can assign it directly to `api_key` for testing purposes.
298
+ The following code assumes you are storing the API key in an [environment variable (recommended)](TROUBLESHOOTING.md#environment-variables-and-your-sendgrid-api-key). If you don't have your key stored in an environment variable, you can assign it directly to `api_key` for testing purposes.
299
299
 
300
300
  ```ruby
301
301
  client = SendGrid::Client.new(api_key: ENV['SENDGRID_API_KEY'])
@@ -326,9 +326,9 @@ puts response.headers
326
326
 
327
327
  # Transactional Templates
328
328
 
329
- The following code assumes you are storing the API key in an [environment variable (recommended)](https://github.com/sendgrid/sendgrid-ruby/blob/master/TROUBLESHOOTING.md#environment-variables-and-your-sendgrid-api-key). If you don't have your key stored in an environment variable, you can assign it directly to `api_key` for testing purposes.
329
+ The following code assumes you are storing the API key in an [environment variable (recommended)](TROUBLESHOOTING.md#environment-variables-and-your-sendgrid-api-key). If you don't have your key stored in an environment variable, you can assign it directly to `api_key` for testing purposes.
330
330
 
331
- For this example, we assume you have created a [transactional template](https://sendgrid.com/docs/User_Guide/Transactional_Templates/index.html). Following is the template content we used for testing.
331
+ For this example, we assume you have created a [legacy transactional template](https://sendgrid.com/docs/User_Guide/Transactional_Templates/index.html) in the UI or via the API. Following is the template content we used for testing.
332
332
 
333
333
  Template ID (replace with your own):
334
334
 
@@ -347,7 +347,7 @@ Template Body:
347
347
  ```html
348
348
  <html>
349
349
  <head>
350
- <title></title>
350
+ <title></title>
351
351
  </head>
352
352
  <body>
353
353
  Hello -name-,
@@ -1,5 +1,4 @@
1
- # coding: utf-8
2
- lib = File.expand_path('../lib', __FILE__)
1
+ lib = File.expand_path('lib', __dir__)
3
2
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
3
  require 'sendgrid/version'
5
4
 
@@ -14,17 +13,19 @@ Gem::Specification.new do |spec|
14
13
 
15
14
  spec.required_ruby_version = '>= 2.2'
16
15
 
17
- spec.license = 'MIT'
16
+ spec.license = 'MIT'
18
17
  spec.files = `git ls-files -z`.split("\x0")
19
18
  spec.executables = spec.files.grep(/^bin/) { |f| File.basename(f) }
20
19
  spec.test_files = spec.files.grep(/^(test|spec|features)/)
21
20
  spec.require_paths = ['lib']
22
21
  spec.add_dependency 'ruby_http_client', '~> 3.4'
23
- spec.add_development_dependency 'sinatra', '>= 1.4.7', '< 3'
22
+ spec.add_development_dependency 'faker'
23
+ spec.add_development_dependency 'minitest', '~> 5.9'
24
+ spec.add_development_dependency 'pry'
25
+ spec.add_development_dependency 'rack'
24
26
  spec.add_development_dependency 'rake', '~> 13.0'
25
27
  spec.add_development_dependency 'rspec'
26
- spec.add_development_dependency 'pry'
27
- spec.add_development_dependency 'faker'
28
28
  spec.add_development_dependency 'rubocop'
29
- spec.add_development_dependency 'minitest', '~> 5.9'
29
+ spec.add_development_dependency 'simplecov', '~> 0.18.5'
30
+ spec.add_development_dependency 'sinatra', '>= 1.4.7', '< 3'
30
31
  end
@@ -0,0 +1,22 @@
1
+ require "json"
2
+
3
+ module Fixtures
4
+ module EventWebhook
5
+ PUBLIC_KEY = 'MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAE83T4O/n84iotIvIW4mdBgQ/7dAfSmpqIM8kF9mN1flpVKS3GRqe62gw+2fNNRaINXvVpiglSI8eNEc6wEA3F+g=='.freeze
6
+ FAILING_PUBLIC_KEY = 'MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEqTxd43gyp8IOEto2LdIfjRQrIbsd4SXZkLW6jDutdhXSJCWHw8REntlo7aNDthvj+y7GjUuFDb/R1NGe1OPzpA=='.freeze
7
+ SIGNATURE = 'MEUCIGHQVtGj+Y3LkG9fLcxf3qfI10QysgDWmMOVmxG0u6ZUAiEAyBiXDWzM+uOe5W0JuG+luQAbPIqHh89M15TluLtEZtM='.freeze
8
+ FAILING_SIGNATURE = 'MEUCIQCtIHJeH93Y+qpYeWrySphQgpNGNr/U+UyUlBkU6n7RAwIgJTz2C+8a8xonZGi6BpSzoQsbVRamr2nlxFDWYNH3j/0='.freeze
9
+ TIMESTAMP = '1600112502'.freeze
10
+ PAYLOAD = [
11
+ {
12
+ email: 'hello@world.com',
13
+ event: 'dropped',
14
+ reason: 'Bounced Address',
15
+ sg_event_id: 'ZHJvcC0xMDk5NDkxOS1MUnpYbF9OSFN0T0doUTRrb2ZTbV9BLTA',
16
+ sg_message_id: 'LRzXl_NHStOGhQ4kofSm_A.filterdrecv-p3mdw1-756b745b58-kmzbl-18-5F5FC76C-9.0',
17
+ 'smtp-id': '<LRzXl_NHStOGhQ4kofSm_A@ismtpd0039p1iad1.sendgrid.net>',
18
+ timestamp: 1_600_112_492
19
+ }
20
+ ].to_json + "\r\n" # Be sure to include the trailing carriage return and newline!
21
+ end
22
+ end
@@ -0,0 +1,116 @@
1
+ require 'spec_helper'
2
+ require 'rack/mock'
3
+ require './spec/fixtures/event_webhook'
4
+
5
+ unless RUBY_PLATFORM == 'java'
6
+ describe Rack::SendGridWebhookVerification do
7
+ let(:public_key) { Fixtures::EventWebhook::PUBLIC_KEY }
8
+ before do
9
+ @app = ->(_env) { [200, { 'Content-Type' => 'text/plain' }, ['Hello']] }
10
+ end
11
+
12
+ describe 'new' do
13
+ it 'should initialize with an app, public key and a path' do
14
+ expect do
15
+ Rack::SendGridWebhookVerification.new(@app, 'ABC', %r{/email})
16
+ end.not_to raise_error
17
+ end
18
+
19
+ it 'should initialize with an app, public key and paths' do
20
+ expect do
21
+ Rack::SendGridWebhookVerification.new(@app, 'ABC', %r{/email}, %r{/event})
22
+ end.not_to raise_error
23
+ end
24
+ end
25
+
26
+ describe 'calling against one path' do
27
+ let(:middleware) { Rack::SendGridWebhookVerification.new(@app, public_key, %r{/email}) }
28
+
29
+ it "should not intercept when the path doesn't match" do
30
+ expect(SendGrid::EventWebhook).to_not receive(:new)
31
+ request = Rack::MockRequest.env_for('/login')
32
+ status, headers, body = middleware.call(request)
33
+ expect(status).to eq(200)
34
+ end
35
+
36
+ it 'should allow a request through if it is verified' do
37
+ options = {
38
+ :input => Fixtures::EventWebhook::PAYLOAD,
39
+ 'Content-Type' => "application/json"
40
+ }
41
+ options[SendGrid::EventWebhookHeader::SIGNATURE] = Fixtures::EventWebhook::SIGNATURE
42
+ options[SendGrid::EventWebhookHeader::TIMESTAMP] = Fixtures::EventWebhook::TIMESTAMP
43
+ request = Rack::MockRequest.env_for('/email', options)
44
+ status, headers, body = middleware.call(request)
45
+ expect(status).to eq(200)
46
+ end
47
+
48
+ it 'should short circuit a request to 403 if there is no signature or timestamp' do
49
+ options = {
50
+ :input => Fixtures::EventWebhook::PAYLOAD,
51
+ 'Content-Type' => "application/json"
52
+ }
53
+ request = Rack::MockRequest.env_for('/email', options)
54
+ status, headers, body = middleware.call(request)
55
+ expect(status).to eq(403)
56
+ end
57
+
58
+ it 'should short circuit a request to 403 if the signature is incorrect' do
59
+ options = {
60
+ :input => Fixtures::EventWebhook::PAYLOAD,
61
+ 'Content-Type' => "application/json"
62
+ }
63
+ options[SendGrid::EventWebhookHeader::SIGNATURE] = Fixtures::EventWebhook::FAILING_SIGNATURE
64
+ options[SendGrid::EventWebhookHeader::TIMESTAMP] = Fixtures::EventWebhook::TIMESTAMP
65
+ request = Rack::MockRequest.env_for('/email', options)
66
+ status, headers, body = middleware.call(request)
67
+ expect(status).to eq(403)
68
+ end
69
+
70
+ it 'should short circuit a request to 403 if the payload is incorrect' do
71
+ options = {
72
+ :input => 'payload',
73
+ 'Content-Type' => "application/json"
74
+ }
75
+ options[SendGrid::EventWebhookHeader::SIGNATURE] = Fixtures::EventWebhook::SIGNATURE
76
+ options[SendGrid::EventWebhookHeader::TIMESTAMP] = Fixtures::EventWebhook::TIMESTAMP
77
+ request = Rack::MockRequest.env_for('/email', options)
78
+ status, headers, body = middleware.call(request)
79
+ expect(status).to eq(403)
80
+ end
81
+ end
82
+
83
+ describe 'calling with multiple paths' do
84
+ let(:middleware) { Rack::SendGridWebhookVerification.new(@app, public_key, %r{/email}, %r{/events}) }
85
+
86
+ it "should not intercept when the path doesn't match" do
87
+ expect(SendGrid::EventWebhook).to_not receive(:new)
88
+ request = Rack::MockRequest.env_for('/sms_events')
89
+ status, headers, body = middleware.call(request)
90
+ expect(status).to eq(200)
91
+ end
92
+
93
+ it 'should allow a request through if it is verified' do
94
+ options = {
95
+ :input => Fixtures::EventWebhook::PAYLOAD,
96
+ 'Content-Type' => "application/json"
97
+ }
98
+ options[SendGrid::EventWebhookHeader::SIGNATURE] = Fixtures::EventWebhook::SIGNATURE
99
+ options[SendGrid::EventWebhookHeader::TIMESTAMP] = Fixtures::EventWebhook::TIMESTAMP
100
+ request = Rack::MockRequest.env_for('/events', options)
101
+ status, headers, body = middleware.call(request)
102
+ expect(status).to eq(200)
103
+ end
104
+
105
+ it 'should short circuit a request to 403 if there is no signature or timestamp' do
106
+ options = {
107
+ :input => Fixtures::EventWebhook::PAYLOAD,
108
+ 'Content-Type' => "application/json"
109
+ }
110
+ request = Rack::MockRequest.env_for('/events', options)
111
+ status, headers, body = middleware.call(request)
112
+ expect(status).to eq(403)
113
+ end
114
+ end
115
+ end
116
+ end