yx-smart_sms 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (41) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +35 -0
  3. data/.rdoc_options +21 -0
  4. data/.travis.yml +9 -0
  5. data/CONTRIBUTING.md +32 -0
  6. data/Gemfile +4 -0
  7. data/Gemfile.lock +145 -0
  8. data/LICENSE +21 -0
  9. data/README.md +283 -0
  10. data/Rakefile +11 -0
  11. data/lib/generators/smart_sms/config_generator.rb +16 -0
  12. data/lib/generators/smart_sms/install_generator.rb +36 -0
  13. data/lib/generators/smart_sms/templates/add_uid_to_smart_sms_messages.rb +6 -0
  14. data/lib/generators/smart_sms/templates/create_smart_sms_messages.rb +19 -0
  15. data/lib/generators/smart_sms/templates/smart_sms_config.rb +15 -0
  16. data/lib/smart_sms.rb +35 -0
  17. data/lib/smart_sms/account.rb +25 -0
  18. data/lib/smart_sms/config.rb +49 -0
  19. data/lib/smart_sms/has_sms_verification.rb +149 -0
  20. data/lib/smart_sms/helpers/fake_sms.rb +31 -0
  21. data/lib/smart_sms/helpers/verification_code.rb +42 -0
  22. data/lib/smart_sms/message_service.rb +98 -0
  23. data/lib/smart_sms/model/message.rb +9 -0
  24. data/lib/smart_sms/request.rb +48 -0
  25. data/lib/smart_sms/template.rb +44 -0
  26. data/lib/smart_sms/version.rb +3 -0
  27. data/smart_sms.gemspec +35 -0
  28. data/spec/account_spec.rb +80 -0
  29. data/spec/config/config_spec.rb +172 -0
  30. data/spec/fake_app/active_record/config.rb +9 -0
  31. data/spec/fake_app/active_record/models.rb +44 -0
  32. data/spec/fake_app/initializers/smart_sms.rb +15 -0
  33. data/spec/fake_app/rails_app.rb +23 -0
  34. data/spec/has_sms_verificaton_spec.rb +275 -0
  35. data/spec/helpers/fake_sms_spec.rb +15 -0
  36. data/spec/helpers/verification_code_spec.rb +62 -0
  37. data/spec/smart_sms_spec.rb +261 -0
  38. data/spec/spec_helper.rb +26 -0
  39. data/spec/support/database_cleaner.rb +13 -0
  40. data/spec/template_spec.rb +168 -0
  41. metadata +264 -0
@@ -0,0 +1,9 @@
1
+ # Database
2
+ ActiveRecord::Base.configurations = {
3
+ 'test' => {
4
+ adapter: 'sqlite3',
5
+ database: ':memory:'
6
+ }
7
+ }
8
+
9
+ ActiveRecord::Base.establish_connection(:test)
@@ -0,0 +1,44 @@
1
+ # Model
2
+ class User < ActiveRecord::Base
3
+ has_sms_verification
4
+ end
5
+
6
+ # Model with customized columns
7
+ class Account < ActiveRecord::Base
8
+ has_sms_verification :mobile, :confirmed_at
9
+ end
10
+
11
+ # Migrations
12
+ class CreateAllTables < ActiveRecord::Migration
13
+ def self.up
14
+ create_table(:users) do |t|
15
+ t.string :phone
16
+ t.datetime :verified_at
17
+ end
18
+
19
+ create_table(:accounts) do |t|
20
+ t.string :mobile
21
+ t.datetime :confirmed_at
22
+ end
23
+
24
+ create_table :smart_sms_messages do |t|
25
+ t.string :sid
26
+ t.string :uid
27
+ t.string :mobile
28
+ t.datetime :send_time
29
+ t.text :text
30
+ t.string :code
31
+ t.string :send_status
32
+ t.string :report_status
33
+ t.string :fee
34
+ t.datetime :user_receive_time
35
+ t.text :error_msg
36
+ t.belongs_to :smsable, polymorphic: true
37
+ end
38
+ add_index :smart_sms_messages, :sid
39
+ add_index :smart_sms_messages, :uid
40
+ add_index :smart_sms_messages, [:smsable_id, :smsable_type]
41
+ end
42
+ end
43
+ ActiveRecord::Migration.verbose = false
44
+ CreateAllTables.up
@@ -0,0 +1,15 @@
1
+ # encoding: utf-8
2
+
3
+ SmartSMS.configure do |config|
4
+ config.api_key = 'fake_api_key'
5
+ config.api_version = :v1
6
+ config.template_id = '2'
7
+ config.template_value = [:code, :company]
8
+ config.page_num = 1
9
+ config.page_size = 20
10
+ config.company = 'Smart SMS'
11
+ config.expires_in = 1.hour
12
+ config.default_interval = 1.day
13
+ config.store_sms_in_local = true
14
+ config.verification_code_algorithm = :simple
15
+ end
@@ -0,0 +1,23 @@
1
+ # require 'rails/all'
2
+ require 'action_controller/railtie'
3
+ require 'action_view/railtie'
4
+
5
+ require 'fake_app/active_record/config' if defined? ActiveRecord
6
+
7
+ # Config
8
+ app = Class.new(Rails::Application)
9
+ app.config.secret_token = '3b7cd727ee24e8444053437c36cc66c4'
10
+ app.config.session_store :cookie_store, key: '_myapp_session'
11
+ app.config.active_support.deprecation = :log
12
+ app.config.eager_load = false
13
+
14
+ # Rais.root
15
+ app.config.root = File.dirname(__FILE__)
16
+ Rails.backtrace_cleaner.remove_silencers!
17
+ app.initialize!
18
+
19
+ # Initializer
20
+ require 'fake_app/initializers/smart_sms'
21
+
22
+ # Model
23
+ require 'fake_app/active_record/models' if defined? ActiveRecord
@@ -0,0 +1,275 @@
1
+ # encoding: utf-8
2
+ require 'spec_helper'
3
+
4
+ describe 'SmartSMS::HasSmsVerification' do
5
+ let(:user) { User.create phone: '13764071479' }
6
+ let(:account) { Account.create mobile: '13764071479' }
7
+
8
+ context '#verify!' do
9
+ let(:verification_code) { SmartSMS::VerificationCode.simple }
10
+
11
+ describe 'Model User with default columns' do
12
+ before do
13
+ 3.times { user.deliver_fake_sms }
14
+ user.deliver_fake_sms verification_code
15
+ end
16
+
17
+ context 'with correct verification_code' do
18
+ before { user.verify! verification_code }
19
+
20
+ it 'should be verified' do
21
+ expect(user.verify! verification_code).to be_truthy
22
+ expect(user.reload.verified?).to be_truthy
23
+ end
24
+
25
+ it 'should have right verification code' do
26
+ expect(user.latest_message.code).to eq verification_code
27
+ end
28
+
29
+ it 'should have 4 messages' do
30
+ expect(user.messages.count).to eq 4
31
+ end
32
+
33
+ it 'should not be verified by using expired verification code' do
34
+ user.verified_at = nil
35
+ user.save
36
+ expect(user.verify!(user.messages.first.code)).to be_nil
37
+ end
38
+
39
+ it 'should be the latest_message' do
40
+ expect(user.latest_message).to eq user.messages.last
41
+ end
42
+ end
43
+
44
+ context 'with incorrect verification_code' do
45
+ before { user.verify! 'kfdsfd' }
46
+
47
+ it 'should not be verified' do
48
+ expect(user.verify!('kfdsfd')).to be_nil
49
+ expect(user.reload.verified?).to be_falsey
50
+ end
51
+
52
+ it 'should not have right verification code' do
53
+ expect(user.latest_message.code).not_to eq 'kfdsfd'
54
+ end
55
+
56
+ it 'should have 4 messages' do
57
+ expect(user.messages.count).to eq 4
58
+ end
59
+
60
+ it 'should not be verified by using expired verification code' do
61
+ user.verified_at = nil
62
+ user.save
63
+ expect(user.verify!(user.messages.first.code)).to be_nil
64
+ end
65
+
66
+ it 'should be the latest_message' do
67
+ expect(user.latest_message).to eq user.messages.last
68
+ end
69
+ end
70
+ end
71
+
72
+ describe 'Model Account with custom columns' do
73
+ before do
74
+ 3.times { account.deliver_fake_sms }
75
+ account.deliver_fake_sms verification_code
76
+ end
77
+
78
+ context 'with correct verification_code' do
79
+ before { account.verify! verification_code }
80
+
81
+ it 'should be verified' do
82
+ expect(account.verify! verification_code).to be_truthy
83
+ expect(account.reload.verified?).to be_truthy
84
+ end
85
+
86
+ it 'should have right verification code' do
87
+ expect(account.latest_message.code).to eq verification_code
88
+ end
89
+
90
+ it 'should have 4 messages' do
91
+ expect(account.messages.count).to eq 4
92
+ end
93
+
94
+ it 'should not be verified by using expired verification code' do
95
+ account.confirmed_at = nil
96
+ account.save
97
+ expect(account.verify!(account.messages.first.code)).to be_nil
98
+ end
99
+
100
+ it 'should be the latest_message' do
101
+ expect(account.latest_message).to eq account.messages.last
102
+ end
103
+
104
+ it 'should be the same value for verified_at and confirmed_at' do
105
+ expect(account.verified_at).to eq account.confirmed_at
106
+ end
107
+ end
108
+
109
+ context 'with incorrect verification_code' do
110
+ before { account.verify! 'kfdsfd' }
111
+
112
+ it 'should not be verified' do
113
+ expect(account.verify!('kfdsfd')).to be_nil
114
+ expect(account.reload.verified?).to be_falsey
115
+ end
116
+
117
+ it 'should not have right verification code' do
118
+ expect(account.latest_message.code).not_to eq 'kfdsfd'
119
+ end
120
+
121
+ it 'should have 4 messages' do
122
+ expect(account.messages.count).to eq 4
123
+ end
124
+
125
+ it 'should not be verified by using expired verification code' do
126
+ account.confirmed_at = nil
127
+ account.save
128
+ expect(account.verify!(account.messages.first.code)).to be_nil
129
+ end
130
+
131
+ it 'should be the latest_message' do
132
+ expect(account.latest_message).to eq account.messages.last
133
+ end
134
+
135
+ it 'should be the same value for verified_at and confirmed_at' do
136
+ expect(account.verified_at).to eq account.confirmed_at
137
+ end
138
+ end
139
+ end
140
+ end
141
+
142
+ context '#verify' do
143
+ let(:verification_code) { SmartSMS::VerificationCode.simple }
144
+
145
+ describe 'Model User with default columns' do
146
+ before do
147
+ 3.times { user.deliver_fake_sms }
148
+ user.deliver_fake_sms verification_code
149
+ end
150
+
151
+ context 'with correct verification_code' do
152
+ before { user.verify verification_code }
153
+
154
+ it 'should be verified' do
155
+ expect(user.verify verification_code).to be_truthy
156
+ expect(user.reload.verified?).to be_falsey
157
+ end
158
+
159
+ it 'should have right verification code' do
160
+ expect(user.latest_message.code).to eq verification_code
161
+ end
162
+
163
+ it 'should have 4 messages' do
164
+ expect(user.messages.count).to eq 4
165
+ end
166
+
167
+ it 'should not be verified by using expired verification code' do
168
+ user.verified_at = nil
169
+ user.save
170
+ expect(user.verify(user.messages.first.code)).to be_falsey
171
+ end
172
+
173
+ it 'should be the latest_message' do
174
+ expect(user.latest_message).to eq user.messages.last
175
+ end
176
+ end
177
+
178
+ context 'with incorrect verification_code' do
179
+ before { user.verify 'kfdsfd' }
180
+
181
+ it 'should not be verified' do
182
+ expect(user.verify('kfdsfd')).to be_falsey
183
+ expect(user.reload.verified?).to be_falsey
184
+ end
185
+
186
+ it 'should not have right verification code' do
187
+ expect(user.latest_message.code).not_to eq 'kfdsfd'
188
+ end
189
+
190
+ it 'should have 4 messages' do
191
+ expect(user.messages.count).to eq 4
192
+ end
193
+
194
+ it 'should not be verified by using expired verification code' do
195
+ user.verified_at = nil
196
+ user.save
197
+ expect(user.verify(user.messages.first.code)).to be_falsey
198
+ end
199
+
200
+ it 'should be the latest_message' do
201
+ expect(user.latest_message).to eq user.messages.last
202
+ end
203
+ end
204
+ end
205
+
206
+ describe 'Model Account with custom columns' do
207
+ before do
208
+ 3.times { account.deliver_fake_sms }
209
+ account.deliver_fake_sms verification_code
210
+ end
211
+
212
+ context 'with correct verification_code' do
213
+ before { account.verify verification_code }
214
+
215
+ it 'should be verified' do
216
+ expect(account.verify verification_code).to be_truthy
217
+ expect(account.reload.verified?).to be_falsey
218
+ end
219
+
220
+ it 'should have right verification code' do
221
+ expect(account.latest_message.code).to eq verification_code
222
+ end
223
+
224
+ it 'should have 4 messages' do
225
+ expect(account.messages.count).to eq 4
226
+ end
227
+
228
+ it 'should not be verified by using expired verification code' do
229
+ account.confirmed_at = nil
230
+ account.save
231
+ expect(user.verify(account.messages.first.code)).to be_falsey
232
+ end
233
+
234
+ it 'should be the latest_message' do
235
+ expect(account.latest_message).to eq account.messages.last
236
+ end
237
+
238
+ it 'should be the same value for verified_at and confirmed_at' do
239
+ expect(account.verified_at).to eq account.confirmed_at
240
+ end
241
+ end
242
+
243
+ context 'with incorrect verification_code' do
244
+ before { account.verify 'kfdsfd' }
245
+
246
+ it 'should not be verified' do
247
+ expect(account.verify('kfdsfd')).to be_falsey
248
+ expect(account.reload.verified?).to be_falsey
249
+ end
250
+
251
+ it 'should not have right verification code' do
252
+ expect(account.latest_message.code).not_to eq 'kfdsfd'
253
+ end
254
+
255
+ it 'should have 4 messages' do
256
+ expect(account.messages.count).to eq 4
257
+ end
258
+
259
+ it 'should not be verified by using expired verification code' do
260
+ account.confirmed_at = nil
261
+ account.save
262
+ expect(account.verify(account.messages.first.code)).to be_falsey
263
+ end
264
+
265
+ it 'should be the latest_message' do
266
+ expect(account.latest_message).to eq account.messages.last
267
+ end
268
+
269
+ it 'should be the same value for verified_at and confirmed_at' do
270
+ expect(account.verified_at).to eq account.confirmed_at
271
+ end
272
+ end
273
+ end
274
+ end
275
+ end
@@ -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