sparkpost_rails_eu 1.5.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,30 @@
1
+ require 'spec_helper'
2
+
3
+ describe SparkPostRails::DeliveryMethod do
4
+
5
+ before(:each) do
6
+ @delivery_method = SparkPostRails::DeliveryMethod.new
7
+ end
8
+
9
+ context "Recipient Data" do
10
+
11
+ it "accepts data matching all recipients" do
12
+ recipients = ['recipient1@email.com', 'recipient2@email.com']
13
+ recipients_data = [
14
+ {substitution_data: {name: "Recipient1"}},
15
+ {substitution_data: {name: "Recipient2"}}
16
+ ]
17
+
18
+ test_email = Mailer.test_email to: recipients, sparkpost_data: {recipients: recipients_data}
19
+
20
+ @delivery_method.deliver!(test_email)
21
+
22
+ actual_recipients = @delivery_method.data[:recipients]
23
+ expect(actual_recipients.length).to eq(recipients.length)
24
+ expect(actual_recipients).to match(recipients.each_with_index.map { |recipient, index| recipients_data[index].merge(address: {email: recipient, header_to: anything}) })
25
+ end
26
+
27
+ end
28
+ end
29
+
30
+
@@ -0,0 +1,34 @@
1
+ require 'spec_helper'
2
+
3
+ describe SparkPostRails::DeliveryMethod do
4
+
5
+ before(:each) do
6
+ @delivery_method = SparkPostRails::DeliveryMethod.new
7
+ end
8
+
9
+ context "Recipients List" do
10
+
11
+ it "handles a list_id" do
12
+ test_email = Mailer.test_email sparkpost_data: {recipient_list_id: "List1"}
13
+ @delivery_method.deliver!(test_email)
14
+
15
+ expect(@delivery_method.data[:recipients]).to eq({list_id: "List1"})
16
+ end
17
+
18
+ it "ignores any CC addresses" do
19
+ test_email = Mailer.test_email cc: "cc@example.com", sparkpost_data: {recipient_list_id: "List1"}
20
+ @delivery_method.deliver!(test_email)
21
+
22
+ expect(@delivery_method.data[:recipients]).to eq({list_id: "List1"})
23
+ expect(@delivery_method.data[:content].has_key?(:headers)).to eq(false)
24
+ end
25
+
26
+ it "ignores any BCC addresses" do
27
+ test_email = Mailer.test_email bcc: "cc@example.com", sparkpost_data: {recipient_list_id: "List1"}
28
+ @delivery_method.deliver!(test_email)
29
+
30
+ expect(@delivery_method.data[:recipients]).to eq({list_id: "List1"})
31
+ end
32
+ end
33
+
34
+ end
@@ -0,0 +1,62 @@
1
+ require 'spec_helper'
2
+
3
+ describe SparkPostRails::DeliveryMethod do
4
+
5
+ before(:each) do
6
+ @delivery_method = SparkPostRails::DeliveryMethod.new
7
+ end
8
+
9
+ context "Recipients" do
10
+
11
+ context "single recipient" do
12
+ it "handles email only" do
13
+ test_email = Mailer.test_email
14
+ @delivery_method.deliver!(test_email)
15
+
16
+ expect(@delivery_method.data[:recipients]).to match([a_hash_including({address: {email: "to@example.com", header_to: anything}})])
17
+ end
18
+
19
+ it "handles name and email" do
20
+ test_email = Mailer.test_email to: "Joe Test <to@example.com>"
21
+ @delivery_method.deliver!(test_email)
22
+
23
+ expect(@delivery_method.data[:recipients]).to match([a_hash_including({address: {email: "to@example.com", name: "Joe Test", header_to: anything}})])
24
+ end
25
+ end
26
+
27
+ context "multiple recipients" do
28
+ it "handles email only" do
29
+ test_email = Mailer.test_email to: "to1@example.com, to2@example.com"
30
+ @delivery_method.deliver!(test_email)
31
+
32
+ expect(@delivery_method.data[:recipients]).to match([a_hash_including({address: {email: "to1@example.com", header_to: anything}}),
33
+ a_hash_including({address: {email: "to2@example.com", header_to: anything}})])
34
+ end
35
+
36
+ it "handles name and email" do
37
+ test_email = Mailer.test_email to: "Sam Test <to1@example.com>, Joe Test <to2@example.com>"
38
+ @delivery_method.deliver!(test_email)
39
+
40
+ expect(@delivery_method.data[:recipients]).to match([a_hash_including({:address=>{:email=>"to1@example.com", :name=>"Sam Test", header_to: anything}}),
41
+ a_hash_including({:address=>{:email=>"to2@example.com", :name=>"Joe Test", header_to: anything}})])
42
+ end
43
+
44
+ it "handles mix of email only and name/email" do
45
+ test_email = Mailer.test_email to: "Sam Test <to1@example.com>, to2@example.com"
46
+ @delivery_method.deliver!(test_email)
47
+
48
+ expect(@delivery_method.data[:recipients]).to match_array([a_hash_including({:address=>{:email=>"to1@example.com", :name=>"Sam Test", header_to: anything}}),
49
+ a_hash_including({:address=>{:email=>"to2@example.com", header_to: anything}})])
50
+ end
51
+
52
+ it "compiles list of email addresses to populate :header_to for each recipient" do
53
+ expected_header_to = "a@a.com,b@b.com"
54
+ test_email = Mailer.test_email to: "a <a@a.com>, b@b.com"
55
+ @delivery_method.deliver!(test_email)
56
+
57
+ expect(@delivery_method.data[:recipients].first[:address][:header_to]).to eql(expected_header_to)
58
+ expect(@delivery_method.data[:recipients].second[:address][:header_to]).to eql(expected_header_to)
59
+ end
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,25 @@
1
+ require 'spec_helper'
2
+
3
+ describe SparkPostRails::DeliveryMethod do
4
+
5
+ before(:each) do
6
+ @delivery_method = SparkPostRails::DeliveryMethod.new
7
+ end
8
+
9
+ context "Reply To" do
10
+ it "handles supplied value" do
11
+ test_email = Mailer.test_email reply_to: "reply_to@example.com"
12
+ @delivery_method.deliver!(test_email)
13
+
14
+ expect(@delivery_method.data[:content][:reply_to]).to eq("reply_to@example.com")
15
+ end
16
+
17
+ it "handles no value supplied" do
18
+ test_email = Mailer.test_email
19
+ @delivery_method.deliver!(test_email)
20
+
21
+ expect(@delivery_method.data[:content].has_key?(:reply_to)).to eq(false)
22
+ end
23
+ end
24
+ end
25
+
@@ -0,0 +1,26 @@
1
+ require 'spec_helper'
2
+
3
+ describe SparkPostRails::DeliveryMethod do
4
+
5
+ before(:each) do
6
+ @delivery_method = SparkPostRails::DeliveryMethod.new
7
+ end
8
+
9
+ context "API Response Handling" do
10
+ it "returns result data on success" do
11
+ test_email = Mailer.test_email
12
+ response = @delivery_method.deliver!(test_email)
13
+
14
+ expect(response).to eq({"total_rejected_recipients"=>0, "total_accepted_recipients"=>1, "id"=>"00000000000000000"})
15
+ end
16
+
17
+ it "raises exception on error" do
18
+ stub_request(:any, "https://api.sparkpost.com/api/v1/transmissions").
19
+ to_return(body: "{\"errors\":[{\"message\":\"required field is missing\",\"description\":\"recipients or list_id required\",\"code\":\"1400\"}]}", status: 403)
20
+
21
+ test_email = Mailer.test_email
22
+
23
+ expect {@delivery_method.deliver!(test_email)}.to raise_exception(SparkPostRails::DeliveryException)
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,50 @@
1
+ require 'spec_helper'
2
+
3
+ describe SparkPostRails::DeliveryMethod do
4
+
5
+ before(:each) do
6
+ SparkPostRails.configuration.set_defaults
7
+ @delivery_method = SparkPostRails::DeliveryMethod.new
8
+ end
9
+
10
+ context "Return Path" do
11
+ it "handles return path set in the configuration" do
12
+ SparkPostRails.configure do |c|
13
+ c.return_path = "BOUNCE-EMAIL@EXAMPLE.COM"
14
+ end
15
+
16
+ test_email = Mailer.test_email
17
+ @delivery_method.deliver!(test_email)
18
+
19
+ expect(@delivery_method.data[:return_path]).to eq('BOUNCE-EMAIL@EXAMPLE.COM')
20
+ end
21
+
22
+ it "handles return path on an individual message" do
23
+ test_email = Mailer.test_email return_path: "bounce@example.com"
24
+
25
+ @delivery_method.deliver!(test_email)
26
+
27
+ expect(@delivery_method.data[:return_path]).to eq('bounce@example.com')
28
+ end
29
+
30
+ it "handles the value on an individual message overriding configuration" do
31
+ SparkPostRails.configure do |c|
32
+ c.return_path = "BOUNCE-EMAIL@EXAMPLE.COM"
33
+ end
34
+
35
+ test_email = Mailer.test_email return_path: "bounce@example.com"
36
+
37
+ @delivery_method.deliver!(test_email)
38
+
39
+ expect(@delivery_method.data[:return_path]).to eq('bounce@example.com')
40
+ end
41
+
42
+ it "handles a default setting of none" do
43
+ test_email = Mailer.test_email
44
+ @delivery_method.deliver!(test_email)
45
+
46
+ expect(@delivery_method.data.has_key?(:return_path)).to eq(false)
47
+ end
48
+
49
+ end
50
+ end
@@ -0,0 +1,53 @@
1
+ require 'spec_helper'
2
+
3
+ describe SparkPostRails::DeliveryMethod do
4
+
5
+ before(:each) do
6
+ SparkPostRails.configuration.set_defaults
7
+ @delivery_method = SparkPostRails::DeliveryMethod.new
8
+ end
9
+
10
+ context "Sandbox Mode" do
11
+ it "handles sandbox mode enabled in the configuration" do
12
+ SparkPostRails.configure do |c|
13
+ c.sandbox = true
14
+ end
15
+
16
+ test_email = Mailer.test_email
17
+
18
+ @delivery_method.deliver!(test_email)
19
+
20
+ expect(@delivery_method.data[:options][:sandbox]).to eq(true)
21
+ end
22
+
23
+ it "handles sandbox mode enabled on an individual message" do
24
+ test_email = Mailer.test_email sparkpost_data: {sandbox: true}
25
+
26
+ @delivery_method.deliver!(test_email)
27
+
28
+ expect(@delivery_method.data[:options][:sandbox]).to eq(true)
29
+ end
30
+
31
+ it "handles the value on an individual message overriding configuration" do
32
+ SparkPostRails.configure do |c|
33
+ c.sandbox = true
34
+ end
35
+
36
+ test_email = Mailer.test_email sparkpost_data: {sandbox: false}
37
+
38
+ @delivery_method.deliver!(test_email)
39
+
40
+ expect(@delivery_method.data[:options].has_key?(:sandbox)).to eq(false)
41
+ end
42
+
43
+ it "handles a default setting of 'false'" do
44
+ test_email = Mailer.test_email
45
+
46
+ @delivery_method.deliver!(test_email)
47
+
48
+ expect(@delivery_method.data[:options].has_key?(:sandbox)).to eq(false)
49
+ end
50
+
51
+ end
52
+ end
53
+
@@ -0,0 +1,27 @@
1
+ require 'spec_helper'
2
+
3
+ describe SparkPostRails::DeliveryMethod do
4
+
5
+ before(:each) do
6
+ @delivery_method = SparkPostRails::DeliveryMethod.new
7
+ end
8
+
9
+ context "Skip Suppression" do
10
+ it "handles value from message" do
11
+ test_email = Mailer.test_email sparkpost_data: {skip_suppression: true}
12
+
13
+ @delivery_method.deliver!(test_email)
14
+
15
+ expect(@delivery_method.data[:options][:skip_suppression]).to eq(true)
16
+ end
17
+
18
+ it "does not include skip_suppression element if not supplied" do
19
+ test_email = Mailer.test_email
20
+ @delivery_method.deliver!(test_email)
21
+
22
+ expect(@delivery_method.data[:options].has_key?(:skip_suppression)).to eq(false)
23
+ end
24
+
25
+ end
26
+ end
27
+
@@ -0,0 +1,91 @@
1
+ require 'webmock/rspec'
2
+ require 'rails'
3
+ require 'action_mailer'
4
+ require "sparkpost_rails"
5
+
6
+ RSpec.configure do |config|
7
+
8
+ config.before(:all) do
9
+ ActionMailer::Base.send :include, SparkPostRails::DataOptions
10
+ end
11
+
12
+ config.before(:each) do |example|
13
+ if example.metadata[:skip_configure]
14
+ SparkPostRails.configuration = nil # Reset configuration
15
+ else
16
+ SparkPostRails.configure do |c|
17
+ c.api_key = "TESTKEY1234"
18
+ end
19
+ end
20
+
21
+ stub_request(:any, "https://api.sparkpost.com/api/v1/transmissions").
22
+ to_return(body: "{\"results\":{\"total_rejected_recipients\":0,\"total_accepted_recipients\":1,\"id\":\"00000000000000000\"}}", status: 200)
23
+ end
24
+
25
+ end
26
+
27
+ #A default mailer to generate the mail object
28
+ class Mailer < ActionMailer::Base
29
+ def test_email options = {}
30
+ data = {
31
+ from: "from@example.com",
32
+ to: options[:to] || "to@example.com",
33
+ subject: "Test Email",
34
+ text_part: "Hello, Testing!"
35
+ }
36
+
37
+ if options.has_key?(:attachments)
38
+ options[:attachments].times do |i|
39
+ attachments["file_#{i}.txt"] = "This is file #{i}"
40
+ end
41
+
42
+ options.delete(:attachments)
43
+ end
44
+
45
+ if options.has_key?(:images)
46
+ options[:images].times do |i|
47
+ attachments["image_#{i}.png"] = sparkpost_logo_contents
48
+ end
49
+
50
+ options.delete(:images)
51
+ end
52
+
53
+ if options.has_key?(:inline_attachments)
54
+ options[:inline_attachments].times do |i|
55
+ attachments.inline["image_#{i}.png"] = sparkpost_logo_contents
56
+ end
57
+
58
+ options.delete(:inline_attachments)
59
+ end
60
+
61
+ if options.has_key?(:headers)
62
+ if options[:headers].class == Hash
63
+ headers options[:headers]
64
+ end
65
+
66
+ options.delete(:headers)
67
+ end
68
+
69
+ data.merge! options
70
+
71
+ if data.has_key?(:html_part)
72
+
73
+ mail(data) do |format|
74
+ format.text {render text: data[:text_part]}
75
+ format.html {render text: data[:html_part]}
76
+ end
77
+
78
+ else
79
+
80
+ mail(data) do |format|
81
+ format.text {render text: data[:text_part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EY4xfIufdgjZrINdBNPuxDnhuhL81u5iJH0y5cE4oGFfKi5zOH+I+NRIpk\nqJ+50Sc1x7mjU5rkyHtyI5suEYjcaNO+sO8po5eqRE5XZmelPo+O/J00fVZM\nCh2R0iRGHi1EeYQ8jBHkxjHtmYkcN6N81CEvOhET/do+SolLR/4AoD4RKg7h\nfp/Vbl1qnsOdGj5ZDvfivMRQaTGGexFPKi2ZTZeATe8a54NCL9fGKn7k8Hi4\nytu5GXBsdkbe86vDB63+6Pl42QLOz3tKXzjyHwC/TfqZfgXg/yj90dPb6kD1\n/Q+Af77e/0v98vny/zLB/+j0+vl5PP1uAP8CV/GqdTJ4tWwAAAAASUVORK5C\nYII=\n"
89
+ Base64.decode64(encoded_contents)
90
+ end
91
+ end
@@ -0,0 +1,75 @@
1
+ require 'spec_helper'
2
+
3
+ describe SparkPostRails::DeliveryMethod do
4
+
5
+ before(:each) do
6
+ SparkPostRails.configuration.set_defaults
7
+ @delivery_method = SparkPostRails::DeliveryMethod.new
8
+ end
9
+
10
+ context "Message-Specific API Key" do
11
+ it "uses supplied API key instead of default" do
12
+ SparkPostRails.configure do |c|
13
+ c.api_key = 'NEW_DEFAULT_API_KEY'
14
+ end
15
+
16
+ test_email = Mailer.test_email sparkpost_data: {api_key: 'SUBACCOUNT_API_KEY'}
17
+ @delivery_method.deliver!(test_email)
18
+
19
+ expect(@delivery_method.headers).to include("Authorization" => "SUBACCOUNT_API_KEY")
20
+ end
21
+
22
+ it "uses default API if no message-specific API key applied" do
23
+ SparkPostRails.configure do |c|
24
+ c.api_key = 'NEW_DEFAULT_API_KEY'
25
+ end
26
+
27
+ test_email = Mailer.test_email
28
+ @delivery_method.deliver!(test_email)
29
+
30
+ expect(@delivery_method.headers).to include("Authorization" => "NEW_DEFAULT_API_KEY")
31
+ end
32
+ end
33
+
34
+ context "Subaccount ID" do
35
+ it "accepts a subaccount ID in the configuration" do
36
+ SparkPostRails.configure do |c|
37
+ c.subaccount = 123
38
+ end
39
+
40
+ test_email = Mailer.test_email
41
+ @delivery_method.deliver!(test_email)
42
+
43
+ expect(@delivery_method.headers).to include("X-MSYS-SUBACCOUNT" => "123")
44
+ end
45
+
46
+ it "defaults to no subaccount ID in the configuration" do
47
+ expect(SparkPostRails.configuration.subaccount).to eq(nil)
48
+ end
49
+
50
+ it "accepts subaccount ID for an individual message" do
51
+ test_email = Mailer.test_email sparkpost_data: {subaccount: 456}
52
+ @delivery_method.deliver!(test_email)
53
+
54
+ expect(@delivery_method.headers).to include("X-MSYS-SUBACCOUNT" => "456")
55
+ end
56
+
57
+ it "uses subaccount ID on message instead of value in configuration" do
58
+ SparkPostRails.configure do |c|
59
+ c.subaccount = 123
60
+ end
61
+
62
+ test_email = Mailer.test_email sparkpost_data: {subaccount: 456}
63
+ @delivery_method.deliver!(test_email)
64
+
65
+ expect(@delivery_method.headers).to include("X-MSYS-SUBACCOUNT" => "456")
66
+ end
67
+
68
+ it "does not include the subaccount header when none is specified" do
69
+ test_email = Mailer.test_email
70
+ @delivery_method.deliver!(test_email)
71
+
72
+ expect(@delivery_method.headers.has_key?("X-MSYS-SUBACCOUNT")).to eq(false)
73
+ end
74
+ end
75
+ end