smart_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,15 @@
1
+ # encoding: utf-8
2
+ require 'spec_helper'
3
+
4
+ describe SmartSMS::FakeSMS do
5
+ let(:mobile) { '15338489342' }
6
+ let(:code) { '12345' }
7
+ let(:company) { 'Edgepeek' }
8
+ let(:fake_message) { SmartSMS::FakeSMS.build_fake_sms mobile, code, company }
9
+
10
+ it 'should get the right mobile and text' do
11
+ Time.zone = 'Beijing'
12
+ expect(fake_message['mobile']).to eq mobile
13
+ expect(fake_message['text']).to eq "您的验证码是#{code}。如非本人操作,请忽略本短信【#{company}】"
14
+ end
15
+ end
@@ -0,0 +1,62 @@
1
+ # encoding: utf-8
2
+ require 'spec_helper'
3
+
4
+ describe SmartSMS::VerificationCode do
5
+ context 'get random verification code' do
6
+ it 'should return a short code' do
7
+ simple_code = SmartSMS::VerificationCode.random :short
8
+ expect(simple_code.length).to eq(4)
9
+ expect(simple_code).to match(/[0-9]/)
10
+ end
11
+
12
+ it 'should return a simple code' do
13
+ simple_code = SmartSMS::VerificationCode.random :simple
14
+ expect(simple_code.length).to eq(6)
15
+ expect(simple_code).to match(/[0-9]/)
16
+ end
17
+
18
+ it 'should return a middle code' do
19
+ middle_code = SmartSMS::VerificationCode.random :middle
20
+ expect(middle_code.length).to eq(6)
21
+ expect(middle_code).to match(/[a-zA-Z0-9]/)
22
+ end
23
+
24
+ it 'should return a complex code' do
25
+ complex_code = SmartSMS::VerificationCode.random :complex
26
+ expect(complex_code.length).to eq(8)
27
+ end
28
+
29
+ it 'should raise NoMethodError when calling non existed algorithm' do
30
+ expect do
31
+ SmartSMS::VerificationCode.random :great_algorithm
32
+ end.to raise_error NoMethodError
33
+ end
34
+ end
35
+
36
+ context 'get simple verification code' do
37
+ subject { SmartSMS::VerificationCode.short }
38
+
39
+ its(:length) { should == 4 }
40
+ it { should match(/[0-9]/) }
41
+ end
42
+
43
+ context 'get simple verification code' do
44
+ subject { SmartSMS::VerificationCode.simple }
45
+
46
+ its(:length) { should == 6 }
47
+ it { should match(/[0-9]/) }
48
+ end
49
+
50
+ context 'get middle verification code' do
51
+ subject { SmartSMS::VerificationCode.middle }
52
+
53
+ its(:length) { should == 6 }
54
+ it { should match(/[a-zA-Z0-9]/) }
55
+ end
56
+
57
+ context 'get complex verification code' do
58
+ subject { SmartSMS::VerificationCode.complex }
59
+
60
+ its(:length) { should == 8 }
61
+ end
62
+ end
@@ -0,0 +1,260 @@
1
+ # encoding: utf-8
2
+ require 'spec_helper'
3
+
4
+ describe SmartSMS do
5
+ let(:phone) { '13394738283' }
6
+ let(:content) { '您的验证码是: 432432, 如果此操作与您无关, 请忽略这条短信' }
7
+
8
+ describe '#deliver' do
9
+ context 'template send api' do
10
+ let(:url) { 'http://yunpian.com/v1/sms/tpl_send.json' }
11
+ subject { SmartSMS.deliver phone, content }
12
+
13
+ before do
14
+ stub_request(:post, url).with(
15
+ body: {
16
+ 'apikey' => SmartSMS.config.api_key,
17
+ 'mobile' => phone,
18
+ 'tpl_id' => '2',
19
+ 'tpl_value' => "#code#=#{content}&#company#=Smart SMS"
20
+ },
21
+ headers: {
22
+ 'Content-Type' => 'application/x-www-form-urlencoded'
23
+ }
24
+ ).to_return(
25
+ body: {
26
+ 'code' => 0,
27
+ 'msg' => 'OK',
28
+ 'result' => {
29
+ 'count' => '1',
30
+ 'fee' => '1',
31
+ 'sid' => '592762800'
32
+ }
33
+ }.to_json
34
+ )
35
+ end
36
+
37
+ its(['code']) { should eq 0 }
38
+ its(['msg']) { should eq 'OK' }
39
+ end
40
+
41
+ context 'general send api' do
42
+ let(:url) { 'http://yunpian.com/v1/sms/send.json' }
43
+ subject { SmartSMS.deliver phone, content, method: :general, extend: true }
44
+
45
+ before do
46
+ stub_request(:post, url).with(
47
+ body: {
48
+ 'apikey' => SmartSMS.config.api_key,
49
+ 'mobile' => phone,
50
+ 'extend' => 'true',
51
+ 'text' => content
52
+ },
53
+ headers: {
54
+ 'Content-Type' => 'application/x-www-form-urlencoded'
55
+ }
56
+ ).to_return(
57
+ body: {
58
+ 'code' => 0,
59
+ 'msg' => 'OK',
60
+ 'result' => {
61
+ 'count' => '1',
62
+ 'fee' => '1',
63
+ 'sid' => '592762800'
64
+ }
65
+ }.to_json
66
+ )
67
+ end
68
+
69
+ its(['code']) { should eq 0 }
70
+ its(['msg']) { should eq 'OK' }
71
+ end
72
+ end
73
+
74
+ describe '#find_by_sid' do
75
+ context 'with a valid sid' do
76
+ let(:valid_sid) { '1234' }
77
+ let(:url) { 'http://yunpian.com/v1/sms/get.json' }
78
+ subject { SmartSMS.find_by_sid valid_sid }
79
+
80
+ before do
81
+ stub_request(:post, url).with(
82
+ body: {
83
+ 'apikey' => SmartSMS.config.api_key,
84
+ 'sid' => valid_sid
85
+ },
86
+ headers: {
87
+ 'Content-Type' => 'application/x-www-form-urlencoded'
88
+ }
89
+ ).to_return(
90
+ body: {
91
+ 'code' => 0,
92
+ 'msg' => 'OK',
93
+ 'sms' => {
94
+ 'sid' => valid_sid,
95
+ 'mobile' => phone,
96
+ 'send_time' => '2014-05-08 09:24:08',
97
+ 'text' => content,
98
+ 'send_status' => 'SUCCESS',
99
+ 'report_status' => 'SUCCESS',
100
+ 'fee' => 1,
101
+ 'user_receive_time' => '2014-05-08 09:26:23',
102
+ 'error_msg' => nil
103
+ }
104
+ }.to_json
105
+ )
106
+ end
107
+
108
+ its(['code']) { should eq 0 }
109
+ its(['msg']) { should eq 'OK' }
110
+ its(:keys) { should include 'sms' }
111
+ end
112
+
113
+ context 'with a invalid sid' do
114
+ let(:invalid_sid) { '4362847632874' }
115
+ let(:url) { 'http://yunpian.com/v1/sms/get.json' }
116
+ subject { SmartSMS.find_by_sid invalid_sid }
117
+
118
+ before do
119
+ stub_request(:post, url).with(
120
+ body: {
121
+ 'apikey' => SmartSMS.config.api_key,
122
+ 'sid' => invalid_sid
123
+ },
124
+ headers: {
125
+ 'Content-Type' => 'application/x-www-form-urlencoded'
126
+ }
127
+ ).to_return(
128
+ body: {
129
+ 'code' => 0,
130
+ 'msg' => 'OK',
131
+ 'sms' => nil
132
+ }.to_json
133
+ )
134
+ end
135
+
136
+ its(['code']) { should eq 0 }
137
+ its(['msg']) { should eq 'OK' }
138
+ its(['sms']) { should eq nil }
139
+ end
140
+ end
141
+
142
+ describe '#find' do
143
+ let(:url) { 'http://yunpian.com/v1/sms/get.json' }
144
+ subject { SmartSMS.find start_time: '2014-05-07 11:33:36', end_time: '2014-05-08 11:33:36' }
145
+
146
+ before do
147
+ stub_request(:post, url).with(
148
+ body: {
149
+ 'apikey' => SmartSMS.config.api_key,
150
+ 'start_time' => '2014-05-07 11:33:36',
151
+ 'end_time' => '2014-05-08 11:33:36',
152
+ 'page_num' => '1',
153
+ 'page_size' => '20'
154
+ },
155
+ headers: {
156
+ 'Content-Type' => 'application/x-www-form-urlencoded'
157
+ }
158
+ ).to_return(
159
+ body: {
160
+ 'code' => 0,
161
+ 'msg' => 'OK',
162
+ 'sms' => [
163
+ {
164
+ 'sid' => 654805009,
165
+ 'mobile' => phone,
166
+ 'send_time' => '2014-05-08 09:25:58',
167
+ 'text' => content,
168
+ 'send_status' => 'SUCCESS',
169
+ 'report_status' => 'SUCCESS',
170
+ 'fee' => 1,
171
+ 'user_receive_time' => '2014-05-08 09:25:58',
172
+ 'error_msg' => nil
173
+ },
174
+ {
175
+ 'sid' => 654804990,
176
+ 'mobile' => phone,
177
+ 'send_time' => '2014-05-08 09:25:56',
178
+ 'text' => content,
179
+ 'send_status' => 'SUCCESS',
180
+ 'report_status' => 'SUCCESS',
181
+ 'fee' => 1,
182
+ 'user_receive_time' => '2014-05-08 09:25:57',
183
+ 'error_msg' => nil
184
+ }
185
+ ]
186
+ }.to_json
187
+ )
188
+ end
189
+
190
+ its(['code']) { should eq 0 }
191
+ its(['msg']) { should eq 'OK' }
192
+ its(:keys) { should include 'sms' }
193
+ its(['sms']) { should_not be_empty }
194
+ end
195
+
196
+ describe '#get_black_word' do
197
+ let(:url) { 'http://yunpian.com/v1/sms/get_black_word.json' }
198
+ let(:text) { '这是一条测试短信' }
199
+ subject { SmartSMS.get_black_word text }
200
+
201
+ before do
202
+ stub_request(:post, url).with(
203
+ body: {
204
+ 'apikey' => SmartSMS.config.api_key,
205
+ 'text' => text
206
+ },
207
+ headers: {
208
+ 'Content-Type' => 'application/x-www-form-urlencoded'
209
+ }
210
+ ).to_return(
211
+ body: {
212
+ 'code' => 0, 'msg' => 'OK', 'result' => { 'black_word' => '测试' }
213
+ }.to_json
214
+ )
215
+ end
216
+
217
+ its(['code']) { should eq 0 }
218
+ its(['msg']) { should eq 'OK' }
219
+ its(:keys) { should include 'result' }
220
+ end
221
+
222
+ describe '#get_reply' do
223
+ let(:url) { 'http://yunpian.com/v1/sms/get_reply.json' }
224
+ subject { SmartSMS.get_reply start_time: '2014-05-07 11:33:36', end_time: '2014-05-08 11:33:36' }
225
+
226
+ before do
227
+ stub_request(:post, url).with(
228
+ body: {
229
+ 'apikey' => SmartSMS.config.api_key,
230
+ 'start_time' => '2014-05-07 11:33:36',
231
+ 'end_time' => '2014-05-08 11:33:36',
232
+ 'page_num' => '1',
233
+ 'page_size' => '20'
234
+ },
235
+ headers: {
236
+ 'Content-Type' => 'application/x-www-form-urlencoded'
237
+ }
238
+ ).to_return(
239
+ body: {
240
+ 'code' => 0,
241
+ 'msg' => 'OK',
242
+ 'sms_reply' => [
243
+ {
244
+ 'id' => nil,
245
+ 'mobile' => '15121000465',
246
+ 'text' => '哈哈',
247
+ 'reply_time' => '2014-05-08 12:03:16',
248
+ 'extend' => '11642'
249
+ }
250
+ ]
251
+ }.to_json
252
+ )
253
+ end
254
+
255
+ its(['code']) { should eq 0 }
256
+ its(['msg']) { should eq 'OK' }
257
+ its(:keys) { should include 'sms_reply' }
258
+ its(['sms_reply']) { should_not be_empty }
259
+ end
260
+ end
data/spec/spec_helper.rb CHANGED
@@ -1,12 +1,25 @@
1
1
  # encoding: utf-8
2
+ begin
3
+ require 'rails'
4
+ rescue LoadError
5
+ end
6
+
2
7
  require 'rubygems'
3
8
  require 'bundler/setup'
9
+ # Bundler.require
4
10
 
5
11
  require 'webmock/rspec'
12
+
6
13
  require 'smart_sms'
7
14
 
8
15
  require 'database_cleaner'
9
16
 
10
- #WebMock.allow_net_connect!
11
- RSpec.configure do |config|
17
+ # WebMock.allow_net_connect!
18
+
19
+ if defined? Rails
20
+ require 'fake_app/rails_app'
21
+
22
+ require 'rspec/rails'
12
23
  end
24
+
25
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
@@ -0,0 +1,13 @@
1
+ DatabaseCleaner[:active_record].strategy = :transaction if defined? ActiveRecord
2
+
3
+ RSpec.configure do |config|
4
+ config.before :suite do
5
+ DatabaseCleaner.clean_with :truncation if defined? ActiveRecord
6
+ end
7
+ config.before :each do
8
+ DatabaseCleaner.start
9
+ end
10
+ config.after :each do
11
+ DatabaseCleaner.clean
12
+ end
13
+ end
@@ -0,0 +1,168 @@
1
+ # encoding: utf-8
2
+ require 'spec_helper'
3
+
4
+ describe SmartSMS::Template do
5
+
6
+ describe '#find_default' do
7
+ let(:url) { 'http://yunpian.com/v1/tpl/get_default.json' }
8
+ let(:tpl_id) { '1' }
9
+ subject { SmartSMS::Template.find_default tpl_id }
10
+
11
+ before do
12
+ stub_request(:post, url).with(
13
+ body: {
14
+ 'apikey' => SmartSMS.config.api_key,
15
+ 'tpl_id' => tpl_id
16
+ },
17
+ headers: {
18
+ 'Content-Type' => 'application/x-www-form-urlencoded'
19
+ }
20
+ ).to_return(
21
+ body: {
22
+ 'code' => 0,
23
+ 'msg' => 'OK',
24
+ 'template' => {
25
+ 'tpl_id' => tpl_id,
26
+ 'tpl_content' => '您的验证码是#code#【#company#】',
27
+ 'check_status' => 'SUCCESS',
28
+ 'reason' => nil
29
+ }
30
+ }.to_json
31
+ )
32
+ end
33
+
34
+ its(['code']) { should eq 0 }
35
+ its(['msg']) { should eq 'OK' }
36
+ its(:keys) { should include 'template' }
37
+ end
38
+
39
+ describe '#find' do
40
+ let(:url) { 'http://yunpian.com/v1/tpl/get.json' }
41
+ let(:tpl_id) { '325207' }
42
+ subject { SmartSMS::Template.find tpl_id }
43
+
44
+ before do
45
+ stub_request(:post, url).with(
46
+ body: {
47
+ 'apikey' => SmartSMS.config.api_key,
48
+ 'tpl_id' => tpl_id
49
+ },
50
+ headers: {
51
+ 'Content-Type' => 'application/x-www-form-urlencoded'
52
+ }
53
+ ).to_return(
54
+ body: {
55
+ 'code' => 0,
56
+ 'msg' => 'OK',
57
+ 'template' => {
58
+ 'tpl_id' => tpl_id,
59
+ 'tpl_content' => '您的验证码是#code#【#company#】',
60
+ 'check_status' => 'SUCCESS',
61
+ 'reason' => nil
62
+ }
63
+ }.to_json
64
+ )
65
+ end
66
+
67
+ its(['code']) { should eq 0 }
68
+ its(['msg']) { should eq 'OK' }
69
+ its(:keys) { should include 'template' }
70
+ end
71
+
72
+ describe '#create' do
73
+ let(:url) { 'http://yunpian.com/v1/tpl/add.json' }
74
+ let(:tpl_content) { '您的验证码是: #code#' }
75
+ subject { SmartSMS::Template.create tpl_content }
76
+
77
+ before do
78
+ stub_request(:post, url).with(
79
+ body: {
80
+ 'apikey' => SmartSMS.config.api_key,
81
+ 'tpl_content' => tpl_content
82
+ },
83
+ headers: {
84
+ 'Content-Type' => 'application/x-www-form-urlencoded'
85
+ }
86
+ ).to_return(
87
+ body: {
88
+ 'code' => 0,
89
+ 'msg' => 'OK',
90
+ 'template' => {
91
+ 'tpl_id' => '43243242',
92
+ 'tpl_content' => tpl_content,
93
+ 'check_status' => 'SUCCESS',
94
+ 'reason' => nil
95
+ }
96
+ }.to_json
97
+ )
98
+ end
99
+
100
+ its(['code']) { should eq 0 }
101
+ its(['msg']) { should eq 'OK' }
102
+ its(:keys) { should include 'template' }
103
+ end
104
+
105
+ describe '#update' do
106
+ let(:url) { 'http://yunpian.com/v1/tpl/update.json' }
107
+ let(:tpl_id) { '43243242' }
108
+ let(:tpl_content) { '您的验证码是: #code#' }
109
+ subject { SmartSMS::Template.update tpl_id, tpl_content }
110
+
111
+ before do
112
+ stub_request(:post, url).with(
113
+ body: {
114
+ 'apikey' => SmartSMS.config.api_key,
115
+ 'tpl_id' => tpl_id,
116
+ 'tpl_content' => tpl_content
117
+ },
118
+ headers: {
119
+ 'Content-Type' => 'application/x-www-form-urlencoded'
120
+ }
121
+ ).to_return(
122
+ body: {
123
+ 'code' => 0,
124
+ 'msg' => 'OK',
125
+ 'template' => {
126
+ 'tpl_id' => tpl_id,
127
+ 'tpl_content' => tpl_content,
128
+ 'check_status' => 'SUCCESS',
129
+ 'reason' => nil
130
+ }
131
+ }.to_json
132
+ )
133
+ end
134
+
135
+ its(['code']) { should eq 0 }
136
+ its(['msg']) { should eq 'OK' }
137
+ its(:keys) { should include 'template' }
138
+ end
139
+
140
+ describe '#destroy' do
141
+ let(:url) { 'http://yunpian.com/v1/tpl/del.json' }
142
+ let(:tpl_id) { '43243242' }
143
+ subject { SmartSMS::Template.destroy tpl_id }
144
+
145
+ before do
146
+ stub_request(:post, url).with(
147
+ body: {
148
+ 'apikey' => SmartSMS.config.api_key,
149
+ 'tpl_id' => tpl_id
150
+ },
151
+ headers: {
152
+ 'Content-Type' => 'application/x-www-form-urlencoded'
153
+ }
154
+ ).to_return(
155
+ body: {
156
+ 'code' => 0,
157
+ 'msg' => 'OK',
158
+ 'detail' => nil
159
+ }.to_json
160
+ )
161
+ end
162
+
163
+ its(['code']) { should eq 0 }
164
+ its(['msg']) { should eq 'OK' }
165
+ its(:keys) { should include 'detail' }
166
+ its(['detail']) { should be_nil }
167
+ end
168
+ end