sms_broker 1.0.0 → 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,29 +1,25 @@
1
1
  describe SmsBroker do
2
-
3
2
  context 'Nexmo' do
4
-
5
- let(:text_message) { 'Hello World' }
6
- let(:from_phone) { ENV['NEXMO_PHONE_NUMBER'] }
7
- let(:api_secret) { ENV['NEXMO_API_SECRET'] }
8
- let(:sender_id) { ENV['NEXMO_SENDER_ID'] }
9
- let(:to_phone) { '44741234567' }
10
- let(:api_key) { ENV['NEXMO_API_SECRET'] }
11
-
12
3
  context '#send_message' do
4
+ let(:text_message) { 'Hello World' }
5
+ let(:from_phone) { '+447476543210' }
6
+ let(:sender_id) { 'SenderID' }
7
+ let(:to_phone) { '+447491234567' }
8
+ let(:api_secret) { 'api_secret' }
9
+ let(:api_key) { 'api_key' }
13
10
 
14
11
  before(:each) do
15
12
  SmsBroker.clear_setup
16
13
  end
17
14
 
18
15
  context 'valid' do
19
-
20
16
  it 'should send message with success' do
21
17
  SmsBroker.setup do |config|
22
18
  config.nexmo_setup \
23
19
  phone_number: from_phone,
24
20
  sender_id: sender_id,
25
21
  secret: api_secret,
26
- key: ENV['NEXMO_API_KEY']
22
+ key: api_key
27
23
  end
28
24
 
29
25
  stub_nexmo_create_message_success(sender_id, to_phone, text_message)
@@ -35,37 +31,32 @@ describe SmsBroker do
35
31
  expect(response.message_id).not_to eq(nil)
36
32
  end
37
33
 
38
- context 'with sender_id' do
39
-
40
- it 'should return error for invalid sender_id' do
41
- SmsBroker.setup do |config|
42
- config.nexmo_setup \
43
- phone_number: from_phone,
44
- sender_id: sender_id,
45
- secret: api_secret,
46
- key: ENV['NEXMO_API_KEY']
47
- end
48
-
49
- stub_nexmo_create_message_success \
50
- from_phone, to_phone, text_message
34
+ it 'should return error for invalid sender_id and fallback to ' \
35
+ 'phone_number' do
36
+ SmsBroker.setup do |config|
37
+ config.nexmo_setup \
38
+ phone_number: from_phone,
39
+ sender_id: sender_id,
40
+ secret: api_secret,
41
+ key: api_key
42
+ end
51
43
 
52
- stub_nexmo_create_message_invalid_sender_id_request \
53
- sender_id, to_phone, text_message
44
+ stub_nexmo_create_message_invalid_sender_id_request \
45
+ sender_id, to_phone, text_message
54
46
 
55
- response = SmsBroker.message(text_message).to(to_phone).deliver
47
+ stub_nexmo_create_message_success \
48
+ from_phone, to_phone, text_message
56
49
 
57
- expect(response.success?).to eq(true)
58
- # this means that it tried to send with sender_id and failed
59
- # and then sent with from_phone
60
- expect(response.from).to eq(from_phone)
61
- end
50
+ response = SmsBroker.message(text_message).to(to_phone).deliver
62
51
 
52
+ expect(response.success?).to eq(true)
53
+ # this means that it tried to send with sender_id and failed
54
+ # and then sent with from_phone
55
+ expect(response.from).to eq(from_phone)
63
56
  end
64
-
65
57
  end
66
58
 
67
59
  context 'invalid' do
68
-
69
60
  it 'should return error for invalid credentials' do
70
61
  SmsBroker.setup do |config|
71
62
  config.nexmo_setup \
@@ -83,10 +74,23 @@ describe SmsBroker do
83
74
  expect(response.serialized.length).to be > 0
84
75
  end
85
76
 
86
- end
77
+ it 'should return error an unknown error' do
78
+ SmsBroker.setup do |config|
79
+ config.nexmo_setup \
80
+ phone_number: from_phone,
81
+ secret: api_secret,
82
+ key: api_key
83
+ end
87
84
 
88
- end
85
+ stub_nexmo_create_message_unknown_error \
86
+ from_phone, to_phone, text_message
89
87
 
90
- end
88
+ response = SmsBroker.message(text_message).to(to_phone).deliver
91
89
 
90
+ expect(response.success?).to eq(false)
91
+ expect(response.serialized.length).to be > 0
92
+ end
93
+ end
94
+ end
95
+ end
92
96
  end
@@ -0,0 +1,63 @@
1
+ describe SmsBroker do
2
+ context 'SmsBroker' do
3
+ let(:text_message) { 'Hello World' }
4
+
5
+ context 'Valid real calls' do
6
+ before(:all) do
7
+ unless ENV['REAL_PHONE_NUMBER']
8
+ skip 'REAL_PHONE_NUMBER env var is required to run this spec'
9
+ end
10
+
11
+ WebMock.allow_net_connect!
12
+
13
+ SmsBroker.clear_setup
14
+
15
+ SmsBroker.setup do |config|
16
+ config.services %w(nexmo twilio)
17
+
18
+ config.default_service 'nexmo'
19
+
20
+ config.nexmo_setup \
21
+ key: ENV['NEXMO_LIVE_API_KEY'],
22
+ sender_id: ENV['NEXMO_LIVE_SENDER_ID'],
23
+ secret: ENV['NEXMO_LIVE_API_SECRET'],
24
+ phone_number: ENV['NEXMO_LIVE_PHONE_NUMBER']
25
+
26
+ config.twilio_setup \
27
+ sender_id: ENV['TWILIO_LIVE_SENDER_ID'],
28
+ auth_token: ENV['TWILIO_LIVE_AUTH_TOKEN'],
29
+ account_sid: ENV['TWILIO_LIVE_ACCOUNT_SID'],
30
+ phone_number: ENV['TWILIO_LIVE_PHONE_NUMBER']
31
+ end
32
+ end
33
+
34
+ after(:all) do
35
+ WebMock.disable_net_connect!
36
+ end
37
+
38
+ context 'Nexmo' do
39
+ it 'should successfuly send message' do
40
+ message = \
41
+ SmsBroker
42
+ .service(:nexmo)
43
+ .message(text_message)
44
+ .to(ENV['REAL_PHONE_NUMBER'])
45
+
46
+ expect(message.deliver.success?).to eq true
47
+ end
48
+ end
49
+
50
+ context 'Twillio' do
51
+ it 'should successfuly send message' do
52
+ message = \
53
+ SmsBroker
54
+ .service(:twilio)
55
+ .message(text_message)
56
+ .to(ENV['REAL_PHONE_NUMBER'])
57
+
58
+ expect(message.deliver.success?).to eq true
59
+ end
60
+ end
61
+ end
62
+ end
63
+ end
@@ -1,7 +1,5 @@
1
1
  describe SmsBroker do
2
-
3
2
  context 'Setup' do
4
-
5
3
  before(:each) do
6
4
  SmsBroker.clear_setup
7
5
  end
@@ -9,7 +7,7 @@ describe SmsBroker do
9
7
  it 'should set configs' do
10
8
  setup = \
11
9
  SmsBroker.setup do |config|
12
- config.services ['nexmo', 'twilio']
10
+ config.services %w(nexmo twilio)
13
11
 
14
12
  config.default_service 'nexmo'
15
13
 
@@ -27,7 +25,7 @@ describe SmsBroker do
27
25
  expect(setup.valid?).to eq(true)
28
26
 
29
27
  expect(setup.options[:services]).to \
30
- eq(['nexmo', 'twilio'])
28
+ eq(%w(nexmo twilio))
31
29
 
32
30
  expect(setup.options[:default_service]).to \
33
31
  eq('nexmo')
@@ -46,13 +44,10 @@ describe SmsBroker do
46
44
  end
47
45
 
48
46
  context 'valid configs' do
49
-
50
47
  before(:each) do
51
48
  SmsBroker.setup do |config|
52
- config.services ['nexmo', 'twilio']
53
-
49
+ config.services %w(nexmo twilio)
54
50
  config.default_service 'nexmo'
55
-
56
51
  config.nexmo_setup \
57
52
  key: 'NEXMO_API_KEY',
58
53
  phone_number: 'phone',
@@ -76,15 +71,12 @@ describe SmsBroker do
76
71
 
77
72
  expect(service.client).to be_a SmsBroker::Client::Twilio
78
73
  end
79
-
80
74
  end
81
75
 
82
76
  context 'invalid configs' do
83
-
84
77
  it 'should return error about available services' do
85
78
  setup = SmsBroker.setup do |config|
86
- config.services ['nexmo', 'twilio']
87
-
79
+ config.services %w(nexmo twilio)
88
80
  config.default_service 'nexmo'
89
81
  end
90
82
 
@@ -108,43 +100,38 @@ describe SmsBroker do
108
100
 
109
101
  it 'should return error default_service' do
110
102
  setup = SmsBroker.setup do |config|
111
- config.services ['nexmo', 'twilio']
112
-
103
+ config.services %w(nexmo twilio)
113
104
  config.default_service 'nope'
114
105
  end
115
106
 
116
107
  expect(setup.valid?).to eq(false)
117
108
 
118
109
  expect(setup.errors[:default_service]).to \
119
- include("must be within [\"nexmo\", \"twilio\"]")
110
+ include('must be within ["nexmo", "twilio"]')
120
111
  end
121
112
 
122
113
  context 'setup!' do
123
-
124
114
  it 'should raise exception for invalid setup' do
125
- expect {
115
+ expect do
126
116
  SmsBroker.setup! do |config|
127
- config.services ['nexmo', 'twilio']
117
+ config.services %w(nexmo twilio)
128
118
  config.default_service 'nope'
129
119
  end
130
- }.to raise_error(SmsBroker::Exceptions::InvalidSetup)
120
+ end.to raise_error(SmsBroker::Exceptions::InvalidSetup)
131
121
  end
132
122
 
133
123
  it 'should return error for missing required service setup' do
134
- expect {
124
+ expect do
135
125
  SmsBroker.setup! do |config|
136
126
  config.default_service 'nexmo'
137
127
 
138
128
  config.nexmo_setup \
139
129
  not_exists: 'key'
140
130
  end
141
- }.to raise_error(SmsBroker::Exceptions::InvalidSetup)
131
+ end.to raise_error(SmsBroker::Exceptions::InvalidSetup)
142
132
  end
143
-
144
133
  end
145
-
146
134
  end
147
-
148
135
  end
149
136
 
150
137
  it 'should respond_to :message' do
@@ -154,8 +141,7 @@ describe SmsBroker do
154
141
  it 'should raise InvalidSetup for service not being setup' do
155
142
  SmsBroker.clear_setup
156
143
 
157
- expect{ SmsBroker::Service.get(:twilio) }.to \
144
+ expect { SmsBroker::Service.get(:twilio) }.to \
158
145
  raise_error SmsBroker::Exceptions::InvalidSetup
159
146
  end
160
-
161
147
  end
@@ -1,23 +1,20 @@
1
1
  describe SmsBroker do
2
-
3
2
  context 'SmsBroker' do
4
-
5
3
  let(:text_message) { 'Hello World' }
4
+ let(:account_sid) { 'account_sid' }
5
+ let(:auth_token) { 'auth_token' }
6
6
 
7
7
  context 'Invalid data when trying to deliver' do
8
-
9
- before(:all) do
8
+ before(:each) do
10
9
  SmsBroker.clear_setup
11
10
 
12
11
  SmsBroker.setup do |config|
13
12
  config.services ['twilio']
14
-
15
13
  config.default_service 'twilio'
16
-
17
14
  config.twilio_setup \
18
15
  phone_number: '15005550001',
19
- account_sid: ENV['TWILIO_ACCOUNT_SID'],
20
- auth_token: ENV['TWILIO_AUTH_TOKEN']
16
+ account_sid: account_sid,
17
+ auth_token: auth_token
21
18
  end
22
19
  end
23
20
 
@@ -25,121 +22,50 @@ describe SmsBroker do
25
22
  response = SmsBroker.service.message(text_message).to(nil).deliver
26
23
 
27
24
  expect(response.success?).to eq(false)
28
- expect(response.serialized[:errors][:to]).to include("is required")
25
+ expect(response.serialized[:errors][:to]).to include('is required')
29
26
  end
30
27
 
31
28
  it 'should return error for missing message' do
32
29
  response = SmsBroker.service.message(nil).to(nil).deliver
33
30
 
34
31
  expect(response.success?).to eq(false)
35
- expect(response.serialized[:errors][:message]).to include("is required")
32
+ expect(response.serialized[:errors][:message]).to include('is required')
36
33
  end
37
34
 
38
35
  it 'should return error for invalid message' do
39
- message_160 = "Lorem Ipsum is simply dummy text of the printing and " \
40
- "typesetting industry. Lorem Ipsum has been the " \
41
- "industry's standard dummy text ever since the 1500s, when an"
36
+ message_160 = 'Lorem Ipsum is simply dummy text of the printing and ' \
37
+ 'typesetting industry. Lorem Ipsum has been the ' \
38
+ 'industrys standard dummy text ever since the 1500s, ' \
39
+ 'when an'
42
40
 
43
41
  response = \
44
42
  SmsBroker.service.message(message_160).to('44123457891').deliver
45
43
 
46
44
  expect(response.success?).to eq(false)
47
45
  expect(response.serialized[:errors][:message]).to \
48
- include("cannot have length greater than 140")
46
+ include('cannot have length greater than 140')
49
47
  end
50
-
51
48
  end
52
49
 
53
50
  context 'Invalid setup when trying to deliver' do
54
-
55
51
  before(:all) do
56
52
  SmsBroker.clear_setup
57
53
 
58
- @setu = SmsBroker.setup do |config|
54
+ SmsBroker.setup do |config|
59
55
  config.services ['nexmo']
60
-
61
56
  config.default_service 'nexmo'
62
-
63
57
  config.nexmo_setup \
64
- key: ENV['NEXMO_API_KEY'],
65
- sender_id: ENV['NEXMO_SENDER_ID'],
66
- phone_number: ENV['NEXMO_PHONE_NUMBER']
67
- # secret: ENV['NEXMO_API_SECRET']
58
+ key: 'nexmo_api_key',
59
+ secret: 'nexmo_api_secret',
60
+ sender_id: 'nexmo_sender_id'
68
61
  end
69
62
  end
70
63
 
71
- it 'should return error for missing number' do
72
- expect {
64
+ it 'should return error for missing phone_number' do
65
+ expect do
73
66
  SmsBroker.service.message(text_message).to('44123457891').deliver
74
- }.to raise_error SmsBroker::Exceptions::InvalidService
75
- end
76
-
77
- end
78
-
79
- context 'Valid real calls' do
80
-
81
- before(:all) do
82
- SmsBroker.clear_setup
83
-
84
- SmsBroker.setup do |config|
85
- config.services ['nexmo', 'twilio']
86
-
87
- config.default_service 'nexmo'
88
-
89
- config.nexmo_setup \
90
- key: ENV['NEXMO_API_KEY'],
91
- secret: ENV['NEXMO_API_SECRET'],
92
- sender_id: ENV['NEXMO_SENDER_ID'],
93
- phone_number: ENV['NEXMO_PHONE_NUMBER']
94
-
95
- config.twilio_setup \
96
- sender_id: ENV['TWILIO_LIVE_SENDER_ID'],
97
- auth_token: ENV['TWILIO_LIVE_AUTH_TOKEN'],
98
- account_sid: ENV['TWILIO_LIVE_ACCOUNT_SID'],
99
- phone_number: ENV['TWILIO_LIVE_PHONE_NUMBER']
100
- end
101
- end
102
-
103
- before(:all) do
104
- unless ENV['REAL_PHONE_NUMBER']
105
- skip "REAL_PHONE_NUMBER env var is required to run this spec"
106
- end
107
-
108
- WebMock.allow_net_connect!
109
- end
110
-
111
- after(:all) do
112
- WebMock.disable_net_connect!
113
- end
114
-
115
- context 'Nexmo' do
116
-
117
- it "should successfuly send message" do
118
- message = \
119
- SmsBroker.service(:nexmo).message('test').to(ENV['REAL_PHONE_NUMBER'])
120
-
121
- response = message.deliver
122
-
123
- expect(response.success?).to eq true
124
- end
125
-
67
+ end.to raise_error SmsBroker::Exceptions::InvalidService
126
68
  end
127
-
128
- context 'Twillio' do
129
-
130
- it "should successfuly send message" do
131
- message = \
132
- SmsBroker.service(:twilio).message('test').to(ENV['REAL_PHONE_NUMBER'])
133
-
134
- response = message.deliver
135
-
136
- expect(response.success?).to eq true
137
- end
138
-
139
- end
140
-
141
69
  end
142
-
143
70
  end
144
-
145
71
  end
@@ -1,93 +1,76 @@
1
1
  describe SmsBroker do
2
-
3
2
  context 'Twilio' do
4
-
5
3
  context '#send_message' do
6
-
7
4
  let(:text_message) { 'Hello World' }
8
- let(:from_phone) { ENV['TWILIO_PHONE_NUMBER'] }
9
- let(:sender_id) { ENV['TWILIO_SENDER_ID'] }
5
+ let(:from_phone) { '15005550006' }
6
+ let(:account_sid) { 'account_sid' }
7
+ let(:auth_token) { 'auth_token' }
10
8
 
11
9
  before(:each) do
12
10
  SmsBroker.clear_setup
13
11
  end
14
12
 
15
- def send_message(text, to)
16
- WebMock.allow_net_connect!
17
-
18
- response = SmsBroker.service.message(text).to(to).deliver
19
-
20
- WebMock.disable_net_connect!
21
-
22
- response
13
+ def send_message(text_message, to)
14
+ SmsBroker.service.message(text_message).to(to).deliver
23
15
  end
24
16
 
25
17
  context 'valid' do
26
-
27
18
  it 'should send message with success' do
28
19
  SmsBroker.setup do |config|
29
20
  config.services ['twilio']
30
-
31
21
  config.default_service 'twilio'
32
-
33
22
  config.twilio_setup \
34
23
  phone_number: from_phone,
35
- account_sid: ENV['TWILIO_ACCOUNT_SID'],
36
- auth_token: ENV['TWILIO_AUTH_TOKEN']
24
+ account_sid: account_sid,
25
+ auth_token: auth_token
37
26
  end
38
27
 
39
- response = send_message(text_message, '15005550006')
28
+ VCR.use_cassette('twilio/create_success') do
29
+ response = send_message(text_message, '15005550006')
40
30
 
41
- expect(response.service).to eq(:twilio)
42
- expect(response.success?).to eq(true)
43
- expect(response.message_id).not_to eq(nil)
31
+ expect(response.service).to eq(:twilio)
32
+ expect(response.success?).to eq(true)
33
+ expect(response.message_id).not_to eq(nil)
34
+ end
44
35
  end
45
36
 
46
37
  context 'with sender_id' do
47
-
48
38
  before(:each) do
49
39
  SmsBroker.setup do |config|
50
40
  config.services ['twilio']
51
-
52
41
  config.default_service 'twilio'
53
-
54
42
  config.twilio_setup \
55
43
  phone_number: '15005550001',
56
- account_sid: ENV['TWILIO_ACCOUNT_SID'],
57
- auth_token: ENV['TWILIO_AUTH_TOKEN']
44
+ account_sid: account_sid,
45
+ auth_token: auth_token
58
46
  end
59
47
  end
60
48
 
61
49
  it 'should return error for invalid sender_id' do
62
- response = send_message(text_message, '15005550006')
63
-
64
- expect(response.service).to eq(:twilio)
65
-
66
- expect(response.success?).to eq(false)
67
-
68
- expect(response.serialized[:errors].keys).to include('21212')
69
- # if the code is 21212 and message includes the phone_number,
70
- # it means that it tried with sender_id and failed
71
- expect(response.serialized[:errors]['21212'][0]).to \
72
- include("The 'From' number 15005550001 is not a valid")
50
+ VCR.use_cassette('twilio/create_invalid_phone_error') do
51
+ response = send_message(text_message, '15005550006')
52
+
53
+ expect(response.service).to eq(:twilio)
54
+ expect(response.success?).to eq(false)
55
+ expect(response.serialized[:errors].keys).to include('21212')
56
+ # if the code is 21212 and message includes the phone_number,
57
+ # it means that it tried with sender_id and failed
58
+ expect(response.serialized[:errors]['21212'][0]).to \
59
+ include("The 'From' number +15005550001 is not a valid")
60
+ end
73
61
  end
74
-
75
62
  end
76
-
77
63
  end
78
64
 
79
65
  context 'invalid' do
80
-
81
66
  it 'should return error for missing required data' do
82
67
  SmsBroker.setup do |config|
83
68
  config.services ['twilio']
84
-
85
69
  config.default_service 'twilio'
86
-
87
70
  config.twilio_setup \
88
71
  phone_number: '15005550001',
89
- account_sid: ENV['TWILIO_ACCOUNT_SID'],
90
- auth_token: ENV['TWILIO_AUTH_TOKEN']
72
+ account_sid: account_sid,
73
+ auth_token: auth_token
91
74
  end
92
75
 
93
76
  response = SmsBroker.message(text_message).deliver
@@ -98,25 +81,21 @@ describe SmsBroker do
98
81
  it 'should return error for invalid from phone' do
99
82
  SmsBroker.setup do |config|
100
83
  config.services ['twilio']
101
-
102
84
  config.default_service 'twilio'
103
-
104
85
  config.twilio_setup \
105
86
  phone_number: '15005550001',
106
- account_sid: ENV['TWILIO_ACCOUNT_SID'],
107
- auth_token: ENV['TWILIO_AUTH_TOKEN']
87
+ account_sid: account_sid,
88
+ auth_token: auth_token
108
89
  end
109
90
 
110
- response = send_message(text_message, '15005550006')
91
+ VCR.use_cassette('twilio/create_invalid_phone_error') do
92
+ response = send_message(text_message, '15005550006')
111
93
 
112
- expect(response.success?).to eq(false)
113
- expect(response.serialized.length).to be > 0
94
+ expect(response.success?).to eq(false)
95
+ expect(response.serialized.length).to be > 0
96
+ end
114
97
  end
115
-
116
98
  end
117
-
118
99
  end
119
-
120
100
  end
121
-
122
101
  end
data/spec/spec_helper.rb CHANGED
@@ -1,7 +1,10 @@
1
1
  require 'pry'
2
- require 'yaml'
2
+ require 'vcr'
3
3
  require 'simplecov'
4
4
  require 'codeclimate-test-reporter'
5
+ require 'sms_broker'
6
+ require 'webmock/rspec'
7
+ require 'support/nexmo_helpers'
5
8
 
6
9
  SimpleCov.start do
7
10
  formatter SimpleCov::Formatter::MultiFormatter.new [
@@ -10,20 +13,11 @@ SimpleCov.start do
10
13
  ]
11
14
  end
12
15
 
13
- # load service_keys.yml to ENV
14
- yml_file = File.expand_path('../support/services_keys.yml', __FILE__)
15
-
16
- YAML.load(File.read(yml_file)).each do |key, value|
17
- ENV[key] = value
18
- end
19
-
20
- require 'sms_broker'
21
- require 'webmock/rspec'
22
-
23
- require 'support/nexmo_helpers'
24
-
25
16
  RSpec.configure do |config|
26
-
27
17
  config.include NexmoHelpers
18
+ end
28
19
 
20
+ VCR.configure do |config|
21
+ config.cassette_library_dir = 'spec/support/vcr_cassettes'
22
+ config.hook_into :webmock
29
23
  end