createsend 2.5.1 → 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,4 @@
1
+ {
2
+ "Code": 121,
3
+ "Message": "Expired OAuth Token"
4
+ }
@@ -0,0 +1,5 @@
1
+ {
2
+ "access_token": "SlAV32hkKG",
3
+ "expires_in": 1209600,
4
+ "refresh_token": "tGzv3JOkF0XG5Qx2TlKWIA"
5
+ }
@@ -0,0 +1,4 @@
1
+ {
2
+ "error": "invalid_grant",
3
+ "error_description": "Specified code was invalid or expired"
4
+ }
@@ -0,0 +1,5 @@
1
+ {
2
+ "access_token": "SlAV32hkKG2e12e",
3
+ "expires_in": 1209600,
4
+ "refresh_token": "tGzv3JOkF0XG5Qx2TlKWIA"
5
+ }
data/test/helper.rb CHANGED
@@ -3,7 +3,6 @@ require 'pathname'
3
3
 
4
4
  require 'shoulda'
5
5
  require 'matchy'
6
- require 'mocha/setup'
7
6
  require 'fakeweb'
8
7
 
9
8
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
@@ -18,19 +17,53 @@ def fixture_file(filename)
18
17
  File.read(file_path)
19
18
  end
20
19
 
21
- def createsend_url(api_key, url)
22
- api_key.nil? ? url : url =~ /^http/ ? url : "https://#{api_key}:x@api.createsend.com/api/v3/#{url}"
20
+ def createsend_url(auth, url)
21
+ if not url =~ /^http/
22
+ auth_section = ''
23
+ auth_section = "#{auth[:api_key]}:x@" if auth and auth.has_key? :api_key
24
+ result = "https://#{auth_section}api.createsend.com/api/v3/#{url}"
25
+ else
26
+ result = url
27
+ end
28
+ result
23
29
  end
24
30
 
25
- def stub_request(method, api_key, url, filename, status=nil)
31
+ def stub_request(method, auth, url, filename, status=nil)
26
32
  options = {:body => ""}
27
33
  options.merge!({:body => fixture_file(filename)}) if filename
28
34
  options.merge!({:status => status}) if status
29
35
  options.merge!(:content_type => "application/json; charset=utf-8")
30
- FakeWeb.register_uri(method, createsend_url(api_key, url), options)
36
+ FakeWeb.register_uri(method, createsend_url(auth, url), options)
31
37
  end
32
38
 
33
39
  def stub_get(*args); stub_request(:get, *args) end
34
40
  def stub_post(*args); stub_request(:post, *args) end
35
41
  def stub_put(*args); stub_request(:put, *args) end
36
42
  def stub_delete(*args); stub_request(:delete, *args) end
43
+
44
+ def multiple_contexts(*contexts, &blk)
45
+ contexts.each do |context|
46
+ send(context, &blk)
47
+ end
48
+ end
49
+
50
+ def authenticated_using_oauth_context(&blk)
51
+ context "when an api caller is authenticated using oauth" do
52
+ setup do
53
+ @access_token = 'joidjo2i3joi3je'
54
+ @refresh_token = 'j89u98eu9e8ufe'
55
+ @auth = {:access_token => @access_token, :refresh_token => @refresh_token}
56
+ end
57
+ merge_block(&blk)
58
+ end
59
+ end
60
+
61
+ def authenticated_using_api_key_context(&blk)
62
+ context "when an api caller is authenticated using an api key" do
63
+ setup do
64
+ @api_key = '123123123123123123123'
65
+ @auth = {:api_key => @api_key}
66
+ end
67
+ merge_block(&blk)
68
+ end
69
+ end
data/test/list_test.rb CHANGED
@@ -1,49 +1,47 @@
1
1
  require File.dirname(__FILE__) + '/helper'
2
2
 
3
3
  class ListTest < 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
6
  @client_id = "87y8d7qyw8d7yq8w7ydwqwd"
9
7
  @list_id = "e3c5f034d68744f7881fdccf13c2daee"
10
- @list = CreateSend::List.new @list_id
8
+ @list = CreateSend::List.new @auth, @list_id
11
9
  end
12
10
 
13
11
  should "create a list without passing in unsubscribe setting" do
14
- stub_post(@api_key, "lists/#{@client_id}.json", "create_list.json")
15
- list_id = CreateSend::List.create @client_id, "List One", "", false, ""
12
+ stub_post(@auth, "lists/#{@client_id}.json", "create_list.json")
13
+ list_id = CreateSend::List.create @auth, @client_id, "List One", "", false, ""
16
14
  list_id.should == "e3c5f034d68744f7881fdccf13c2daee"
17
15
  end
18
16
 
19
17
  should "create a list passing in unsubscribe setting" do
20
- stub_post(@api_key, "lists/#{@client_id}.json", "create_list.json")
21
- list_id = CreateSend::List.create @client_id, "List One", "", false, "", "OnlyThisList"
18
+ stub_post(@auth, "lists/#{@client_id}.json", "create_list.json")
19
+ list_id = CreateSend::List.create @auth, @client_id, "List One", "", false, "", "OnlyThisList"
22
20
  list_id.should == "e3c5f034d68744f7881fdccf13c2daee"
23
21
  end
24
22
 
25
23
  should "update a list without passing in unsubscribe setting" do
26
- stub_put(@api_key, "lists/#{@list.list_id}.json", nil)
24
+ stub_put(@auth, "lists/#{@list.list_id}.json", nil)
27
25
  @list.update "List One Renamed", "", false, ""
28
26
  end
29
27
 
30
28
  should "update a list passing in unsubscribe setting" do
31
- stub_put(@api_key, "lists/#{@list.list_id}.json", nil)
29
+ stub_put(@auth, "lists/#{@list.list_id}.json", nil)
32
30
  @list.update "List One Renamed", "", false, "", "OnlyThisList"
33
31
  end
34
32
 
35
33
  should "update a list passing in unsubscribe setting and suppression list options" do
36
- stub_put(@api_key, "lists/#{@list.list_id}.json", nil)
34
+ stub_put(@auth, "lists/#{@list.list_id}.json", nil)
37
35
  @list.update "List One Renamed", "", false, "", "OnlyThisList", true, true
38
36
  end
39
37
 
40
38
  should "delete a list" do
41
- stub_delete(@api_key, "lists/#{@list.list_id}.json", nil)
39
+ stub_delete(@auth, "lists/#{@list.list_id}.json", nil)
42
40
  @list.delete
43
41
  end
44
42
 
45
43
  should "create a custom field" do
46
- stub_post(@api_key, "lists/#{@list.list_id}/customfields.json", "create_custom_field.json")
44
+ stub_post(@auth, "lists/#{@list.list_id}/customfields.json", "create_custom_field.json")
47
45
  personalisation_tag = @list.create_custom_field "new date field", "Date"
48
46
  request = FakeWeb.last_request.body
49
47
  request.include?("\"FieldName\":\"new date field\"").should == true
@@ -54,7 +52,7 @@ class ListTest < Test::Unit::TestCase
54
52
  end
55
53
 
56
54
  should "create a custom field with options and visible_in_preference_center" do
57
- stub_post(@api_key, "lists/#{@list.list_id}/customfields.json", "create_custom_field.json")
55
+ stub_post(@auth, "lists/#{@list.list_id}/customfields.json", "create_custom_field.json")
58
56
  options = ["one", "two"]
59
57
  personalisation_tag = @list.create_custom_field("newsletter format",
60
58
  "MultiSelectOne", options, false)
@@ -68,7 +66,7 @@ class ListTest < Test::Unit::TestCase
68
66
 
69
67
  should "update a custom field" do
70
68
  key = "[mycustomfield]"
71
- stub_put(@api_key, "lists/#{@list.list_id}/customfields/#{CGI.escape(key)}.json", "update_custom_field.json")
69
+ stub_put(@auth, "lists/#{@list.list_id}/customfields/#{CGI.escape(key)}.json", "update_custom_field.json")
72
70
  personalisation_tag = @list.update_custom_field key, "my renamed custom field", true
73
71
  request = FakeWeb.last_request.body
74
72
  request.include?("\"FieldName\":\"my renamed custom field\"").should == true
@@ -78,19 +76,19 @@ class ListTest < Test::Unit::TestCase
78
76
 
79
77
  should "delete a custom field" do
80
78
  custom_field_key = "[newdatefield]"
81
- stub_delete(@api_key, "lists/#{@list.list_id}/customfields/#{CGI.escape(custom_field_key)}.json", nil)
79
+ stub_delete(@auth, "lists/#{@list.list_id}/customfields/#{CGI.escape(custom_field_key)}.json", nil)
82
80
  @list.delete_custom_field custom_field_key
83
81
  end
84
82
 
85
83
  should "update the options of a multi-optioned custom field" do
86
84
  custom_field_key = "[newdatefield]"
87
85
  new_options = [ "one", "two", "three" ]
88
- stub_put(@api_key, "lists/#{@list.list_id}/customfields/#{CGI.escape(custom_field_key)}/options.json", nil)
86
+ stub_put(@auth, "lists/#{@list.list_id}/customfields/#{CGI.escape(custom_field_key)}/options.json", nil)
89
87
  @list.update_custom_field_options custom_field_key, new_options, true
90
88
  end
91
89
 
92
90
  should "get the details of a list" do
93
- stub_get(@api_key, "lists/#{@list.list_id}.json", "list_details.json")
91
+ stub_get(@auth, "lists/#{@list.list_id}.json", "list_details.json")
94
92
  details = @list.details
95
93
  details.ConfirmedOptIn.should == false
96
94
  details.Title.should == "a non-basic list :)"
@@ -101,7 +99,7 @@ class ListTest < Test::Unit::TestCase
101
99
  end
102
100
 
103
101
  should "get the custom fields for a list" do
104
- stub_get(@api_key, "lists/#{@list.list_id}/customfields.json", "custom_fields.json")
102
+ stub_get(@auth, "lists/#{@list.list_id}/customfields.json", "custom_fields.json")
105
103
  cfs = @list.custom_fields
106
104
  cfs.size.should == 3
107
105
  cfs.first.FieldName.should == "website"
@@ -112,7 +110,7 @@ class ListTest < Test::Unit::TestCase
112
110
  end
113
111
 
114
112
  should "get the segments for a list" do
115
- stub_get(@api_key, "lists/#{@list.list_id}/segments.json", "segments.json")
113
+ stub_get(@auth, "lists/#{@list.list_id}/segments.json", "segments.json")
116
114
  segments = @list.segments
117
115
  segments.size.should == 2
118
116
  segments.first.ListID.should == 'a58ee1d3039b8bec838e6d1482a8a965'
@@ -121,7 +119,7 @@ class ListTest < Test::Unit::TestCase
121
119
  end
122
120
 
123
121
  should "get the stats for a list" do
124
- stub_get(@api_key, "lists/#{@list.list_id}/stats.json", "list_stats.json")
122
+ stub_get(@auth, "lists/#{@list.list_id}/stats.json", "list_stats.json")
125
123
  stats = @list.stats
126
124
  stats.TotalActiveSubscribers.should == 6
127
125
  stats.TotalUnsubscribes.should == 2
@@ -131,7 +129,7 @@ class ListTest < Test::Unit::TestCase
131
129
 
132
130
  should "get the active subscribers for a list" do
133
131
  min_date = "2010-01-01"
134
- stub_get(@api_key, "lists/#{@list.list_id}/active.json?pagesize=1000&orderfield=email&page=1&orderdirection=asc&date=#{CGI.escape(min_date)}",
132
+ stub_get(@auth, "lists/#{@list.list_id}/active.json?pagesize=1000&orderfield=email&page=1&orderdirection=asc&date=#{CGI.escape(min_date)}",
135
133
  "active_subscribers.json")
136
134
  res = @list.active min_date
137
135
  res.ResultsOrderedBy.should == "email"
@@ -158,7 +156,7 @@ class ListTest < Test::Unit::TestCase
158
156
 
159
157
  should "get the unconfirmed subscribers for a list" do
160
158
  min_date = "2010-01-01"
161
- stub_get(@api_key, "lists/#{@list.list_id}/unconfirmed.json?pagesize=1000&orderfield=email&page=1&orderdirection=asc&date=#{CGI.escape(min_date)}",
159
+ stub_get(@auth, "lists/#{@list.list_id}/unconfirmed.json?pagesize=1000&orderfield=email&page=1&orderdirection=asc&date=#{CGI.escape(min_date)}",
162
160
  "unconfirmed_subscribers.json")
163
161
  res = @list.unconfirmed min_date
164
162
  res.ResultsOrderedBy.should == "email"
@@ -176,7 +174,7 @@ class ListTest < Test::Unit::TestCase
176
174
 
177
175
  should "get the unsubscribed subscribers for a list" do
178
176
  min_date = "2010-01-01"
179
- stub_get(@api_key, "lists/#{@list.list_id}/unsubscribed.json?pagesize=1000&orderfield=email&page=1&orderdirection=asc&date=#{CGI.escape(min_date)}",
177
+ stub_get(@auth, "lists/#{@list.list_id}/unsubscribed.json?pagesize=1000&orderfield=email&page=1&orderdirection=asc&date=#{CGI.escape(min_date)}",
180
178
  "unsubscribed_subscribers.json")
181
179
  res = @list.unsubscribed min_date
182
180
  res.ResultsOrderedBy.should == "email"
@@ -197,7 +195,7 @@ class ListTest < Test::Unit::TestCase
197
195
 
198
196
  should "get the deleted subscribers for a list" do
199
197
  min_date = "2010-01-01"
200
- stub_get(@api_key, "lists/#{@list.list_id}/deleted.json?pagesize=1000&orderfield=email&page=1&orderdirection=asc&date=#{CGI.escape(min_date)}",
198
+ stub_get(@auth, "lists/#{@list.list_id}/deleted.json?pagesize=1000&orderfield=email&page=1&orderdirection=asc&date=#{CGI.escape(min_date)}",
201
199
  "deleted_subscribers.json")
202
200
  res = @list.deleted min_date
203
201
  res.ResultsOrderedBy.should == "email"
@@ -218,7 +216,7 @@ class ListTest < Test::Unit::TestCase
218
216
 
219
217
  should "get the bounced subscribers for a list" do
220
218
  min_date = "2010-01-01"
221
- stub_get(@api_key, "lists/#{@list.list_id}/bounced.json?pagesize=1000&orderfield=email&page=1&orderdirection=asc&date=#{CGI.escape(min_date)}",
219
+ stub_get(@auth, "lists/#{@list.list_id}/bounced.json?pagesize=1000&orderfield=email&page=1&orderdirection=asc&date=#{CGI.escape(min_date)}",
222
220
  "bounced_subscribers.json")
223
221
  res = @list.bounced min_date
224
222
  res.ResultsOrderedBy.should == "email"
@@ -238,7 +236,7 @@ class ListTest < Test::Unit::TestCase
238
236
  end
239
237
 
240
238
  should "get the webhooks for a list" do
241
- stub_get(@api_key, "lists/#{@list.list_id}/webhooks.json", "list_webhooks.json")
239
+ stub_get(@auth, "lists/#{@list.list_id}/webhooks.json", "list_webhooks.json")
242
240
  hooks = @list.webhooks
243
241
  hooks.size.should == 2
244
242
  hooks.first.WebhookID.should == "943678317049bc13"
@@ -250,32 +248,32 @@ class ListTest < Test::Unit::TestCase
250
248
  end
251
249
 
252
250
  should "create a webhook for a list" do
253
- stub_post(@api_key, "lists/#{@list.list_id}/webhooks.json", "create_list_webhook.json")
251
+ stub_post(@auth, "lists/#{@list.list_id}/webhooks.json", "create_list_webhook.json")
254
252
  webhook_id = @list.create_webhook ["Unsubscribe", "Spam"], "http://example.com/unsub", "json"
255
253
  webhook_id.should == "6a783d359bd44ef62c6ca0d3eda4412a"
256
254
  end
257
255
 
258
256
  should "test a webhook for a list" do
259
257
  webhook_id = "jiuweoiwueoiwueowiueo"
260
- stub_get(@api_key, "lists/#{@list.list_id}/webhooks/#{webhook_id}/test.json", nil)
258
+ stub_get(@auth, "lists/#{@list.list_id}/webhooks/#{webhook_id}/test.json", nil)
261
259
  @list.test_webhook webhook_id
262
260
  end
263
261
 
264
262
  should "delete a webhook for a list" do
265
263
  webhook_id = "jiuweoiwueoiwueowiueo"
266
- stub_delete(@api_key, "lists/#{@list.list_id}/webhooks/#{webhook_id}.json", nil)
264
+ stub_delete(@auth, "lists/#{@list.list_id}/webhooks/#{webhook_id}.json", nil)
267
265
  @list.delete_webhook webhook_id
268
266
  end
269
267
 
270
268
  should "activate a webhook for a list" do
271
269
  webhook_id = "jiuweoiwueoiwueowiueo"
272
- stub_put(@api_key, "lists/#{@list.list_id}/webhooks/#{webhook_id}/activate.json", nil)
270
+ stub_put(@auth, "lists/#{@list.list_id}/webhooks/#{webhook_id}/activate.json", nil)
273
271
  @list.activate_webhook webhook_id
274
272
  end
275
273
 
276
274
  should "de-activate a webhook for a list" do
277
275
  webhook_id = "jiuweoiwueoiwueowiueo"
278
- stub_put(@api_key, "lists/#{@list.list_id}/webhooks/#{webhook_id}/deactivate.json", nil)
276
+ stub_put(@auth, "lists/#{@list.list_id}/webhooks/#{webhook_id}/deactivate.json", nil)
279
277
  @list.deactivate_webhook webhook_id
280
278
  end
281
279
  end
data/test/person_test.rb CHANGED
@@ -1,18 +1,16 @@
1
1
  require File.dirname(__FILE__) + '/helper'
2
2
 
3
3
  class PersonTest < 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
6
  @client_id = "d98h2938d9283d982u3d98u88"
9
- @person = CreateSend::Person.new @client_id, "person@example.com"
7
+ @person = CreateSend::Person.new @auth, @client_id, "person@example.com"
10
8
  end
11
-
9
+
12
10
  should "get a person by client id and email address" do
13
11
  email = "person@example.com"
14
- stub_get(@api_key, "clients/#{@client_id}/people.json?email=#{CGI.escape(email)}", "person_details.json")
15
- person = CreateSend::Person.get @client_id, email
12
+ stub_get(@auth, "clients/#{@client_id}/people.json?email=#{CGI.escape(email)}", "person_details.json")
13
+ person = CreateSend::Person.get @auth, @client_id, email
16
14
  person.EmailAddress.should == email
17
15
  person.Name.should == "Person One"
18
16
  person.AccessLevel.should == 1023
@@ -20,21 +18,21 @@ class PersonTest < Test::Unit::TestCase
20
18
  end
21
19
 
22
20
  should "add a person" do
23
- stub_post(@api_key, "clients/#{@client_id}/people.json", "add_person.json")
24
- result = CreateSend::Person.add @client_id, "person@example.com", "Person", 0, "Password"
21
+ stub_post(@auth, "clients/#{@client_id}/people.json", "add_person.json")
22
+ result = CreateSend::Person.add @auth, @client_id, "person@example.com", "Person", 0, "Password"
25
23
  result.EmailAddress.should == "person@example.com"
26
24
  end
27
25
 
28
26
  should "update a person" do
29
27
  email = "person@example.com"
30
28
  new_email = "new_email_address@example.com"
31
- stub_put(@api_key, "clients/#{@client_id}/people.json?email=#{CGI.escape(email)}", nil)
29
+ stub_put(@auth, "clients/#{@client_id}/people.json?email=#{CGI.escape(email)}", nil)
32
30
  @person.update new_email, "Person", 1023, "NewPassword"
33
31
  @person.email_address.should == new_email
34
32
  end
35
33
 
36
34
  should "delete a person" do
37
- stub_delete(@api_key, "clients/#{@person.client_id}/people.json?email=#{CGI.escape(@person.email_address)}", nil)
35
+ stub_delete(@auth, "clients/#{@person.client_id}/people.json?email=#{CGI.escape(@person.email_address)}", nil)
38
36
  @person.delete
39
37
  end
40
38
  end
data/test/segment_test.rb CHANGED
@@ -1,36 +1,34 @@
1
1
  require File.dirname(__FILE__) + '/helper'
2
2
 
3
3
  class SegmentTest < 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
- @segment = CreateSend::Segment.new('98y2e98y289dh89h938389')
6
+ @segment = CreateSend::Segment.new @auth, '98y2e98y289dh89h938389'
9
7
  end
10
8
 
11
9
  should "create a new segment" do
12
10
  list_id = "2983492834987394879837498"
13
11
  rules = [ { :Subject => "EmailAddress", :Clauses => [ "CONTAINS example.com" ] } ]
14
- stub_post(@api_key, "segments/#{list_id}.json", "create_segment.json")
15
- res = CreateSend::Segment.create list_id, "new segment title", rules
12
+ stub_post(@auth, "segments/#{list_id}.json", "create_segment.json")
13
+ res = CreateSend::Segment.create @auth, list_id, "new segment title", rules
16
14
  res.should == "0246c2aea610a3545d9780bf6ab89006"
17
15
  end
18
16
 
19
17
  should "update a segment" do
20
18
  rules = [ { :Subject => "Name", :Clauses => [ "EQUALS subscriber" ] } ]
21
- stub_put(@api_key, "segments/#{@segment.segment_id}.json", nil)
19
+ stub_put(@auth, "segments/#{@segment.segment_id}.json", nil)
22
20
  @segment.update "new title for segment", rules
23
21
  end
24
-
22
+
25
23
  should "add a rule to a segment" do
26
24
  clauses = [ "CONTAINS example.com" ]
27
- stub_post(@api_key, "segments/#{@segment.segment_id}/rules.json", nil)
25
+ stub_post(@auth, "segments/#{@segment.segment_id}/rules.json", nil)
28
26
  @segment.add_rule "EmailAddress", clauses
29
27
  end
30
28
 
31
29
  should "get the active subscribers for a particular segment in the list" do
32
30
  min_date = "2010-01-01"
33
- stub_get(@api_key, "segments/#{@segment.segment_id}/active.json?pagesize=1000&orderfield=email&page=1&orderdirection=asc&date=#{CGI.escape(min_date)}",
31
+ stub_get(@auth, "segments/#{@segment.segment_id}/active.json?pagesize=1000&orderfield=email&page=1&orderdirection=asc&date=#{CGI.escape(min_date)}",
34
32
  "segment_subscribers.json")
35
33
  res = @segment.subscribers min_date
36
34
  res.ResultsOrderedBy.should == "email"
@@ -47,14 +45,14 @@ class SegmentTest < Test::Unit::TestCase
47
45
  res.Results.first.State.should == "Active"
48
46
  res.Results.first.CustomFields.should == []
49
47
  end
50
-
48
+
51
49
  should "delete a segment" do
52
- stub_delete(@api_key, "segments/#{@segment.segment_id}.json", nil)
50
+ stub_delete(@auth, "segments/#{@segment.segment_id}.json", nil)
53
51
  @segment.delete
54
52
  end
55
53
 
56
54
  should "get the details of a segment" do
57
- stub_get(@api_key, "segments/#{@segment.segment_id}.json", "segment_details.json")
55
+ stub_get(@auth, "segments/#{@segment.segment_id}.json", "segment_details.json")
58
56
  res = @segment.details
59
57
  res.ActiveSubscribers.should == 0
60
58
  res.Rules.size.should == 2
@@ -67,7 +65,7 @@ class SegmentTest < Test::Unit::TestCase
67
65
  end
68
66
 
69
67
  should "clear a segment's rules" do
70
- stub_delete(@api_key, "segments/#{@segment.segment_id}/rules.json", nil)
68
+ stub_delete(@auth, "segments/#{@segment.segment_id}/rules.json", nil)
71
69
  @segment.clear_rules
72
70
  end
73
71
 
@@ -1,18 +1,16 @@
1
1
  require File.dirname(__FILE__) + '/helper'
2
2
 
3
3
  class SubscriberTest < 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
6
  @list_id = "d98h2938d9283d982u3d98u88"
9
- @subscriber = CreateSend::Subscriber.new @list_id, "subscriber@example.com"
7
+ @subscriber = CreateSend::Subscriber.new @auth, @list_id, "subscriber@example.com"
10
8
  end
11
9
 
12
10
  should "get a subscriber by list id and email address" do
13
11
  email = "subscriber@example.com"
14
- stub_get(@api_key, "subscribers/#{@list_id}.json?email=#{CGI.escape(email)}", "subscriber_details.json")
15
- subscriber = CreateSend::Subscriber.get @list_id, email
12
+ stub_get(@auth, "subscribers/#{@list_id}.json?email=#{CGI.escape(email)}", "subscriber_details.json")
13
+ subscriber = CreateSend::Subscriber.get @auth, @list_id, email
16
14
  subscriber.EmailAddress.should == email
17
15
  subscriber.Name.should == "Subscriber One"
18
16
  subscriber.Date.should == "2010-10-25 10:28:00"
@@ -24,31 +22,31 @@ class SubscriberTest < Test::Unit::TestCase
24
22
  end
25
23
 
26
24
  should "add a subscriber without custom fields" do
27
- stub_post(@api_key, "subscribers/#{@list_id}.json", "add_subscriber.json")
28
- email_address = CreateSend::Subscriber.add @list_id, "subscriber@example.com", "Subscriber", [], true
25
+ stub_post(@auth, "subscribers/#{@list_id}.json", "add_subscriber.json")
26
+ email_address = CreateSend::Subscriber.add @auth, @list_id, "subscriber@example.com", "Subscriber", [], true
29
27
  email_address.should == "subscriber@example.com"
30
28
  end
31
29
 
32
30
  should "add a subscriber with custom fields" do
33
- stub_post(@api_key, "subscribers/#{@list_id}.json", "add_subscriber.json")
31
+ stub_post(@auth, "subscribers/#{@list_id}.json", "add_subscriber.json")
34
32
  custom_fields = [ { :Key => 'website', :Value => 'http://example.com/' } ]
35
- email_address = CreateSend::Subscriber.add @list_id, "subscriber@example.com", "Subscriber", custom_fields, true
33
+ email_address = CreateSend::Subscriber.add @auth, @list_id, "subscriber@example.com", "Subscriber", custom_fields, true
36
34
  email_address.should == "subscriber@example.com"
37
35
  end
38
36
 
39
37
  should "add a subscriber with custom fields including multi-option fields" do
40
- stub_post(@api_key, "subscribers/#{@list_id}.json", "add_subscriber.json")
38
+ stub_post(@auth, "subscribers/#{@list_id}.json", "add_subscriber.json")
41
39
  custom_fields = [ { :Key => 'multioptionselectone', :Value => 'myoption' },
42
40
  { :Key => 'multioptionselectmany', :Value => 'firstoption' },
43
41
  { :Key => 'multioptionselectmany', :Value => 'secondoption' } ]
44
- email_address = CreateSend::Subscriber.add @list_id, "subscriber@example.com", "Subscriber", custom_fields, true
42
+ email_address = CreateSend::Subscriber.add @auth, @list_id, "subscriber@example.com", "Subscriber", custom_fields, true
45
43
  email_address.should == "subscriber@example.com"
46
44
  end
47
45
 
48
46
  should "update a subscriber with custom fields" do
49
47
  email = "subscriber@example.com"
50
48
  new_email = "new_email_address@example.com"
51
- stub_put(@api_key, "subscribers/#{@list_id}.json?email=#{CGI.escape(email)}", nil)
49
+ stub_put(@auth, "subscribers/#{@list_id}.json?email=#{CGI.escape(email)}", nil)
52
50
  custom_fields = [ { :Key => 'website', :Value => 'http://example.com/' } ]
53
51
  @subscriber.update new_email, "Subscriber", custom_fields, true
54
52
  @subscriber.email_address.should == new_email
@@ -57,20 +55,20 @@ class SubscriberTest < Test::Unit::TestCase
57
55
  should "update a subscriber with custom fields including the clear option" do
58
56
  email = "subscriber@example.com"
59
57
  new_email = "new_email_address@example.com"
60
- stub_put(@api_key, "subscribers/#{@list_id}.json?email=#{CGI.escape(email)}", nil)
58
+ stub_put(@auth, "subscribers/#{@list_id}.json?email=#{CGI.escape(email)}", nil)
61
59
  custom_fields = [ { :Key => 'website', :Value => '', :Clear => true } ]
62
60
  @subscriber.update new_email, "Subscriber", custom_fields, true
63
61
  @subscriber.email_address.should == new_email
64
62
  end
65
63
 
66
64
  should "import many subscribers at once" do
67
- stub_post(@api_key, "subscribers/#{@list_id}/import.json", "import_subscribers.json")
65
+ stub_post(@auth, "subscribers/#{@list_id}/import.json", "import_subscribers.json")
68
66
  subscribers = [
69
67
  { :EmailAddress => "example+1@example.com", :Name => "Example One" },
70
68
  { :EmailAddress => "example+2@example.com", :Name => "Example Two" },
71
69
  { :EmailAddress => "example+3@example.com", :Name => "Example Three" },
72
70
  ]
73
- import_result = CreateSend::Subscriber.import @list_id, subscribers, true
71
+ import_result = CreateSend::Subscriber.import @auth, @list_id, subscribers, true
74
72
  import_result.FailureDetails.size.should == 0
75
73
  import_result.TotalUniqueEmailsSubmitted.should == 3
76
74
  import_result.TotalExistingSubscribers.should == 0
@@ -79,13 +77,13 @@ class SubscriberTest < Test::Unit::TestCase
79
77
  end
80
78
 
81
79
  should "import many subscribers at once, and start subscription-based autoresponders" do
82
- stub_post(@api_key, "subscribers/#{@list_id}/import.json", "import_subscribers.json")
80
+ stub_post(@auth, "subscribers/#{@list_id}/import.json", "import_subscribers.json")
83
81
  subscribers = [
84
82
  { :EmailAddress => "example+1@example.com", :Name => "Example One" },
85
83
  { :EmailAddress => "example+2@example.com", :Name => "Example Two" },
86
84
  { :EmailAddress => "example+3@example.com", :Name => "Example Three" },
87
85
  ]
88
- import_result = CreateSend::Subscriber.import @list_id, subscribers, true, true
86
+ import_result = CreateSend::Subscriber.import @auth, @list_id, subscribers, true, true
89
87
  import_result.FailureDetails.size.should == 0
90
88
  import_result.TotalUniqueEmailsSubmitted.should == 3
91
89
  import_result.TotalExistingSubscribers.should == 0
@@ -94,13 +92,13 @@ class SubscriberTest < Test::Unit::TestCase
94
92
  end
95
93
 
96
94
  should "import many subscribers at once with custom fields, including the clear option" do
97
- stub_post(@api_key, "subscribers/#{@list_id}/import.json", "import_subscribers.json")
95
+ stub_post(@auth, "subscribers/#{@list_id}/import.json", "import_subscribers.json")
98
96
  subscribers = [
99
97
  { :EmailAddress => "example+1@example.com", :Name => "Example One", :CustomFields => [ { :Key => 'website', :Value => '', :Clear => true } ] },
100
98
  { :EmailAddress => "example+2@example.com", :Name => "Example Two", :CustomFields => [ { :Key => 'website', :Value => '', :Clear => false } ] },
101
99
  { :EmailAddress => "example+3@example.com", :Name => "Example Three", :CustomFields => [ { :Key => 'website', :Value => '', :Clear => false } ] },
102
100
  ]
103
- import_result = CreateSend::Subscriber.import @list_id, subscribers, true
101
+ import_result = CreateSend::Subscriber.import @auth, @list_id, subscribers, true
104
102
  import_result.FailureDetails.size.should == 0
105
103
  import_result.TotalUniqueEmailsSubmitted.should == 3
106
104
  import_result.TotalExistingSubscribers.should == 0
@@ -110,13 +108,13 @@ class SubscriberTest < Test::Unit::TestCase
110
108
 
111
109
  should "import many subscribers at once with partial success" do
112
110
  # Stub request with 400 Bad Request as the expected response status
113
- stub_post(@api_key, "subscribers/#{@list_id}/import.json", "import_subscribers_partial_success.json", 400)
111
+ stub_post(@auth, "subscribers/#{@list_id}/import.json", "import_subscribers_partial_success.json", 400)
114
112
  subscribers = [
115
113
  { :EmailAddress => "example+1@example", :Name => "Example One" },
116
114
  { :EmailAddress => "example+2@example.com", :Name => "Example Two" },
117
115
  { :EmailAddress => "example+3@example.com", :Name => "Example Three" },
118
116
  ]
119
- import_result = CreateSend::Subscriber.import @list_id, subscribers, true
117
+ import_result = CreateSend::Subscriber.import @auth, @list_id, subscribers, true
120
118
  import_result.FailureDetails.size.should == 1
121
119
  import_result.FailureDetails.first.EmailAddress.should == "example+1@example"
122
120
  import_result.FailureDetails.first.Code.should == 1
@@ -128,12 +126,12 @@ class SubscriberTest < Test::Unit::TestCase
128
126
  end
129
127
 
130
128
  should "unsubscribe a subscriber" do
131
- stub_post(@api_key, "subscribers/#{@subscriber.list_id}/unsubscribe.json", nil)
129
+ stub_post(@auth, "subscribers/#{@subscriber.list_id}/unsubscribe.json", nil)
132
130
  @subscriber.unsubscribe
133
131
  end
134
-
132
+
135
133
  should "get a subscriber's history" do
136
- stub_get(@api_key, "subscribers/#{@subscriber.list_id}/history.json?email=#{CGI.escape(@subscriber.email_address)}", "subscriber_history.json")
134
+ stub_get(@auth, "subscribers/#{@subscriber.list_id}/history.json?email=#{CGI.escape(@subscriber.email_address)}", "subscriber_history.json")
137
135
  history = @subscriber.history
138
136
  history.size.should == 1
139
137
  history.first.Name.should == "Campaign One"
@@ -147,7 +145,7 @@ class SubscriberTest < Test::Unit::TestCase
147
145
  end
148
146
 
149
147
  should "delete a subscriber" do
150
- stub_delete(@api_key, "subscribers/#{@subscriber.list_id}.json?email=#{CGI.escape(@subscriber.email_address)}", nil)
148
+ stub_delete(@auth, "subscribers/#{@subscriber.list_id}.json?email=#{CGI.escape(@subscriber.email_address)}", nil)
151
149
  @subscriber.delete
152
150
  end
153
151
  end