4info 1.3.4 → 2.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.
- data/README.markdown +10 -10
- data/VERSION +1 -1
- data/init.rb +1 -1
- data/lib/{templates → 4info_templates}/confirm.haml +0 -0
- data/lib/{templates → 4info_templates}/deliver.haml +0 -0
- data/lib/{templates → 4info_templates}/unblock.haml +0 -0
- data/lib/configuration.rb +3 -1
- data/lib/contactable.rb +45 -60
- data/lib/controller.rb +3 -3
- data/lib/gateway.rb +45 -0
- data/lib/gateway_4info.rb +114 -0
- data/lib/gateway_twilio.rb +45 -0
- data/lib/{four_info.rb → txter.rb} +6 -5
- data/test/gateway_4info_test.rb +75 -0
- data/test/gateway_twilio_test.rb +15 -0
- data/test/test_helper.rb +6 -5
- data/test/{four_info_contactable_test.rb → txter_contactable_test.rb} +51 -73
- data/test/{four_info_controller_test.rb → txter_controller_test.rb} +8 -8
- data/test/txter_module_test.rb +56 -0
- data/{4info.gemspec → txter.gemspec} +7 -7
- metadata +20 -16
- data/lib/4info.rb +0 -3
- data/lib/request.rb +0 -75
- data/lib/response.rb +0 -27
- data/test/four_info_module_test.rb +0 -56
@@ -1,10 +1,12 @@
|
|
1
|
-
module
|
1
|
+
module Txter
|
2
2
|
class << self
|
3
3
|
def gateway
|
4
|
-
|
5
|
-
URI.parse 'http://gateway.4info.net/msg'
|
4
|
+
Txter::Gateway.current
|
6
5
|
end
|
7
6
|
|
7
|
+
delegate :deliver, :to => :gateway
|
8
|
+
delegate :unblock, :to => :gateway
|
9
|
+
|
8
10
|
def log(msg)
|
9
11
|
if defined?(Rails)
|
10
12
|
Rails.logger.info msg
|
@@ -43,7 +45,6 @@ module FourInfo
|
|
43
45
|
end
|
44
46
|
|
45
47
|
require File.join(File.dirname(__FILE__), 'configuration')
|
48
|
+
require File.join(File.dirname(__FILE__), 'gateway')
|
46
49
|
require File.join(File.dirname(__FILE__), 'contactable')
|
47
50
|
require File.join(File.dirname(__FILE__), 'controller')
|
48
|
-
require File.join(File.dirname(__FILE__), 'request')
|
49
|
-
require File.join(File.dirname(__FILE__), 'response')
|
@@ -0,0 +1,75 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), 'test_helper')
|
2
|
+
|
3
|
+
class Gateway4infoTest < ActiveSupport::TestCase
|
4
|
+
|
5
|
+
Error = Txter::Gateway4info::Response.new('<?xml version="1.0" encoding="UTF-8"?>
|
6
|
+
<response>
|
7
|
+
<status>
|
8
|
+
<id>4</id>
|
9
|
+
<message>Error</message>
|
10
|
+
</status>
|
11
|
+
</response>')
|
12
|
+
Success = Txter::Gateway4info::Response.new('<?xml version="1.0" ?>
|
13
|
+
<response>
|
14
|
+
<requestId>F81D4FAE-7DEC-11D0-A765-00A0C91E6BF6</requestId>
|
15
|
+
<status>
|
16
|
+
<id>1</id>
|
17
|
+
<message>Success</message>
|
18
|
+
</status>
|
19
|
+
</response>')
|
20
|
+
|
21
|
+
context "with 4info gateway" do
|
22
|
+
setup {
|
23
|
+
Txter.configure do |config|
|
24
|
+
config.client_id = 12345
|
25
|
+
config.client_key = 'ABC123'
|
26
|
+
config.gateway = '4info'
|
27
|
+
end
|
28
|
+
}
|
29
|
+
context "with stubbed success" do
|
30
|
+
setup {
|
31
|
+
Txter::Gateway4info.stubs(:perform).returns(Success)
|
32
|
+
}
|
33
|
+
should "generate a success response object" do
|
34
|
+
assert Txter.deliver("msg", "1-555-867-5309").success?
|
35
|
+
end
|
36
|
+
end
|
37
|
+
context "with stubbed error" do
|
38
|
+
setup {
|
39
|
+
Txter::Gateway4info.stubs(:perform).returns(Error)
|
40
|
+
}
|
41
|
+
should "generate a success response object" do
|
42
|
+
assert !Txter.deliver("msg", "1-555-867-5309").success?
|
43
|
+
end
|
44
|
+
end
|
45
|
+
should "create proper xml for delivery" do
|
46
|
+
expected = <<-EOXML
|
47
|
+
<?xml version='1.0' encoding='utf-8' ?>
|
48
|
+
<request clientId='12345' clientKey='ABC123' type='MESSAGE'>
|
49
|
+
<message>
|
50
|
+
<recipient>
|
51
|
+
<type>5</type>
|
52
|
+
<id>+15558675309</id>
|
53
|
+
</recipient>
|
54
|
+
<text>msg</text>
|
55
|
+
</message>
|
56
|
+
</request>
|
57
|
+
EOXML
|
58
|
+
assert_equal expected, Txter::Gateway4info::Request.new.deliver_message("msg", "1-555-867-5309")
|
59
|
+
end
|
60
|
+
should "create proper xml for unblock" do
|
61
|
+
expected = <<-EOXML
|
62
|
+
<?xml version='1.0' encoding='utf-8' ?>
|
63
|
+
<request clientId='12345' clientKey='ABC123' type='UNBLOCK'>
|
64
|
+
<unblock>
|
65
|
+
<recipient>
|
66
|
+
<type>5</type>
|
67
|
+
<id>+15558675309</id>
|
68
|
+
</recipient>
|
69
|
+
</unblock>
|
70
|
+
</request>
|
71
|
+
EOXML
|
72
|
+
assert_equal expected, Txter::Gateway4info::Request.new.unblock("1-555-867-5309")
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), 'test_helper')
|
2
|
+
|
3
|
+
class Gateway4infoTest < ActiveSupport::TestCase
|
4
|
+
|
5
|
+
context "with twilio gateway" do
|
6
|
+
setup {
|
7
|
+
Txter.configure do |config|
|
8
|
+
config.client_id = 12345
|
9
|
+
config.client_key = 'ABC123'
|
10
|
+
config.gateway = 'twilio'
|
11
|
+
end
|
12
|
+
}
|
13
|
+
# TODO: figure out what's left to test
|
14
|
+
end
|
15
|
+
end
|
data/test/test_helper.rb
CHANGED
@@ -6,15 +6,16 @@ require 'active_support'
|
|
6
6
|
require 'active_record'
|
7
7
|
require 'active_support/test_case'
|
8
8
|
require 'shoulda'
|
9
|
-
require File.join(File.dirname(__FILE__), "..", 'lib', '
|
9
|
+
require File.join(File.dirname(__FILE__), "..", 'lib', 'txter')
|
10
10
|
|
11
11
|
# default test configuration
|
12
|
-
|
12
|
+
Txter.configure do |config|
|
13
13
|
config.client_id = '1'
|
14
14
|
config.client_key = 'ABCDEF'
|
15
|
+
config.gateway = 'test'
|
15
16
|
end
|
16
17
|
|
17
|
-
|
18
|
+
Txter.mode = :test
|
18
19
|
config = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml'))
|
19
20
|
ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + "/debug.log")
|
20
21
|
ActiveRecord::Base.establish_connection(config[ENV['DB'] || 'sqlite3'])
|
@@ -32,11 +33,11 @@ ActiveRecord::Schema.define(:version => 1) do
|
|
32
33
|
end
|
33
34
|
|
34
35
|
class User < ActiveRecord::Base
|
35
|
-
include
|
36
|
+
include Txter::Contactable
|
36
37
|
end
|
37
38
|
|
38
39
|
# kill all network access
|
39
|
-
module
|
40
|
+
module Txter
|
40
41
|
class Request
|
41
42
|
def start
|
42
43
|
raise "You forgot to stub out your net requests!"
|
@@ -1,45 +1,21 @@
|
|
1
1
|
require File.join(File.dirname(__FILE__), 'test_helper')
|
2
2
|
|
3
|
-
class
|
3
|
+
class TxterContactableTest < ActiveSupport::TestCase
|
4
4
|
|
5
|
-
|
6
|
-
|
7
|
-
<status>
|
8
|
-
<id>4</id>
|
9
|
-
<message>Validation Error</message>
|
10
|
-
</status>
|
11
|
-
</response>'
|
12
|
-
ValidationSuccess = '<?xml version=”1.0” ?>
|
13
|
-
<response>
|
14
|
-
<requestId>F81D4FAE-7DEC-11D0-A765-00A0C91E6BF6</requestId>
|
15
|
-
<confCode>123ABC</confCode>
|
16
|
-
<status>
|
17
|
-
<id>1</id>
|
18
|
-
<message>Success</message>
|
19
|
-
</status>
|
20
|
-
</response>'
|
21
|
-
SendMsgSuccess = '<?xml version="1.0" ?>
|
22
|
-
<response>
|
23
|
-
<requestId>F81D4FAE-7DEC-11D0-A765-00A0C91E6BF6</requestId>
|
24
|
-
<status>
|
25
|
-
<id>1</id>
|
26
|
-
<message>Success</message>
|
27
|
-
</status>
|
28
|
-
</response>'
|
29
|
-
UnblockSuccess = '<?xml version=”1.0” ?>
|
30
|
-
<response>
|
31
|
-
<status>
|
32
|
-
<id>1</id>
|
33
|
-
<message>Success</message>
|
34
|
-
</status>
|
35
|
-
</response>'
|
5
|
+
Success = Txter::Gateway::Response.new(:status => :success)
|
6
|
+
Error = Txter::Gateway::Response.new(:status => :error)
|
36
7
|
|
37
8
|
context "contactable class" do
|
38
9
|
setup {
|
39
10
|
@klass = Class.new
|
40
|
-
@klass.send :include,
|
11
|
+
@klass.send :include, Txter::Contactable
|
12
|
+
Txter.configure do |config|
|
13
|
+
config.client_id = 12345
|
14
|
+
config.client_key = 'ABC123'
|
15
|
+
config.gateway = 'test'
|
16
|
+
end
|
41
17
|
}
|
42
|
-
|
18
|
+
Txter::Contactable::Attributes.each do |attribute|
|
43
19
|
should "begin with appropriate default for #{attribute}_column" do
|
44
20
|
assert_equal attribute, @klass.send("#{attribute}_column")
|
45
21
|
end
|
@@ -58,10 +34,10 @@ class FourInfoContactableTest < ActiveSupport::TestCase
|
|
58
34
|
}
|
59
35
|
|
60
36
|
should "normalize phone number" do
|
61
|
-
assert_equal '5551234567', @user.
|
37
|
+
assert_equal '5551234567', @user.txter_sms_phone_number
|
62
38
|
end
|
63
39
|
context "when phone number is blank" do
|
64
|
-
setup { @user.
|
40
|
+
setup { @user.txter_sms_phone_number = nil}
|
65
41
|
context "confirming phone number" do
|
66
42
|
setup { @user.send_sms_confirmation! }
|
67
43
|
should_not_change "any attributes" do
|
@@ -70,7 +46,7 @@ class FourInfoContactableTest < ActiveSupport::TestCase
|
|
70
46
|
end
|
71
47
|
context "sending message" do
|
72
48
|
setup {
|
73
|
-
|
49
|
+
Txter.gateway.stubs(:perform).returns(Success)
|
74
50
|
@worked = @user.send_sms!('message')
|
75
51
|
}
|
76
52
|
should "not work" do assert !@worked end
|
@@ -81,45 +57,45 @@ class FourInfoContactableTest < ActiveSupport::TestCase
|
|
81
57
|
end
|
82
58
|
|
83
59
|
context "when phone number exists" do
|
84
|
-
setup { @user.
|
60
|
+
setup { @user.txter_sms_phone_number = "206-555-5555"}
|
85
61
|
context "confirming phone number" do
|
86
62
|
setup {
|
87
|
-
|
63
|
+
Txter::Request.any_instance.stubs(:perform).returns(Success)
|
88
64
|
@worked = @user.send_sms_confirmation!
|
89
65
|
}
|
90
66
|
should "work" do assert @worked end
|
91
67
|
should "save confirmation number in proper attribute" do
|
92
|
-
|
68
|
+
assert @user.txter_sms_confirmation_code
|
93
69
|
end
|
94
70
|
should "set confirmation attempted time" do
|
95
|
-
assert @user.
|
71
|
+
assert @user.txter_sms_confirmation_attempted > 3.minutes.ago
|
96
72
|
end
|
97
73
|
should_change "stored code" do
|
98
|
-
@user.
|
74
|
+
@user.txter_sms_confirmation_code
|
99
75
|
end
|
100
76
|
should "not have number confirmed yet" do
|
101
77
|
assert !@user.sms_confirmed?
|
102
78
|
end
|
103
79
|
context "calling sms_confirm_with(right_code)" do
|
104
|
-
setup { @user.sms_confirm_with(@user.
|
80
|
+
setup { @user.sms_confirm_with(@user.txter_sms_confirmation_code) }
|
105
81
|
should "work" do
|
106
82
|
assert @worked
|
107
83
|
end
|
108
84
|
should "save the phone number into the confirmed attribute" do
|
109
|
-
assert_equal @user.
|
110
|
-
@user.
|
85
|
+
assert_equal @user.txter_sms_confirmed_phone_number,
|
86
|
+
@user.txter_sms_phone_number
|
111
87
|
end
|
112
88
|
should_change "confirmed phone number attribute" do
|
113
|
-
@user.
|
89
|
+
@user.txter_sms_confirmed_phone_number
|
114
90
|
end
|
115
91
|
context "and then attempting to confirm another number" do
|
116
92
|
setup {
|
117
|
-
@user.
|
118
|
-
|
93
|
+
@user.txter_sms_phone_number = "206-555-5555"
|
94
|
+
Txter.stubs(:deliver).returns(Success).once
|
119
95
|
@user.send_sms_confirmation!
|
120
96
|
}
|
121
97
|
should "eliminate the previous confirmed phone number" do
|
122
|
-
assert @user.
|
98
|
+
assert @user.txter_sms_confirmed_phone_number.blank?
|
123
99
|
end
|
124
100
|
should "un-confirm the record" do
|
125
101
|
assert !@user.sms_confirmed?
|
@@ -128,19 +104,19 @@ class FourInfoContactableTest < ActiveSupport::TestCase
|
|
128
104
|
end
|
129
105
|
context "calling sms_confirm_with(right code, wrong case)" do
|
130
106
|
setup {
|
131
|
-
@downcased_code = @user.
|
107
|
+
@downcased_code = @user.txter_sms_confirmation_code.downcase
|
132
108
|
@worked = @user.sms_confirm_with(@downcased_code)
|
133
109
|
}
|
134
110
|
should "have good test data" do
|
135
111
|
assert_not_equal @downcased_code,
|
136
|
-
@user.
|
112
|
+
@user.txter_sms_confirmation_code
|
137
113
|
end
|
138
114
|
should "work" do
|
139
115
|
assert @worked
|
140
116
|
end
|
141
117
|
should "save the phone number into the confirmed attribute" do
|
142
|
-
assert_equal @user.
|
143
|
-
@user.
|
118
|
+
assert_equal @user.txter_sms_confirmed_phone_number,
|
119
|
+
@user.txter_sms_phone_number
|
144
120
|
end
|
145
121
|
end
|
146
122
|
context "calling sms_confirm_with(wrong_code)" do
|
@@ -149,37 +125,39 @@ class FourInfoContactableTest < ActiveSupport::TestCase
|
|
149
125
|
assert !@worked
|
150
126
|
end
|
151
127
|
should "not save the phone number into the confirmed attribute" do
|
152
|
-
assert_not_equal @user.
|
153
|
-
@user.
|
128
|
+
assert_not_equal @user.txter_sms_confirmed_phone_number,
|
129
|
+
@user.txter_sms_phone_number
|
154
130
|
end
|
155
131
|
should_not_change "confirmed phone number attribute" do
|
156
|
-
@user.reload.
|
132
|
+
@user.reload.txter_sms_confirmed_phone_number
|
157
133
|
end
|
158
134
|
end
|
159
135
|
end
|
160
136
|
context "confirming phone number with a custom short code" do
|
161
137
|
context "with expectations" do
|
162
138
|
setup {
|
163
|
-
|
139
|
+
Txter.configure do |config|
|
164
140
|
config.short_code = '0005'
|
141
|
+
config.gateway = 'test'
|
165
142
|
config.client_id = 1
|
166
143
|
config.client_key = 'ABC123'
|
167
144
|
end
|
168
145
|
message = "long message blah blah MYCODE blah"
|
169
|
-
|
170
|
-
|
171
|
-
|
146
|
+
Txter.expects(:generate_confirmation_code).returns('MYCODE').once
|
147
|
+
Txter.expects(:confirmation_message).returns(message).once
|
148
|
+
Txter::Request.any_instance.expects(:deliver_message).with(message, @user.txter_sms_phone_number).once
|
172
149
|
@user.send_sms_confirmation!
|
173
150
|
}
|
174
151
|
end
|
175
152
|
context "(normal)" do
|
176
153
|
setup {
|
177
|
-
|
154
|
+
Txter.configure do |config|
|
178
155
|
config.short_code = '0005'
|
156
|
+
config.gateway = 'test'
|
179
157
|
config.client_id = 1
|
180
158
|
config.client_key = 'ABC123'
|
181
159
|
end
|
182
|
-
|
160
|
+
Txter::Request.any_instance.stubs(:perform).returns(Success)
|
183
161
|
@worked = @user.send_sms_confirmation!
|
184
162
|
}
|
185
163
|
should "work" do
|
@@ -189,15 +167,15 @@ class FourInfoContactableTest < ActiveSupport::TestCase
|
|
189
167
|
end
|
190
168
|
context "confirming phone number when the confirmation fails for some reason" do
|
191
169
|
setup {
|
192
|
-
|
170
|
+
Txter.stubs(:deliver).returns(Error)
|
193
171
|
@worked = @user.send_sms_confirmation!
|
194
172
|
}
|
195
173
|
should "not work" do assert !@worked end
|
196
174
|
should "not save confirmation number" do
|
197
|
-
assert @user.
|
175
|
+
assert @user.txter_sms_confirmation_code.blank?
|
198
176
|
end
|
199
177
|
should_not_change "stored code" do
|
200
|
-
@user.
|
178
|
+
@user.txter_sms_confirmation_code
|
201
179
|
end
|
202
180
|
end
|
203
181
|
end
|
@@ -205,7 +183,7 @@ class FourInfoContactableTest < ActiveSupport::TestCase
|
|
205
183
|
context "when the number is not confirmed" do
|
206
184
|
context "sending a message" do
|
207
185
|
setup {
|
208
|
-
|
186
|
+
Txter::Request.any_instance.stubs(:perform).returns(Success)
|
209
187
|
@result = @user.send_sms!('message')
|
210
188
|
}
|
211
189
|
should "send send no messages" do
|
@@ -215,7 +193,7 @@ class FourInfoContactableTest < ActiveSupport::TestCase
|
|
215
193
|
end
|
216
194
|
context "when the number is blocked" do
|
217
195
|
setup {
|
218
|
-
@user.
|
196
|
+
@user.txter_sms_blocked = true
|
219
197
|
@user.save!
|
220
198
|
}
|
221
199
|
context "sending a message" do
|
@@ -227,13 +205,13 @@ class FourInfoContactableTest < ActiveSupport::TestCase
|
|
227
205
|
end
|
228
206
|
context "when the number is confirmed" do
|
229
207
|
setup {
|
230
|
-
|
208
|
+
Txter::Request.any_instance.stubs(:perform).returns(Success)
|
231
209
|
@user.stubs(:sms_confirmed?).returns(true)
|
232
210
|
}
|
233
211
|
context "sending a message" do
|
234
212
|
setup { @result = @user.send_sms!('message') }
|
235
|
-
should "send send exactly one message
|
236
|
-
assert_equal [
|
213
|
+
should "send send exactly one message" do
|
214
|
+
assert_equal [7], @result
|
237
215
|
end
|
238
216
|
end
|
239
217
|
context "sending a blank message" do
|
@@ -253,7 +231,7 @@ class FourInfoContactableTest < ActiveSupport::TestCase
|
|
253
231
|
context "with the allow_multiple flag" do
|
254
232
|
setup { @result = @user.send_sms!("A"*200, true) }
|
255
233
|
should "send multiple messages" do
|
256
|
-
assert_equal [
|
234
|
+
assert_equal [160, 40], @result
|
257
235
|
end
|
258
236
|
end
|
259
237
|
end
|
@@ -261,7 +239,7 @@ class FourInfoContactableTest < ActiveSupport::TestCase
|
|
261
239
|
|
262
240
|
context "when the number is not blocked" do
|
263
241
|
setup {
|
264
|
-
|
242
|
+
Txter::Request.any_instance.expects(:perform).never
|
265
243
|
}
|
266
244
|
context "unblocking" do
|
267
245
|
setup { @worked = @user.unblock_sms! }
|
@@ -275,7 +253,7 @@ class FourInfoContactableTest < ActiveSupport::TestCase
|
|
275
253
|
end
|
276
254
|
context "when the number is blocked" do
|
277
255
|
setup {
|
278
|
-
|
256
|
+
Txter::Request.any_instance.stubs(:perform).returns(Success)
|
279
257
|
@user.update_attributes!(:sms_blocked => true)
|
280
258
|
}
|
281
259
|
context "unblocking" do
|
@@ -5,13 +5,13 @@ require 'shoulda/action_controller'
|
|
5
5
|
require 'shoulda/action_controller/macros'
|
6
6
|
require 'shoulda/action_controller/matchers'
|
7
7
|
|
8
|
-
class
|
9
|
-
include
|
8
|
+
class TxterController < ActionController::Base
|
9
|
+
include Txter::Controller
|
10
10
|
|
11
11
|
sms_contactable User
|
12
12
|
end
|
13
13
|
ActionController::Routing::Routes.draw do |map|
|
14
|
-
map.route '*:url', :controller => '
|
14
|
+
map.route '*:url', :controller => 'txter', :action => :index
|
15
15
|
end
|
16
16
|
|
17
17
|
class UserWithSMSReceiving < User
|
@@ -19,7 +19,7 @@ class UserWithSMSReceiving < User
|
|
19
19
|
end
|
20
20
|
end
|
21
21
|
|
22
|
-
class
|
22
|
+
class TxterControllerTest < ActionController::TestCase
|
23
23
|
|
24
24
|
context "with a user" do
|
25
25
|
setup {
|
@@ -36,10 +36,10 @@ class FourInfoControllerTest < ActionController::TestCase
|
|
36
36
|
}
|
37
37
|
should_respond_with :success
|
38
38
|
should "block user" do
|
39
|
-
assert @user.reload.
|
39
|
+
assert @user.reload.txter_sms_blocked?
|
40
40
|
end
|
41
41
|
should_change "user block status" do
|
42
|
-
@user.reload.
|
42
|
+
@user.reload.txter_sms_blocked?
|
43
43
|
end
|
44
44
|
end
|
45
45
|
context "receiving MESSAGE" do
|
@@ -55,7 +55,7 @@ class FourInfoControllerTest < ActionController::TestCase
|
|
55
55
|
}
|
56
56
|
should_respond_with :success
|
57
57
|
should "not block user" do
|
58
|
-
assert !@user.reload.
|
58
|
+
assert !@user.reload.txter_sms_blocked?
|
59
59
|
end
|
60
60
|
end
|
61
61
|
context "when the user is set up to receive" do
|
@@ -68,7 +68,7 @@ class FourInfoControllerTest < ActionController::TestCase
|
|
68
68
|
}
|
69
69
|
should_respond_with :success
|
70
70
|
should "not block user" do
|
71
|
-
assert !@new_user.reload.
|
71
|
+
assert !@new_user.reload.txter_sms_blocked?
|
72
72
|
end
|
73
73
|
end
|
74
74
|
end
|