bvr 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,335 @@
1
+ require_relative "../../spec_helper"
2
+ require_relative "../../helpers/faraday_stub"
3
+
4
+ describe Bvr::Customer do
5
+
6
+ describe '.authenticate(id, password)' do
7
+ include FaradayStub
8
+
9
+ let(:customer_id) { 'foo' }
10
+ let(:password) { 'bar' }
11
+ let(:options) do
12
+ {
13
+ command: Bvr::Customer::API_COMMANDS[:authenticate],
14
+ customer: customer_id,
15
+ customerpassword: password
16
+ }
17
+ end
18
+ let(:response) do
19
+ File.read(File.join(File.dirname(__FILE__), '/..', '/..', '/fixtures/validateuser.xml'))
20
+ end
21
+
22
+ subject{ Bvr::Customer.authenticate(customer_id, password) }
23
+
24
+ before do
25
+ faraday_adapter.get(Bvr::Connection.uri_from_h(options)) { [200, {}, response] }
26
+ end
27
+
28
+ it 'returns a boolean' do
29
+ subject.must_equal true
30
+ end
31
+ end
32
+
33
+ describe '.block(id, block=true)' do
34
+ include FaradayStub
35
+
36
+ let(:customer_id) { 'foo' }
37
+ let(:block) { true }
38
+ let(:options) do
39
+ {
40
+ command: Bvr::Customer::API_COMMANDS[:changeuserinfo],
41
+ customer: customer_id,
42
+ customerblocked: block
43
+ }
44
+ end
45
+ let(:response) do
46
+ File.read(File.join(File.dirname(__FILE__), '/..', '/..', '/fixtures/customerblocked.xml'))
47
+ end
48
+
49
+ subject{ Bvr::Customer.block(customer_id, block) }
50
+
51
+ before do
52
+ faraday_adapter.get(Bvr::Connection.uri_from_h(options)) { [200, {}, response] }
53
+ end
54
+
55
+ it 'return a response' do
56
+ subject['Result'][0].must_be_instance_of String
57
+ subject['Customer'].must_equal "#{customer_id}*provider"
58
+ end
59
+
60
+ describe 'when block is not a boolean' do
61
+ let(:block) { 'foo' }
62
+
63
+ subject{ Bvr::Customer.block(customer_id, block) }
64
+
65
+ it 'raise an ArgumentError' do
66
+ proc { subject }.must_raise ArgumentError
67
+ end
68
+ end
69
+ end
70
+
71
+ describe '.change_password(id, old_pass, new_pass)' do
72
+ include FaradayStub
73
+
74
+ let(:customer_id) { 'foo' }
75
+ let(:old_password) { 'bar' }
76
+ let(:new_password) { 'barbar' }
77
+ let(:options) do
78
+ {
79
+ command: Bvr::Customer::API_COMMANDS[:changepassword],
80
+ customer: customer_id,
81
+ oldcustomerpassword: old_password,
82
+ newcustomerpassword: new_password
83
+ }
84
+ end
85
+ let(:response) do
86
+ File.read(File.join(File.dirname(__FILE__), '/..', '/..', '/fixtures/changepassword.xml'))
87
+ end
88
+
89
+ before do
90
+ faraday_adapter.get(Bvr::Connection.uri_from_h(options)) { [200, {}, response] }
91
+ end
92
+
93
+ subject { Bvr::Customer.change_password(customer_id, old_password, new_password) }
94
+
95
+ it 'return response' do
96
+ subject['Result'].must_be_instance_of String
97
+ end
98
+ end
99
+
100
+ describe '.create(options)' do
101
+ include FaradayStub
102
+
103
+ let(:options) { { customer: 'foo', customerpassword: 'bar' } }
104
+ let(:response) do
105
+ File.read(File.join(File.dirname(__FILE__), '/..', '/..', '/fixtures/createcustomer_response.xml'))
106
+ end
107
+
108
+ subject{ Bvr::Customer.create(options) }
109
+
110
+ before do
111
+ faraday_adapter.get(Bvr::Connection.uri_from_h(options)) { [200, {}, response] }
112
+ end
113
+
114
+ it 'returns the response' do
115
+ subject['Result'].must_be_instance_of String
116
+ end
117
+
118
+ describe 'when options are invalid' do
119
+ let(:options) { { foo: 'bar' } }
120
+
121
+ it 'raise an ArgumentError' do
122
+ proc { subject }.must_raise ArgumentError
123
+ end
124
+ end
125
+ end
126
+
127
+ describe '.find(id)' do
128
+ include FaradayStub
129
+
130
+ let(:connection) { Minitest::Mock.new }
131
+ let(:id) { 'foo' }
132
+ let(:params) { { command: Bvr::Customer::API_COMMANDS[:find], customer: id } }
133
+
134
+ subject { Bvr::Customer.find(id) }
135
+
136
+ it 'calls the api with the right params' do
137
+ Bvr.connection = connection
138
+ Bvr::Customer.stub(:new_from_response, nil) do
139
+ connection.expect :get, {}, [params]
140
+ subject
141
+ connection.verify
142
+ end
143
+ end
144
+
145
+ describe 'when there is a response' do
146
+ let(:response) do
147
+ File.read(File.join(File.dirname(__FILE__), '/..', '/..', '/fixtures/getuserinfo.xml'))
148
+ end
149
+
150
+ before do
151
+ faraday_adapter.get(Bvr::Connection.uri_from_h(params)) { [200, {}, response] }
152
+ end
153
+
154
+ it 'creates a new Bvr::Customer from the response' do
155
+ subject.must_be_instance_of Bvr::Customer
156
+ end
157
+ end
158
+ end
159
+
160
+ describe '.new_from_response(h)' do
161
+ let(:h) do
162
+ ::XmlSimple.xml_in File.read(File.join(File.dirname(__FILE__), '/..', '/..', '/fixtures/getuserinfo.xml')), {ForceArray: false}
163
+ end
164
+
165
+ subject { Bvr::Customer.new_from_response(h) }
166
+
167
+ it 'returns a new Bvr::Customer with the right params' do
168
+ subject.must_be_instance_of Bvr::Customer
169
+ subject.id.must_equal 'foo*provider'
170
+ subject.email.must_equal 'JohnDoe@gmail.com'
171
+ subject.raw_blocked.must_equal 'False'
172
+
173
+ subject.credit.must_be_instance_of Bvr::Credit
174
+ subject.credit.raw_balance.must_equal '1.86'
175
+ subject.credit.raw_specific_balance.must_equal '1.86828'
176
+
177
+ subject.phones.must_be_instance_of Bvr::PhoneCollection
178
+ subject.phones.must_include Bvr::Phone.new('+4412345678')
179
+ subject.phones.must_include Bvr::Phone.new('+4412345679')
180
+ end
181
+ end
182
+
183
+ describe '#blocked?' do
184
+ subject { customer.blocked? }
185
+
186
+ describe 'when raw_blocked is false' do
187
+ let(:customer) { Bvr::Customer.new('foo').tap{ |c| c.raw_blocked = 'False' } }
188
+ it 'returns true' do
189
+ subject.must_equal false
190
+ end
191
+ end
192
+
193
+ describe 'when raw_blocked is true' do
194
+ let(:customer) { Bvr::Customer.new('foo').tap{ |c| c.raw_blocked = 'True' } }
195
+ it 'returns true' do
196
+ subject.must_equal true
197
+ end
198
+ end
199
+ end
200
+
201
+ describe '#block!' do
202
+ include FaradayStub
203
+
204
+ let(:customer_id) { 'foo' }
205
+ let(:raw_blocked) { 'bar' }
206
+ let(:customer) { Bvr::Customer.new(customer_id).tap{ |c| c.raw_blocked = raw_blocked } }
207
+ let(:block) { true }
208
+ let(:options) do
209
+ {
210
+ command: Bvr::Customer::API_COMMANDS[:changeuserinfo],
211
+ customer: customer_id,
212
+ customerblocked: block
213
+ }
214
+ end
215
+ let(:response) do
216
+ File.read(File.join(File.dirname(__FILE__), '/..', '/..', '/fixtures/customerblocked.xml'))
217
+ end
218
+
219
+ before do
220
+ faraday_adapter.get(Bvr::Connection.uri_from_h(options)) { [200, {}, response] }
221
+ end
222
+
223
+ subject { customer.block! }
224
+
225
+ it 'updates the attribute of the customer' do
226
+ customer.raw_blocked.must_equal raw_blocked
227
+ subject
228
+ customer.raw_blocked.must_equal Bvr::Customer::BLOCKED_VALUES[true]
229
+ end
230
+ end
231
+
232
+ describe '#change_password(new_password)' do
233
+ include FaradayStub
234
+
235
+ let(:customer_id) { 'foo' }
236
+ let(:old_password) { 'bar' }
237
+ let(:new_password) { 'barbar' }
238
+ let(:customer) { Bvr::Customer.new(customer_id).tap{ |c| c.password = old_password } }
239
+ let(:options) do
240
+ {
241
+ command: Bvr::Customer::API_COMMANDS[:changepassword],
242
+ customer: customer_id,
243
+ oldcustomerpassword: old_password,
244
+ newcustomerpassword: new_password
245
+ }
246
+ end
247
+ let(:response) do
248
+ File.read(File.join(File.dirname(__FILE__), '/..', '/..', '/fixtures/changepassword.xml'))
249
+ end
250
+
251
+ before do
252
+ faraday_adapter.get(Bvr::Connection.uri_from_h(options)) { [200, {}, response] }
253
+ end
254
+
255
+ subject { customer.change_password(new_password) }
256
+
257
+ it 'changes the password of the user' do
258
+ customer.password.must_equal old_password
259
+ subject
260
+ customer.password.must_equal new_password
261
+ end
262
+
263
+ end
264
+
265
+ describe '#unblock!' do
266
+ include FaradayStub
267
+
268
+ let(:customer_id) { 'foo' }
269
+ let(:raw_blocked) { 'bar' }
270
+ let(:customer) { Bvr::Customer.new(customer_id).tap{ |c| c.raw_blocked = raw_blocked } }
271
+ let(:block) { false }
272
+ let(:options) do
273
+ {
274
+ command: Bvr::Customer::API_COMMANDS[:changeuserinfo],
275
+ customer: customer_id,
276
+ customerblocked: block
277
+ }
278
+ end
279
+ let(:response) do
280
+ File.read(File.join(File.dirname(__FILE__), '/..', '/..', '/fixtures/customerblocked.xml'))
281
+ end
282
+
283
+ before do
284
+ faraday_adapter.get(Bvr::Connection.uri_from_h(options)) { [200, {}, response] }
285
+ end
286
+
287
+ subject { customer.unblock! }
288
+
289
+ it 'updates the attribute of the customer' do
290
+ customer.raw_blocked.must_equal raw_blocked
291
+ subject
292
+ customer.raw_blocked.must_equal Bvr::Customer::BLOCKED_VALUES[false]
293
+ end
294
+ end
295
+
296
+ describe '#calls(options={})' do
297
+ include FaradayStub
298
+
299
+ let(:h) do
300
+ ::XmlSimple.xml_in File.read(File.join(File.dirname(__FILE__), '/..', '/..', '/fixtures/getuserinfo.xml')), {ForceArray: false}
301
+ end
302
+ let(:calls_response) do
303
+ File.read(File.join(File.dirname(__FILE__), '/..', '/..', '/fixtures/calloverview.xml'))
304
+ end
305
+ let(:customer) { Bvr::Customer.new_from_response(h) }
306
+ let(:options) { {} }
307
+ let(:params) { { command: Bvr::CallCollection::API_COMMANDS[:find_by_customer_id], customer: customer.id } }
308
+
309
+ subject { customer.calls(options) }
310
+
311
+ before do
312
+ faraday_adapter.get(Bvr::Connection.uri_from_h(params)) { [200, {}, calls_response] }
313
+ end
314
+
315
+ it 'returns an instance of Bvr::CallCollection' do
316
+ subject.must_be_instance_of Bvr::CallCollection
317
+ end
318
+
319
+ describe 'when options are the same' do
320
+ let(:call_collection_id) { customer.calls(options).__id__ }
321
+
322
+ it 'returns the same object' do
323
+ subject.__id__.must_equal call_collection_id
324
+ end
325
+ end
326
+
327
+ describe 'wen options are not the same' do
328
+ let(:call_collection_id) { customer.calls({date: 'bar'}).__id__ }
329
+
330
+ it 'returns the same object' do
331
+ subject.__id__.wont_equal call_collection_id
332
+ end
333
+ end
334
+ end
335
+ end
@@ -0,0 +1,95 @@
1
+ require_relative '../../spec_helper'
2
+ require_relative "../../helpers/faraday_stub"
3
+
4
+ describe Bvr::PhoneCollection do
5
+ describe '.add(customer_id, phone, add="add")' do
6
+ include FaradayStub
7
+
8
+ let(:customer_id) { 'foo' }
9
+ let(:phone) { "+4412345678" }
10
+ let(:geocallcli_options) { "add" }
11
+ let(:options) do
12
+ {
13
+ command: Bvr::PhoneCollection::API_COMMANDS[:add],
14
+ customer: customer_id,
15
+ geocallcli_options: geocallcli_options,
16
+ geocallcli: phone
17
+ }
18
+ end
19
+ let(:response) do
20
+ File.read(File.join(File.dirname(__FILE__), '/..', '/..', '/fixtures/customerblocked.xml'))
21
+ end
22
+
23
+ subject{ Bvr::PhoneCollection.add(customer_id, phone) }
24
+
25
+ before do
26
+ faraday_adapter.get(Bvr::Connection.uri_from_h(options)) { [200, {}, response] }
27
+ end
28
+
29
+ it 'return result' do
30
+ subject['Result'][0].must_be_instance_of String
31
+ end
32
+ end
33
+
34
+ describe '#add(phone)' do
35
+ include FaradayStub
36
+
37
+ let(:customer) { Bvr::Customer.new('foo') }
38
+ let(:phone) { Bvr::Phone.new("+4412345678") }
39
+ let(:geocallcli_options) { "add" }
40
+ let(:options) do
41
+ {
42
+ command: Bvr::PhoneCollection::API_COMMANDS[:add],
43
+ customer: customer.id,
44
+ geocallcli_options: geocallcli_options,
45
+ geocallcli: phone.number
46
+ }
47
+ end
48
+ let(:response) do
49
+ File.read(File.join(File.dirname(__FILE__), '/..', '/..', '/fixtures/customerblocked.xml'))
50
+ end
51
+
52
+ subject{ customer.phones.add(phone) }
53
+
54
+ before do
55
+ faraday_adapter.get(Bvr::Connection.uri_from_h(options)) { [200, {}, response] }
56
+ end
57
+
58
+ it 'adds a phone' do
59
+ customer.phones.collection.wont_include phone
60
+ subject.must_equal true
61
+ customer.phones.collection.must_include phone
62
+ end
63
+ end
64
+
65
+ describe '#rm(phone)' do
66
+ include FaradayStub
67
+
68
+ let(:phone) { Bvr::Phone.new("+4412345678") }
69
+ let(:customer) { Bvr::Customer.new('foo').tap{ |c| c.phones.collection << phone } }
70
+ let(:geocallcli_options) { "delete" }
71
+ let(:options) do
72
+ {
73
+ command: Bvr::PhoneCollection::API_COMMANDS[:add],
74
+ customer: customer.id,
75
+ geocallcli_options: geocallcli_options,
76
+ geocallcli: phone.number
77
+ }
78
+ end
79
+ let(:response) do
80
+ File.read(File.join(File.dirname(__FILE__), '/..', '/..', '/fixtures/customerblocked.xml'))
81
+ end
82
+
83
+ subject{ customer.phones.rm(phone) }
84
+
85
+ before do
86
+ faraday_adapter.get(Bvr::Connection.uri_from_h(options)) { [200, {}, response] }
87
+ end
88
+
89
+ it 'adds a phone' do
90
+ customer.phones.collection.must_include phone
91
+ subject.must_equal true
92
+ customer.phones.collection.wont_include phone
93
+ end
94
+ end
95
+ end
@@ -19,4 +19,24 @@ describe Bvr do
19
19
  subject.config.password.must_equal password
20
20
  end
21
21
  end
22
+
23
+ describe '.connection' do
24
+ subject { Bvr.connection }
25
+
26
+ describe 'when @config is nil' do
27
+ before { Bvr.config = nil }
28
+
29
+ it 'raise an exception if @config is nil' do
30
+ proc { subject }.must_raise Exception
31
+ end
32
+ end
33
+
34
+ describe 'when @config is set' do
35
+ before { Bvr.config = 'foo' }
36
+
37
+ it 'returns a Bvr::Connection' do
38
+ subject.must_be_instance_of Bvr::Connection
39
+ end
40
+ end
41
+ end
22
42
  end