createsend 2.5.1 → 3.0.0

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.
@@ -1,3 +1,3 @@
1
1
  module CreateSend
2
- VERSION = "2.5.1" unless defined?(CreateSend::VERSION)
2
+ VERSION = "3.0.0" unless defined?(CreateSend::VERSION)
3
3
  end
@@ -1,38 +1,36 @@
1
1
  require File.dirname(__FILE__) + '/helper'
2
2
 
3
3
  class AdministratorTest < Test::Unit::TestCase
4
- context "when an api caller is authenticated" do
4
+ multiple_contexts "authenticated_using_oauth_context", "authenticated_using_api_key_context" do
5
5
  setup do
6
- @api_key = '123123123123123123123'
7
- CreateSend.api_key @api_key
8
- @admin = CreateSend::Administrator.new "admin@example.com"
6
+ @admin = CreateSend::Administrator.new @auth, "admin@example.com"
9
7
  end
10
-
8
+
11
9
  should "get a administrator by email address" do
12
10
  email = "admin@example.com"
13
- stub_get(@api_key, "admins.json?email=#{CGI.escape(email)}", "admin_details.json")
14
- admin = CreateSend::Administrator.get email
11
+ stub_get(@auth, "admins.json?email=#{CGI.escape(email)}", "admin_details.json")
12
+ admin = CreateSend::Administrator.get @auth, email
15
13
  admin.EmailAddress.should == email
16
14
  admin.Name.should == "Admin One"
17
15
  admin.Status.should == "Active"
18
16
  end
19
17
 
20
18
  should "add an administrator" do
21
- stub_post(@api_key, "admins.json", "add_admin.json")
22
- result = CreateSend::Administrator.add "admin@example.com", "Admin"
19
+ stub_post(@auth, "admins.json", "add_admin.json")
20
+ result = CreateSend::Administrator.add @auth, "admin@example.com", "Admin"
23
21
  result.EmailAddress.should == "admin@example.com"
24
22
  end
25
23
 
26
24
  should "update an administrator" do
27
25
  email = "admin@example.com"
28
26
  new_email = "new_email_address@example.com"
29
- stub_put(@api_key, "admins.json?email=#{CGI.escape(email)}", nil)
27
+ stub_put(@auth, "admins.json?email=#{CGI.escape(email)}", nil)
30
28
  @admin.update new_email, "Admin Name"
31
29
  @admin.email_address.should == new_email
32
30
  end
33
-
31
+
34
32
  should "delete an admin" do
35
- stub_delete(@api_key, "admins.json?email=#{CGI.escape(@admin.email_address)}", nil)
33
+ stub_delete(@auth, "admins.json?email=#{CGI.escape(@admin.email_address)}", nil)
36
34
  @admin.delete
37
35
  end
38
36
  end
@@ -1,17 +1,15 @@
1
1
  require File.dirname(__FILE__) + '/helper'
2
2
 
3
3
  class CampaignTest < Test::Unit::TestCase
4
- context "when an api caller is authenticated" do
4
+ multiple_contexts "authenticated_using_oauth_context", "authenticated_using_api_key_context" do
5
5
  setup do
6
- @api_key = '123123123123123123123'
7
- CreateSend.api_key @api_key
8
- @campaign = CreateSend::Campaign.new('787y87y87y87y87y87y87')
6
+ @campaign = CreateSend::Campaign.new @auth, '787y87y87y87y87y87y87'
9
7
  end
10
8
 
11
9
  should "create a campaign" do
12
10
  client_id = '87y8d7qyw8d7yq8w7ydwqwd'
13
- stub_post(@api_key, "campaigns/#{client_id}.json", "create_campaign.json")
14
- campaign_id = CreateSend::Campaign.create client_id, "subject", "name", "g'day", "good.day@example.com", "good.day@example.com",
11
+ stub_post(@auth, "campaigns/#{client_id}.json", "create_campaign.json")
12
+ campaign_id = CreateSend::Campaign.create @auth, client_id, "subject", "name", "g'day", "good.day@example.com", "good.day@example.com",
15
13
  "http://example.com/campaign.html", "http://example.com/campaign.txt", [ '7y12989e82ue98u2e', 'dh9w89q8w98wudwd989' ],
16
14
  [ 'y78q9w8d9w8ud9q8uw', 'djw98quw9duqw98uwd98' ]
17
15
  request = FakeWeb.last_request.body
@@ -21,8 +19,8 @@ class CampaignTest < Test::Unit::TestCase
21
19
 
22
20
  should "create a campaign with a nil text_url param" do
23
21
  client_id = '87y8d7qyw8d7yq8w7ydwqwd'
24
- stub_post(@api_key, "campaigns/#{client_id}.json", "create_campaign.json")
25
- campaign_id = CreateSend::Campaign.create client_id, "subject", "name", "g'day", "good.day@example.com", "good.day@example.com",
22
+ stub_post(@auth, "campaigns/#{client_id}.json", "create_campaign.json")
23
+ campaign_id = CreateSend::Campaign.create @auth, client_id, "subject", "name", "g'day", "good.day@example.com", "good.day@example.com",
26
24
  "http://example.com/campaign.html", nil, [ '7y12989e82ue98u2e', 'dh9w89q8w98wudwd989' ],
27
25
  [ 'y78q9w8d9w8ud9q8uw', 'djw98quw9duqw98uwd98' ]
28
26
  request = FakeWeb.last_request.body
@@ -104,40 +102,40 @@ class CampaignTest < Test::Unit::TestCase
104
102
  # </html>
105
103
 
106
104
  client_id = '87y8d7qyw8d7yq8w7ydwqwd'
107
- stub_post(@api_key, "campaigns/#{client_id}/fromtemplate.json", "create_campaign.json")
108
- campaign_id = CreateSend::Campaign.create_from_template client_id, "subject", "name", "g'day", "good.day@example.com", "good.day@example.com",
105
+ stub_post(@auth, "campaigns/#{client_id}/fromtemplate.json", "create_campaign.json")
106
+ campaign_id = CreateSend::Campaign.create_from_template @auth, client_id, "subject", "name", "g'day", "good.day@example.com", "good.day@example.com",
109
107
  [ '7y12989e82ue98u2e', 'dh9w89q8w98wudwd989' ], [ 'y78q9w8d9w8ud9q8uw', 'djw98quw9duqw98uwd98' ],
110
108
  "7j8uw98udowy12989e8298u2e", template_content
111
109
  campaign_id.should == "787y87y87y87y87y87y87"
112
110
  end
113
111
 
114
112
  should "send a preview of a draft campaign to a single recipient" do
115
- stub_post(@api_key, "campaigns/#{@campaign.campaign_id}/sendpreview.json", nil)
113
+ stub_post(@auth, "campaigns/#{@campaign.campaign_id}/sendpreview.json", nil)
116
114
  @campaign.send_preview "test+89898u9@example.com", "random"
117
115
  end
118
116
 
119
117
  should "send a preview of a draft campaign to multiple recipients" do
120
- stub_post(@api_key, "campaigns/#{@campaign.campaign_id}/sendpreview.json", nil)
118
+ stub_post(@auth, "campaigns/#{@campaign.campaign_id}/sendpreview.json", nil)
121
119
  @campaign.send_preview [ "test+89898u9@example.com", "test+787y8y7y8@example.com" ], "random"
122
120
  end
123
121
 
124
122
  should "send a campaign" do
125
- stub_post(@api_key, "campaigns/#{@campaign.campaign_id}/send.json", nil)
123
+ stub_post(@auth, "campaigns/#{@campaign.campaign_id}/send.json", nil)
126
124
  @campaign.send "confirmation@example.com"
127
125
  end
128
126
 
129
127
  should "unschedule a campaign" do
130
- stub_post(@api_key, "campaigns/#{@campaign.campaign_id}/unschedule.json", nil)
128
+ stub_post(@auth, "campaigns/#{@campaign.campaign_id}/unschedule.json", nil)
131
129
  @campaign.unschedule
132
130
  end
133
131
 
134
132
  should "delete a campaign" do
135
- stub_delete(@api_key, "campaigns/#{@campaign.campaign_id}.json", nil)
133
+ stub_delete(@auth, "campaigns/#{@campaign.campaign_id}.json", nil)
136
134
  @campaign.delete
137
135
  end
138
-
136
+
139
137
  should "get the summary for a campaign" do
140
- stub_get(@api_key, "campaigns/#{@campaign.campaign_id}/summary.json", "campaign_summary.json")
138
+ stub_get(@auth, "campaigns/#{@campaign.campaign_id}/summary.json", "campaign_summary.json")
141
139
  summary = @campaign.summary
142
140
  summary.Recipients.should == 5
143
141
  summary.TotalOpened.should == 10
@@ -155,7 +153,7 @@ class CampaignTest < Test::Unit::TestCase
155
153
  end
156
154
 
157
155
  should "get the email client usage for a campaign" do
158
- stub_get(@api_key, "campaigns/#{@campaign.campaign_id}/emailclientusage.json", "email_client_usage.json")
156
+ stub_get(@auth, "campaigns/#{@campaign.campaign_id}/emailclientusage.json", "email_client_usage.json")
159
157
  ecu = @campaign.email_client_usage
160
158
  ecu.size.should == 6
161
159
  ecu.first.Client.should == "iOS Devices"
@@ -165,7 +163,7 @@ class CampaignTest < Test::Unit::TestCase
165
163
  end
166
164
 
167
165
  should "get the lists and segments for a campaign" do
168
- stub_get(@api_key, "campaigns/#{@campaign.campaign_id}/listsandsegments.json", "campaign_listsandsegments.json")
166
+ stub_get(@auth, "campaigns/#{@campaign.campaign_id}/listsandsegments.json", "campaign_listsandsegments.json")
169
167
  ls = @campaign.lists_and_segments
170
168
  ls.Lists.size.should == 1
171
169
  ls.Segments.size.should == 1
@@ -177,7 +175,7 @@ class CampaignTest < Test::Unit::TestCase
177
175
  end
178
176
 
179
177
  should "get the recipients for a campaign" do
180
- stub_get(@api_key, "campaigns/#{@campaign.campaign_id}/recipients.json?pagesize=20&orderfield=email&page=1&orderdirection=asc", "campaign_recipients.json")
178
+ stub_get(@auth, "campaigns/#{@campaign.campaign_id}/recipients.json?pagesize=20&orderfield=email&page=1&orderdirection=asc", "campaign_recipients.json")
181
179
  res = @campaign.recipients page=1, page_size=20
182
180
  res.ResultsOrderedBy.should == "email"
183
181
  res.OrderDirection.should == "asc"
@@ -193,7 +191,7 @@ class CampaignTest < Test::Unit::TestCase
193
191
 
194
192
  should "get the opens for a campaign" do
195
193
  min_date = "2010-01-01"
196
- stub_get(@api_key, "campaigns/#{@campaign.campaign_id}/opens.json?page=1&pagesize=1000&orderfield=date&orderdirection=asc&date=#{CGI.escape(min_date)}", "campaign_opens.json")
194
+ stub_get(@auth, "campaigns/#{@campaign.campaign_id}/opens.json?page=1&pagesize=1000&orderfield=date&orderdirection=asc&date=#{CGI.escape(min_date)}", "campaign_opens.json")
197
195
  opens = @campaign.opens min_date
198
196
  opens.Results.size.should == 5
199
197
  opens.Results.first.EmailAddress.should == "subs+6576576576@example.com"
@@ -217,7 +215,7 @@ class CampaignTest < Test::Unit::TestCase
217
215
 
218
216
  should "get the subscriber clicks for a campaign" do
219
217
  min_date = "2010-01-01"
220
- stub_get(@api_key, "campaigns/#{@campaign.campaign_id}/clicks.json?page=1&pagesize=1000&orderfield=date&orderdirection=asc&date=#{CGI.escape(min_date)}", "campaign_clicks.json")
218
+ stub_get(@auth, "campaigns/#{@campaign.campaign_id}/clicks.json?page=1&pagesize=1000&orderfield=date&orderdirection=asc&date=#{CGI.escape(min_date)}", "campaign_clicks.json")
221
219
  clicks = @campaign.clicks min_date
222
220
  clicks.Results.size.should == 3
223
221
  clicks.Results.first.EmailAddress.should == "subs+6576576576@example.com"
@@ -242,7 +240,7 @@ class CampaignTest < Test::Unit::TestCase
242
240
 
243
241
  should "get the unsubscribes for a campaign" do
244
242
  min_date = "2010-01-01"
245
- stub_get(@api_key, "campaigns/#{@campaign.campaign_id}/unsubscribes.json?page=1&pagesize=1000&orderfield=date&orderdirection=asc&date=#{CGI.escape(min_date)}", "campaign_unsubscribes.json")
243
+ stub_get(@auth, "campaigns/#{@campaign.campaign_id}/unsubscribes.json?page=1&pagesize=1000&orderfield=date&orderdirection=asc&date=#{CGI.escape(min_date)}", "campaign_unsubscribes.json")
246
244
  unsubscribes = @campaign.unsubscribes min_date
247
245
  unsubscribes.Results.size.should == 1
248
246
  unsubscribes.Results.first.EmailAddress.should == "subs+6576576576@example.com"
@@ -260,7 +258,7 @@ class CampaignTest < Test::Unit::TestCase
260
258
 
261
259
  should "get the spam complaints for a campaign" do
262
260
  min_date = "2010-01-01"
263
- stub_get(@api_key, "campaigns/#{@campaign.campaign_id}/spam.json?page=1&pagesize=1000&orderfield=date&orderdirection=asc&date=#{CGI.escape(min_date)}", "campaign_spam.json")
261
+ stub_get(@auth, "campaigns/#{@campaign.campaign_id}/spam.json?page=1&pagesize=1000&orderfield=date&orderdirection=asc&date=#{CGI.escape(min_date)}", "campaign_spam.json")
264
262
  spam = @campaign.spam min_date
265
263
  spam.Results.size.should == 1
266
264
  spam.Results.first.EmailAddress.should == "subs+6576576576@example.com"
@@ -277,7 +275,7 @@ class CampaignTest < Test::Unit::TestCase
277
275
 
278
276
  should "get the bounces for a campaign" do
279
277
  min_date = "2010-01-01"
280
- stub_get(@api_key, "campaigns/#{@campaign.campaign_id}/bounces.json?page=1&pagesize=1000&orderfield=date&orderdirection=asc&date=#{CGI.escape(min_date)}", "campaign_bounces.json")
278
+ stub_get(@auth, "campaigns/#{@campaign.campaign_id}/bounces.json?page=1&pagesize=1000&orderfield=date&orderdirection=asc&date=#{CGI.escape(min_date)}", "campaign_bounces.json")
281
279
  bounces = @campaign.bounces min_date
282
280
  bounces.Results.size.should == 2
283
281
  bounces.Results.first.EmailAddress.should == "asdf@softbouncemyemail.com"
data/test/client_test.rb CHANGED
@@ -1,22 +1,20 @@
1
1
  require File.dirname(__FILE__) + '/helper'
2
2
 
3
3
  class ClientTest < Test::Unit::TestCase
4
- context "when an api caller is authenticated" do
4
+ multiple_contexts "authenticated_using_oauth_context", "authenticated_using_api_key_context" do
5
5
  setup do
6
- @api_key = '123123123123123123123'
7
- CreateSend.api_key @api_key
8
- @client = CreateSend::Client.new('321iuhiuhi1u23hi2u3')
6
+ @client = CreateSend::Client.new(@auth, '321iuhiuhi1u23hi2u3')
9
7
  @client.client_id.should == '321iuhiuhi1u23hi2u3'
10
8
  end
11
9
 
12
10
  should "create a client" do
13
- stub_post(@api_key, "clients.json", "create_client.json")
14
- client_id = CreateSend::Client.create "Client Company Name", "(GMT+10:00) Canberra, Melbourne, Sydney", "Australia"
11
+ stub_post(@auth, "clients.json", "create_client.json")
12
+ client_id = CreateSend::Client.create @auth, "Client Company Name", "(GMT+10:00) Canberra, Melbourne, Sydney", "Australia"
15
13
  client_id.should == "32a381c49a2df99f1d0c6f3c112352b9"
16
14
  end
17
15
 
18
16
  should "get details of a client" do
19
- stub_get(@api_key, "clients/#{@client.client_id}.json", "client_details.json")
17
+ stub_get(@auth, "clients/#{@client.client_id}.json", "client_details.json")
20
18
  cl = @client.details
21
19
  cl.ApiKey.should == "639d8cc27198202f5fe6037a8b17a29a59984b86d3289bc9"
22
20
  cl.BasicDetails.ClientID.should == "4a397ccaaa55eb4e6aa1221e1e2d7122"
@@ -28,7 +26,7 @@ class ClientTest < Test::Unit::TestCase
28
26
  end
29
27
 
30
28
  should "get all campaigns" do
31
- stub_get(@api_key, "clients/#{@client.client_id}/campaigns.json", "campaigns.json")
29
+ stub_get(@auth, "clients/#{@client.client_id}/campaigns.json", "campaigns.json")
32
30
  campaigns = @client.campaigns
33
31
  campaigns.size.should == 2
34
32
  campaigns.first.CampaignID.should == 'fc0ce7105baeaf97f47c99be31d02a91'
@@ -44,7 +42,7 @@ class ClientTest < Test::Unit::TestCase
44
42
  end
45
43
 
46
44
  should "get scheduled campaigns" do
47
- stub_get(@api_key, "clients/#{@client.client_id}/scheduled.json", "scheduled_campaigns.json")
45
+ stub_get(@auth, "clients/#{@client.client_id}/scheduled.json", "scheduled_campaigns.json")
48
46
  campaigns = @client.scheduled
49
47
  campaigns.size.should == 2
50
48
  campaigns.first.DateScheduled.should == "2011-05-25 10:40:00"
@@ -61,7 +59,7 @@ class ClientTest < Test::Unit::TestCase
61
59
  end
62
60
 
63
61
  should "get all drafts" do
64
- stub_get(@api_key, "clients/#{@client.client_id}/drafts.json", "drafts.json")
62
+ stub_get(@auth, "clients/#{@client.client_id}/drafts.json", "drafts.json")
65
63
  drafts = @client.drafts
66
64
  drafts.size.should == 2
67
65
  drafts.first.CampaignID.should == '7c7424792065d92627139208c8c01db1'
@@ -76,7 +74,7 @@ class ClientTest < Test::Unit::TestCase
76
74
  end
77
75
 
78
76
  should "get all lists" do
79
- stub_get(@api_key, "clients/#{@client.client_id}/lists.json", "lists.json")
77
+ stub_get(@auth, "clients/#{@client.client_id}/lists.json", "lists.json")
80
78
  lists = @client.lists
81
79
  lists.size.should == 2
82
80
  lists.first.ListID.should == 'a58ee1d3039b8bec838e6d1482a8a965'
@@ -85,7 +83,7 @@ class ClientTest < Test::Unit::TestCase
85
83
 
86
84
  should "get all lists to which a subscriber with a particular email address belongs" do
87
85
  email = "valid@example.com"
88
- stub_get(@api_key, "clients/#{@client.client_id}/listsforemail.json?email=#{CGI.escape(email)}", "listsforemail.json")
86
+ stub_get(@auth, "clients/#{@client.client_id}/listsforemail.json?email=#{CGI.escape(email)}", "listsforemail.json")
89
87
  lists = @client.lists_for_email(email)
90
88
  lists.size.should == 2
91
89
  lists.first.ListID.should == 'ab4a2b57c7c8f1ba62f898a1af1a575b'
@@ -95,7 +93,7 @@ class ClientTest < Test::Unit::TestCase
95
93
  end
96
94
 
97
95
  should "get all segments for a client" do
98
- stub_get(@api_key, "clients/#{@client.client_id}/segments.json", "segments.json")
96
+ stub_get(@auth, "clients/#{@client.client_id}/segments.json", "segments.json")
99
97
  segments = @client.segments
100
98
  segments.size.should == 2
101
99
  segments.first.ListID.should == 'a58ee1d3039b8bec838e6d1482a8a965'
@@ -104,7 +102,7 @@ class ClientTest < Test::Unit::TestCase
104
102
  end
105
103
 
106
104
  should "get suppression list" do
107
- stub_get(@api_key, "clients/#{@client.client_id}/suppressionlist.json?pagesize=1000&orderfield=email&page=1&orderdirection=asc", "suppressionlist.json")
105
+ stub_get(@auth, "clients/#{@client.client_id}/suppressionlist.json?pagesize=1000&orderfield=email&page=1&orderdirection=asc", "suppressionlist.json")
108
106
  res = @client.suppressionlist
109
107
  res.ResultsOrderedBy.should == "email"
110
108
  res.OrderDirection.should == "asc"
@@ -122,23 +120,23 @@ class ClientTest < Test::Unit::TestCase
122
120
 
123
121
  should "suppress a single email address" do
124
122
  email = "example@example.com"
125
- stub_post(@api_key, "clients/#{@client.client_id}/suppress.json", nil)
123
+ stub_post(@auth, "clients/#{@client.client_id}/suppress.json", nil)
126
124
  result = @client.suppress email
127
125
  end
128
126
 
129
127
  should "suppress multiple email address" do
130
- stub_post(@api_key, "clients/#{@client.client_id}/suppress.json", nil)
128
+ stub_post(@auth, "clients/#{@client.client_id}/suppress.json", nil)
131
129
  result = @client.suppress [ "one@example.com", "two@example.com" ]
132
130
  end
133
131
 
134
132
  should "unsuppress an email address" do
135
133
  email = "example@example.com"
136
- stub_put(@api_key, "clients/#{@client.client_id}/unsuppress.json?email=#{CGI.escape(email)}", nil)
134
+ stub_put(@auth, "clients/#{@client.client_id}/unsuppress.json?email=#{CGI.escape(email)}", nil)
137
135
  result = @client.unsuppress email
138
136
  end
139
137
 
140
138
  should "get all people" do
141
- stub_get(@api_key, "clients/#{@client.client_id}/people.json", "people.json")
139
+ stub_get(@auth, "clients/#{@client.client_id}/people.json", "people.json")
142
140
  people = @client.people
143
141
  people.size.should == 2
144
142
  people.first.EmailAddress.should == "person1@blackhole.com"
@@ -148,7 +146,7 @@ class ClientTest < Test::Unit::TestCase
148
146
  end
149
147
 
150
148
  should "get all templates" do
151
- stub_get(@api_key, "clients/#{@client.client_id}/templates.json", "templates.json")
149
+ stub_get(@auth, "clients/#{@client.client_id}/templates.json", "templates.json")
152
150
  templates = @client.templates
153
151
  templates.size.should == 2
154
152
  templates.first.TemplateID.should == '5cac213cf061dd4e008de5a82b7a3621'
@@ -157,29 +155,29 @@ class ClientTest < Test::Unit::TestCase
157
155
 
158
156
  should "set primary contact" do
159
157
  email = 'person@blackhole.com'
160
- stub_put(@api_key, "clients/#{@client.client_id}/primarycontact.json?email=#{CGI.escape(email)}", 'client_set_primary_contact.json')
158
+ stub_put(@auth, "clients/#{@client.client_id}/primarycontact.json?email=#{CGI.escape(email)}", 'client_set_primary_contact.json')
161
159
  result = @client.set_primary_contact email
162
160
  result.EmailAddress.should == email
163
161
  end
164
162
 
165
163
  should "get primary contact" do
166
- stub_get(@api_key, "clients/#{@client.client_id}/primarycontact.json", 'client_get_primary_contact.json')
164
+ stub_get(@auth, "clients/#{@client.client_id}/primarycontact.json", 'client_get_primary_contact.json')
167
165
  result = @client.get_primary_contact
168
166
  result.EmailAddress.should == 'person@blackhole.com'
169
167
  end
170
168
 
171
169
  should "set basics" do
172
- stub_put(@api_key, "clients/#{@client.client_id}/setbasics.json", nil)
170
+ stub_put(@auth, "clients/#{@client.client_id}/setbasics.json", nil)
173
171
  @client.set_basics "Client Company Name", "(GMT+10:00) Canberra, Melbourne, Sydney", "Australia"
174
172
  end
175
173
 
176
174
  should "set payg billing" do
177
- stub_put(@api_key, "clients/#{@client.client_id}/setpaygbilling.json", nil)
175
+ stub_put(@auth, "clients/#{@client.client_id}/setpaygbilling.json", nil)
178
176
  @client.set_payg_billing "CAD", true, true, 150
179
177
  end
180
178
 
181
179
  should "set monthly billing (implicit)" do
182
- stub_put(@api_key, "clients/#{@client.client_id}/setmonthlybilling.json", nil)
180
+ stub_put(@auth, "clients/#{@client.client_id}/setmonthlybilling.json", nil)
183
181
  @client.set_monthly_billing "CAD", true, 150
184
182
  request = FakeWeb.last_request.body
185
183
  request.include?("\"Currency\":\"CAD\"").should == true
@@ -189,7 +187,7 @@ class ClientTest < Test::Unit::TestCase
189
187
  end
190
188
 
191
189
  should "set monthly billing (basic)" do
192
- stub_put(@api_key, "clients/#{@client.client_id}/setmonthlybilling.json", nil)
190
+ stub_put(@auth, "clients/#{@client.client_id}/setmonthlybilling.json", nil)
193
191
  @client.set_monthly_billing "CAD", true, 150, "Basic"
194
192
  request = FakeWeb.last_request.body
195
193
  request.include?("\"Currency\":\"CAD\"").should == true
@@ -199,7 +197,7 @@ class ClientTest < Test::Unit::TestCase
199
197
  end
200
198
 
201
199
  should "set monthly billing (unlimited)" do
202
- stub_put(@api_key, "clients/#{@client.client_id}/setmonthlybilling.json", nil)
200
+ stub_put(@auth, "clients/#{@client.client_id}/setmonthlybilling.json", nil)
203
201
  @client.set_monthly_billing "CAD", false, 120, "Unlimited"
204
202
  request = FakeWeb.last_request.body
205
203
  request.include?("\"Currency\":\"CAD\"").should == true
@@ -209,14 +207,14 @@ class ClientTest < Test::Unit::TestCase
209
207
  end
210
208
 
211
209
  should "transfer credits to a client" do
212
- stub_post(@api_key, "clients/#{@client.client_id}/credits.json", "transfer_credits.json")
210
+ stub_post(@auth, "clients/#{@client.client_id}/credits.json", "transfer_credits.json")
213
211
  result = @client.transfer_credits 200, false
214
212
  result.AccountCredits.should == 800
215
213
  result.ClientCredits.should == 200
216
214
  end
217
215
 
218
216
  should "delete a client" do
219
- stub_delete(@api_key, "clients/#{@client.client_id}.json", nil)
217
+ stub_delete(@auth, "clients/#{@client.client_id}.json", nil)
220
218
  @client.delete
221
219
  end
222
220
 
@@ -1,34 +1,156 @@
1
1
  require File.dirname(__FILE__) + '/helper'
2
2
 
3
3
  class CreateSendTest < Test::Unit::TestCase
4
- context "when an api caller is authenticated" do
4
+
5
+ context "when an api caller requires createsend" do
5
6
  setup do
6
- @api_key = '123123123123123123123'
7
- @base_uri = 'https://api.createsend.com/api/v3'
8
- CreateSend.api_key @api_key
9
- @cs = CreateSend::CreateSend.new
7
+ @access_token = "h9898wu98u9dqjoijnwld"
8
+ @refresh_token = "tGzv3JOkF0XG5Qx2TlKWIA"
9
+ @api_key = "hiuhqiw78hiqhwdwdqwdqw2s2e2"
10
10
  end
11
-
12
- should "include the CreateSend module VERSION constant as part of the user agent when making a call" do
13
- # This test is done to ensure that the version from HTTParty isn't included instead
14
- assert CreateSend::CreateSend.headers["User-Agent"] == "createsend-ruby-#{CreateSend::VERSION}"
15
- stub_get(@api_key, "clients.json", "clients.json")
16
- clients = @cs.clients
17
- clients.size.should == 2
11
+
12
+ should "authenticate using an oauth access token and refresh token" do
13
+ auth = {
14
+ :access_token => @access_token,
15
+ :refresh_token => @refresh_token
16
+ }
17
+ cs = CreateSend::CreateSend.new auth
18
+ cs.auth_details.should == auth
18
19
  end
19
-
20
- should "get api key" do
21
- uri = URI.parse(@base_uri)
20
+
21
+ should "authenticate using an api key" do
22
+ auth = {:api_key => @api_key}
23
+ cs = CreateSend::CreateSend.new auth
24
+ cs.auth_details.should == auth
25
+ end
26
+
27
+ should "get the authorization url without state included" do
28
+ client_id = 8998879
29
+ redirect_uri = 'http://example.com/auth'
30
+ scope = 'ViewReports,CreateCampaigns,SendCampaigns'
31
+ url = CreateSend::CreateSend.authorize_url(client_id, redirect_uri, scope)
32
+ url.should == "https://api.createsend.com/oauth?client_id=8998879&redirect_uri=http%3A%2F%2Fexample.com%2Fauth&scope=ViewReports%2CCreateCampaigns%2CSendCampaigns"
33
+ end
34
+
35
+ should "get the authorization url with state included" do
36
+ client_id = 8998879
37
+ redirect_uri = 'http://example.com/auth'
38
+ scope = 'ViewReports,CreateCampaigns,SendCampaigns'
39
+ state = 89879287
40
+ url = CreateSend::CreateSend.authorize_url(client_id, redirect_uri, scope, state)
41
+ url.should == "https://api.createsend.com/oauth?client_id=8998879&redirect_uri=http%3A%2F%2Fexample.com%2Fauth&scope=ViewReports%2CCreateCampaigns%2CSendCampaigns&state=89879287"
42
+ end
43
+
44
+ should "exchange an OAuth token for an access token, 'expires in' value, and refresh token" do
45
+ client_id = 8998879
46
+ client_secret = 'iou0q9wud0q9wd0q9wid0q9iwd0q9wid0q9wdqwd'
47
+ redirect_uri = 'http://example.com/auth'
48
+ code = 'jdiwouo8uowi9o9o'
49
+ options = {
50
+ :body => fixture_file("oauth_exchange_token.json"),
51
+ :content_type => "application/json; charset=utf-8" }
52
+ FakeWeb.register_uri(:post, "https://api.createsend.com/oauth/token", options)
53
+ access_token, expires_in, refresh_token = CreateSend::CreateSend.exchange_token(
54
+ client_id, client_secret, redirect_uri, code)
55
+
56
+ FakeWeb.last_request.body.should == "grant_type=authorization_code&client_id=8998879&client_secret=iou0q9wud0q9wd0q9wid0q9iwd0q9wid0q9wdqwd&redirect_uri=http%3A%2F%2Fexample.com%2Fauth&code=jdiwouo8uowi9o9o"
57
+ access_token.should == "SlAV32hkKG"
58
+ expires_in.should == 1209600
59
+ refresh_token.should == "tGzv3JOkF0XG5Qx2TlKWIA"
60
+ end
61
+
62
+ should "raise an error when an attempt to exchange an OAuth token for an access token fails" do
63
+ client_id = 8998879
64
+ client_secret = 'iou0q9wud0q9wd0q9wid0q9iwd0q9wid0q9wdqwd'
65
+ redirect_uri = 'http://example.com/auth'
66
+ code = 'invalidcode'
67
+ options = {
68
+ :body => fixture_file("oauth_exchange_token_error.json"),
69
+ :content_type => "application/json; charset=utf-8" }
70
+ FakeWeb.register_uri(:post, "https://api.createsend.com/oauth/token", options)
71
+ lambda { access_token, expires_in, refresh_token = CreateSend::CreateSend.exchange_token(
72
+ client_id, client_secret, redirect_uri, code) }.should raise_error(
73
+ Exception, 'Error exchanging code for access token: invalid_grant - Specified code was invalid or expired')
74
+ FakeWeb.last_request.body.should == "grant_type=authorization_code&client_id=8998879&client_secret=iou0q9wud0q9wd0q9wid0q9iwd0q9wid0q9wdqwd&redirect_uri=http%3A%2F%2Fexample.com%2Fauth&code=invalidcode"
75
+ end
76
+
77
+ should "get a person's api key" do
78
+ base_uri = "https://api.createsend.com/api/v3"
79
+ uri = URI.parse(base_uri)
22
80
  site_url = "http://iamadesigner.createsend.com/"
23
81
  username = "myusername"
24
82
  password = "mypassword"
83
+ cs = CreateSend::CreateSend.new
25
84
  stub_get(nil, "https://#{username}:#{password}@#{uri.host}#{uri.path}/apikey.json?SiteUrl=#{CGI.escape(site_url)}", "apikey.json")
26
- apikey = @cs.apikey(site_url, username, password).ApiKey
85
+ apikey = cs.apikey(site_url, username, password).ApiKey
27
86
  apikey.should == "981298u298ue98u219e8u2e98u2"
87
+ cs.auth_details.should == {:api_key => apikey}
88
+ end
89
+
90
+ end
91
+
92
+ context "when an api caller is authenticated using oauth" do
93
+ setup do
94
+ @access_token = "h9898wu98u9dqjoijnwld"
95
+ @refresh_token = "tGzv3JOkF0XG5Qx2TlKWIA"
96
+ @auth = {
97
+ :access_token => @access_token,
98
+ :refresh_token => @refresh_token
99
+ }
100
+ end
101
+
102
+ should "refresh the current access token" do
103
+ options = {
104
+ :body => fixture_file("refresh_oauth_token.json"),
105
+ :content_type => "application/json; charset=utf-8" }
106
+ FakeWeb.register_uri(:post, "https://api.createsend.com/oauth/token", options)
107
+ cs = CreateSend::CreateSend.new @auth
108
+ new_access_token, new_expires_in, new_refresh_token = cs.refresh_token
109
+
110
+ FakeWeb.last_request.body.should == "grant_type=refresh_token&refresh_token=tGzv3JOkF0XG5Qx2TlKWIA"
111
+ new_access_token.should == "SlAV32hkKG2e12e"
112
+ new_expires_in.should == 1209600
113
+ new_refresh_token.should == "tGzv3JOkF0XG5Qx2TlKWIA"
114
+ cs.auth_details.should == {
115
+ :access_token => new_access_token,
116
+ :refresh_token => new_refresh_token
117
+ }
118
+ end
119
+
120
+ should "raise an error when an attempt to refresh the access token is made but refresh token is nil" do
121
+ cs = CreateSend::CreateSend.new :access_token => 'any token', :refresh_token => nil
122
+ lambda { new_access_token, new_refresh_token = cs.refresh_token }.should raise_error(
123
+ Exception, '@auth_details[:refresh_token] does not contain a refresh token.')
124
+ end
125
+
126
+ should "raise an error when an attempt to refresh the access token is made but no there was no refresh token passed in" do
127
+ cs = CreateSend::CreateSend.new :access_token => 'any token'
128
+ lambda { new_access_token, new_refresh_token = cs.refresh_token }.should raise_error(
129
+ Exception, '@auth_details[:refresh_token] does not contain a refresh token.')
130
+ end
131
+
132
+ should "raise an error when an attempt to refresh the access token is made but no there was no auth hash passed in" do
133
+ cs = CreateSend::CreateSend.new
134
+ lambda { new_access_token, new_refresh_token = cs.refresh_token }.should raise_error(
135
+ Exception, '@auth_details[:refresh_token] does not contain a refresh token.')
136
+ end
137
+ end
138
+
139
+ multiple_contexts "authenticated_using_oauth_context", "authenticated_using_api_key_context" do
140
+ setup do
141
+ @cs = CreateSend::CreateSend.new @auth
142
+ end
143
+
144
+ should "include the CreateSend module VERSION constant as part of the user agent when making a call" do
145
+ # This test is done to ensure that the version from HTTParty isn't included instead
146
+ assert CreateSend::CreateSend.headers["User-Agent"] == "createsend-ruby-#{CreateSend::VERSION}"
147
+ stub_get(@auth, "clients.json", "clients.json")
148
+ clients = @cs.clients
149
+ clients.size.should == 2
28
150
  end
29
151
 
30
152
  should "get all clients" do
31
- stub_get(@api_key, "clients.json", "clients.json")
153
+ stub_get(@auth, "clients.json", "clients.json")
32
154
  clients = @cs.clients
33
155
  clients.size.should == 2
34
156
  clients.first.ClientID.should == '4a397ccaaa55eb4e6aa1221e1e2d7122'
@@ -36,98 +158,117 @@ class CreateSendTest < Test::Unit::TestCase
36
158
  end
37
159
 
38
160
  should "get billing details" do
39
- stub_get(@api_key, "billingdetails.json", "billingdetails.json")
161
+ stub_get(@auth, "billingdetails.json", "billingdetails.json")
40
162
  bd = @cs.billing_details
41
163
  bd.Credits.should == 3021
42
164
  end
43
165
 
44
166
  should "get all countries" do
45
- stub_get(@api_key, "countries.json", "countries.json")
167
+ stub_get(@auth, "countries.json", "countries.json")
46
168
  countries = @cs.countries
47
169
  countries.size.should == 245
48
170
  assert countries.include? "Australia"
49
171
  end
50
172
 
51
173
  should "get system date" do
52
- stub_get(@api_key, "systemdate.json", "systemdate.json")
174
+ stub_get(@auth, "systemdate.json", "systemdate.json")
53
175
  systemdate = @cs.systemdate.SystemDate
54
176
  systemdate.should == "2010-10-15 09:27:00"
55
177
  end
56
178
 
57
179
  should "get all timezones" do
58
- stub_get(@api_key, "timezones.json", "timezones.json")
180
+ stub_get(@auth, "timezones.json", "timezones.json")
59
181
  timezones = @cs.timezones
60
182
  timezones.size.should == 97
61
183
  assert timezones.include? "(GMT+12:00) Fiji"
62
184
  end
63
185
 
64
186
  should "get all administrators" do
65
- stub_get(@api_key, "admins.json", "administrators.json")
187
+ stub_get(@auth, "admins.json", "administrators.json")
66
188
  administrators = @cs.administrators
67
189
  administrators.size.should == 2
68
190
  administrators.first.EmailAddress.should == "admin1@blackhole.com"
69
191
  administrators.first.Name.should == 'Admin One'
70
192
  administrators.first.Status.should == 'Active'
71
193
  end
72
-
194
+
73
195
  should "set primary contact" do
74
196
  email = 'admin@blackhole.com'
75
- stub_put(@api_key, "primarycontact.json?email=#{CGI.escape(email)}", 'admin_set_primary_contact.json')
197
+ stub_put(@auth, "primarycontact.json?email=#{CGI.escape(email)}", 'admin_set_primary_contact.json')
76
198
  result = @cs.set_primary_contact email
77
199
  result.EmailAddress.should == email
78
200
  end
79
-
201
+
80
202
  should "get primary contact" do
81
- stub_get(@api_key, "primarycontact.json", 'admin_get_primary_contact.json')
203
+ stub_get(@auth, "primarycontact.json", 'admin_get_primary_contact.json')
82
204
  result = @cs.get_primary_contact
83
205
  result.EmailAddress.should == 'admin@blackhole.com'
84
206
  end
85
-
207
+
86
208
  end
87
209
 
88
210
  context "when the CreateSend API responds with an error" do
89
- setup do
90
- @api_key = '123123123123123123123'
91
- @base_uri = 'https://api.createsend.com/api/v3'
92
- CreateSend.api_key @api_key
93
- @cs = CreateSend::CreateSend.new
94
- @template = CreateSend::Template.new('98y2e98y289dh89h938389')
95
- end
96
-
97
- { ["400", "Bad Request"] => CreateSend::BadRequest,
98
- ["401", "Unauthorized"] => CreateSend::Unauthorized,
99
- ["404", "Not Found"] => CreateSend::NotFound,
100
- ["500", "Server Error"] => CreateSend::ServerError
101
- }.each do |status, exception|
102
- context "#{status.first}, a get" do
103
- should "raise a #{exception.name} error" do
104
- stub_get(@api_key, "countries.json", (status.first == '400' or status.first == '401') ? 'custom_api_error.json' : nil, status)
105
- lambda { c = @cs.countries }.should raise_error(exception)
106
- end
211
+
212
+ multiple_contexts "authenticated_using_oauth_context", "authenticated_using_api_key_context" do
213
+ setup do
214
+ @cs = CreateSend::CreateSend.new @auth
215
+ @template = CreateSend::Template.new @auth, '98y2e98y289dh89h938389'
107
216
  end
108
217
 
109
- context "#{status.first}, a post" do
110
- should "raise a #{exception.name} error" do
111
- stub_post(@api_key, "clients.json", (status.first == '400' or status.first == '401') ? 'custom_api_error.json' : nil, status)
112
- lambda { CreateSend::Client.create "Client Company Name",
113
- "(GMT+10:00) Canberra, Melbourne, Sydney", "Australia" }.should raise_error(exception)
218
+ { ["400", "Bad Request"] => CreateSend::BadRequest,
219
+ ["401", "Unauthorized"] => CreateSend::Unauthorized,
220
+ ["404", "Not Found"] => CreateSend::NotFound,
221
+ ["500", "Server Error"] => CreateSend::ServerError
222
+ }.each do |status, exception|
223
+ context "#{status.first}, a get" do
224
+ should "raise a #{exception.name} error" do
225
+ stub_get(@auth, "countries.json", (status.first == '400' or status.first == '401') ? 'custom_api_error.json' : nil, status)
226
+ lambda { c = @cs.countries }.should raise_error(exception)
227
+ end
114
228
  end
115
- end
116
229
 
117
- context "#{status.first}, a put" do
118
- should "raise a #{exception.name} error" do
119
- stub_put(@api_key, "templates/#{@template.template_id}.json", (status.first == '400' or status.first == '401') ? 'custom_api_error.json' : nil, status)
120
- lambda { @template.update "Template One Updated", "http://templates.org/index.html",
121
- "http://templates.org/files.zip" }.should raise_error(exception)
230
+ context "#{status.first}, a post" do
231
+ should "raise a #{exception.name} error" do
232
+ stub_post(@auth, "clients.json", (status.first == '400' or status.first == '401') ? 'custom_api_error.json' : nil, status)
233
+ lambda { CreateSend::Client.create @auth, "Client Company Name",
234
+ "(GMT+10:00) Canberra, Melbourne, Sydney", "Australia" }.should raise_error(exception)
235
+ end
236
+ end
237
+
238
+ context "#{status.first}, a put" do
239
+ should "raise a #{exception.name} error" do
240
+ stub_put(@auth, "templates/#{@template.template_id}.json", (status.first == '400' or status.first == '401') ? 'custom_api_error.json' : nil, status)
241
+ lambda { @template.update "Template One Updated", "http://templates.org/index.html",
242
+ "http://templates.org/files.zip" }.should raise_error(exception)
243
+ end
122
244
  end
123
- end
124
245
 
125
- context "#{status.first}, a delete" do
126
- should "raise a #{exception.name} error" do
127
- stub_delete(@api_key, "templates/#{@template.template_id}.json", (status.first == '400' or status.first == '401') ? 'custom_api_error.json' : nil, status)
128
- lambda { @template.delete }.should raise_error(exception)
246
+ context "#{status.first}, a delete" do
247
+ should "raise a #{exception.name} error" do
248
+ stub_delete(@auth, "templates/#{@template.template_id}.json", (status.first == '400' or status.first == '401') ? 'custom_api_error.json' : nil, status)
249
+ lambda { @template.delete }.should raise_error(exception)
250
+ end
129
251
  end
130
252
  end
131
253
  end
254
+
255
+ context "when authenticated using oauth and the access token has expired" do
256
+ setup do
257
+ @access_token = '98y98u98u98ue212'
258
+ @refresh_token = 'kj9wud09wi0qi0w'
259
+ @auth = {
260
+ :access_token => @access_token,
261
+ :refresh_token => @refresh_token
262
+ }
263
+ @cs = CreateSend::CreateSend.new @auth
264
+ end
265
+
266
+ should "raise a CreateSend::ExpiredOAuthToken error" do
267
+ stub_get(@auth, "countries.json", "expired_oauth_token_api_error.json", ["401", "Unauthorized"])
268
+ lambda { c = @cs.countries }.should raise_error(CreateSend::ExpiredOAuthToken)
269
+ end
270
+ end
271
+
132
272
  end
273
+
133
274
  end