elk 0.0.13 → 0.5.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.
@@ -1,117 +0,0 @@
1
- require 'spec_helper'
2
- require 'elk'
3
-
4
- describe Elk::Number do
5
- before { configure_elk }
6
-
7
- it 'allocates a number' do
8
- stub_request(:post, "https://USERNAME:PASSWORD@api.46elks.com/a1/Numbers").
9
- with(:body => {"country" => "se", "sms_url" => "http://localhost/receive"},
10
- :headers => {'Accept'=>'application/json', 'Content-Type'=>'application/x-www-form-urlencoded'}).
11
- to_return(fixture('allocates_a_number.txt'))
12
-
13
- number = described_class.allocate(:sms_url => 'http://localhost/receive', :country => 'se')
14
- number.status.should == :active
15
- number.sms_url.should == 'http://localhost/receive'
16
- number.country.should == 'se'
17
- number.number.should == '+46766861012'
18
- number.capabilities.should == [:sms]
19
- end
20
-
21
- it 'gets allocated numbers' do
22
- stub_request(:get, "https://USERNAME:PASSWORD@api.46elks.com/a1/Numbers").
23
- with(:headers => {'Accept'=>'application/json'}).
24
- to_return(fixture('gets_allocated_numbers.txt'))
25
-
26
- numbers = described_class.all
27
- numbers.size.should == 2
28
- numbers[0].number_id.should == 'nea19c8e291676fb7003fa1d63bba7899'
29
- numbers[0].number.should == '+46704508449'
30
- numbers[0].sms_url == 'http://localhost/receive1'
31
-
32
- numbers[1].number_id.should == 'nea19c8e291676fb7003fa1d63bba789A'
33
- numbers[1].number.should == '+46761042247'
34
- numbers[0].sms_url == 'http://localhost/receive2'
35
- end
36
-
37
- it 'updates a number' do
38
- stub_request(:get, "https://USERNAME:PASSWORD@api.46elks.com/a1/Numbers").
39
- with(:headers => {'Accept'=>'application/json'}).
40
- to_return(fixture('gets_allocated_numbers.txt'))
41
- stub_request(:post, "https://USERNAME:PASSWORD@api.46elks.com/a1/Numbers/nea19c8e291676fb7003fa1d63bba7899").
42
- with(:body => {"sms_url" => "http://otherhost/receive", "voice_start" => ""},
43
- :headers => {'Accept'=>'application/json', 'Content-Type'=>'application/x-www-form-urlencoded'}).
44
- to_return(fixture('updates_a_number.txt'))
45
-
46
- number = described_class.all[0]
47
- number.country = 'no'
48
- number.sms_url = 'http://otherhost/receive'
49
- number.save.should == true
50
- number.country.should == 'no'
51
- number.sms_url.should == 'http://otherhost/receive'
52
- end
53
-
54
- it 'deallocates a number' do
55
- stub_request(:get, "https://USERNAME:PASSWORD@api.46elks.com/a1/Numbers").
56
- with(:headers => {'Accept'=>'application/json'}).
57
- to_return(fixture('gets_allocated_numbers.txt'))
58
- stub_request(:post, "https://USERNAME:PASSWORD@api.46elks.com/a1/Numbers/nea19c8e291676fb7003fa1d63bba7899").
59
- with(:body => {"active" => "no"},
60
- :headers => {'Accept'=>'application/json', 'Content-Type'=>'application/x-www-form-urlencoded'}).
61
- to_return(fixture('deallocates_a_number.txt'))
62
-
63
- number = described_class.all[0]
64
- number.status.should == :active
65
- number.deallocate!.should == true
66
- number.status.should == :deallocated
67
- end
68
-
69
- it 'reloads a number' do
70
- stub_request(:get, "https://USERNAME:PASSWORD@api.46elks.com/a1/Numbers").
71
- with(:headers => {'Accept'=>'application/json'}).
72
- to_return(fixture('gets_allocated_numbers.txt'))
73
- stub_request(:get, "https://USERNAME:PASSWORD@api.46elks.com/a1/Numbers/nea19c8e291676fb7003fa1d63bba7899").
74
- with(:headers => {'Accept'=>'application/json'}).
75
- to_return(fixture('reloads_a_number.txt'))
76
-
77
- number = described_class.all[0]
78
- object_id = number.object_id
79
- loaded_at = number.loaded_at
80
- number.country = 'blah'
81
- number.reload.should == true
82
- number.country.should == 'se'
83
- number.object_id.should == object_id
84
- number.loaded_at.should_not == loaded_at
85
- end
86
-
87
- it 'has wrong password' do
88
- stub_request(:get, "https://USERNAME:WRONG@api.46elks.com/a1/Numbers").
89
- with(:headers => {'Accept'=>'application/json'}).
90
- to_return(fixture('auth_error.txt'))
91
-
92
- Elk.configure do |config|
93
- config.username = 'USERNAME'
94
- config.password = 'WRONG'
95
- end
96
-
97
- expect {
98
- described_class.all
99
- }.to raise_error(Elk::AuthError)
100
- end
101
-
102
- it 'gets server error when looking for all numbers' do
103
- stub_request(:get, "https://USERNAME:PASSWORD@api.46elks.com/a1/Numbers").
104
- with(:headers => {'Accept'=>'application/json'}).
105
- to_return(fixture('server_error.txt'))
106
-
107
- expect {
108
- described_class.all
109
- }.to raise_error(Elk::ServerError)
110
- end
111
-
112
- it 'should handle no parameters' do
113
- expect {
114
- sms = described_class.allocate({})
115
- }.to raise_error(Elk::MissingParameter)
116
- end
117
- end
@@ -1,168 +0,0 @@
1
- require 'spec_helper'
2
- require 'elk'
3
-
4
- describe Elk::SMS do
5
- before { configure_elk }
6
- let(:url) { "https://USERNAME:PASSWORD@api.46elks.com/a1/SMS" }
7
-
8
- it 'sends a SMS' do
9
- stub_request(:post, url).
10
- with(:body => {:from => "+46761042247", :message => "Your order #171 has now been sent!", :to => "+46704508449"},
11
- :headers => {'Accept'=>'application/json', 'Content-Type'=>'application/x-www-form-urlencoded'}).
12
- to_return(fixture('sends_a_sms.txt'))
13
-
14
- # Fake $stderr to check warnings
15
- begin
16
- old_stderr, $stderr = $stderr, StringIO.new
17
-
18
- sms = described_class.send(:from => '+46761042247',
19
- :to => '+46704508449',
20
- :message => 'Your order #171 has now been sent!')
21
-
22
- $stderr.string.should == ""
23
- ensure
24
- $stderr = old_stderr
25
- end
26
-
27
- sms.class.should == described_class
28
- sms.from.should == '+46761042247'
29
- sms.to.should == '+46704508449'
30
- sms.message.should == 'Your order #171 has now been sent!'
31
- sms.direction.should == 'outgoing'
32
- sms.status.should == 'delivered'
33
- end
34
-
35
- it "sends a flash SMS" do
36
- stub = stub_request(:post, url).
37
- to_return(fixture('sends_a_sms.txt'))
38
-
39
- described_class.send(
40
- :from => '+46761042247',
41
- :to => '+46704508449',
42
- :message => 'Your order #171 has now been sent!',
43
- :flash => true
44
- )
45
-
46
- stub.with(
47
- :body => {
48
- :from => '+46761042247',
49
- :to => '+46704508449',
50
- :message => 'Your order #171 has now been sent!',
51
- :flashsms => 'yes'
52
- },
53
- :headers => {'Accept'=>'application/json', 'Content-Type'=>'application/x-www-form-urlencoded'}).
54
- should have_been_made
55
- end
56
-
57
- context 'when sending a SMS to multiple recipients' do
58
- before do
59
- stub_request(:post, url).
60
- with(:body => {:from => "+46761042247", :message => "Your order #171 has now been sent!", :to => "+46704508449,+46704508449"},
61
- :headers => {'Accept'=>'application/json', 'Content-Type'=>'application/x-www-form-urlencoded'}).
62
- to_return(fixture('sends_a_sms_to_multiple_recipients.txt'))
63
- end
64
-
65
- it "sends the SMS when passing `to` as comma separated string" do
66
- smses = described_class.send(:from => '+46761042247',
67
- :to => '+46704508449,+46704508449',
68
- :message => 'Your order #171 has now been sent!')
69
-
70
- smses.size.should == 2
71
- smses[0].class.should == described_class
72
- smses[0].to.should == "+46704508449"
73
-
74
- smses[0].message_id.should == "sb326c7a214f9f4abc90a11bd36d6abc3"
75
- smses[1].message_id.should == "s47a89d6cc51d8db395d45ae7e16e86b7"
76
- end
77
-
78
- it "sends the SMS when passing `to` as array" do
79
- smses = described_class.send(:from => '+46761042247',
80
- :to => ['+46704508449', '+46704508449'],
81
- :message => 'Your order #171 has now been sent!')
82
-
83
- smses.size.should == 2
84
- smses[0].class.should == described_class
85
- smses[0].to.should == "+46704508449"
86
-
87
- smses[0].message_id.should == "sb326c7a214f9f4abc90a11bd36d6abc3"
88
- smses[1].message_id.should == "s47a89d6cc51d8db395d45ae7e16e86b7"
89
- end
90
- end
91
-
92
- it 'gets SMS-history' do
93
- stub_request(:get, url).
94
- with(:headers => {'Accept'=>'application/json'}).
95
- to_return(fixture('sms_history.txt'))
96
-
97
- sms_history = described_class.all
98
-
99
- sms_history.size.should == 3
100
- sms_history[0].class.should == described_class
101
- sms_history[0].created_at.class.should == Time
102
-
103
- sms_history[0].message.should == "Your order #171 has now been sent!"
104
- sms_history[1].message.should == "I'd like to order a pair of elks!"
105
- sms_history[2].message.should == "Want an elk?"
106
- end
107
-
108
- it 'reloads a SMS' do
109
- stub_request(:get, url).
110
- with(:headers => {'Accept'=>'application/json'}).
111
- to_return(fixture('sms_history.txt'))
112
- stub_request(:get, "https://USERNAME:PASSWORD@api.46elks.com/a1/SMS/s8952031bb83bf3e64f8e13b071c131c0").
113
- with(:headers => {'Accept'=>'application/json'}).
114
- to_return(fixture('reloads_a_sms.txt'))
115
-
116
- sms_history = described_class.all
117
- sms = sms_history[0]
118
- loaded_at = sms.loaded_at
119
- object_id = sms.object_id
120
- sms.reload.should == true
121
- sms.object_id.should == object_id
122
- sms.loaded_at.should_not == loaded_at
123
- end
124
-
125
- it 'should warn about capped sms sender' do
126
- stub_request(:post, url).
127
- with(:body => {:from => "VeryVeryVeryVeryLongSenderName", :message => "Your order #171 has now been sent!", :to => "+46704508449"},
128
- :headers => {'Accept'=>'application/json', 'Content-Type'=>'application/x-www-form-urlencoded'}).
129
- to_return(fixture('sends_a_sms_with_long_sender.txt'))
130
-
131
- # Fake $stderr to check warnings
132
- begin
133
- old_stderr, $stderr = $stderr, StringIO.new
134
-
135
- sms = described_class.send(:from => 'VeryVeryVeryVeryLongSenderName',
136
- :to => '+46704508449',
137
- :message => 'Your order #171 has now been sent!')
138
-
139
- $stderr.string.should == "SMS 'from' value VeryVeryVeryVeryLongSenderName will be capped at 11 chars\n"
140
- ensure
141
- $stderr = old_stderr
142
- end
143
-
144
- sms.class.should == described_class
145
- sms.from.should == 'VeryVeryVeryVeryLongSenderName'
146
- sms.to.should == '+46704508449'
147
- sms.message.should == 'Your order #171 has now been sent!'
148
- end
149
-
150
- it 'should handle invalid to number' do
151
- stub_request(:post, url).
152
- with(:body => {:from => "+46761042247", :message => "Your order #171 has now been sent!", :to => "monkey"},
153
- :headers => {'Accept'=>'application/json', 'Content-Type'=>'application/x-www-form-urlencoded'}).
154
- to_return(fixture('invalid_to_number.txt'))
155
-
156
- expect {
157
- sms = described_class.send(:from => '+46761042247',
158
- :to => 'monkey',
159
- :message => 'Your order #171 has now been sent!')
160
- }.to raise_error(Elk::BadRequest, 'Invalid to number')
161
- end
162
-
163
- it 'should handle no parameters' do
164
- expect {
165
- sms = described_class.send({})
166
- }.to raise_error(Elk::MissingParameter)
167
- end
168
- end
@@ -1,58 +0,0 @@
1
- require 'spec_helper'
2
- require 'elk'
3
-
4
- describe Elk do
5
- context "detect missing username and/or password" do
6
- context "when nothing is configured" do
7
- specify do
8
- Elk.configure do |config|
9
- config.username = nil
10
- config.password = nil
11
- end
12
-
13
- expect { Elk.base_url }.to raise_error(Elk::AuthError)
14
- end
15
- end
16
-
17
- context "when username is missing" do
18
- specify do
19
- Elk.configure do |config|
20
- config.username = nil
21
- config.password = 'PASSWORD'
22
- end
23
-
24
- expect { Elk.base_url }.to raise_error(Elk::AuthError)
25
- end
26
- end
27
-
28
- context "when password is missing" do
29
- specify do
30
- Elk.configure do |config|
31
- config.username = 'USERNAME'
32
- config.password = nil
33
- end
34
-
35
- expect { Elk.base_url }.to raise_error(Elk::AuthError)
36
- end
37
- end
38
-
39
- context "when all is configured" do
40
- specify do
41
- Elk.configure do |config|
42
- config.username = 'USERNAME'
43
- config.password = 'PASSWORD'
44
- end
45
-
46
- expect { Elk.base_url }.to_not raise_error(Elk::AuthError)
47
- end
48
- end
49
- end
50
-
51
- it 'should handle garbage json' do
52
- bad_response_body = fixture('bad_response_body.txt').read
53
-
54
- expect {
55
- Elk.parse_json(bad_response_body)
56
- }.to raise_error(Elk::BadResponse)
57
- end
58
- end