sparkpost 0.1.2 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,276 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe SparkPost::Template do
4
+ describe '#initialize' do
5
+ context 'when api key and host are passed'
6
+ let(:template) do
7
+ SparkPost::Template.new('123', 'http://sparkpost.com')
8
+ end
9
+
10
+ it { expect(template.instance_variables).to include(:@api_key) }
11
+ it { expect(template.instance_variables).to include(:@api_host) }
12
+ it { expect(template.instance_variable_get(:@api_key)).to eq('123') }
13
+ it do
14
+ expect(template.instance_variable_get(:@api_host))
15
+ .to eq('http://sparkpost.com')
16
+ end
17
+
18
+ context 'when api key or host not passed' do
19
+ it 'raises when api key and host not passed' do
20
+ expect { SparkPost::Template.new }
21
+ .to raise_error(ArgumentError)
22
+ end
23
+ it 'raises when host not passed' do
24
+ expect { SparkPost::Template.new(123) }
25
+ .to raise_error(ArgumentError)
26
+ end
27
+ end
28
+ end
29
+
30
+ describe '#endpoint' do
31
+ let(:template) do
32
+ SparkPost::Template.new('123456', 'https://api.sparkpost.com')
33
+ end
34
+ let(:url) { 'https://api.sparkpost.com/api/v1/templates' }
35
+
36
+ it 'returns correct endpoint' do
37
+ expect(template.endpoint).to eq(url)
38
+ end
39
+
40
+ it 'returns correct endpoint on subsequent calls' do
41
+ template.endpoint
42
+
43
+ expect(template.endpoint).to eq(url)
44
+ end
45
+ end
46
+
47
+ describe '#create' do
48
+ let(:template) do
49
+ SparkPost::Template.new('123456', 'https://api.sparkpost.com')
50
+ end
51
+ let(:url) { 'https://api.sparkpost.com/api/v1/templates' }
52
+
53
+ it 'calls request with correct data' do
54
+ prepared_data = {
55
+ content: {
56
+ from: {
57
+ email: 'me@me.com',
58
+ name: 'Sparky'
59
+ },
60
+ subject: 'test subject',
61
+ text: 'Hello Sparky',
62
+ html: '<h1>Hello Sparky</h1>'
63
+ },
64
+ options: {
65
+ transactional: true
66
+ },
67
+ id: 'sample-template',
68
+ name: 'Sample Template'
69
+ }
70
+
71
+ allow(template).to receive(:request) do |req_url, req_api_key, req_data, req_verb|
72
+ expect(req_verb).to eq('POST')
73
+ expect(req_api_key).to eq('123456')
74
+ expect(req_url).to eq(url)
75
+ expect(req_data).to eq(prepared_data)
76
+ end
77
+
78
+ template.create(
79
+ 'sample-template',
80
+ nil,
81
+ 'test subject',
82
+ '<h1>Hello Sparky</h1>',
83
+ text: 'Hello Sparky',
84
+ is_transactional: true,
85
+ name: 'Sample Template',
86
+ from_name: 'Sparky',
87
+ from_email: 'me@me.com'
88
+ )
89
+ end
90
+ end
91
+
92
+ describe '#update' do
93
+ context 'draft update' do
94
+ let(:template) do
95
+ SparkPost::Template.new('123456', 'https://api.sparkpost.com')
96
+ end
97
+ let(:url) { 'https://api.sparkpost.com/api/v1/templates/sample-template' }
98
+
99
+ it 'calls request with correct data' do
100
+ prepared_data = {
101
+ content: {
102
+ from: 'me@me.com',
103
+ subject: 'test subject',
104
+ text: 'Hello Sparky',
105
+ html: '<h1>Hello Sparky</h1>'
106
+ },
107
+ options: {
108
+ transactional: true
109
+ }
110
+ }
111
+
112
+ allow(template).to receive(:request) do |req_url, req_api_key, req_data, req_verb|
113
+ expect(req_verb).to eq('PUT')
114
+ expect(req_api_key).to eq('123456')
115
+ expect(req_url).to eq(url)
116
+ expect(req_data).to eq(prepared_data)
117
+ end
118
+
119
+ template.update(
120
+ 'sample-template',
121
+ 'me@me.com',
122
+ 'test subject',
123
+ '<h1>Hello Sparky</h1>',
124
+ text: 'Hello Sparky',
125
+ is_transactional: true
126
+ )
127
+ end
128
+ end
129
+
130
+ context 'published update' do
131
+ let(:template) do
132
+ SparkPost::Template.new('123456', 'https://api.sparkpost.com')
133
+ end
134
+ let(:url) { 'https://api.sparkpost.com/api/v1/templates/sample-template?update_published=true' }
135
+
136
+ it 'calls request with correct data' do
137
+ prepared_data = {
138
+ content: {
139
+ from: 'me@me.com',
140
+ subject: 'test subject',
141
+ text: 'Hello Sparky',
142
+ html: '<h1>Hello Sparky</h1>'
143
+ },
144
+ options: {
145
+ transactional: true
146
+ }
147
+ }
148
+
149
+ allow(template).to receive(:request) do |req_url, req_api_key, req_data, req_verb|
150
+ expect(req_verb).to eq('PUT')
151
+ expect(req_api_key).to eq('123456')
152
+ expect(req_url).to eq(url)
153
+ expect(req_data).to eq(prepared_data)
154
+ end
155
+
156
+ template.update(
157
+ 'sample-template',
158
+ 'me@me.com',
159
+ 'test subject',
160
+ '<h1>Hello Sparky</h1>',
161
+ text: 'Hello Sparky',
162
+ is_transactional: true,
163
+ update_published: true
164
+ )
165
+ end
166
+ end
167
+ end
168
+
169
+ describe '#delete' do
170
+ let(:template) do
171
+ SparkPost::Template.new('123456', 'https://api.sparkpost.com')
172
+ end
173
+ let(:url) { 'https://api.sparkpost.com/api/v1/templates/sample-template' }
174
+
175
+ it 'calls request with correct data' do
176
+ allow(template).to receive(:request) do |req_url, req_api_key, _req_data, req_verb|
177
+ expect(req_verb).to eq('DELETE')
178
+ expect(req_api_key).to eq('123456')
179
+ expect(req_url).to eq(url)
180
+ end
181
+
182
+ template.delete('sample-template')
183
+ end
184
+ end
185
+
186
+ describe '#get' do
187
+ let(:template) do
188
+ SparkPost::Template.new('123456', 'https://api.sparkpost.com')
189
+ end
190
+
191
+ let(:template_id) { 'my-email-template' }
192
+
193
+ context 'when retrieve published template' do
194
+ let(:url) { "https://api.sparkpost.com/api/v1/templates/#{template_id}" }
195
+
196
+ it 'calls request with correct data' do
197
+ allow(template).to receive(:request) do |req_url, req_api_key, _req_data, req_verb|
198
+ expect(req_verb).to eq('GET')
199
+ expect(req_api_key).to eq('123456')
200
+ expect(req_url).to eq(url)
201
+ end
202
+
203
+ template.get(template_id)
204
+ end
205
+ end
206
+
207
+ context 'when retrieve draft template' do
208
+ let(:url) { "https://api.sparkpost.com/api/v1/templates/#{template_id}?draft=true" }
209
+
210
+ it 'calls request with correct data' do
211
+ allow(template).to receive(:request) do |req_url, req_api_key, _req_data, req_verb|
212
+ expect(req_verb).to eq('GET')
213
+ expect(req_api_key).to eq('123456')
214
+ expect(req_url).to eq(url)
215
+ end
216
+
217
+ template.get(template_id, true)
218
+ end
219
+ end
220
+ end
221
+
222
+ describe '#list' do
223
+ let(:template) do
224
+ SparkPost::Template.new('123456', 'https://api.sparkpost.com')
225
+ end
226
+ let(:url) { 'https://api.sparkpost.com/api/v1/templates' }
227
+
228
+ it 'calls request with correct data' do
229
+ allow(template).to receive(:request) do |req_url, req_api_key, _req_data, req_verb|
230
+ expect(req_verb).to eq('GET')
231
+ expect(req_api_key).to eq('123456')
232
+ expect(req_url).to eq(url)
233
+ end
234
+
235
+ template.list
236
+ end
237
+ end
238
+
239
+ describe '#preview' do
240
+ let(:template) do
241
+ SparkPost::Template.new('123456', 'https://api.sparkpost.com')
242
+ end
243
+
244
+ let(:template_id) { 'my-email-template' }
245
+
246
+ context 'when retrieve published template' do
247
+ let(:url) { "https://api.sparkpost.com/api/v1/templates/#{template_id}/preview" }
248
+
249
+ it 'calls request with correct data' do
250
+ allow(template).to receive(:request) do |req_url, req_api_key, req_data, req_verb|
251
+ expect(req_verb).to eq('POST')
252
+ expect(req_api_key).to eq('123456')
253
+ expect(req_data).to eq(substitution_data: {})
254
+ expect(req_url).to eq(url)
255
+ end
256
+
257
+ template.preview(template_id, {})
258
+ end
259
+ end
260
+
261
+ context 'when retrieve draft template' do
262
+ let(:url) { "https://api.sparkpost.com/api/v1/templates/#{template_id}/preview?draft=true" }
263
+
264
+ it 'calls request with correct data' do
265
+ allow(template).to receive(:request) do |req_url, req_api_key, req_data, req_verb|
266
+ expect(req_verb).to eq('POST')
267
+ expect(req_api_key).to eq('123456')
268
+ expect(req_data).to eq(substitution_data: {})
269
+ expect(req_url).to eq(url)
270
+ end
271
+
272
+ template.preview(template_id, {}, true)
273
+ end
274
+ end
275
+ end
276
+ end
@@ -1,67 +1,292 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  RSpec.describe SparkPost::Transmission do
4
- describe '#initialize' do
4
+ describe '#initialize' do
5
5
  context 'when api key and host are passed'
6
- let(:transmission) { SparkPost::Transmission.new('123', 'http://sparkpost.com') }
6
+ let(:transmission) do
7
+ SparkPost::Transmission.new('123', 'http://sparkpost.com')
8
+ end
7
9
 
8
10
  it { expect(transmission.instance_variables).to include(:@api_key) }
9
11
  it { expect(transmission.instance_variables).to include(:@api_host) }
10
12
  it { expect(transmission.instance_variable_get(:@api_key)).to eq('123') }
11
- it { expect(transmission.instance_variable_get(:@api_host)).to eq('http://sparkpost.com') }
13
+ it do
14
+ expect(transmission.instance_variable_get(:@api_host))
15
+ .to eq('http://sparkpost.com')
16
+ end
12
17
 
13
- context 'when api key or host not passed' do
14
- it {expect {SparkPost::Transmission.new }.to raise_error(ArgumentError) }
15
- it {expect {SparkPost::Transmission.new(123) }.to raise_error(ArgumentError) }
18
+ context 'when api key or host not passed' do
19
+ it 'raises when api key and host not passed' do
20
+ expect { SparkPost::Transmission.new }
21
+ .to raise_error(ArgumentError)
22
+ end
23
+ it 'raises when host not passed' do
24
+ expect { SparkPost::Transmission.new(123) }
25
+ .to raise_error(ArgumentError)
26
+ end
16
27
  end
17
28
  end
18
29
 
19
- describe '#send_message' do
20
- let(:transmission) { SparkPost::Transmission.new('123456', 'https://api.sparkpost.com')}
30
+ describe '#endpoint' do
31
+ let(:transmission) do
32
+ SparkPost::Transmission.new('123456', 'https://api.sparkpost.com')
33
+ end
21
34
  let(:url) { 'https://api.sparkpost.com/api/v1/transmissions' }
22
- before do
23
- allow(transmission).to receive(:request).and_return({})
35
+
36
+ it 'returns correct endpoint' do
37
+ expect(transmission.endpoint).to eq(url)
24
38
  end
25
39
 
26
- it 'requests correct endpoint' do
27
- allow(transmission).to receive(:request) do |_url|
28
- expect(_url).to eq(url)
29
- end
40
+ it 'returns correct endpoint on subsequent calls' do
41
+ transmission.endpoint
42
+
43
+ expect(transmission.endpoint).to eq(url)
44
+ end
45
+ end
30
46
 
31
- transmission.send_message('to@example.com', 'from@example.com', 'test subject', '<h1>Hello World</h1>')
47
+ describe '#send_payload' do
48
+ let(:transmission) do
49
+ SparkPost::Transmission.new('123456', 'https://api.sparkpost.com')
50
+ end
51
+ let(:url) { 'https://api.sparkpost.com/api/v1/transmissions' }
52
+ let(:data) do
53
+ {
54
+ recipients: [
55
+ {
56
+ address: {
57
+ email: 'to@me.com', name: 'Me', header_to: 'no@reply.com'
58
+ }
59
+ }
60
+ ],
61
+ content: {
62
+ from: { email: 'me@me.com', name: 'Me' },
63
+ subject: 'test subject',
64
+ text: 'Hello Sparky',
65
+ html: '<h1>Hello Sparky</h1>'
66
+ }
67
+ }
32
68
  end
33
69
 
34
- it 'requests with correct parameters' do
35
- allow(transmission).to receive(:request) do |_url, api_key, data|
36
- expect(data[:recipients].length).to eq(1)
37
- expect(data[:recipients][0][:address]).to eq({email: 'to@example.com'})
38
- expect(data[:content][:from]).to eq('from@example.com')
39
- expect(data[:content][:subject]).to eq('test subject')
40
- expect(data[:content][:html]).to eq('<h1>Hello World</h1>')
41
- end
42
- transmission.send_message('to@example.com', 'from@example.com', 'test subject', '<h1>Hello World</h1>')
70
+ it 'calls request with correct data' do
71
+ allow(transmission).to receive(:request) do |req_url, req_api_key, req_data|
72
+ expect(req_api_key).to eq('123456')
73
+ expect(req_url).to eq(url)
74
+ expect(req_data).to eq(data)
75
+ end
76
+
77
+ transmission.send_payload(data)
43
78
  end
44
79
 
45
- it 'handles array of recipients correctly' do
46
- allow(transmission).to receive(:request) do |_url, api_key, data|
47
- expect(data[:recipients].length).to eq(1)
48
- expect(data[:recipients][0][:address]).to eq({email: 'to@example.com'})
49
- end
50
- transmission.send_message(['to@example.com'], 'from@example.com', 'test subject', '<h1>Hello World</h1>')
80
+ it 'passes through delivery exception' do
81
+ allow(transmission).to receive(:request).and_raise(
82
+ SparkPost::DeliveryException.new('Some delivery error'))
83
+
84
+ bad_data = data.merge(recipients: [])
85
+
86
+ expect do
87
+ transmission.send_payload(bad_data)
88
+ end.to raise_error(SparkPost::DeliveryException).with_message(
89
+ /Some delivery error/)
51
90
  end
52
91
 
53
- it { expect {transmission.send_message(['to@example.com'], 'from@example.com', 'test subject')}.to raise_error(ArgumentError).with_message(/Content missing/) }
54
- it { expect {transmission.send_message(['to@example.com'], 'from@example.com', 'test subject', nil, {text_message: 'hello world'})}.to_not raise_error }
92
+ it 'passes responses' do
93
+ allow(transmission).to receive(:request).and_return(success: 1)
94
+ expect(transmission.send_payload(data)).to eq(success: 1)
95
+ end
96
+ end
55
97
 
56
- it 'passes through delivery exception' do
57
- allow(transmission).to receive(:request).and_raise(SparkPost::DeliveryException.new('Some delivery error'))
98
+ describe '#send_message' do
99
+ let(:transmission) do
100
+ SparkPost::Transmission.new('123456', 'https://api.sparkpost.com')
101
+ end
102
+ let(:url) { 'https://api.sparkpost.com/api/v1/transmissions' }
103
+ before do
104
+ allow(transmission).to receive(:request).and_return({})
105
+ end
106
+
107
+ it 'calls send_payload with prepared data' do
108
+ prepared_data = {
109
+ recipients: [
110
+ {
111
+ address: {
112
+ email: 'to@me.com'
113
+ }
114
+ }
115
+ ],
116
+ content: {
117
+ from: 'me@me.com',
118
+ subject: 'test subject',
119
+ text: 'hello sparky',
120
+ html: '<h1>Hello Sparky</h1>'
121
+ },
122
+ options: {}
123
+ }
124
+
125
+ expect(transmission).to receive(:send_payload).with(prepared_data)
126
+ transmission.send_message(
127
+ 'to@me.com',
128
+ 'me@me.com',
129
+ 'test subject',
130
+ '<h1>Hello Sparky</h1>',
131
+ text_message: 'hello sparky'
132
+ )
133
+ end
134
+
135
+ it 'requests correct endpoint' do
136
+ allow(transmission).to receive(:request) do |request_url|
137
+ expect(request_url).to eq(url)
138
+ end
139
+
140
+ transmission.send_message(
141
+ 'to@example.com',
142
+ 'from@example.com',
143
+ 'test subject',
144
+ '<h1>Hello World</h1>')
145
+ end
146
+
147
+ it 'calls prepare_attachments' do
148
+ allow(transmission).to receive(:request)
149
+
150
+ allow(transmission).to receive(:prepare_recipients) do |recipients|
151
+ expect(recipients).to eq('to@example.com')
152
+ end
153
+
154
+ transmission.send_message(
155
+ 'to@example.com',
156
+ 'from@example.com',
157
+ 'test subject',
158
+ '<h1>Hello World</h1>')
159
+ end
160
+
161
+ it 'requests with correct parameters' do
162
+ allow(transmission).to receive(:request) do |_url, _api_key, data|
163
+ expect(data[:recipients].length).to eq(1)
164
+ expect(data[:recipients][0][:address]).to eq(email: 'to@example.com')
165
+ expect(data[:content][:from]).to eq('from@example.com')
166
+ expect(data[:content][:subject]).to eq('test subject')
167
+ expect(data[:content][:html]).to eq('<h1>Hello World</h1>')
168
+ end
169
+ transmission.send_message(
170
+ 'to@example.com',
171
+ 'from@example.com',
172
+ 'test subject',
173
+ '<h1>Hello World</h1>')
174
+ end
175
+
176
+ it 'raises erorr when no content passed' do
177
+ expect do
178
+ transmission.send_message(
179
+ ['to@example.com'],
180
+ 'from@example.com',
181
+ 'test subject')
182
+ end.to raise_error(ArgumentError).with_message(/Content missing/)
183
+ end
184
+
185
+ it 'it does not raise error when text_message passed' do
186
+ expect do
187
+ transmission.send_message(
188
+ ['to@example.com'],
189
+ 'from@example.com',
190
+ 'test subject',
191
+ nil,
192
+ text_message: 'hello world')
193
+ end.to_not raise_error
194
+ end
195
+
196
+ it 'passes through delivery exception' do
197
+ allow(transmission).to receive(:request).and_raise(
198
+ SparkPost::DeliveryException.new('Some delivery error'))
199
+
200
+ expect do
201
+ transmission.send_message(
202
+ 'to@example.com',
203
+ 'from@example.com',
204
+ 'test subject',
205
+ '<h1>Hello World</h1>')
206
+ end.to raise_error(SparkPost::DeliveryException).with_message(
207
+ /Some delivery error/)
208
+ end
209
+
210
+ it 'passes responses' do
211
+ allow(transmission).to receive(:request).and_return(success: 1)
212
+ expect(transmission.send_message(
213
+ 'to@example.com',
214
+ 'from@example.com',
215
+ 'test subject',
216
+ '<h1>Hello World</h1>')).to eq(success: 1)
217
+ end
218
+
219
+ it 'sends attachments' do
220
+ attachment = {
221
+ name: 'attachment.txt',
222
+ type: 'text/plain',
223
+ data: Base64.encode64('Hello World')
224
+ }
225
+ options = {
226
+ attachments: [attachment]
227
+ }
228
+
229
+ allow(transmission).to receive(:request) do |_url, _api_key, data|
230
+ expect(data[:content][:attachments]).to be_kind_of(Array)
231
+ expect(data[:content][:attachments].length).to eq(1)
232
+ expect(data[:content][:attachments][0]).to eq(attachment)
233
+ end
234
+ transmission.send_message(
235
+ ['to@example.com'],
236
+ 'from@example.com',
237
+ 'test subject',
238
+ '<h1>Hello World</h1>',
239
+ options)
240
+ end
241
+ end
242
+
243
+ describe '#prepare_recipients' do
244
+ let(:transmission) do
245
+ SparkPost::Transmission.new('123456', 'https://api.sparkpost.com')
246
+ end
247
+
248
+ it 'returns an array' do
249
+ expect(transmission.prepare_recipients('to@domain.com')).to be_kind_of(Array)
250
+ end
251
+
252
+ it 'handles a recipient hash with email, name and header_to correctly' do
253
+ prepared_recipients = transmission.prepare_recipients(
254
+ email: 'to@me.com', name: 'Me', header_to: 'no@reply.com'
255
+ )
256
+
257
+ expect(prepared_recipients.length).to eq(1)
258
+ expect(prepared_recipients[0]).to eq(address: { email: 'to@me.com', name: 'Me', header_to: 'no@reply.com' })
259
+ end
260
+
261
+ it 'handles array of recipient hashes correctly' do
262
+ prepared_recipients = transmission.prepare_recipients(
263
+ [{ email: 'to@me.com', name: 'Me', header_to: 'no@reply.com' }]
264
+ )
265
+ expect(prepared_recipients.length).to eq(1)
266
+ expect(prepared_recipients[0]).to eq(address: { email: 'to@me.com', name: 'Me', header_to: 'no@reply.com' })
267
+ end
268
+
269
+ it 'handles array of recipients correctly' do
270
+ expect(
271
+ transmission.prepare_recipients(['to@example.com'])
272
+ ).to eq([{ address: { email: 'to@example.com' } }])
273
+ end
58
274
 
59
- expect { transmission.send_message('to@example.com', 'from@example.com', 'test subject', '<h1>Hello World</h1>')}.to raise_error(SparkPost::DeliveryException).with_message(/Some delivery error/)
275
+ it 'raises for invalid recipient hash' do
276
+ expect do
277
+ transmission.prepare_recipients(name: 'Me', header_to: 'no@reply.com')
278
+ end.to raise_error(ArgumentError, /email missing/)
60
279
  end
61
280
 
62
- it 'passes responses' do
63
- allow(transmission).to receive(:request).and_return({success: 1})
64
- expect(transmission.send_message('to@example.com', 'from@example.com', 'test subject', '<h1>Hello World</h1>')).to eq({success: 1})
281
+ it 'throws for an array of invalid recipient hashes' do
282
+ expect do
283
+ transmission.prepare_recipients(
284
+ [
285
+ { to: 'to@me.com', name: 'Me', header_to: 'no@reply.com' },
286
+ { name: 'You', header_to: 'no@reply.com' }
287
+ ]
288
+ )
289
+ end.to raise_error(ArgumentError, /email missing/)
65
290
  end
66
291
  end
67
292
  end