action_sms 0.0.3 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,58 @@
1
+ require 'spec_helper'
2
+
3
+ describe ActionSms::ConnectionAdapters::AbstractAdapter do
4
+ let(:adapter) { ActionSms::ConnectionAdapters::AbstractAdapter.new }
5
+
6
+ # Interesting methods
7
+
8
+ describe "#authenticate" do
9
+ let (:request_params) { {} }
10
+ context "the incoming message's 'authentication_key' query parameter value is the same as the adapter's authentication key" do
11
+ before do
12
+ adapter.authentication_key = "my_secret_key"
13
+ request_params["authentication_key"] = "my_secret_key"
14
+ end
15
+ it "should not be nil" do
16
+ adapter.authenticate(request_params).should_not be_nil
17
+ end
18
+ it "should remove the key from the request params hash" do
19
+ adapter.authenticate(request_params)
20
+ request_params["userfield"].should be_nil
21
+ end
22
+ end
23
+ context "the incoming message's 'authentication_key' query parameter value is different from the adapter's authentication key" do
24
+ before do
25
+ request_params["authentication_key"] = "invalid_key"
26
+ end
27
+ it "should return nil" do
28
+ adapter.authenticate(request_params).should be_nil
29
+ end
30
+ it "should not remove the key from the request params hash" do
31
+ adapter.authenticate(request_params)
32
+ request_params["authentication_key"].should_not be_nil
33
+ end
34
+ end
35
+ end
36
+
37
+ describe "#authentication_key" do
38
+ it "should set the authentication key" do
39
+ adapter.authentication_key = "my_secret_key"
40
+ adapter.authentication_key.should == "my_secret_key"
41
+ end
42
+ end
43
+
44
+ describe "#configuration" do
45
+ it "should set the configuration" do
46
+ adapter.configuration = {:config_key => "some config value"}
47
+ adapter.configuration.should == {:config_key => "some config value"}
48
+ end
49
+ end
50
+
51
+ describe "#use_ssl" do
52
+ it "it should use ssl" do
53
+ adapter.use_ssl = true
54
+ adapter.use_ssl.should == true
55
+ end
56
+ end
57
+ end
58
+
@@ -0,0 +1,259 @@
1
+ require 'spec_helper'
2
+
3
+ describe ActionSms::ConnectionAdapters::SMSGlobalAdapter do
4
+ let(:adapter) { ActionSms::ConnectionAdapters::SMSGlobalAdapter.new }
5
+
6
+ describe "#deliver" do
7
+ let(:sms) { mock("sms").as_null_object }
8
+ before do
9
+ adapter.stub!(:send_http_request)
10
+ end
11
+
12
+ it "should try to send the sms to the correct url" do
13
+ adapter.should_receive(:send_http_request).with(
14
+ "http://smsglobal.com.au/http-api.php",
15
+ anything
16
+ )
17
+ adapter.deliver(sms)
18
+ end
19
+
20
+ it "should try to send the sms with the correct 'action' value" do
21
+ adapter.should_receive(:send_http_request).with(
22
+ anything,
23
+ hash_including(:action => "sendsms")
24
+ )
25
+ adapter.deliver(sms)
26
+ end
27
+
28
+ it "should try to send the sms with the 'user' configuration value" do
29
+ adapter.should_receive(:send_http_request).with(
30
+ anything,
31
+ hash_including(:user)
32
+ )
33
+ adapter.deliver(sms)
34
+ end
35
+
36
+ it "should try to send the sms with the 'password' configuration value" do
37
+ adapter.should_receive(:send_http_request).with(
38
+ anything,
39
+ hash_including(:password)
40
+ )
41
+ adapter.deliver(sms)
42
+ end
43
+
44
+ it "should try to send the sms with the correct 'maxsplit' value" do
45
+ adapter.should_receive(:send_http_request).with(
46
+ anything,
47
+ hash_including(:maxsplit => "19")
48
+ )
49
+ adapter.deliver(sms)
50
+ end
51
+
52
+ context "sms' responds to '#from'" do
53
+ before do
54
+ sms.stub!(:from).and_return("anybody")
55
+ end
56
+ it "should try to send the sms with the result from 'sms#from'" do
57
+ adapter.should_receive(:send_http_request).with(
58
+ anything,
59
+ hash_including(:from => "anybody")
60
+ )
61
+ adapter.deliver(sms)
62
+ end
63
+ end
64
+
65
+ context "sms does not respond to '#from'" do
66
+ before do
67
+ sms.stub!(:respond_to?).and_return(false)
68
+ end
69
+ it "should try to send the sms with 'from' set to 'reply2email'" do
70
+ adapter.should_receive(:send_http_request).with(
71
+ anything,
72
+ hash_including(:from => "reply2email")
73
+ )
74
+ adapter.deliver(sms)
75
+ end
76
+ end
77
+
78
+ it "should try to send the sms with the result from 'sms#recipient'" do
79
+ adapter.should_receive(:send_http_request).with(
80
+ anything,
81
+ hash_including(:to)
82
+ )
83
+ adapter.deliver(sms)
84
+ end
85
+
86
+ context "sms#body is not nil" do
87
+ it "should try to send the sms with the sms' body" do
88
+ adapter.should_receive(:send_http_request).with(
89
+ anything,
90
+ hash_including(:text)
91
+ )
92
+ adapter.deliver(sms)
93
+ end
94
+ end
95
+
96
+ context "sms#body is nil" do
97
+ before do
98
+ sms.stub!(:body).and_return(nil)
99
+ end
100
+
101
+ it "should try to send the sms with blank text" do
102
+ adapter.should_receive(:send_http_request).with(
103
+ anything,
104
+ hash_including(:text => "")
105
+ )
106
+ adapter.deliver(sms)
107
+ end
108
+ end
109
+
110
+ context "sms responds to #userfield" do
111
+ it "should try to send the sms with the result from 'sms#userfield'" do
112
+ adapter.should_receive(:send_http_request).with(
113
+ anything,
114
+ hash_including(:userfield)
115
+ )
116
+ adapter.deliver(sms)
117
+ end
118
+ end
119
+
120
+ context "sms does not respond to #userfield" do
121
+ before do
122
+ sms.stub!(:respond_to?).and_return(false)
123
+ end
124
+ it "should try to send the sms with the 'userfield' parameter" do
125
+ adapter.should_receive(:send_http_request).with(
126
+ anything,
127
+ hash_not_including(:userfield)
128
+ )
129
+ adapter.deliver(sms)
130
+ end
131
+ end
132
+ end
133
+
134
+ describe "#delivery_request_successful?" do
135
+ context "the gateway response was successful" do
136
+ let (:delivery_response) {
137
+ "OK: 0; Sent queued message ID: 86b1a945370734f4 SMSGlobalMsgID: 6942744494999745"
138
+ }
139
+ it "should not return nil" do
140
+ adapter.delivery_request_successful?(delivery_response).should_not be_nil
141
+ end
142
+ end
143
+ context "the gateway response was not successful" do
144
+ let (:delivery_response) { "ERROR: No action requested" }
145
+ it "should return nil" do
146
+ adapter.delivery_request_successful?(delivery_response).should be_nil
147
+ end
148
+ end
149
+ end
150
+
151
+ describe "#message_id" do
152
+ context "argument is a String" do
153
+ context "and includes an SMSGlobal message id" do
154
+ it "should return the message id with an SMSGlobal prefix" do
155
+ adapter.message_id("Blah blah blah SMSGlobalMsgID: 123556blah").should == "SMSGlobalMsgID:123556"
156
+ end
157
+ end
158
+ context "but does not include an SMSGlobal message id" do
159
+ it "should return nil" do
160
+ adapter.message_id("Blah blah blah SMSGlobalMsID: 123556blah").should be_nil
161
+ end
162
+ end
163
+ end
164
+ context "argument is a Hash" do
165
+ context "that includes an SMSGlobal message id" do
166
+ it "should return the message id with the SMSGlobal prefix" do
167
+ adapter.message_id(
168
+ {
169
+ "msgid" => "12345"
170
+ }
171
+ ).should == "SMSGlobalMsgID:12345"
172
+ end
173
+ end
174
+ context "that does not include an SMSGlobal message id" do
175
+ it "should return nil" do
176
+ adapter.message_id(
177
+ {
178
+ "something" => "12345"
179
+ }
180
+ ).should be_nil
181
+ end
182
+ end
183
+ end
184
+ end
185
+
186
+ describe "#message_text" do
187
+ let (:request_params) { {} }
188
+ context "given valid incoming message request params" do
189
+ before do
190
+ request_params["msg"] = "ANYTHING"
191
+ end
192
+ it "should return the message" do
193
+ adapter.message_text(request_params).should_not be_nil
194
+ end
195
+ end
196
+ context "given invalid incoming message request params" do
197
+ it "should return nil" do
198
+ adapter.message_text(request_params).should be_nil
199
+ end
200
+ end
201
+ end
202
+
203
+ describe "#sender" do
204
+ let (:request_params) { {} }
205
+ context "given valid incoming message request params" do
206
+ before do
207
+ request_params["from"] = "ANYTHING"
208
+ end
209
+ it "should return the message" do
210
+ adapter.sender(request_params).should_not be_nil
211
+ end
212
+ end
213
+ context "given invalid incoming message request params" do
214
+ it "should return nil" do
215
+ adapter.sender(request_params).should be_nil
216
+ end
217
+ end
218
+ end
219
+
220
+ describe "#service_url" do
221
+ it "should be the SMS Global service url" do
222
+ adapter.service_url.should == "http://smsglobal.com.au/http-api.php"
223
+ end
224
+ context "#use_ssl=false" do
225
+ before do
226
+ adapter.use_ssl = false
227
+ end
228
+ it "should be 'http'" do
229
+ URI.parse(adapter.service_url).scheme.should == "http"
230
+ end
231
+ end
232
+ context "#use_ssl=true" do
233
+ before do
234
+ adapter.use_ssl = true
235
+ end
236
+ it "should be 'https'" do
237
+ URI.parse(adapter.service_url).scheme.should == "https"
238
+ end
239
+ end
240
+ end
241
+
242
+ describe "#status" do
243
+ let (:delivery_receipt) { {} }
244
+ context "given a valid delivery receipt" do
245
+ before do
246
+ delivery_receipt["dlrstatus"] = "ANYTHING"
247
+ end
248
+ it "should return the delivery status" do
249
+ adapter.status(delivery_receipt).should_not be_nil
250
+ end
251
+ end
252
+ context "given a invalid delivery receipt" do
253
+ it "should return nil" do
254
+ adapter.status(delivery_receipt).should be_nil
255
+ end
256
+ end
257
+ end
258
+ end
259
+
@@ -0,0 +1,204 @@
1
+ require 'spec_helper'
2
+
3
+ describe ActionSms::ConnectionAdapters::SMSGlobalAdapter do
4
+ let (:adapter_name) { "sms_global" }
5
+ context "is not in test mode" do
6
+ let(:adapter) {
7
+ ActionSms::Base.sms_global_connection(
8
+ :adapter => adapter_name
9
+ )
10
+ }
11
+
12
+ it "should not respond to #sample_configuration" do
13
+ adapter.should_not be_respond_to(:sample_configuration)
14
+ end
15
+
16
+ it "should not respond to #sample_delivery_receipt" do
17
+ adapter.should_not be_respond_to(:sample_delivery_receipt)
18
+ end
19
+
20
+ it "should not respond to #sample_delivery_response" do
21
+ adapter.should_not be_respond_to(:sample_delivery_response)
22
+ end
23
+
24
+ it "should not respond to #sample_delivery_response_with_message_id" do
25
+ adapter.should_not be_respond_to(:sample_delivery_response_with_message_id)
26
+ end
27
+
28
+ it "should not respond to #sample_incoming_sms" do
29
+ adapter.should_not be_respond_to(:sample_incoming_sms)
30
+ end
31
+
32
+ it "should not respond to #sample_message_id" do
33
+ adapter.should_not be_respond_to(:sample_message_id)
34
+ end
35
+ end
36
+
37
+ context "is in test mode" do
38
+ let(:adapter) {
39
+ ActionSms::Base.sms_global_connection(
40
+ :adapter => adapter_name,
41
+ :environment => "test"
42
+ )
43
+ }
44
+
45
+ # Additional methods available in test mode
46
+
47
+ describe "#sample_configuration" do
48
+ it "should contain SMSGlobal specific configuration" do
49
+ adapter.sample_configuration.should include(
50
+ :user, :password
51
+ )
52
+ end
53
+ it "should contain the correct adapter name" do
54
+ adapter.sample_configuration.should include(
55
+ :adapter => "sms_global"
56
+ )
57
+ end
58
+ context "with options" do
59
+ context "authentication_key => true" do
60
+ it "should contain an authentication key" do
61
+ adapter.sample_configuration(
62
+ :authentication_key => true
63
+ ).should include(:authentication_key)
64
+ end
65
+ end
66
+ end
67
+ end
68
+
69
+ describe "#sample_delivery_receipt" do
70
+ context "with no options" do
71
+ it "should return the default values" do
72
+ adapter.sample_delivery_receipt.should include(
73
+ "msgid", "dlrstatus", "dlr_err", "donedate"
74
+ )
75
+ end
76
+ end
77
+ context "with options" do
78
+ context "'message_id'" do
79
+ it "should include the option" do
80
+ adapter.sample_delivery_receipt(
81
+ :message_id => "SMSGlobalMsgID:12345"
82
+ ).should include("msgid" => "12345")
83
+ end
84
+ end
85
+ context "'status'" do
86
+ it "should include the option" do
87
+ adapter.sample_delivery_receipt(
88
+ :status => "no good"
89
+ ).should include("dlrstatus" => "no good")
90
+ end
91
+ end
92
+ context "'error'" do
93
+ it "should include the option" do
94
+ adapter.sample_delivery_receipt(
95
+ :error => "some error"
96
+ ).should include("dlr_err" => "some error")
97
+ end
98
+ end
99
+ context "'date'" do
100
+ it "should include the option" do
101
+ adapter.sample_delivery_receipt(
102
+ :date => "today"
103
+ ).should include("donedate" => "today")
104
+ end
105
+ end
106
+ end
107
+ end
108
+
109
+ describe "#sample_delivery_response" do
110
+ context "with no options" do
111
+ it "should return a successful delivery response" do
112
+ adapter.sample_delivery_response.should == "OK: 0; Sent queued message ID: 86b1a945370734f4 SMSGlobalMsgID:6942744494999745"
113
+ end
114
+ end
115
+ context "with options" do
116
+ context ":failed => true" do
117
+ it "should return a failed delivery response" do
118
+ adapter.sample_delivery_response(:failed => true).should == "ERROR: No action requested"
119
+ end
120
+ end
121
+ context "'message_id' => 'anything'" do
122
+ it "should include the option" do
123
+ adapter.sample_delivery_response(:message_id => "12345").should =~ /SMSGlobalMsgID:12345/
124
+ end
125
+ end
126
+ end
127
+ end
128
+
129
+ describe "#sample_delivery_response_with_message_id" do
130
+ it "should call sample_delivery_response with the message id" do
131
+ options = {:my_option => "some option"}
132
+ adapter.should_receive(:sample_message_id).with(
133
+ hash_including(:message_id => "12345")
134
+ )
135
+ adapter.sample_delivery_response_with_message_id(
136
+ "12345", options
137
+ )
138
+ end
139
+ end
140
+
141
+ describe "#sample_incoming_sms" do
142
+ context "with no options" do
143
+ it "should return the default values" do
144
+ adapter.sample_incoming_sms.should include(
145
+ "msg", "to", "from", "date"
146
+ )
147
+ end
148
+ end
149
+ context "with options" do
150
+ context "'authentic'" do
151
+ it "should include an authentication key" do
152
+ adapter.sample_incoming_sms(:authentic => true).should include(
153
+ "authentication_key"
154
+ )
155
+ end
156
+ end
157
+ context "'message'" do
158
+ it "should include the option" do
159
+ adapter.sample_incoming_sms(:message => "hello").should include(
160
+ "msg" => "hello"
161
+ )
162
+ end
163
+ end
164
+ context "'to'" do
165
+ it "should include the option" do
166
+ adapter.sample_incoming_sms(:to => "someone").should include(
167
+ "to" => "someone"
168
+ )
169
+ end
170
+ end
171
+ context "'from'" do
172
+ it "should include the option" do
173
+ adapter.sample_incoming_sms(:from => "anyone").should include(
174
+ "from" => "anyone"
175
+ )
176
+ end
177
+ end
178
+ context "'date'" do
179
+ it "should include the option" do
180
+ adapter.sample_incoming_sms(:date => "today").should include(
181
+ "date" => "today"
182
+ )
183
+ end
184
+ end
185
+ end
186
+ end
187
+
188
+ describe "#sample_message_id" do
189
+ context "with no options" do
190
+ it "should return a default message id" do
191
+ adapter.sample_message_id.should == "SMSGlobalMsgID:6942744494999745"
192
+ end
193
+ end
194
+ context "with options" do
195
+ context "'message_id'" do
196
+ it "should include the option" do
197
+ adapter.sample_message_id(:message_id => "12345").should == "SMSGlobalMsgID:12345"
198
+ end
199
+ end
200
+ end
201
+ end
202
+ end
203
+ end
204
+