signal_api 0.0.1
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 +19 -0
- data/.travis.yml +5 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +43 -0
- data/Rakefile +49 -0
- data/lib/signal_api/carrier.rb +43 -0
- data/lib/signal_api/contact.rb +60 -0
- data/lib/signal_api/core_ext/array.rb +7 -0
- data/lib/signal_api/core_ext/hash.rb +7 -0
- data/lib/signal_api/core_ext/nil_class.rb +7 -0
- data/lib/signal_api/core_ext/string.rb +7 -0
- data/lib/signal_api/coupon_group.rb +47 -0
- data/lib/signal_api/deliver_sms.rb +54 -0
- data/lib/signal_api/exceptions.rb +19 -0
- data/lib/signal_api/list.rb +224 -0
- data/lib/signal_api/mocks/api_mock.rb +70 -0
- data/lib/signal_api/mocks/contact.rb +13 -0
- data/lib/signal_api/mocks/deliver_sms.rb +14 -0
- data/lib/signal_api/mocks/list.rb +19 -0
- data/lib/signal_api/mocks/short_url.rb +9 -0
- data/lib/signal_api/segment.rb +161 -0
- data/lib/signal_api/short_url.rb +56 -0
- data/lib/signal_api/signal_http_api.rb +49 -0
- data/lib/signal_api/util/email_address.rb +10 -0
- data/lib/signal_api/util/phone.rb +37 -0
- data/lib/signal_api/version.rb +3 -0
- data/lib/signal_api.rb +114 -0
- data/signal_api.gemspec +27 -0
- data/test/api/carrier_test.rb +43 -0
- data/test/api/contact_test.rb +93 -0
- data/test/api/coupon_group_test.rb +36 -0
- data/test/api/deliver_sms_test.rb +66 -0
- data/test/api/general_test.rb +26 -0
- data/test/api/list_test.rb +261 -0
- data/test/api/segment_test.rb +144 -0
- data/test/api/short_url_test.rb +50 -0
- data/test/mocks/contact_mock_test.rb +24 -0
- data/test/mocks/deliver_sms_mock_test.rb +21 -0
- data/test/mocks/list_mock_test.rb +33 -0
- data/test/mocks/short_url_mock_test.rb +17 -0
- data/test/test_helper.rb +20 -0
- metadata +248 -0
@@ -0,0 +1,93 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require 'signal_api/mocks/contact'
|
3
|
+
|
4
|
+
class ContactTest < Test::Unit::TestCase
|
5
|
+
|
6
|
+
def setup
|
7
|
+
SignalApi.api_key = 'foobar'
|
8
|
+
FakeWeb.allow_net_connect = false
|
9
|
+
end
|
10
|
+
|
11
|
+
def teardown
|
12
|
+
FakeWeb.clean_registry
|
13
|
+
FakeWeb.allow_net_connect = true
|
14
|
+
end
|
15
|
+
|
16
|
+
should "be able to save a contact with lots of info" do
|
17
|
+
contact = SignalApi::Contact.new('mobile_phone' => '3125551212', 'email_address' => 'bill.johnson@domain.com', 'first_name' => 'bill', 'last_name' => 'johnson', 'zip_code' => '60606')
|
18
|
+
contact.save
|
19
|
+
|
20
|
+
attributes = SignalApi::Contact.mock_method_calls[:save].last[:attributes]
|
21
|
+
assert_equal '3125551212', attributes['mobile_phone']
|
22
|
+
assert_equal 'bill.johnson@domain.com', attributes['email_address']
|
23
|
+
assert_equal 'bill', attributes['first_name']
|
24
|
+
assert_equal 'johnson', attributes['last_name']
|
25
|
+
assert_equal '60606', attributes['zip_code']
|
26
|
+
end
|
27
|
+
|
28
|
+
should "be able to save a contact with mobile_phone and email" do
|
29
|
+
contact = SignalApi::Contact.new('mobile_phone' => '3125551212', 'email_address' => 'bill.johnson@domain.com')
|
30
|
+
contact.save
|
31
|
+
|
32
|
+
attributes = SignalApi::Contact.mock_method_calls[:save].last[:attributes]
|
33
|
+
assert_equal '3125551212', attributes['mobile_phone']
|
34
|
+
assert_equal 'bill.johnson@domain.com', attributes['email_address']
|
35
|
+
end
|
36
|
+
|
37
|
+
|
38
|
+
should "be able to save a contact with mobile_phone and something else" do
|
39
|
+
contact = SignalApi::Contact.new('mobile_phone' => '3125551212', 'first_name' => 'bill')
|
40
|
+
contact.save
|
41
|
+
|
42
|
+
attributes = SignalApi::Contact.mock_method_calls[:save].last[:attributes]
|
43
|
+
assert_equal '3125551212', attributes['mobile_phone']
|
44
|
+
assert_equal 'bill', attributes['first_name']
|
45
|
+
end
|
46
|
+
|
47
|
+
should "be able to save a contact with email and something else" do
|
48
|
+
contact = SignalApi::Contact.new('email_address' => 'bill.johnson@domain.com', 'first_name' => 'bill')
|
49
|
+
contact.save
|
50
|
+
|
51
|
+
attributes = SignalApi::Contact.mock_method_calls[:save].last[:attributes]
|
52
|
+
assert_equal 'bill.johnson@domain.com', attributes['email_address']
|
53
|
+
assert_equal 'bill', attributes['first_name']
|
54
|
+
end
|
55
|
+
|
56
|
+
should "should throw exceptions no parms" do
|
57
|
+
contact = SignalApi::Contact.new()
|
58
|
+
|
59
|
+
assert_raise SignalApi::InvalidParameterException do
|
60
|
+
contact.send(:validate_contact_update)
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
should "should throw exceptions one parm not mobile_phone or email" do
|
65
|
+
contact = SignalApi::Contact.new('first_name' => 'bill')
|
66
|
+
|
67
|
+
assert_raise SignalApi::InvalidParameterException do
|
68
|
+
contact.send(:validate_contact_update)
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
should "should throw exceptions one parm mobile_phone or email" do
|
73
|
+
contact = SignalApi::Contact.new('mobile_phone' => '3125551212')
|
74
|
+
|
75
|
+
assert_raise SignalApi::InvalidParameterException do
|
76
|
+
contact.send(:validate_contact_update)
|
77
|
+
end
|
78
|
+
contact = SignalApi::Contact.new('email_address' => 'bill.johnson@domain.com')
|
79
|
+
|
80
|
+
assert_raise SignalApi::InvalidParameterException do
|
81
|
+
contact.send(:validate_contact_update)
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
should "should throw exceptions two parms no mobile_phone or email" do
|
86
|
+
contact = SignalApi::Contact.new('first_name' => 'bill', 'last_name' => 'johnson')
|
87
|
+
|
88
|
+
assert_raise SignalApi::InvalidParameterException do
|
89
|
+
contact.send(:validate_contact_update)
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class CouponGroupTest < Test::Unit::TestCase
|
4
|
+
|
5
|
+
def setup
|
6
|
+
SignalApi.api_key = 'foobar'
|
7
|
+
FakeWeb.allow_net_connect = false
|
8
|
+
end
|
9
|
+
|
10
|
+
def teardown
|
11
|
+
FakeWeb.clean_registry
|
12
|
+
FakeWeb.allow_net_connect = true
|
13
|
+
end
|
14
|
+
|
15
|
+
should "be able to consume a coupon given a tag and phone number" do
|
16
|
+
body = <<-END
|
17
|
+
<?xml version="1.0" encoding="UTF-8"?>
|
18
|
+
<coupon_code>AB1234</coupon_code>
|
19
|
+
END
|
20
|
+
|
21
|
+
FakeWeb.register_uri(:get, SignalApi.base_uri + '/api/coupon_groups/consume_coupon.xml', :content_type => 'application/xml', :status => ['200', 'Ok'], :body => body)
|
22
|
+
coupon_code = SignalApi::CouponGroup.consume_coupon("tag", "6843456782")
|
23
|
+
assert_equal "AB1234", coupon_code
|
24
|
+
end
|
25
|
+
|
26
|
+
should "should throw exceptions for missing params" do
|
27
|
+
assert_raise SignalApi::InvalidParameterException do
|
28
|
+
SignalApi::CouponGroup.consume_coupon(nil, "3125551212")
|
29
|
+
end
|
30
|
+
|
31
|
+
assert_raise SignalApi::InvalidParameterException do
|
32
|
+
SignalApi::CouponGroup.consume_coupon("asdfa", nil)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
@@ -0,0 +1,66 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class DeliverSmsTest < Test::Unit::TestCase
|
4
|
+
|
5
|
+
def setup
|
6
|
+
FakeWeb.allow_net_connect = false
|
7
|
+
end
|
8
|
+
|
9
|
+
def teardown
|
10
|
+
FakeWeb.clean_registry
|
11
|
+
FakeWeb.allow_net_connect = true
|
12
|
+
end
|
13
|
+
|
14
|
+
should "be able to send a SMS message" do
|
15
|
+
FakeWeb.register_uri(:post, uri, :status => ['200', 'Success'], :body => "Message ID: 7f9e82c0efbc012a166c0030482ef624")
|
16
|
+
deliver_sms = SignalApi::DeliverSms.new('joe', 'joepassword')
|
17
|
+
message_id = deliver_sms.deliver('3125551212', 'This is a test message')
|
18
|
+
assert_equal "7f9e82c0efbc012a166c0030482ef624", message_id
|
19
|
+
end
|
20
|
+
|
21
|
+
should "handle API failures" do
|
22
|
+
FakeWeb.register_uri(:post, uri, :status => ['500', 'Server Error'], :body => "Something bad happened")
|
23
|
+
deliver_sms = SignalApi::DeliverSms.new('joe', 'joepassword')
|
24
|
+
assert_raise SignalApi::ApiException do
|
25
|
+
message_id = deliver_sms.deliver('3125551212', 'This is a test message')
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
should "raise an error if the message is blank" do
|
30
|
+
deliver_sms = SignalApi::DeliverSms.new('joe', 'joepassword')
|
31
|
+
exception = assert_raise SignalApi::InvalidParameterException do
|
32
|
+
deliver_sms.deliver('3125551212', nil)
|
33
|
+
end
|
34
|
+
assert_equal "A message must be provided", exception.message
|
35
|
+
end
|
36
|
+
|
37
|
+
should "raise an error if the mobile phone is blank" do
|
38
|
+
deliver_sms = SignalApi::DeliverSms.new('joe', 'joepassword')
|
39
|
+
exception = assert_raise SignalApi::InvalidParameterException do
|
40
|
+
deliver_sms.deliver(nil, 'This is a test message')
|
41
|
+
end
|
42
|
+
assert_equal "An invalid mobile phone was specified: ", exception.message
|
43
|
+
end
|
44
|
+
|
45
|
+
should "raise an error if the mobile phone is invalid" do
|
46
|
+
deliver_sms = SignalApi::DeliverSms.new('joe', 'joepassword')
|
47
|
+
exception = assert_raise SignalApi::InvalidParameterException do
|
48
|
+
deliver_sms.deliver("foo", 'This is a test message')
|
49
|
+
end
|
50
|
+
assert_equal "An invalid mobile phone was specified: foo", exception.message
|
51
|
+
end
|
52
|
+
|
53
|
+
should "raise an error if the username and password were not specified" do
|
54
|
+
exception = assert_raise SignalApi::InvalidParameterException do
|
55
|
+
SignalApi::DeliverSms.new('joe', nil)
|
56
|
+
end
|
57
|
+
assert_equal "username and password must be provided", exception.message
|
58
|
+
end
|
59
|
+
|
60
|
+
private
|
61
|
+
|
62
|
+
def uri
|
63
|
+
%r|https://joe:joepassword@api.imws.us/messages/send|
|
64
|
+
end
|
65
|
+
|
66
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class ShortUrlTest < Test::Unit::TestCase
|
4
|
+
|
5
|
+
should "raise an exception if the api_key is nil" do
|
6
|
+
SignalApi.api_key = nil
|
7
|
+
assert_raise SignalApi::InvalidApiKeyException do
|
8
|
+
SignalApi::ShortUrl.create("http://www.google.com", "ix.ly")
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
should "raise an exception if the api_key is blank" do
|
13
|
+
SignalApi.api_key = ' '
|
14
|
+
assert_raise SignalApi::InvalidApiKeyException do
|
15
|
+
SignalApi::ShortUrl.create("http://www.google.com", "ix.ly")
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
should "raise an exception if authentication failed" do
|
20
|
+
FakeWeb.register_uri(:post, SignalApi.base_uri + '/api/short_urls.xml', :content_type => 'application/xml', :status => ['401', 'Unauthorized'])
|
21
|
+
assert_raise SignalApi::AuthFailedException do
|
22
|
+
short_url = SignalApi::ShortUrl.create("http://www.google.com", "ix.ly")
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
@@ -0,0 +1,261 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class ListTest < Test::Unit::TestCase
|
4
|
+
|
5
|
+
def setup
|
6
|
+
SignalApi.api_key = 'foobar'
|
7
|
+
FakeWeb.allow_net_connect = false
|
8
|
+
|
9
|
+
@list = SignalApi::List.new(1)
|
10
|
+
end
|
11
|
+
|
12
|
+
def teardown
|
13
|
+
FakeWeb.clean_registry
|
14
|
+
FakeWeb.allow_net_connect = true
|
15
|
+
end
|
16
|
+
|
17
|
+
should "not be able to create a List with a nil list id" do
|
18
|
+
assert_raise SignalApi::InvalidParameterException do
|
19
|
+
SignalApi::List.new(nil)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
#
|
24
|
+
# create_subscription
|
25
|
+
#
|
26
|
+
should "raise an error if trying to create a subscription with an invalid subscription type" do
|
27
|
+
exception = assert_raise SignalApi::InvalidParameterException do
|
28
|
+
@list.create_subscription("foo", SignalApi::Contact.new)
|
29
|
+
end
|
30
|
+
assert_equal "Invalid subscription type", exception.message
|
31
|
+
end
|
32
|
+
|
33
|
+
should "raise an error if calling create_subscription and no contact was provided" do
|
34
|
+
exception = assert_raise SignalApi::InvalidParameterException do
|
35
|
+
@list.create_subscription(SignalApi::SubscriptionType::SMS, nil)
|
36
|
+
end
|
37
|
+
assert_equal "A contact must be provided", exception.message
|
38
|
+
end
|
39
|
+
|
40
|
+
should "not be able to create a SMS subscription without a mobile phone number" do
|
41
|
+
exception = assert_raise SignalApi::InvalidMobilePhoneException do
|
42
|
+
@list.create_subscription(SignalApi::SubscriptionType::SMS, SignalApi::Contact.new('first-name' => 'John'))
|
43
|
+
end
|
44
|
+
assert_equal "A valid mobile phone number required for SMS subscriptions", exception.message
|
45
|
+
end
|
46
|
+
|
47
|
+
should "not be able to create a SMS subscription with an invalid mobile phone number" do
|
48
|
+
exception = assert_raise SignalApi::InvalidMobilePhoneException do
|
49
|
+
@list.create_subscription(SignalApi::SubscriptionType::SMS, SignalApi::Contact.new('mobile-phone' => '1234', 'first-name' => 'John'))
|
50
|
+
end
|
51
|
+
assert_equal "A valid mobile phone number required for SMS subscriptions", exception.message
|
52
|
+
end
|
53
|
+
|
54
|
+
should "not be able to create an email subscription without an email address" do
|
55
|
+
exception = assert_raise SignalApi::InvalidParameterException do
|
56
|
+
@list.create_subscription(SignalApi::SubscriptionType::EMAIL, SignalApi::Contact.new('first-name' => 'John'))
|
57
|
+
end
|
58
|
+
assert_equal "A valid email address required for EMAIL subscriptions", exception.message
|
59
|
+
end
|
60
|
+
|
61
|
+
should "not be able to create an email subscription with an invalid email address" do
|
62
|
+
exception = assert_raise SignalApi::InvalidParameterException do
|
63
|
+
@list.create_subscription(SignalApi::SubscriptionType::EMAIL, SignalApi::Contact.new('email-address' => 'foo', 'first-name' => 'John'))
|
64
|
+
end
|
65
|
+
assert_equal "A valid email address required for EMAIL subscriptions", exception.message
|
66
|
+
end
|
67
|
+
|
68
|
+
should "be able to create a new subscription" do
|
69
|
+
FakeWeb.register_uri(:post, SignalApi.base_uri + '/api/subscription_campaigns/1/subscriptions.xml', :status => ['200', 'Success'])
|
70
|
+
@list.create_subscription(SignalApi::SubscriptionType::SMS, SignalApi::Contact.new('mobile-phone' => '3125551212', 'first-name' => 'John'), :source_keyword => 'FOO')
|
71
|
+
end
|
72
|
+
|
73
|
+
should "returns false if the subscription could not be created" do
|
74
|
+
FakeWeb.register_uri(:post, SignalApi.base_uri + '/api/subscription_campaigns/1/subscriptions.xml', :status => ['422', 'Bad Request'], :body => <<-END)
|
75
|
+
<?xml version="1.0" encoding="UTF-8"?>
|
76
|
+
<error>
|
77
|
+
<request>/api/subscription_campaigns/1/subscriptions.xml</request>
|
78
|
+
<message>You are already signed up to the TEST mobile subscription list. To opt-out, send the text message 'STOP TEST' to 839863.</message>
|
79
|
+
</error>
|
80
|
+
END
|
81
|
+
assert_equal false, @list.create_subscription(SignalApi::SubscriptionType::SMS, SignalApi::Contact.new('mobile-phone' => '3125551212', 'first-name' => 'John'), :source_keyword => 'FOO')
|
82
|
+
end
|
83
|
+
|
84
|
+
should "returns false if the subscription could not be created because of recent unsubscribe" do
|
85
|
+
FakeWeb.register_uri(:post, SignalApi.base_uri + '/api/subscription_campaigns/1/subscriptions.xml', :status => ['422', 'Bad Request'], :body => <<-END)
|
86
|
+
<?xml version="1.0" encoding="UTF-8"?>
|
87
|
+
<error>
|
88
|
+
<request>/api/subscription_campaigns/1/subscriptions.xml</request>
|
89
|
+
<message>Subscriber cannot be re-added since they have unsubscribed within the past 30 days</message>
|
90
|
+
</error>
|
91
|
+
END
|
92
|
+
assert_equal false, @list.create_subscription(SignalApi::SubscriptionType::SMS, SignalApi::Contact.new('mobile-phone' => '3125551212', 'first-name' => 'John'), :source_keyword => 'FOO')
|
93
|
+
end
|
94
|
+
|
95
|
+
should "returns false if the subscription already exists but was unconfirmed" do
|
96
|
+
FakeWeb.register_uri(:post, SignalApi.base_uri + '/api/subscription_campaigns/1/subscriptions.xml', :status => ['422', 'Bad Request'], :body => <<-END)
|
97
|
+
<?xml version="1.0" encoding="UTF-8"?>
|
98
|
+
<error>
|
99
|
+
<request>/api/subscription_campaigns/1/subscriptions.xml</request>
|
100
|
+
<message>User already subscribed, resending confirmation message: to confirm reply y</message>
|
101
|
+
</error>
|
102
|
+
END
|
103
|
+
assert_equal false, @list.create_subscription(SignalApi::SubscriptionType::SMS, SignalApi::Contact.new('mobile-phone' => '3125551212', 'first-name' => 'John'), :source_keyword => 'FOO')
|
104
|
+
end
|
105
|
+
|
106
|
+
should "raise an exception if the subscription could not be created due to invalid mobile" do
|
107
|
+
FakeWeb.register_uri(:post, SignalApi.base_uri + '/api/subscription_campaigns/1/subscriptions.xml', :status => ['422', 'Bad Request'], :body => <<-END)
|
108
|
+
<?xml version="1.0" encoding="UTF-8"?>
|
109
|
+
<error>
|
110
|
+
<request>/api/subscription_campaigns/1/subscriptions.xml</request>
|
111
|
+
<message>Could not find the carrier for mobile phone 3125551212</message>
|
112
|
+
</error>
|
113
|
+
END
|
114
|
+
assert_raise SignalApi::InvalidMobilePhoneException do
|
115
|
+
@list.create_subscription(SignalApi::SubscriptionType::SMS, SignalApi::Contact.new('mobile-phone' => '3125551212', 'first-name' => 'John'), :source_keyword => 'FOO')
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
should "not retry the request if it failed because the mobile phone is invalid" do
|
120
|
+
begin
|
121
|
+
SignalApi.retries = 3
|
122
|
+
response = stub(:code => 422, :body => <<-END)
|
123
|
+
<?xml version="1.0" encoding="UTF-8"?>
|
124
|
+
<error>
|
125
|
+
<request>/api/subscription_campaigns/1/subscriptions.xml</request>
|
126
|
+
<message>Could not find the carrier for mobile phone 3125551212</message>
|
127
|
+
</error>
|
128
|
+
END
|
129
|
+
SignalApi::List.expects(:post).returns(response).once
|
130
|
+
assert_raise SignalApi::InvalidMobilePhoneException do
|
131
|
+
@list.create_subscription(SignalApi::SubscriptionType::SMS, SignalApi::Contact.new('mobile-phone' => '3125551212', 'first-name' => 'John'), :source_keyword => 'FOO')
|
132
|
+
end
|
133
|
+
ensure
|
134
|
+
SignalApi.retries = 0
|
135
|
+
end
|
136
|
+
end
|
137
|
+
|
138
|
+
#
|
139
|
+
# destroy_subscription
|
140
|
+
#
|
141
|
+
should "raise an error if trying to destroy a subscription with an invalid subscription type" do
|
142
|
+
exception = assert_raise SignalApi::InvalidParameterException do
|
143
|
+
@list.destroy_subscription("foo", SignalApi::Contact.new)
|
144
|
+
end
|
145
|
+
assert_equal "Invalid subscription type", exception.message
|
146
|
+
end
|
147
|
+
|
148
|
+
should "raise an error if calling destroy_subscription andno contact was provided" do
|
149
|
+
exception = assert_raise SignalApi::InvalidParameterException do
|
150
|
+
@list.destroy_subscription(SignalApi::SubscriptionType::SMS, nil)
|
151
|
+
end
|
152
|
+
assert_equal "A contact must be provided", exception.message
|
153
|
+
end
|
154
|
+
|
155
|
+
should "not be able to destroy an SMS subscription without a mobile phone number" do
|
156
|
+
exception = assert_raise SignalApi::InvalidMobilePhoneException do
|
157
|
+
@list.destroy_subscription(SignalApi::SubscriptionType::SMS, SignalApi::Contact.new('first-name' => 'John'))
|
158
|
+
end
|
159
|
+
assert_equal "A valid mobile phone number required for SMS subscriptions", exception.message
|
160
|
+
end
|
161
|
+
|
162
|
+
should "not be able to destroy an SMS subscription with an invalid mobile phone number" do
|
163
|
+
exception = assert_raise SignalApi::InvalidMobilePhoneException do
|
164
|
+
@list.create_subscription(SignalApi::SubscriptionType::SMS, SignalApi::Contact.new('mobile-phone' => '1234', 'first-name' => 'John'))
|
165
|
+
end
|
166
|
+
assert_equal "A valid mobile phone number required for SMS subscriptions", exception.message
|
167
|
+
end
|
168
|
+
|
169
|
+
should "not be able to destroy an email subscription without an email address" do
|
170
|
+
exception = assert_raise SignalApi::InvalidParameterException do
|
171
|
+
@list.destroy_subscription(SignalApi::SubscriptionType::EMAIL, SignalApi::Contact.new('first-name' => 'John'))
|
172
|
+
end
|
173
|
+
assert_equal "A valid email address required for EMAIL subscriptions", exception.message
|
174
|
+
end
|
175
|
+
|
176
|
+
should "not be able to destroy an email subscription with an invalid email address" do
|
177
|
+
exception = assert_raise SignalApi::InvalidParameterException do
|
178
|
+
@list.destroy_subscription(SignalApi::SubscriptionType::EMAIL, SignalApi::Contact.new('email-address' => 'foo', 'first-name' => 'John'))
|
179
|
+
end
|
180
|
+
assert_equal "A valid email address required for EMAIL subscriptions", exception.message
|
181
|
+
end
|
182
|
+
|
183
|
+
should "be able to destroy a subscription" do
|
184
|
+
FakeWeb.register_uri(:delete, SignalApi.base_uri + '/api/subscription_campaigns/1/subscriptions/3125551212.xml', :status => ['200', 'Success'])
|
185
|
+
@list.destroy_subscription(SignalApi::SubscriptionType::SMS, SignalApi::Contact.new('mobile-phone' => '3125551212', 'first-name' => 'John'))
|
186
|
+
end
|
187
|
+
|
188
|
+
should "return false if the subscription could not be destroyed because user not subscribed" do
|
189
|
+
FakeWeb.register_uri(:delete, SignalApi.base_uri + '/api/subscription_campaigns/1/subscriptions/3125551212.xml', :status => ['422', 'Bad Request'], :body => <<-END)
|
190
|
+
<?xml version="1.0" encoding="UTF-8"?>
|
191
|
+
<error>
|
192
|
+
<request>/api/subscription_campaigns/1/3125551212.xml</request>
|
193
|
+
<message>3125551212 is not subscribed to campaign ID 1</message>
|
194
|
+
</error>
|
195
|
+
END
|
196
|
+
assert_equal false, @list.destroy_subscription(SignalApi::SubscriptionType::SMS, SignalApi::Contact.new('mobile-phone' => '3125551212', 'first-name' => 'John'))
|
197
|
+
end
|
198
|
+
|
199
|
+
should "return false if the subscription could not be destroyed because invalid user" do
|
200
|
+
FakeWeb.register_uri(:delete, SignalApi.base_uri + '/api/subscription_campaigns/1/subscriptions/3125551212.xml', :status => ['422', 'Bad Request'], :body => <<-END)
|
201
|
+
<?xml version="1.0" encoding="UTF-8"?>
|
202
|
+
<error>
|
203
|
+
<request>/api/subscription_campaigns/1/3125551212.xml</request>
|
204
|
+
<message>Invalid user ID: 3125551212</message>
|
205
|
+
</error>
|
206
|
+
END
|
207
|
+
assert_equal false, @list.destroy_subscription(SignalApi::SubscriptionType::SMS, SignalApi::Contact.new('mobile-phone' => '3125551212', 'first-name' => 'John'))
|
208
|
+
end
|
209
|
+
|
210
|
+
#
|
211
|
+
# send_message
|
212
|
+
#
|
213
|
+
should "raise an error if trying to send a mesasge without a description" do
|
214
|
+
exception = assert_raise SignalApi::InvalidParameterException do
|
215
|
+
@list.send_message(nil, "This is the message")
|
216
|
+
end
|
217
|
+
assert_equal "A description must be provided", exception.message
|
218
|
+
end
|
219
|
+
|
220
|
+
should "raise an error if trying to send a mesasge without a message" do
|
221
|
+
exception = assert_raise SignalApi::InvalidParameterException do
|
222
|
+
@list.send_message("This is the description", "")
|
223
|
+
end
|
224
|
+
assert_equal "A text message must be provided", exception.message
|
225
|
+
end
|
226
|
+
|
227
|
+
should "raise an error if trying to send a mesasge greater than 160 characters" do
|
228
|
+
message = 161.times.inject("") { |msg, i| msg << "a" }
|
229
|
+
exception = assert_raise SignalApi::InvalidParameterException do
|
230
|
+
@list.send_message("This is the description", message)
|
231
|
+
end
|
232
|
+
assert_equal "The text message must not be greater than 160 characters", exception.message
|
233
|
+
end
|
234
|
+
|
235
|
+
should "be able to send a message to a subscription list" do
|
236
|
+
FakeWeb.register_uri(:post, SignalApi.base_uri + '/api/subscription_campaigns/1/send_message.xml', :status => ['200', 'Success'], :body => <<-END)
|
237
|
+
<?xml version="1.0" encoding="UTF-8"?>
|
238
|
+
<scheduled-message>
|
239
|
+
<id type="integer">101</id>
|
240
|
+
</scheduled-message>
|
241
|
+
END
|
242
|
+
message_id = @list.send_message("Some description", "A message", :send_at => Time.local(2012, 4, 17, 10, 05, 07), :segment_id => 7, :tags => [1, 2],
|
243
|
+
:carrier_overrides => [SignalApi::CarrierOverrideMessage.new(2, "some override message")])
|
244
|
+
assert_equal 101, message_id
|
245
|
+
end
|
246
|
+
|
247
|
+
should "raise an exception if unable to send the message successfully" do
|
248
|
+
FakeWeb.register_uri(:post, SignalApi.base_uri + '/api/subscription_campaigns/1/send_message.xml', :status => ['422', 'Bad Request'], :body => <<-END)
|
249
|
+
<?xml version="1.0" encoding="UTF-8"?>
|
250
|
+
<error>
|
251
|
+
<request>/api/subscription_campaigns/1/send_message.xml</request>
|
252
|
+
<message>No segment could be found with an id of 404</message>
|
253
|
+
</error>
|
254
|
+
END
|
255
|
+
assert_raise SignalApi::ApiException do
|
256
|
+
@list.send_message("Some description", "A message", :send_at => Time.local(2012, 4, 17, 10, 05, 07), :segment_id => 404,
|
257
|
+
:carrier_overrides => [SignalApi::CarrierOverrideMessage.new(2, "some override message")])
|
258
|
+
end
|
259
|
+
end
|
260
|
+
|
261
|
+
end
|
@@ -0,0 +1,144 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class SegmentTest < Test::Unit::TestCase
|
4
|
+
|
5
|
+
def setup
|
6
|
+
SignalApi.api_key = 'foobar'
|
7
|
+
FakeWeb.allow_net_connect = false
|
8
|
+
end
|
9
|
+
|
10
|
+
def teardown
|
11
|
+
FakeWeb.clean_registry
|
12
|
+
FakeWeb.allow_net_connect = true
|
13
|
+
end
|
14
|
+
|
15
|
+
#
|
16
|
+
# SegmentUser
|
17
|
+
#
|
18
|
+
should "raise an error if trying to create a SegmentUser with an identifying attribute that is not a mobile phone number or email address" do
|
19
|
+
assert_raise SignalApi::InvalidParameterException do
|
20
|
+
SignalApi::SegmentUser.new("foo")
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
should "be able to create a SegmentUser with a mobile phone and user data" do
|
25
|
+
segment_user = SignalApi::SegmentUser.new("3125551212", :foo_1 => 'bar_1', :foo_2 => 'bar_2')
|
26
|
+
assert_equal "3125551212", segment_user.mobile_phone
|
27
|
+
assert_equal Hash[:foo_1 => 'bar_1', :foo_2 => 'bar_2'], segment_user.user_data
|
28
|
+
end
|
29
|
+
|
30
|
+
should "be able to create a SegmentUser with an email address and user data" do
|
31
|
+
segment_user = SignalApi::SegmentUser.new("john@test.com", :foo_1 => 'bar_1', :foo_2 => 'bar_2')
|
32
|
+
assert_equal "john@test.com", segment_user.email_address
|
33
|
+
assert_equal Hash[:foo_1 => 'bar_1', :foo_2 => 'bar_2'], segment_user.user_data
|
34
|
+
end
|
35
|
+
|
36
|
+
#
|
37
|
+
# create
|
38
|
+
#
|
39
|
+
should "raise an error if any of the required parameters are missing" do
|
40
|
+
assert_raise(SignalApi::InvalidParameterException) { SignalApi::Segment.create(nil, "description", SignalApi::SegmentType::STATIC) }
|
41
|
+
assert_raise(SignalApi::InvalidParameterException) { SignalApi::Segment.create("", "description", SignalApi::SegmentType::STATIC) }
|
42
|
+
assert_raise(SignalApi::InvalidParameterException) { SignalApi::Segment.create("name", nil, SignalApi::SegmentType::STATIC) }
|
43
|
+
assert_raise(SignalApi::InvalidParameterException) { SignalApi::Segment.create("name", "", SignalApi::SegmentType::STATIC) }
|
44
|
+
assert_raise(SignalApi::InvalidParameterException) { SignalApi::Segment.create("name", "description", nil) }
|
45
|
+
end
|
46
|
+
|
47
|
+
should "raise an exception if an invalid segment type is provided" do
|
48
|
+
assert_raise(SignalApi::InvalidParameterException) { SignalApi::Segment.create("name", "description", "foo") }
|
49
|
+
end
|
50
|
+
|
51
|
+
should "be able to create a segment" do
|
52
|
+
FakeWeb.register_uri(:post, SignalApi.base_uri + '/api/filter_groups/create.xml', :content_type => 'application/xml', :status => ['200', 'Success'], :body => <<-END)
|
53
|
+
<?xml version="1.0" encoding="UTF-8"?>
|
54
|
+
<subscription-list-filter-group>
|
55
|
+
<name>Segment 1</name>
|
56
|
+
<created-at type="datetime">2012-04-13T18:58:50Z</created-at>
|
57
|
+
<updated-at type="datetime">2012-04-13T18:58:50Z</updated-at>
|
58
|
+
<account-id type="integer">1</account-id>
|
59
|
+
<id type="integer">25</id>
|
60
|
+
<filter-group-type-id type="integer">2</filter-group-type-id>
|
61
|
+
<description>Test segment 1</description>
|
62
|
+
<blast-only type="boolean">false</blast-only>
|
63
|
+
<active type="boolean">true</active>
|
64
|
+
</subscription-list-filter-group>
|
65
|
+
END
|
66
|
+
segment = SignalApi::Segment.create("Segment 1", "Test segment 1", SignalApi::SegmentType::STATIC)
|
67
|
+
assert_equal 25, segment.id
|
68
|
+
assert_equal "Segment 1", segment.name
|
69
|
+
assert_equal "Test segment 1", segment.description
|
70
|
+
assert_equal SignalApi::SegmentType::STATIC, segment.segment_type
|
71
|
+
assert_equal 1, segment.account_id
|
72
|
+
end
|
73
|
+
|
74
|
+
should "raise an exception if unable to create the segment" do
|
75
|
+
FakeWeb.register_uri(:post, SignalApi.base_uri + '/api/filter_groups/create.xml', :content_type => 'application/xml', :status => ['422', 'Unprocessable Entity'], :body => <<-END)
|
76
|
+
<errors><error>name^A filter group with this name already exists</error></errors>
|
77
|
+
END
|
78
|
+
assert_raise SignalApi::ApiException do
|
79
|
+
SignalApi::Segment.create("Segment 1", "Test segment 1", SignalApi::SegmentType::STATIC)
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
#
|
84
|
+
# add_users
|
85
|
+
#
|
86
|
+
should "raise an error if no segment users were passed in" do
|
87
|
+
assert_raise SignalApi::InvalidParameterException do
|
88
|
+
segment = SignalApi::Segment.new(1)
|
89
|
+
segment.add_users(nil)
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
should "be able to add a collection of segment users to a static segment" do
|
94
|
+
FakeWeb.register_uri(:post, SignalApi.base_uri + '/api/filter_segments/1/update.xml', :content_type => 'application/xml', :status => ['200', 'Success'], :body => <<-END)
|
95
|
+
<subscription_list_segment_results>
|
96
|
+
<users_not_found>
|
97
|
+
<user_not_found>user with email john@test.com not found</user_not_found>
|
98
|
+
<user_not_found>user with email bill@test.com not found</user_not_found>
|
99
|
+
</users_not_found>
|
100
|
+
<total_users_processed>4</total_users_processed>
|
101
|
+
<total_users_added>2</total_users_added>
|
102
|
+
<total_users_not_found>2</total_users_not_found>
|
103
|
+
<total_duplicate_users>0</total_duplicate_users>
|
104
|
+
</subscription_list_segment_results>
|
105
|
+
END
|
106
|
+
|
107
|
+
segment_users = [
|
108
|
+
SignalApi::SegmentUser.new("3125551212", :foo_1 => 'bar 1', :foo_2 => 'bar 2'),
|
109
|
+
SignalApi::SegmentUser.new("john@test.com", :foo_3 => 'bar 3', :foo_4 => 'bar 4'),
|
110
|
+
SignalApi::SegmentUser.new("3125551213"),
|
111
|
+
SignalApi::SegmentUser.new("bill@test.com")
|
112
|
+
]
|
113
|
+
|
114
|
+
segment = SignalApi::Segment.new(1)
|
115
|
+
results = segment.add_users(segment_users)
|
116
|
+
assert_equal 2, results[:total_users_added]
|
117
|
+
assert_equal 2, results[:total_users_not_found]
|
118
|
+
assert_equal 0, results[:total_duplicate_users]
|
119
|
+
assert_equal 4, results[:total_users_processed]
|
120
|
+
end
|
121
|
+
|
122
|
+
should "raise an exception if unable to add users to the segment" do
|
123
|
+
FakeWeb.register_uri(:post, SignalApi.base_uri + '/api/filter_segments/1/update.xml', :content_type => 'application/xml', :status => ['422', 'Success'], :body => <<-END)
|
124
|
+
<?xml version="1.0" encoding="UTF-8"?>
|
125
|
+
<error>
|
126
|
+
<request>http://textme.dev/api/filter_segments/63/update.xml</request>
|
127
|
+
<message>trying access invalid filter group</message>
|
128
|
+
</error>
|
129
|
+
END
|
130
|
+
|
131
|
+
segment_users = [
|
132
|
+
SignalApi::SegmentUser.new("3125551212", :foo_1 => 'bar 1', :foo_2 => 'bar 2'),
|
133
|
+
SignalApi::SegmentUser.new("john@test.com", :foo_3 => 'bar 3', :foo_4 => 'bar 4'),
|
134
|
+
SignalApi::SegmentUser.new("3125551213"),
|
135
|
+
SignalApi::SegmentUser.new("bill@test.com")
|
136
|
+
]
|
137
|
+
|
138
|
+
segment = SignalApi::Segment.new(1)
|
139
|
+
assert_raise SignalApi::ApiException do
|
140
|
+
segment.add_users(segment_users)
|
141
|
+
end
|
142
|
+
end
|
143
|
+
|
144
|
+
end
|