sendgrid-api 0.0.2 → 0.0.3
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.
- data/.gitignore +4 -2
- data/.yardopts +6 -0
- data/Gemfile +2 -0
- data/Gemfile.lock +5 -1
- data/README.md +200 -12
- data/Rakefile +3 -0
- data/lib/sendgrid/api/client.rb +16 -0
- data/lib/sendgrid/api/entities/category.rb +13 -0
- data/lib/sendgrid/api/entities/email.rb +13 -0
- data/lib/sendgrid/api/entities/entity.rb +2 -2
- data/lib/sendgrid/api/entities/list.rb +30 -0
- data/lib/sendgrid/api/entities/marketing_email.rb +20 -0
- data/lib/sendgrid/api/entities/response_insert.rb +23 -0
- data/lib/sendgrid/api/entities/response_remove.rb +23 -0
- data/lib/sendgrid/api/entities/schedule.rb +13 -0
- data/lib/sendgrid/api/entities/sender_address.rb +13 -0
- data/lib/sendgrid/api/newsletter/categories.rb +74 -0
- data/lib/sendgrid/api/newsletter/emails.rb +69 -0
- data/lib/sendgrid/api/newsletter/lists.rb +64 -0
- data/lib/sendgrid/api/newsletter/marketing_emails.rb +72 -0
- data/lib/sendgrid/api/newsletter/recipients.rb +55 -0
- data/lib/sendgrid/api/newsletter/schedule.rb +70 -0
- data/lib/sendgrid/api/newsletter/sender_addresses.rb +80 -0
- data/lib/sendgrid/api/newsletter/utils.rb +34 -0
- data/lib/sendgrid/api/rest/errors/error.rb +9 -3
- data/lib/sendgrid/api/rest/resource.rb +3 -1
- data/lib/sendgrid/api/version.rb +1 -1
- data/lib/sendgrid/api/web/mail.rb +44 -0
- data/lib/sendgrid/api/web/profile.rb +3 -3
- data/lib/sendgrid/api/web/stats.rb +3 -3
- data/spec/fixtures/categories.json +11 -0
- data/spec/fixtures/emails/email.json +6 -0
- data/spec/fixtures/emails/emails.json +10 -0
- data/spec/fixtures/errors/already_exists.json +3 -0
- data/spec/fixtures/errors/bad_request.json +6 -0
- data/spec/fixtures/errors/database_error.json +3 -0
- data/spec/fixtures/errors/does_not_exist.json +3 -0
- data/spec/fixtures/{forbidden.json → errors/forbidden.json} +0 -0
- data/spec/fixtures/errors/invalid_fields.json +3 -0
- data/spec/fixtures/errors/not_scheduled.json +3 -0
- data/spec/fixtures/errors/unauthorized.json +6 -0
- data/spec/fixtures/lists/list.json +5 -0
- data/spec/fixtures/lists/lists.json +11 -0
- data/spec/fixtures/marketing_emails/marketing_email.json +19 -0
- data/spec/fixtures/marketing_emails/marketing_emails.json +10 -0
- data/spec/fixtures/recipients.json +8 -0
- data/spec/fixtures/schedule.json +3 -0
- data/spec/fixtures/sender_addresses/sender_address.json +11 -0
- data/spec/fixtures/sender_addresses/sender_addresses.json +11 -0
- data/spec/sendgrid/api/client_spec.rb +16 -0
- data/spec/sendgrid/api/entities/category_spec.rb +14 -0
- data/spec/sendgrid/api/entities/email_spec.rb +15 -0
- data/spec/sendgrid/api/entities/list_spec.rb +34 -0
- data/spec/sendgrid/api/entities/marketing_email_spec.rb +31 -0
- data/spec/sendgrid/api/entities/response_insert_spec.rb +28 -0
- data/spec/sendgrid/api/entities/response_remove_spec.rb +28 -0
- data/spec/sendgrid/api/entities/schedule_spec.rb +14 -0
- data/spec/sendgrid/api/entities/sender_address_spec.rb +21 -0
- data/spec/sendgrid/api/newsletter/categories_spec.rb +247 -0
- data/spec/sendgrid/api/newsletter/emails_spec.rb +265 -0
- data/spec/sendgrid/api/newsletter/lists_spec.rb +307 -0
- data/spec/sendgrid/api/newsletter/marketing_emails_spec.rb +306 -0
- data/spec/sendgrid/api/newsletter/recipients_spec.rb +252 -0
- data/spec/sendgrid/api/newsletter/schedule_spec.rb +263 -0
- data/spec/sendgrid/api/newsletter/sender_addresses_spec.rb +300 -0
- data/spec/sendgrid/api/rest/errors/error_spec.rb +40 -16
- data/spec/sendgrid/api/rest/resource_spec.rb +2 -0
- data/spec/sendgrid/api/web/mail_spec.rb +111 -0
- data/spec/sendgrid/api/web/profile_spec.rb +13 -29
- data/spec/sendgrid/api/web/stats_spec.rb +9 -15
- data/spec/support/helpers.rb +8 -0
- data/spec/support/mock.rb +6 -2
- data/spec/support/online.rb +114 -0
- data/spec/support/shared_examples.rb +93 -0
- metadata +96 -10
- data/spec/fixtures/unauthorized.json +0 -6
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module Sendgrid
|
4
|
+
module API
|
5
|
+
module Entities
|
6
|
+
describe List do
|
7
|
+
subject { described_class.new }
|
8
|
+
|
9
|
+
it { should respond_to(:list) }
|
10
|
+
|
11
|
+
describe '.from_object' do
|
12
|
+
context 'when object is a String' do
|
13
|
+
let(:object) { 'my list name' }
|
14
|
+
subject { described_class.from_object(object) }
|
15
|
+
it { should be_instance_of(described_class) }
|
16
|
+
its(:list) { should == object }
|
17
|
+
end
|
18
|
+
|
19
|
+
context 'when object is a List' do
|
20
|
+
let(:object) { described_class.new(:list => 'my list name') }
|
21
|
+
subject { described_class.from_object(object) }
|
22
|
+
it { should == object }
|
23
|
+
end
|
24
|
+
|
25
|
+
context 'when object is nil' do
|
26
|
+
let(:object) { nil }
|
27
|
+
subject { described_class.from_object(object) }
|
28
|
+
it { should be_nil }
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module Sendgrid
|
4
|
+
module API
|
5
|
+
module Entities
|
6
|
+
describe MarketingEmail do
|
7
|
+
subject { described_class.new }
|
8
|
+
|
9
|
+
it { should respond_to(:identity) }
|
10
|
+
it { should respond_to(:name) }
|
11
|
+
it { should respond_to(:subject) }
|
12
|
+
it { should respond_to(:text) }
|
13
|
+
it { should respond_to(:html) }
|
14
|
+
|
15
|
+
it { should respond_to(:can_edit) }
|
16
|
+
it { should respond_to(:content_preview) }
|
17
|
+
it { should respond_to(:date_schedule) }
|
18
|
+
it { should respond_to(:is_deleted) }
|
19
|
+
it { should respond_to(:is_split) }
|
20
|
+
it { should respond_to(:is_winner) }
|
21
|
+
it { should respond_to(:newsletter_id) }
|
22
|
+
it { should respond_to(:nl_type) }
|
23
|
+
it { should respond_to(:timezone_id) }
|
24
|
+
it { should respond_to(:total_recipients) }
|
25
|
+
it { should respond_to(:type) }
|
26
|
+
it { should respond_to(:winner_sending_time) }
|
27
|
+
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module Sendgrid
|
4
|
+
module API
|
5
|
+
module Entities
|
6
|
+
describe ResponseInsert do
|
7
|
+
subject { described_class.new }
|
8
|
+
|
9
|
+
it { should respond_to(:inserted) }
|
10
|
+
|
11
|
+
context 'with inserts' do
|
12
|
+
subject { described_class.new(:inserted => 3) }
|
13
|
+
|
14
|
+
its(:any?) { should be_true }
|
15
|
+
its(:none?) { should be_false }
|
16
|
+
end
|
17
|
+
|
18
|
+
context 'without inserts' do
|
19
|
+
subject { described_class.new(:inserted => 0) }
|
20
|
+
|
21
|
+
its(:any?) { should be_false }
|
22
|
+
its(:none?) { should be_true }
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module Sendgrid
|
4
|
+
module API
|
5
|
+
module Entities
|
6
|
+
describe ResponseRemove do
|
7
|
+
subject { described_class.new }
|
8
|
+
|
9
|
+
it { should respond_to(:removed) }
|
10
|
+
|
11
|
+
context 'with removals' do
|
12
|
+
subject { described_class.new(:removed => 3) }
|
13
|
+
|
14
|
+
its(:any?) { should be_true }
|
15
|
+
its(:none?) { should be_false }
|
16
|
+
end
|
17
|
+
|
18
|
+
context 'without removals' do
|
19
|
+
subject { described_class.new(:removed => 0) }
|
20
|
+
|
21
|
+
its(:any?) { should be_false }
|
22
|
+
its(:none?) { should be_true }
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module Sendgrid
|
4
|
+
module API
|
5
|
+
module Entities
|
6
|
+
describe SenderAddress do
|
7
|
+
subject { described_class.new }
|
8
|
+
|
9
|
+
it { should respond_to(:identity) }
|
10
|
+
it { should respond_to(:name) }
|
11
|
+
it { should respond_to(:email) }
|
12
|
+
it { should respond_to(:replyto) }
|
13
|
+
it { should respond_to(:address) }
|
14
|
+
it { should respond_to(:city) }
|
15
|
+
it { should respond_to(:state) }
|
16
|
+
it { should respond_to(:zip) }
|
17
|
+
it { should respond_to(:country) }
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,247 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module Sendgrid
|
4
|
+
module API
|
5
|
+
module Newsletter
|
6
|
+
module Categories
|
7
|
+
describe Services do
|
8
|
+
|
9
|
+
subject { service }
|
10
|
+
let(:service) { described_class.new(resource) }
|
11
|
+
let(:resource) { REST::Resource.new(user, key) }
|
12
|
+
let(:user) { 'my_user' }
|
13
|
+
let(:key) { 'my_key' }
|
14
|
+
let(:sg_mock) { Sendgrid::Mock.new(user, key) }
|
15
|
+
|
16
|
+
describe '#create' do
|
17
|
+
let(:url) { 'newsletter/category/create.json' }
|
18
|
+
let(:category_name) { 'my category' }
|
19
|
+
let(:stub_post) { sg_mock.stub_post(url, :category => category_name) }
|
20
|
+
subject { service.create(category_name) }
|
21
|
+
|
22
|
+
context 'when create a category successfully' do
|
23
|
+
context 'with name' do
|
24
|
+
it_behaves_like 'a success response'
|
25
|
+
end
|
26
|
+
|
27
|
+
context 'with objects' do
|
28
|
+
let(:category) { Entities::Category.new(:category => category_name) }
|
29
|
+
subject { service.create(category) }
|
30
|
+
it_behaves_like 'a success response'
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
context 'when the category already exists' do
|
35
|
+
it_behaves_like 'an already exists unauthorized response'
|
36
|
+
end
|
37
|
+
|
38
|
+
context 'when permission failed' do
|
39
|
+
it_behaves_like 'a forbidden response'
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
describe '#add' do
|
44
|
+
let(:url) { 'newsletter/category/add.json' }
|
45
|
+
let(:category_name) { 'my category' }
|
46
|
+
let(:marketing_email_name) { 'my marketing email' }
|
47
|
+
let(:stub_post) { sg_mock.stub_post(url, :name => marketing_email_name, :category => category_name) }
|
48
|
+
subject { service.add(marketing_email_name, category_name) }
|
49
|
+
|
50
|
+
context 'when add a category successfully' do
|
51
|
+
context 'with name' do
|
52
|
+
it_behaves_like 'a success response'
|
53
|
+
end
|
54
|
+
|
55
|
+
context 'with objects' do
|
56
|
+
let(:category) { Entities::Category.new(:category => category_name) }
|
57
|
+
let(:marketing_email) { Entities::MarketingEmail.new(:name => marketing_email_name) }
|
58
|
+
subject { service.add(marketing_email, category) }
|
59
|
+
it_behaves_like 'a success response'
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
context 'when the marketing email does not exist' do
|
64
|
+
it_behaves_like 'a does not exist response'
|
65
|
+
end
|
66
|
+
|
67
|
+
context 'when permission failed' do
|
68
|
+
it_behaves_like 'a forbidden response'
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
describe '#remove' do
|
73
|
+
let(:url) { 'newsletter/category/remove.json' }
|
74
|
+
let(:category_name) { 'my category' }
|
75
|
+
let(:marketing_email_name) { 'my marketing email' }
|
76
|
+
let(:stub_post) { sg_mock.stub_post(url, :name => marketing_email_name, :category => category_name) }
|
77
|
+
subject { service.remove(marketing_email_name, category_name) }
|
78
|
+
|
79
|
+
context 'when remove a category successfully' do
|
80
|
+
context 'with name' do
|
81
|
+
it_behaves_like 'a success response'
|
82
|
+
end
|
83
|
+
|
84
|
+
context 'with objects' do
|
85
|
+
let(:category) { Entities::Category.new(:category => category_name) }
|
86
|
+
let(:marketing_email) { Entities::MarketingEmail.new(:name => marketing_email_name) }
|
87
|
+
subject { service.remove(marketing_email, category) }
|
88
|
+
it_behaves_like 'a success response'
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
context 'when the category does not exist' do
|
93
|
+
it_behaves_like 'a does not exist response'
|
94
|
+
end
|
95
|
+
|
96
|
+
context 'when the marketing email does not exist' do
|
97
|
+
it_behaves_like 'a does not exist response'
|
98
|
+
end
|
99
|
+
|
100
|
+
context 'when permission failed' do
|
101
|
+
it_behaves_like 'a forbidden response'
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
describe '#list' do
|
106
|
+
let(:url) { 'newsletter/category/list.json' }
|
107
|
+
let(:stub_post) { sg_mock.stub_post(url) }
|
108
|
+
subject { service.list }
|
109
|
+
|
110
|
+
context 'when list all categories successfully' do
|
111
|
+
before do
|
112
|
+
stub_post.to_return(:body => fixture('categories.json'))
|
113
|
+
end
|
114
|
+
|
115
|
+
it { should have(3).items }
|
116
|
+
|
117
|
+
describe 'first item' do
|
118
|
+
subject { service.list.first }
|
119
|
+
its(:category) { should == 'sendgrid' }
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
123
|
+
context 'when permission failed' do
|
124
|
+
it_behaves_like 'a forbidden response'
|
125
|
+
end
|
126
|
+
end
|
127
|
+
|
128
|
+
describe 'online tests', :online => true do
|
129
|
+
include_examples 'online tests'
|
130
|
+
let(:online) { Online.new(env_user, env_key) }
|
131
|
+
let(:category) { online.category_example }
|
132
|
+
let(:marketing_email) { online.marketing_email_example }
|
133
|
+
|
134
|
+
context 'when credentials are valid' do
|
135
|
+
before do
|
136
|
+
# try to create the test category
|
137
|
+
# note: categories can't be removed for now
|
138
|
+
begin
|
139
|
+
subject.create(category)
|
140
|
+
rescue REST::Errors::Unauthorized
|
141
|
+
# it already exists, do nothing
|
142
|
+
end
|
143
|
+
end
|
144
|
+
let(:resource) { REST::Resource.new(env_user, env_key) }
|
145
|
+
|
146
|
+
describe '#create' do
|
147
|
+
context 'when the category already exists' do
|
148
|
+
it 'raises an error' do
|
149
|
+
expect { subject.create(category) }.to raise_error(REST::Errors::Unauthorized)
|
150
|
+
end
|
151
|
+
end
|
152
|
+
end
|
153
|
+
|
154
|
+
describe '#add' do
|
155
|
+
context 'when add a category successfully' do
|
156
|
+
before do
|
157
|
+
online.add_marketing_email
|
158
|
+
end
|
159
|
+
after do
|
160
|
+
online.delete_marketing_email
|
161
|
+
end
|
162
|
+
it 'adds the category' do
|
163
|
+
subject.add(marketing_email, category).success?.should be_true
|
164
|
+
end
|
165
|
+
end
|
166
|
+
|
167
|
+
context 'when the marketing email does not exist' do
|
168
|
+
it 'raises an error' do
|
169
|
+
expect { subject.add(marketing_email, category) }.to raise_error(REST::Errors::Unauthorized)
|
170
|
+
end
|
171
|
+
end
|
172
|
+
end
|
173
|
+
|
174
|
+
describe '#remove' do
|
175
|
+
context 'when remove a category successfully' do
|
176
|
+
before do
|
177
|
+
online.add_marketing_email
|
178
|
+
subject.add(marketing_email, category)
|
179
|
+
end
|
180
|
+
after do
|
181
|
+
online.delete_marketing_email
|
182
|
+
end
|
183
|
+
it 'removes the category' do
|
184
|
+
subject.remove(marketing_email, category).success?.should be_true
|
185
|
+
end
|
186
|
+
end
|
187
|
+
|
188
|
+
context 'when the marketing email does not exist' do
|
189
|
+
it 'raises an error' do
|
190
|
+
expect { subject.remove(marketing_email, category) }.to raise_error(REST::Errors::Unauthorized)
|
191
|
+
end
|
192
|
+
end
|
193
|
+
|
194
|
+
context 'when the category does not exist' do
|
195
|
+
before do
|
196
|
+
online.add_marketing_email
|
197
|
+
end
|
198
|
+
after do
|
199
|
+
online.delete_marketing_email
|
200
|
+
end
|
201
|
+
it 'raises an error' do
|
202
|
+
expect { subject.remove(marketing_email, category) }.to raise_error(REST::Errors::Unauthorized)
|
203
|
+
end
|
204
|
+
end
|
205
|
+
end
|
206
|
+
|
207
|
+
describe '#list' do
|
208
|
+
context 'when list all categories successfully' do
|
209
|
+
it 'get all categories' do
|
210
|
+
subject.list.should_not be_empty
|
211
|
+
end
|
212
|
+
end
|
213
|
+
end
|
214
|
+
end
|
215
|
+
|
216
|
+
context 'when credentials are invalid' do
|
217
|
+
describe '#create' do
|
218
|
+
it 'raises an error' do
|
219
|
+
expect { subject.create(category) }.to raise_error(REST::Errors::Forbidden)
|
220
|
+
end
|
221
|
+
end
|
222
|
+
|
223
|
+
describe '#add' do
|
224
|
+
it 'raises an error' do
|
225
|
+
expect { subject.add(marketing_email, category) }.to raise_error(REST::Errors::Forbidden)
|
226
|
+
end
|
227
|
+
end
|
228
|
+
|
229
|
+
describe '#remove' do
|
230
|
+
it 'raises an error' do
|
231
|
+
expect { subject.remove(marketing_email, category) }.to raise_error(REST::Errors::Forbidden)
|
232
|
+
end
|
233
|
+
end
|
234
|
+
|
235
|
+
describe '#list' do
|
236
|
+
it 'raises an error' do
|
237
|
+
expect { subject.list }.to raise_error(REST::Errors::Forbidden)
|
238
|
+
end
|
239
|
+
end
|
240
|
+
end
|
241
|
+
end
|
242
|
+
|
243
|
+
end
|
244
|
+
end
|
245
|
+
end
|
246
|
+
end
|
247
|
+
end
|
@@ -0,0 +1,265 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module Sendgrid
|
4
|
+
module API
|
5
|
+
module Newsletter
|
6
|
+
module Emails
|
7
|
+
describe Services do
|
8
|
+
|
9
|
+
subject { service }
|
10
|
+
let(:service) { described_class.new(resource) }
|
11
|
+
let(:resource) { REST::Resource.new(user, key) }
|
12
|
+
let(:user) { 'my_user' }
|
13
|
+
let(:key) { 'my_key' }
|
14
|
+
let(:sg_mock) { Sendgrid::Mock.new(user, key) }
|
15
|
+
|
16
|
+
let(:listname) { 'my list' }
|
17
|
+
let(:email) { Entities::Email.new(:email => 'john@example.com', :name => 'John') }
|
18
|
+
let(:email2) { Entities::Email.new(:email => 'brian@example.com', :name => 'Brian') }
|
19
|
+
let(:emails) { [email, email2] }
|
20
|
+
|
21
|
+
describe '#add' do
|
22
|
+
let(:url) { 'newsletter/lists/email/add.json' }
|
23
|
+
|
24
|
+
context 'when add an email successfully' do
|
25
|
+
before do
|
26
|
+
sg_mock.stub_post(url, :list => listname, :data => [email.to_json]).
|
27
|
+
to_return(:body => {:inserted => 1}.to_json)
|
28
|
+
end
|
29
|
+
subject { service.add(listname, email) }
|
30
|
+
its(:inserted) { should == 1 }
|
31
|
+
its(:any?) { should be_true }
|
32
|
+
its(:none?) { should be_false }
|
33
|
+
end
|
34
|
+
|
35
|
+
context 'when add multiple emails successfully' do
|
36
|
+
before do
|
37
|
+
sg_mock.stub_post(url, :list => listname, :data => emails.map(&:to_json)).
|
38
|
+
to_return(:body => {:inserted => 2}.to_json)
|
39
|
+
end
|
40
|
+
subject { service.add(listname, emails) }
|
41
|
+
its(:inserted) { should == 2 }
|
42
|
+
its(:any?) { should be_true }
|
43
|
+
its(:none?) { should be_false }
|
44
|
+
end
|
45
|
+
|
46
|
+
context 'when add an email that already exists' do
|
47
|
+
before do
|
48
|
+
sg_mock.stub_post(url, :list => listname, :data => [email.to_json]).
|
49
|
+
to_return(:body => {:inserted => 0}.to_json)
|
50
|
+
end
|
51
|
+
subject { service.add(listname, email) }
|
52
|
+
its(:inserted) { should == 0 }
|
53
|
+
its(:any?) { should be_false }
|
54
|
+
its(:none?) { should be_true }
|
55
|
+
end
|
56
|
+
|
57
|
+
context 'when permission failed' do
|
58
|
+
let(:stub_post) { sg_mock.stub_post(url, :list => listname, :data => [email.to_json]) }
|
59
|
+
subject { service.add(listname, email) }
|
60
|
+
it_behaves_like 'a forbidden response'
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
describe '#get' do
|
65
|
+
let(:url) { 'newsletter/lists/email/get.json' }
|
66
|
+
|
67
|
+
context 'when get all emails successfully' do
|
68
|
+
before do
|
69
|
+
sg_mock.stub_post(url, :list => listname).
|
70
|
+
to_return(:body => fixture('emails/emails.json'))
|
71
|
+
end
|
72
|
+
let(:response) { service.get(listname) }
|
73
|
+
subject { response }
|
74
|
+
it { should have(2).items }
|
75
|
+
describe 'first result' do
|
76
|
+
subject { response.first }
|
77
|
+
its(:email) { should == 'john@example.com' }
|
78
|
+
its(:name) { should == 'John' }
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
context 'when get an email successfully' do
|
83
|
+
before do
|
84
|
+
sg_mock.stub_post(url, :list => listname, :email => [email.email]).
|
85
|
+
to_return(:body => fixture('emails/email.json'))
|
86
|
+
end
|
87
|
+
let(:response) { service.get(listname, email) }
|
88
|
+
subject { response }
|
89
|
+
it { should have(1).item }
|
90
|
+
describe 'first result' do
|
91
|
+
subject { response.first }
|
92
|
+
its(:email) { should == 'john@example.com' }
|
93
|
+
its(:name) { should == 'John' }
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
context 'when permission failed' do
|
98
|
+
let(:stub_post) { sg_mock.stub_post(url, :list => listname) }
|
99
|
+
subject { service.get(listname) }
|
100
|
+
it_behaves_like 'a forbidden response'
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
describe '#delete' do
|
105
|
+
let(:url) { 'newsletter/lists/email/delete.json' }
|
106
|
+
|
107
|
+
context 'when delete an email successfully' do
|
108
|
+
before do
|
109
|
+
sg_mock.stub_post(url, :list => listname, :email => [email.email]).
|
110
|
+
to_return(:body => {:removed => 1}.to_json)
|
111
|
+
end
|
112
|
+
let(:response) { service.delete(listname, email) }
|
113
|
+
subject { response }
|
114
|
+
its(:removed) { should == 1 }
|
115
|
+
its(:any?) { should be_true }
|
116
|
+
its(:none?) { should be_false }
|
117
|
+
end
|
118
|
+
|
119
|
+
context 'when delete multiple emails successfully' do
|
120
|
+
before do
|
121
|
+
sg_mock.stub_post(url, :list => listname, :email => emails.map(&:email)).
|
122
|
+
to_return(:body => {:removed => 2}.to_json)
|
123
|
+
end
|
124
|
+
let(:response) { service.delete(listname, emails) }
|
125
|
+
subject { response }
|
126
|
+
its(:removed) { should == 2 }
|
127
|
+
its(:any?) { should be_true }
|
128
|
+
its(:none?) { should be_false }
|
129
|
+
end
|
130
|
+
|
131
|
+
context 'when no emails were deleted' do
|
132
|
+
before do
|
133
|
+
sg_mock.stub_post(url, :list => listname, :email => [email.email]).
|
134
|
+
to_return(:body => {:removed => 0}.to_json)
|
135
|
+
end
|
136
|
+
let(:response) { service.delete(listname, email) }
|
137
|
+
subject { response }
|
138
|
+
its(:removed) { should == 0 }
|
139
|
+
its(:any?) { should be_false }
|
140
|
+
its(:none?) { should be_true }
|
141
|
+
end
|
142
|
+
|
143
|
+
context 'when permission failed' do
|
144
|
+
let(:stub_post) { sg_mock.stub_post(url, :list => listname, :email => [email.email]) }
|
145
|
+
subject { service.delete(listname, email) }
|
146
|
+
it_behaves_like 'a forbidden response'
|
147
|
+
end
|
148
|
+
end
|
149
|
+
|
150
|
+
describe 'online tests', :online => true do
|
151
|
+
include_examples 'online tests'
|
152
|
+
let(:online) { Online.new(env_user, env_key) }
|
153
|
+
let(:list) { online.list_example }
|
154
|
+
let(:emails) { online.emails_example }
|
155
|
+
let(:email) { emails.first }
|
156
|
+
|
157
|
+
context 'when credentials are valid' do
|
158
|
+
let(:lists) { Newsletter::Lists::Services.new(resource) }
|
159
|
+
let(:resource) { REST::Resource.new(env_user, env_key) }
|
160
|
+
|
161
|
+
before do
|
162
|
+
lists.add(list).success? or raise 'could not create the list'
|
163
|
+
end
|
164
|
+
after do
|
165
|
+
lists.delete(list).success? or raise 'could not remove the created list'
|
166
|
+
end
|
167
|
+
|
168
|
+
describe '#add' do
|
169
|
+
context 'when add an email successfully' do
|
170
|
+
it 'adds the email' do
|
171
|
+
response = subject.add(list, email)
|
172
|
+
response.any?.should be_true
|
173
|
+
response.inserted.should == 1
|
174
|
+
end
|
175
|
+
end
|
176
|
+
|
177
|
+
context 'when add multiple emails successfully' do
|
178
|
+
it 'adds the emails' do
|
179
|
+
response = subject.add(list, emails)
|
180
|
+
response.any?.should be_true
|
181
|
+
response.inserted.should == 2
|
182
|
+
end
|
183
|
+
end
|
184
|
+
|
185
|
+
context 'when add an email that already exists' do
|
186
|
+
before do
|
187
|
+
subject.add(list, email)
|
188
|
+
end
|
189
|
+
it 'do not add the email' do
|
190
|
+
response = subject.add(list, email)
|
191
|
+
response.any?.should be_false
|
192
|
+
response.inserted.should == 0
|
193
|
+
end
|
194
|
+
end
|
195
|
+
end
|
196
|
+
|
197
|
+
describe '#get' do
|
198
|
+
before do
|
199
|
+
service.add(list, emails)
|
200
|
+
end
|
201
|
+
|
202
|
+
context 'when get all emails successfully' do
|
203
|
+
it 'gets the emails' do
|
204
|
+
response = subject.get(list)
|
205
|
+
response.should have(2).items
|
206
|
+
end
|
207
|
+
end
|
208
|
+
|
209
|
+
context 'when get an email successfully' do
|
210
|
+
it 'gets the email' do
|
211
|
+
response = subject.get(list, email)
|
212
|
+
response.should have(1).item
|
213
|
+
end
|
214
|
+
end
|
215
|
+
end
|
216
|
+
|
217
|
+
describe '#delete' do
|
218
|
+
before do
|
219
|
+
service.add(list, emails)
|
220
|
+
end
|
221
|
+
|
222
|
+
context 'when delete an email successfully' do
|
223
|
+
it 'deletes the email' do
|
224
|
+
response = service.delete(list, email)
|
225
|
+
response.any?.should be_true
|
226
|
+
response.removed.should == 1
|
227
|
+
end
|
228
|
+
end
|
229
|
+
|
230
|
+
context 'when delete multiple emails successfully' do
|
231
|
+
it 'deletes the emails' do
|
232
|
+
response = service.delete(list, emails)
|
233
|
+
response.any?.should be_true
|
234
|
+
response.removed.should == 2
|
235
|
+
end
|
236
|
+
end
|
237
|
+
end
|
238
|
+
end
|
239
|
+
|
240
|
+
context 'when credentials are invalid' do
|
241
|
+
describe '#add' do
|
242
|
+
it 'raises an error' do
|
243
|
+
expect { subject.add(list, email) }.to raise_error(REST::Errors::Forbidden)
|
244
|
+
end
|
245
|
+
end
|
246
|
+
|
247
|
+
describe '#get' do
|
248
|
+
it 'raises an error' do
|
249
|
+
expect { subject.get(list, email) }.to raise_error(REST::Errors::Forbidden)
|
250
|
+
end
|
251
|
+
end
|
252
|
+
|
253
|
+
describe '#delete' do
|
254
|
+
it 'raises an error' do
|
255
|
+
expect { subject.delete(list, email) }.to raise_error(REST::Errors::Forbidden)
|
256
|
+
end
|
257
|
+
end
|
258
|
+
end
|
259
|
+
end
|
260
|
+
|
261
|
+
end
|
262
|
+
end
|
263
|
+
end
|
264
|
+
end
|
265
|
+
end
|