sendgrid-ruby 1.1.6 → 6.6.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (160) hide show
  1. checksums.yaml +5 -5
  2. data/.env_sample +1 -3
  3. data/.github/ISSUE_TEMPLATE/config.yml +10 -0
  4. data/.github/workflows/test-and-deploy.yml +120 -0
  5. data/.gitignore +3 -0
  6. data/.rubocop.yml +5 -27
  7. data/.rubocop_todo.yml +127 -0
  8. data/CHANGELOG.md +417 -1
  9. data/CODE_OF_CONDUCT.md +73 -0
  10. data/CONTRIBUTING.md +202 -0
  11. data/Dockerfile +14 -0
  12. data/FIRST_TIMERS.md +79 -0
  13. data/Gemfile +4 -4
  14. data/ISSUE_TEMPLATE.md +30 -0
  15. data/LICENSE +21 -0
  16. data/Makefile +14 -0
  17. data/PULL_REQUEST_TEMPLATE.md +31 -0
  18. data/README.md +153 -229
  19. data/Rakefile +9 -3
  20. data/TROUBLESHOOTING.md +151 -0
  21. data/UPGRADE.md +5 -0
  22. data/USAGE.md +5144 -0
  23. data/config.ru +4 -0
  24. data/examples/accesssettings/accesssettings.rb +80 -0
  25. data/examples/alerts/alerts.rb +59 -0
  26. data/examples/apikeys/apikeys.rb +81 -0
  27. data/examples/asm/asm.rb +170 -0
  28. data/examples/browsers/browsers.rb +13 -0
  29. data/examples/campaigns/campaigns.rb +150 -0
  30. data/examples/categories/categories.rb +33 -0
  31. data/examples/clients/clients.rb +24 -0
  32. data/examples/contactdb/contactdb.rb +392 -0
  33. data/examples/devices/devices.rb +13 -0
  34. data/examples/emailactivity/emailactivity.rb +52 -0
  35. data/examples/geo/geo.rb +13 -0
  36. data/examples/helpers/eventwebhook/example.rb +16 -0
  37. data/examples/helpers/mail/example.rb +158 -0
  38. data/examples/helpers/settings/example.rb +23 -0
  39. data/examples/helpers/stats/example.rb +42 -0
  40. data/examples/ips/ips.rb +164 -0
  41. data/examples/mail/mail.rb +170 -0
  42. data/examples/mailboxproviders/mailboxproviders.rb +13 -0
  43. data/examples/mailsettings/mailsettings.rb +216 -0
  44. data/examples/partnersettings/partnersettings.rb +36 -0
  45. data/examples/scopes/scopes.rb +59 -0
  46. data/examples/senderauthentication/senderauthentication.rb +307 -0
  47. data/examples/senders/senders.rb +95 -0
  48. data/examples/stats/stats.rb +13 -0
  49. data/examples/subusers/subusers.rb +166 -0
  50. data/examples/suppression/suppression.rb +198 -0
  51. data/examples/templates/templates.rb +127 -0
  52. data/examples/trackingsettings/trackingsettings.rb +107 -0
  53. data/examples/user/user.rb +290 -0
  54. data/gemfiles/Sinatra_1.gemfile +6 -0
  55. data/gemfiles/Sinatra_2.gemfile +6 -0
  56. data/lib/rack/sendgrid_webhook_verification.rb +55 -0
  57. data/lib/sendgrid/base_interface.rb +40 -0
  58. data/lib/sendgrid/helpers/eventwebhook/eventwebhook.rb +50 -0
  59. data/lib/sendgrid/helpers/inbound/README.md +98 -0
  60. data/lib/sendgrid/helpers/inbound/app.rb +32 -0
  61. data/lib/sendgrid/helpers/inbound/config.yml +26 -0
  62. data/lib/sendgrid/helpers/inbound/public/index.html +10 -0
  63. data/lib/sendgrid/helpers/inbound/sample_data/default_data.txt +58 -0
  64. data/lib/sendgrid/helpers/inbound/sample_data/raw_data.txt +57 -0
  65. data/lib/sendgrid/helpers/inbound/sample_data/raw_data_with_attachments.txt +298 -0
  66. data/lib/sendgrid/helpers/inbound/send.rb +26 -0
  67. data/lib/sendgrid/helpers/ip_management/ip_management.rb +17 -0
  68. data/lib/sendgrid/helpers/mail/README.md +14 -0
  69. data/lib/sendgrid/helpers/mail/asm.rb +19 -0
  70. data/lib/sendgrid/helpers/mail/attachment.rb +55 -0
  71. data/lib/sendgrid/helpers/mail/bcc_settings.rb +19 -0
  72. data/lib/sendgrid/helpers/mail/bypass_list_management.rb +31 -0
  73. data/lib/sendgrid/helpers/mail/category.rb +18 -0
  74. data/lib/sendgrid/helpers/mail/click_tracking.rb +19 -0
  75. data/lib/sendgrid/helpers/mail/content.rb +19 -0
  76. data/lib/sendgrid/helpers/mail/custom_arg.rb +18 -0
  77. data/lib/sendgrid/helpers/mail/email.rb +32 -0
  78. data/lib/sendgrid/helpers/mail/footer.rb +21 -0
  79. data/lib/sendgrid/helpers/mail/ganalytics.rb +28 -0
  80. data/lib/sendgrid/helpers/mail/header.rb +18 -0
  81. data/lib/sendgrid/helpers/mail/mail.rb +122 -0
  82. data/lib/sendgrid/helpers/mail/mail_settings.rb +45 -0
  83. data/lib/sendgrid/helpers/mail/open_tracking.rb +19 -0
  84. data/lib/sendgrid/helpers/mail/personalization.rb +93 -0
  85. data/lib/sendgrid/helpers/mail/section.rb +18 -0
  86. data/lib/sendgrid/helpers/mail/spam_check.rb +21 -0
  87. data/lib/sendgrid/helpers/mail/subscription_tracking.rb +23 -0
  88. data/lib/sendgrid/helpers/mail/substitution.rb +18 -0
  89. data/lib/sendgrid/helpers/mail/tracking_settings.rb +39 -0
  90. data/lib/sendgrid/helpers/permissions/scope.rb +28 -0
  91. data/lib/sendgrid/helpers/permissions/scopes.yml +309 -0
  92. data/lib/sendgrid/helpers/settings/README.md +14 -0
  93. data/lib/sendgrid/helpers/settings/mail_settings_dto.rb +13 -0
  94. data/lib/sendgrid/helpers/settings/partner_settings_dto.rb +13 -0
  95. data/lib/sendgrid/helpers/settings/settings.rb +28 -0
  96. data/lib/sendgrid/helpers/settings/tracking_settings_dto.rb +22 -0
  97. data/lib/sendgrid/helpers/settings/user_settings_dto.rb +13 -0
  98. data/lib/sendgrid/helpers/stats/email_stats.rb +46 -0
  99. data/lib/sendgrid/helpers/stats/metrics.rb +33 -0
  100. data/lib/sendgrid/helpers/stats/stats_response.rb +29 -0
  101. data/lib/sendgrid/sendgrid.rb +21 -0
  102. data/lib/sendgrid/twilio_email.rb +21 -0
  103. data/lib/sendgrid/version.rb +1 -1
  104. data/lib/sendgrid-ruby.rb +32 -7
  105. data/mail_helper_v3.md +390 -0
  106. data/sendgrid-ruby.gemspec +16 -20
  107. data/spec/fixtures/event_webhook.rb +22 -0
  108. data/spec/rack/sendgrid_webhook_verification_spec.rb +142 -0
  109. data/spec/sendgrid/helpers/eventwebhook/eventwebhook_spec.rb +105 -0
  110. data/spec/sendgrid/helpers/ip_management/ip_management_spec.rb +12 -0
  111. data/spec/sendgrid/helpers/settings/mail_settings_dto_spec.rb +32 -0
  112. data/spec/sendgrid/helpers/settings/partner_settings_dto_spec.rb +24 -0
  113. data/spec/sendgrid/helpers/settings/settings_spec.rb +25 -0
  114. data/spec/sendgrid/helpers/settings/tracking_settings_dto_spec.rb +27 -0
  115. data/spec/sendgrid/helpers/settings/user_settings_dto_spec.rb +24 -0
  116. data/spec/sendgrid/helpers/stats/email_stats_spec.rb +111 -0
  117. data/spec/sendgrid/helpers/stats/metrics_spec.rb +45 -0
  118. data/spec/sendgrid/helpers/stats/stats_response_spec.rb +75 -0
  119. data/spec/sendgrid/sendgrid_spec.rb +11 -0
  120. data/spec/sendgrid/twilio_email_spec.rb +11 -0
  121. data/spec/spec_helper.rb +12 -1
  122. data/static/img/github-fork.png +0 -0
  123. data/static/img/github-sign-up.png +0 -0
  124. data/test/sendgrid/helpers/mail/test_attachment.rb +33 -0
  125. data/test/sendgrid/helpers/mail/test_category.rb +25 -0
  126. data/test/sendgrid/helpers/mail/test_email.rb +41 -0
  127. data/test/sendgrid/helpers/mail/test_mail.rb +260 -0
  128. data/test/sendgrid/helpers/mail/test_personalizations.rb +214 -0
  129. data/test/sendgrid/permissions/test_scopes.rb +36 -0
  130. data/test/sendgrid/test_sendgrid-ruby.rb +2729 -0
  131. data/twilio_sendgrid_logo.png +0 -0
  132. data/use-cases/README.md +17 -0
  133. data/use-cases/domain-authentication.md +5 -0
  134. data/use-cases/email-statistics.md +52 -0
  135. data/use-cases/legacy-templates.md +98 -0
  136. data/use-cases/personalizations.md +34 -0
  137. data/use-cases/sms.md +39 -0
  138. data/use-cases/transactional-templates.md +111 -0
  139. data/use-cases/twilio-email.md +13 -0
  140. data/use-cases/twilio-setup.md +54 -0
  141. metadata +187 -112
  142. data/.rspec +0 -2
  143. data/.travis.yml +0 -20
  144. data/FETCH_HEAD +0 -0
  145. data/Guardfile +0 -10
  146. data/LICENSE.txt +0 -22
  147. data/example.rb +0 -41
  148. data/lib/sendgrid/client.rb +0 -62
  149. data/lib/sendgrid/exceptions.rb +0 -7
  150. data/lib/sendgrid/mail.rb +0 -182
  151. data/lib/sendgrid/recipient.rb +0 -29
  152. data/lib/sendgrid/response.rb +0 -14
  153. data/lib/sendgrid/template.rb +0 -26
  154. data/lib/sendgrid/template_mailer.rb +0 -59
  155. data/spec/lib/sendgrid/client_spec.rb +0 -87
  156. data/spec/lib/sendgrid/mail_spec.rb +0 -151
  157. data/spec/lib/sendgrid/recipient_spec.rb +0 -91
  158. data/spec/lib/sendgrid/template_mailer_spec.rb +0 -86
  159. data/spec/lib/sendgrid/template_spec.rb +0 -61
  160. data/spec/lib/sendgrid_spec.rb +0 -7
@@ -0,0 +1,260 @@
1
+ require_relative '../../../../lib/sendgrid/helpers/mail/mail'
2
+ require_relative '../../../../lib/sendgrid/sendgrid'
3
+ include SendGrid
4
+ require 'json'
5
+ require 'minitest/autorun'
6
+
7
+ class TestMail < Minitest::Test
8
+ def setup; end
9
+
10
+ def test_hello_world
11
+ from = Email.new(email: 'test@example.com')
12
+ to = Email.new(email: 'test@example.com')
13
+ subject = 'Sending with Twilio SendGrid is Fun'
14
+ content = Content.new(type: 'text/plain', value: 'and easy to do anywhere, even with Ruby')
15
+ mail = SendGrid::Mail.new(from, subject, to, content)
16
+
17
+ assert_equal(mail.to_json, JSON.parse('{"from":{"email":"test@example.com"}, "subject":"Sending with Twilio SendGrid is Fun", "personalizations":[{"to":[{"email":"test@example.com"}]}], "content":[{"type":"text/plain", "value":"and easy to do anywhere, even with Ruby"}]}'))
18
+ end
19
+
20
+ def test_kitchen_sink
21
+ mail = SendGrid::Mail.new
22
+ mail.from = Email.new(email: 'test@example.com')
23
+ mail.subject = 'Hello World from the Twilio SendGrid Ruby Library'
24
+ personalization = Personalization.new
25
+ personalization.add_to(Email.new(email: 'test1@example.com', name: 'Example User 1'))
26
+ personalization.add_to(Email.new(email: 'test2@example.com', name: 'Example User 2'))
27
+ personalization.add_cc(Email.new(email: 'test3@example.com', name: 'Example User 3'))
28
+ personalization.add_cc(Email.new(email: 'test4@example.com', name: 'Example User 4'))
29
+ personalization.add_bcc(Email.new(email: 'test5@example.com', name: 'Example User 5'))
30
+ personalization.add_bcc(Email.new(email: 'test6@example.com', name: 'Example User 6'))
31
+ personalization.subject = 'Hello World from the Personalized Twilio SendGrid Ruby Library'
32
+ personalization.add_header(Header.new(key: 'X-Test', value: 'True'))
33
+ personalization.add_header(Header.new(key: 'X-Mock', value: 'False'))
34
+ personalization.add_substitution(Substitution.new(key: '%name%', value: 'Example User'))
35
+ personalization.add_substitution(Substitution.new(key: '%city%', value: 'Denver'))
36
+ personalization.add_custom_arg(CustomArg.new(key: 'user_id', value: '343'))
37
+ personalization.add_custom_arg(CustomArg.new(key: 'type', value: 'marketing'))
38
+ personalization.send_at = 1_443_636_843
39
+ mail.add_personalization(personalization)
40
+
41
+ personalization2 = Personalization.new
42
+ personalization2.add_to(Email.new(email: 'test1@example.com', name: 'Example User 1'))
43
+ personalization2.add_to(Email.new(email: 'test2@example.com', name: 'Example User 2'))
44
+ personalization2.add_cc(Email.new(email: 'test3@example.com', name: 'Example User 3'))
45
+ personalization2.add_cc(Email.new(email: 'test4@example.com', name: 'Example User 4'))
46
+ personalization2.add_bcc(Email.new(email: 'test5@example.com', name: 'Example User 5'))
47
+ personalization2.add_bcc(Email.new(email: 'test6@example.com', name: 'Example User 6'))
48
+ personalization2.subject = 'Hello World from the Personalized Twilio SendGrid Ruby Library'
49
+ personalization2.add_header(Header.new(key: 'X-Test', value: 'True'))
50
+ personalization2.add_header(Header.new(key: 'X-Mock', value: 'False'))
51
+ personalization2.add_substitution(Substitution.new(key: '%name%', value: 'Example User'))
52
+ personalization2.add_substitution(Substitution.new(key: '%city%', value: 'Denver'))
53
+ personalization2.add_custom_arg(CustomArg.new(key: 'user_id', value: '343'))
54
+ personalization2.add_custom_arg(CustomArg.new(key: 'type', value: 'marketing'))
55
+ personalization2.send_at = 1_443_636_843
56
+ mail.add_personalization(personalization2)
57
+
58
+ mail.add_content(Content.new(type: 'text/plain', value: 'some text here'))
59
+ mail.add_content(Content.new(type: 'text/html', value: '<html><body>some text here</body></html>'))
60
+
61
+ attachment = Attachment.new
62
+ attachment.content = 'TG9yZW0gaXBzdW0gZG9sb3Igc2l0IGFtZXQsIGNvbnNlY3RldHVyIGFkaXBpc2NpbmcgZWxpdC4gQ3JhcyBwdW12'
63
+ attachment.type = 'application/pdf'
64
+ attachment.filename = 'balance_001.pdf'
65
+ attachment.disposition = 'attachment'
66
+ attachment.content_id = 'Balance Sheet'
67
+
68
+ mail.add_attachment(attachment)
69
+
70
+ attachment2 = Attachment.new
71
+ attachment2.content = 'BwdW'
72
+ attachment2.type = 'image/png'
73
+ attachment2.filename = 'banner.png'
74
+ attachment2.disposition = 'inline'
75
+ attachment2.content_id = 'Banner'
76
+ mail.add_attachment(attachment2)
77
+
78
+ mail.template_id = '13b8f94f-bcae-4ec6-b752-70d6cb59f932'
79
+
80
+ mail.add_section(Section.new(key: '%section1%', value: 'Substitution Text for Section 1'))
81
+ mail.add_section(Section.new(key: '%section2%', value: 'Substitution Text for Section 2'))
82
+
83
+ mail.add_header(Header.new(key: 'X-Test3', value: 'test3'))
84
+ mail.add_header(Header.new(key: 'X-Test4', value: 'test4'))
85
+
86
+ mail.add_category(Category.new(name: 'May'))
87
+ mail.add_category(Category.new(name: '2016'))
88
+
89
+ mail.add_custom_arg(CustomArg.new(key: 'campaign', value: 'welcome'))
90
+ mail.add_custom_arg(CustomArg.new(key: 'weekday', value: 'morning'))
91
+
92
+ mail.send_at = 1_443_636_842
93
+
94
+ mail.batch_id = 'sendgrid_batch_id'
95
+
96
+ mail.asm = ASM.new(group_id: 99, groups_to_display: [4, 5, 6, 7, 8])
97
+
98
+ mail.ip_pool_name = '23'
99
+
100
+ mail_settings = MailSettings.new
101
+ mail_settings.bcc = BccSettings.new(enable: true, email: 'test@example.com')
102
+ mail_settings.bypass_list_management = BypassListManagement.new(enable: true)
103
+ mail_settings.footer = Footer.new(enable: true, text: 'Footer Text', html: '<html><body>Footer Text</body></html>')
104
+ mail_settings.sandbox_mode = SandBoxMode.new(enable: true)
105
+ mail_settings.spam_check = SpamCheck.new(enable: true, threshold: 1, post_to_url: 'https://spamcatcher.sendgrid.com')
106
+ mail.mail_settings = mail_settings
107
+
108
+ tracking_settings = TrackingSettings.new
109
+ tracking_settings.click_tracking = ClickTracking.new(enable: false, enable_text: false)
110
+ tracking_settings.open_tracking = OpenTracking.new(enable: true, substitution_tag: 'Optional tag to replace with the open image in the body of the message')
111
+ tracking_settings.subscription_tracking = SubscriptionTracking.new(enable: true, text: 'text to insert into the text/plain portion of the message', html: 'html to insert into the text/html portion of the message', substitution_tag: 'Optional tag to replace with the open image in the body of the message')
112
+ tracking_settings.ganalytics = Ganalytics.new(enable: true, utm_source: 'some source', utm_medium: 'some medium', utm_term: 'some term', utm_content: 'some content', utm_campaign: 'some campaign')
113
+ mail.tracking_settings = tracking_settings
114
+
115
+ mail.reply_to = Email.new(email: 'test@example.com')
116
+
117
+ # rubocop:disable Layout/LineLength
118
+ assert_equal(mail.to_json, JSON.parse('{"asm":{"group_id":99,"groups_to_display":[4,5,6,7,8]},"attachments":[{"content":"TG9yZW0gaXBzdW0gZG9sb3Igc2l0IGFtZXQsIGNvbnNlY3RldHVyIGFkaXBpc2NpbmcgZWxpdC4gQ3JhcyBwdW12","content_id":"Balance Sheet","disposition":"attachment","filename":"balance_001.pdf","type":"application/pdf"},{"content":"BwdW","content_id":"Banner","disposition":"inline","filename":"banner.png","type":"image/png"}],"batch_id":"sendgrid_batch_id","categories":["May","2016"],"content":[{"type":"text/plain","value":"some text here"},{"type":"text/html","value":"<html><body>some text here</body></html>"}],"custom_args":{"campaign":"welcome","weekday":"morning"},"from":{"email":"test@example.com"},"headers":{"X-Test3":"test3","X-Test4":"test4"},"ip_pool_name":"23","mail_settings":{"bcc":{"email":"test@example.com","enable":true},"bypass_list_management":{"enable":true},"footer":{"enable":true,"html":"<html><body>Footer Text</body></html>","text":"Footer Text"},"sandbox_mode":{"enable":true},"spam_check":{"enable":true,"post_to_url":"https://spamcatcher.sendgrid.com","threshold":1}},"personalizations":[{"bcc":[{"email":"test5@example.com","name":"Example User 5"},{"email":"test6@example.com","name":"Example User 6"}],"cc":[{"email":"test3@example.com","name":"Example User 3"},{"email":"test4@example.com","name":"Example User 4"}],"custom_args":{"type":"marketing","user_id":"343"},"headers":{"X-Mock":"False","X-Test":"True"},"send_at":1443636843,"subject":"Hello World from the Personalized Twilio SendGrid Ruby Library","substitutions":{"%city%":"Denver","%name%":"Example User"},"to":[{"email":"test1@example.com","name":"Example User 1"},{"email":"test2@example.com","name":"Example User 2"}]},{"bcc":[{"email":"test5@example.com","name":"Example User 5"},{"email":"test6@example.com","name":"Example User 6"}],"cc":[{"email":"test3@example.com","name":"Example User 3"},{"email":"test4@example.com","name":"Example User 4"}],"custom_args":{"type":"marketing","user_id":"343"},"headers":{"X-Mock":"False","X-Test":"True"},"send_at":1443636843,"subject":"Hello World from the Personalized Twilio SendGrid Ruby Library","substitutions":{"%city%":"Denver","%name%":"Example User"},"to":[{"email":"test1@example.com","name":"Example User 1"},{"email":"test2@example.com","name":"Example User 2"}]}],"reply_to":{"email":"test@example.com"},"sections":{"%section1%":"Substitution Text for Section 1","%section2%":"Substitution Text for Section 2"},"send_at":1443636842,"subject":"Hello World from the Twilio SendGrid Ruby Library","template_id":"13b8f94f-bcae-4ec6-b752-70d6cb59f932","tracking_settings":{"click_tracking":{"enable":false,"enable_text":false},"ganalytics":{"enable":true,"utm_campaign":"some campaign","utm_content":"some content","utm_medium":"some medium","utm_source":"some source","utm_term":"some term"},"open_tracking":{"enable":true,"substitution_tag":"Optional tag to replace with the open image in the body of the message"},"subscription_tracking":{"enable":true,"html":"html to insert into the text/html portion of the message","substitution_tag":"Optional tag to replace with the open image in the body of the message","text":"text to insert into the text/plain portion of the message"}}}'))
119
+ # rubocop:enable all
120
+ end
121
+
122
+ def test_that_personalizations_is_empty_initially
123
+ mail = SendGrid::Mail.new
124
+ assert_equal([], mail.personalizations)
125
+ end
126
+
127
+ def test_that_contents_is_empty_initially
128
+ mail = SendGrid::Mail.new
129
+ assert_equal([], mail.contents)
130
+ end
131
+
132
+ def test_that_attachments_is_empty_initially
133
+ mail = SendGrid::Mail.new
134
+ assert_equal([], mail.attachments)
135
+ end
136
+
137
+ def test_that_categories_is_empty_initially
138
+ mail = SendGrid::Mail.new
139
+ assert_equal([], mail.categories)
140
+ end
141
+
142
+ def test_add_personalization
143
+ mail = SendGrid::Mail.new
144
+ mail.add_personalization('foo')
145
+ assert_equal(['foo'.to_json], mail.personalizations)
146
+ end
147
+
148
+ def test_add_content
149
+ mail = SendGrid::Mail.new
150
+ mail.add_content('foo')
151
+ assert_equal(['foo'.to_json], mail.contents)
152
+ end
153
+
154
+ def test_add_section
155
+ mail = SendGrid::Mail.new
156
+ mail.add_section(Section.new(key: '%section1%', value: 'Substitution Text for Section 1'))
157
+ expected_json = {
158
+ 'sections' => {
159
+ '%section1%' => 'Substitution Text for Section 1'
160
+ }
161
+ }
162
+ assert_equal mail.to_json, expected_json
163
+ mail.add_section(Section.new(key: '%section2%', value: 'Substitution Text for Section 2'))
164
+ expected_json = {
165
+ 'sections' => {
166
+ '%section1%' => 'Substitution Text for Section 1',
167
+ '%section2%' => 'Substitution Text for Section 2'
168
+ }
169
+ }
170
+ assert_equal mail.to_json, expected_json
171
+ end
172
+
173
+ def test_add_header
174
+ mail = SendGrid::Mail.new
175
+ mail.add_header(Header.new(key: 'X-Test3', value: 'test3'))
176
+ expected_json = {
177
+ 'headers' => {
178
+ 'X-Test3' => 'test3'
179
+ }
180
+ }
181
+ assert_equal mail.to_json, expected_json
182
+ mail.add_header(Header.new(key: 'X-Test4', value: 'test4'))
183
+ expected_json = {
184
+ 'headers' => {
185
+ 'X-Test3' => 'test3',
186
+ 'X-Test4' => 'test4'
187
+ }
188
+ }
189
+ assert_equal mail.to_json, expected_json
190
+ end
191
+
192
+ def test_add_custom_arg
193
+ mail = SendGrid::Mail.new
194
+ mail.add_custom_arg(CustomArg.new(key: 'campaign 1', value: 'welcome 1'))
195
+ expected_json = {
196
+ 'custom_args' => {
197
+ 'campaign 1' => 'welcome 1'
198
+ }
199
+ }
200
+ assert_equal mail.to_json, expected_json
201
+ mail.add_custom_arg(CustomArg.new(key: 'campaign 2', value: 'welcome 2'))
202
+ expected_json = {
203
+ 'custom_args' => {
204
+ 'campaign 1' => 'welcome 1',
205
+ 'campaign 2' => 'welcome 2'
206
+ }
207
+ }
208
+ assert_equal mail.to_json, expected_json
209
+ end
210
+
211
+ def test_add_non_string_custom_arg
212
+ mail = Mail.new
213
+ mail.add_custom_arg(CustomArg.new(key: 'Integer', value: 1))
214
+ mail.add_custom_arg(CustomArg.new(key: 'Array', value: [1, 'a', true]))
215
+ mail.add_custom_arg(CustomArg.new(key: 'Hash', value: { 'a' => 1, 'b' => 2 }))
216
+ expected_json = {
217
+ 'custom_args' => {
218
+ 'Integer' => '1',
219
+ 'Array' => '[1, "a", true]',
220
+ 'Hash' => '{"a"=>1, "b"=>2}'
221
+ }
222
+ }
223
+ assert_equal mail.to_json, expected_json
224
+ end
225
+
226
+ def test_add_attachment
227
+ mail = SendGrid::Mail.new
228
+ mail.add_attachment('foo')
229
+ assert_equal(['foo'.to_json], mail.attachments)
230
+ end
231
+
232
+ def test_add_valid_category
233
+ mail = SendGrid::Mail.new
234
+ category = Category.new(name: 'foo')
235
+ mail.add_category(category)
236
+ assert_equal(['foo'], mail.categories)
237
+ end
238
+
239
+ def test_add_more_than_1_valid_category
240
+ mail = SendGrid::Mail.new
241
+ mail.add_category(Category.new(name: 'foo'))
242
+ mail.add_category(Category.new(name: 'bar'))
243
+ assert_equal(%w[foo bar], mail.categories)
244
+ end
245
+
246
+ def test_add_invalid_category
247
+ mail = SendGrid::Mail.new
248
+ assert_raises(NoMethodError) do
249
+ mail.add_category('foo')
250
+ end
251
+ end
252
+
253
+ def test_check_for_secrets
254
+ mail = Mail.new
255
+ mail.add_content(Content.new(type: 'text/plain', value: 'Sensitive information: SG.a123b456'))
256
+ assert_raises(SecurityError) do
257
+ mail.check_for_secrets([/SG.[a-zA-Z0-9_-]*/])
258
+ end
259
+ end
260
+ end
@@ -0,0 +1,214 @@
1
+ require_relative '../../../../lib/sendgrid/helpers/mail/personalization'
2
+ require 'minitest/autorun'
3
+
4
+ class TestPersonalization < Minitest::Test
5
+ include SendGrid
6
+
7
+ def test_add_to
8
+ @personalization = Personalization.new
9
+ @personalization.add_to(Email.new(email: 'test1@example.com', name: 'Example User'))
10
+ @personalization.add_to(Email.new(email: 'test2@example.com', name: 'Example User 2'))
11
+ expected_json = {
12
+ 'to' => [
13
+ {
14
+ 'email' => 'test1@example.com',
15
+ 'name' => 'Example User'
16
+ },
17
+ {
18
+ 'email' => 'test2@example.com',
19
+ 'name' => 'Example User 2'
20
+ }
21
+ ]
22
+ }
23
+ assert_equal @personalization.to_json, expected_json
24
+ end
25
+
26
+ def test_duplicate_add_to
27
+ @personalization = Personalization.new
28
+ @personalization.add_to(Email.new(email: 'test1@example.com', name: 'Example User'))
29
+ @personalization.add_to(Email.new(email: 'TEST2@EXAMPLE.COM', name: 'Example User 2'))
30
+
31
+ assert_raises(DuplicatePersonalizationError) do
32
+ @personalization.add_to(Email.new(email: 'test1@example.com', name: 'Duplicate User'))
33
+ end
34
+
35
+ assert_raises(DuplicatePersonalizationError) do
36
+ @personalization.add_to(Email.new(email: 'TEST1@EXAMPLE.COM', name: 'Duplicate User'))
37
+ end
38
+
39
+ assert_raises(DuplicatePersonalizationError) do
40
+ @personalization.add_to(Email.new(email: 'test2@example.com', name: 'Duplicate User 2'))
41
+ end
42
+
43
+ assert_raises(DuplicatePersonalizationError) do
44
+ @personalization.add_to(Email.new(email: 'TEST2@EXAMPLE.COM', name: 'Duplicate User 2'))
45
+ end
46
+ end
47
+
48
+ def test_add_from
49
+ @personalization = Personalization.new
50
+ @personalization.add_from(Email.new(email: 'from1@example.com', name: 'Example Sender'))
51
+ expected_json = {
52
+ 'from' => {
53
+ 'email' => 'from1@example.com',
54
+ 'name' => 'Example Sender'
55
+ }
56
+ }
57
+
58
+ assert_equal @personalization.to_json, expected_json
59
+ end
60
+
61
+ def test_add_cc
62
+ @personalization = Personalization.new
63
+ @personalization.add_cc(Email.new(email: 'test1@example.com', name: 'Example User'))
64
+ @personalization.add_cc(Email.new(email: 'test2@example.com', name: 'Example User 2'))
65
+ expected_json = {
66
+ 'cc' => [
67
+ {
68
+ 'email' => 'test1@example.com',
69
+ 'name' => 'Example User'
70
+ },
71
+ {
72
+ 'email' => 'test2@example.com',
73
+ 'name' => 'Example User 2'
74
+ }
75
+ ]
76
+ }
77
+ assert_equal @personalization.to_json, expected_json
78
+ end
79
+
80
+ def test_duplicate_add_cc
81
+ @personalization = Personalization.new
82
+ @personalization.add_cc(Email.new(email: 'test1@example.com', name: 'Example User'))
83
+ @personalization.add_cc(Email.new(email: 'TEST2@EXAMPLE.COM', name: 'Example User 2'))
84
+
85
+ assert_raises(DuplicatePersonalizationError) do
86
+ @personalization.add_cc(Email.new(email: 'test1@example.com', name: 'Duplicate User'))
87
+ end
88
+
89
+ assert_raises(DuplicatePersonalizationError) do
90
+ @personalization.add_cc(Email.new(email: 'TEST1@EXAMPLE.COM', name: 'Duplicate User'))
91
+ end
92
+
93
+ assert_raises(DuplicatePersonalizationError) do
94
+ @personalization.add_cc(Email.new(email: 'test2@example.com', name: 'Duplicate User 2'))
95
+ end
96
+
97
+ assert_raises(DuplicatePersonalizationError) do
98
+ @personalization.add_cc(Email.new(email: 'TEST2@EXAMPLE.COM', name: 'Duplicate User 2'))
99
+ end
100
+ end
101
+
102
+ def test_add_bcc
103
+ @personalization = Personalization.new
104
+ @personalization.add_bcc(Email.new(email: 'test1@example.com', name: 'Example User'))
105
+ @personalization.add_bcc(Email.new(email: 'test2@example.com', name: 'Example User 2'))
106
+ expected_json = {
107
+ 'bcc' => [
108
+ {
109
+ 'email' => 'test1@example.com',
110
+ 'name' => 'Example User'
111
+ },
112
+ {
113
+ 'email' => 'test2@example.com',
114
+ 'name' => 'Example User 2'
115
+ }
116
+ ]
117
+ }
118
+ assert_equal @personalization.to_json, expected_json
119
+ end
120
+
121
+ def test_duplicate_add_bcc
122
+ @personalization = Personalization.new
123
+ @personalization.add_bcc(Email.new(email: 'test1@example.com', name: 'Example User'))
124
+ @personalization.add_bcc(Email.new(email: 'TEST2@EXAMPLE.COM', name: 'Example User 2'))
125
+
126
+ assert_raises(DuplicatePersonalizationError) do
127
+ @personalization.add_bcc(Email.new(email: 'test1@example.com', name: 'Duplicate User'))
128
+ end
129
+
130
+ assert_raises(DuplicatePersonalizationError) do
131
+ @personalization.add_bcc(Email.new(email: 'TEST1@EXAMPLE.COM', name: 'Duplicate User'))
132
+ end
133
+
134
+ assert_raises(DuplicatePersonalizationError) do
135
+ @personalization.add_bcc(Email.new(email: 'test2@example.com', name: 'Duplicate User 2'))
136
+ end
137
+
138
+ assert_raises(DuplicatePersonalizationError) do
139
+ @personalization.add_bcc(Email.new(email: 'TEST2@EXAMPLE.COM', name: 'Duplicate User 2'))
140
+ end
141
+ end
142
+
143
+ def test_add_header
144
+ @personalization = Personalization.new
145
+ @personalization.add_header(Header.new(key: 'X-Test', value: 'True'))
146
+ expected_json = {
147
+ 'headers' => {
148
+ 'X-Test' => 'True'
149
+ }
150
+ }
151
+ assert_equal @personalization.to_json, expected_json
152
+ @personalization.add_header(Header.new(key: 'X-Test 1', value: 'False'))
153
+ expected_json = {
154
+ 'headers' => {
155
+ 'X-Test' => 'True',
156
+ 'X-Test 1' => 'False'
157
+ }
158
+ }
159
+ assert_equal @personalization.to_json, expected_json
160
+ end
161
+
162
+ def test_add_substitution
163
+ @personalization = Personalization.new
164
+ @personalization.add_substitution(Substitution.new(key: '%name%', value: 'Example User'))
165
+ expected_json = {
166
+ 'substitutions' => {
167
+ '%name%' => 'Example User'
168
+ }
169
+ }
170
+ assert_equal @personalization.to_json, expected_json
171
+ @personalization.add_substitution(Substitution.new(key: '%name 1%', value: 'Example User 1'))
172
+ expected_json = {
173
+ 'substitutions' => {
174
+ '%name%' => 'Example User',
175
+ '%name 1%' => 'Example User 1'
176
+ }
177
+ }
178
+ assert_equal @personalization.to_json, expected_json
179
+ end
180
+
181
+ def test_add_custom_arg
182
+ @personalization = Personalization.new
183
+ @personalization.add_custom_arg(CustomArg.new(key: 'user_id', value: '343'))
184
+ expected_json = {
185
+ 'custom_args' => {
186
+ 'user_id' => '343'
187
+ }
188
+ }
189
+ assert_equal @personalization.to_json, expected_json
190
+ @personalization.add_custom_arg(CustomArg.new(key: 'city', value: 'denver'))
191
+ expected_json = {
192
+ 'custom_args' => {
193
+ 'user_id' => '343',
194
+ 'city' => 'denver'
195
+ }
196
+ }
197
+ assert_equal @personalization.to_json, expected_json
198
+ end
199
+
200
+ def test_add_dynamic_template_data
201
+ @personalization = Personalization.new
202
+ @personalization.add_dynamic_template_data(
203
+ 'name' => 'Example User',
204
+ 'city' => 'Denver'
205
+ )
206
+ expected_json = {
207
+ 'dynamic_template_data' => {
208
+ 'name' => 'Example User',
209
+ 'city' => 'Denver'
210
+ }
211
+ }
212
+ assert_equal @personalization.to_json, expected_json
213
+ end
214
+ end
@@ -0,0 +1,36 @@
1
+ require_relative '../../../lib/sendgrid/helpers/permissions/scope'
2
+ require 'minitest/autorun'
3
+
4
+ class TestCategory < Minitest::Test
5
+ include SendGrid
6
+
7
+ # usecases
8
+ # 1. test admin scopes
9
+ # 2. test read only scopes
10
+ # 3. test read only and full access scopes for a method by hardcoding
11
+ # 4. test read only and full access scopes by loading scopes.yaml
12
+
13
+ def setup
14
+ @scopes_from_yaml = YAML.load_file("#{File.dirname(__FILE__)}/../../../lib/sendgrid/helpers/permissions/scopes.yml").freeze
15
+ end
16
+
17
+ def test_admin_scopes
18
+ assert_equal Scope.admin_permissions, @scopes_from_yaml.values.map(&:values).flatten
19
+ end
20
+
21
+ def test_read_only_scopes
22
+ assert_equal Scope.read_only_permissions, @scopes_from_yaml.map { |_, v| v[:read] }.flatten
23
+ end
24
+
25
+ def test_read_only_and_full_access_for_mail_hardcoded
26
+ assert_equal Scope.mail_read_only_permissions, ["mail.batch.read"]
27
+ assert_equal Scope.mail_full_access_permissions, ["mail.send", "mail.batch.create", "mail.batch.delete", "mail.batch.read", "mail.batch.update"]
28
+ end
29
+
30
+ def test_read_only_and_full_access_from_file
31
+ @scopes_from_yaml.each_key do |endpoint|
32
+ assert_equal Scope.send("#{endpoint}_read_only_permissions"), @scopes_from_yaml[endpoint][:read]
33
+ assert_equal Scope.send("#{endpoint}_full_access_permissions"), @scopes_from_yaml[endpoint].values.flatten
34
+ end
35
+ end
36
+ end