charging-client 0.0.2

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.
@@ -0,0 +1,289 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ describe Charging::ChargeAccount, :vcr do
6
+ let(:attributes) do
7
+ {
8
+ bank: '237',
9
+ name: 'Conta de cobrança',
10
+ agreement_code: '12345',
11
+ portfolio_code: '25',
12
+ account: {number: '12345', digit: '6'},
13
+ agency: {number: '12345', digit: '6'},
14
+ currency: 9
15
+ }
16
+ end
17
+ let(:domain) do
18
+ Factory.create_resource(Charging::Domain, current_account, Factory.domain_attributes(Faker.cnpj_generator))
19
+ end
20
+
21
+ context 'for new instance' do
22
+ CA_ATTRIBUTES = [
23
+ :bank, :name, :agreement_code, :portfolio_code, :account, :agency,
24
+ :currency, :supplier_name, :address, :sequence_numbers, :advance_days
25
+ ]
26
+
27
+ let(:response) { double(:response, code: 500) }
28
+
29
+ before do
30
+ VCR.use_cassette('ChargeAccount/for new instance') do
31
+ attributes = Hash[*CA_ATTRIBUTES.map {|attr| [attr, "#{attr} value"] }.flatten]
32
+
33
+ @domain = domain
34
+ @new_charge_account = described_class.new(attributes, domain, response)
35
+ end
36
+ end
37
+
38
+ subject { @new_charge_account }
39
+
40
+ CA_ATTRIBUTES.each do |attribute|
41
+ describe attribute do
42
+ subject { super().send(attribute) }
43
+ it { is_expected.to eq "#{attribute} value"}
44
+ end
45
+ end
46
+
47
+ [:uuid, :uri, :etag, :national_identifier].each do |attribute|
48
+ describe attribute do
49
+ subject { super().send(attribute) }
50
+ it { is_expected.to be_nil }
51
+ end
52
+ end
53
+
54
+ describe '#domain' do
55
+ subject { super().domain }
56
+ it { is_expected.to eq @domain }
57
+ end
58
+
59
+ describe '#last_response' do
60
+ subject { super().last_response }
61
+ it { is_expected.to eq response }
62
+ end
63
+
64
+ describe '#errors' do
65
+ subject { super().errors }
66
+ it { is_expected.to eq [] }
67
+ end
68
+
69
+ specify('#persisted?') { expect(subject).to_not be_persisted }
70
+ specify('#deleted?') { expect(subject).to_not be_deleted }
71
+
72
+ describe '#attributes' do
73
+ subject { super().attributes }
74
+ it do
75
+ is_expected.to eq({
76
+ account: 'account value',
77
+ address: 'address value',
78
+ advance_days: "advance_days value",
79
+ agency: "agency value",
80
+ agreement_code: "agreement_code value",
81
+ bank: "bank value",
82
+ city_state: nil,
83
+ currency: "currency value",
84
+ default_charging_features: nil,
85
+ name: "name value",
86
+ our_number_range: nil,
87
+ portfolio_code: "portfolio_code value",
88
+ sequence_numbers: "sequence_numbers value",
89
+ supplier_name: "supplier_name value",
90
+ zipcode: nil
91
+ })
92
+ end
93
+ end
94
+ end
95
+
96
+ describe '#create!' do
97
+ it 'should require a domain and load errors' do
98
+ charge_account = described_class.new(attributes, nil)
99
+
100
+ expect(charge_account.errors).to be_empty
101
+
102
+ expected_error = [StandardError, 'can not create without a domain']
103
+ expect { charge_account.create! }.to raise_error(*expected_error)
104
+
105
+ expect(charge_account.errors).to eq ['can not create without a domain']
106
+ end
107
+
108
+ context 'when everything is OK' do
109
+ before do
110
+ VCR.use_cassette('ChargeAccount/creating a charge account') do
111
+ @domain = domain
112
+ @created_charge_account = described_class.new(attributes, @domain).create!
113
+ end
114
+ end
115
+
116
+ subject { @created_charge_account }
117
+
118
+ [:uuid, :uri, :etag].each do |attribute|
119
+ describe attribute do
120
+ subject { super().send(attribute) }
121
+ it { is_expected.not_to be_nil }
122
+ end
123
+ end
124
+
125
+ it 'should be persisted' do
126
+ expect(subject).to be_persisted
127
+ end
128
+ end
129
+ end
130
+
131
+ describe '#destroy!' do
132
+ it 'should delete charge account at API' do
133
+ VCR.use_cassette('ChargeAccount/deleting a charge account') do
134
+ charge_account = Factory.create_resource(described_class, domain, attributes).create!
135
+
136
+ expect { charge_account.destroy! }.to_not raise_error
137
+
138
+ expect(charge_account).to be_deleted
139
+ expect(charge_account).to_not be_persisted
140
+ end
141
+ end
142
+ end
143
+
144
+ describe '#update_attribute!' do
145
+ let!(:charge_account) do
146
+ VCR.use_cassette('ChargeAccount/get a charge account for update attribute tests') do
147
+ Factory.create_resource(described_class, domain, attributes).create!
148
+ end
149
+ end
150
+
151
+ context 'try update a readonly attribute on charge account' do
152
+ it 'should raise' do
153
+ VCR.use_cassette('ChargeAccount/try update bank attribute on charge account') do
154
+ expect(charge_account.bank).to eq '237'
155
+
156
+ expect { charge_account.update_attribute! :bank, '341' }.to raise_error Charging::Http::LastResponseError
157
+
158
+ expect(charge_account.bank).to eq '237'
159
+ end
160
+ end
161
+ end
162
+
163
+ it 'should delete charge account at API' do
164
+ VCR.use_cassette('ChargeAccount/updating an attribute at charge account') do
165
+ expect { charge_account.update_attribute! :address, '123 New Address St.' }.to_not raise_error
166
+
167
+ expect(charge_account).to be_persisted
168
+ expect(charge_account.address).to eq '123 New Address St.'
169
+ end
170
+ end
171
+ end
172
+
173
+ describe '#update_attributes!' do
174
+ it 'should delegate to update_attribute! for each attribute' do
175
+ VCR.use_cassette('ChargeAccount/for update attributes') do
176
+ charge_account = described_class.new(attributes, domain)
177
+
178
+ expect(charge_account).to receive(:update_attribute!).exactly(3)
179
+ expect(charge_account).to receive(:reload_attributes!).once
180
+
181
+ charge_account.update_attributes!(address: 'New Address', zipcode: '12345', city: 'City')
182
+ end
183
+ end
184
+ end
185
+
186
+ describe '.find_all' do
187
+ it 'should require an account' do
188
+ expected_error = [ArgumentError, 'domain required']
189
+
190
+ expect { described_class.find_all(nil) }.to raise_error(*expected_error)
191
+ end
192
+
193
+ context 'for an account' do
194
+ before do
195
+ VCR.use_cassette('ChargeAccount/list all available charge accounts') do
196
+ @domain = domain
197
+ @charge_account = Factory.create_resource(described_class, @domain, attributes).create!
198
+ @find_all_result = described_class.find_all(@domain)
199
+ end
200
+ end
201
+
202
+ subject { @find_all_result }
203
+
204
+ it { is_expected.to be_an_instance_of(Charging::ChargeAccount::Collection) }
205
+
206
+ describe '#size' do
207
+ subject { super().size }
208
+ it { is_expected.not_to eq 0}
209
+ end
210
+
211
+ it 'should contain last response information' do
212
+ expect(@find_all_result.last_response.code).to eq 200
213
+ end
214
+ end
215
+ end
216
+
217
+ describe '.find_by_uuid' do
218
+ let(:charge_account) do
219
+ Factory.create_resource(described_class, domain, attributes).create!
220
+ end
221
+
222
+ let(:uuid) { charge_account.uuid }
223
+
224
+ it 'should require an account' do
225
+ expected_error = [ArgumentError, 'domain required']
226
+
227
+ expect { described_class.find_by_uuid(nil, '') }.to raise_error(*expected_error)
228
+ end
229
+
230
+ it 'should require an uuid' do
231
+ VCR.use_cassette('ChargeAccount/can not find without uuid') do
232
+ expected_error = [ArgumentError, 'uuid required']
233
+
234
+ expect { described_class.find_by_uuid(domain, nil) }.to raise_error(*expected_error)
235
+ end
236
+ end
237
+
238
+ it 'should raise for invalid uuid' do
239
+ VCR.use_cassette('ChargeAccount/finding invalid charge account') do
240
+ expect { described_class.find_by_uuid(domain, 'invalid-uuid') }.to raise_error Charging::Http::LastResponseError
241
+ end
242
+ end
243
+
244
+ it 'should raise if not response to success (200)' do
245
+ VCR.use_cassette('ChargeAccount/response not success') do
246
+ response_mock = double('AcceptedResponse', code: 202, to_s: 'AcceptedResponse')
247
+
248
+ expect(described_class)
249
+ .to receive(:get_charge_account)
250
+ .and_return(response_mock)
251
+
252
+ expect {
253
+ described_class.find_by_uuid(domain, uuid)
254
+ }.to raise_error Charging::Http::LastResponseError, 'AcceptedResponse'
255
+ end
256
+ end
257
+
258
+ context 'for a valid uuid' do
259
+ before do
260
+ VCR.use_cassette('ChargeAccount/finding a charging account by uuid via domain') do
261
+ @domain = domain
262
+ @charge_account = charge_account
263
+ @uuid = @charge_account.uuid
264
+ @find_result = described_class.find_by_uuid(@domain, @uuid)
265
+ end
266
+ end
267
+
268
+ subject { @find_result }
269
+
270
+ it 'should instantiate a charge account' do
271
+ expect(subject).to be_an_instance_of(Charging::ChargeAccount)
272
+ end
273
+
274
+ it 'should be a persisted instance' do
275
+ expect(subject).to be_persisted
276
+ end
277
+
278
+ describe '#uri' do
279
+ subject { super().uri }
280
+ it { is_expected.to eq "http://sandbox.charging.financeconnect.com.br/charge-accounts/#{@uuid}/" }
281
+ end
282
+
283
+ describe '#domain' do
284
+ subject { super().domain }
285
+ it { is_expected.to eq @domain }
286
+ end
287
+ end
288
+ end
289
+ end
@@ -0,0 +1,61 @@
1
+ # encoding: utf-8
2
+ require 'spec_helper'
3
+
4
+ describe Charging::Configuration do
5
+ let(:application_token) { 'some-app-token' }
6
+ let(:config) { Charging::Configuration.new }
7
+
8
+ context 'for default settings' do
9
+ describe '#application_token' do
10
+ subject { super().application_token }
11
+ it { is_expected.to be_nil }
12
+ end
13
+
14
+ describe '#url' do
15
+ subject { super().url }
16
+ it { is_expected.to eq 'https://charging.financeconnect.com.br' }
17
+ end
18
+
19
+ describe '#user_agent' do
20
+ subject { super().user_agent }
21
+ it { is_expected.to match(/Charging Ruby Client v\d+\.\d+\.\d+/) }
22
+ end
23
+ end
24
+
25
+ context "with configuration parameters" do
26
+ subject do
27
+ Charging::Configuration.new.tap do |config|
28
+ config.application_token = application_token
29
+ config.url = 'https://sandbox.app.charging.com.br'
30
+ config.user_agent = 'My amazing app'
31
+ end
32
+ end
33
+
34
+ describe '#application_token' do
35
+ subject { super().application_token }
36
+ it { is_expected.to eq application_token }
37
+ end
38
+
39
+ describe '#url' do
40
+ subject { super().url }
41
+ it { is_expected.to eq 'https://sandbox.app.charging.com.br'}
42
+ end
43
+
44
+ describe '#user_agent' do
45
+ subject { super().user_agent }
46
+ it { is_expected.to eq "My amazing app" }
47
+ end
48
+ end
49
+
50
+ describe "#credentials_for" do
51
+ [nil, '', ' '].each do |invalid_token|
52
+ it "should reject #{invalid_token.inspect} as token" do
53
+ expect { config.credentials_for(invalid_token) }.to raise_error(ArgumentError, "#{invalid_token.inspect} is not a valid token")
54
+ end
55
+ end
56
+
57
+ it 'should generate credential for token' do
58
+ expect(config.credentials_for(application_token)).to eq "Basic OnNvbWUtYXBwLXRva2Vu"
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,101 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ describe Charging::Domain::Collection, :vcr do
6
+ it 'should raise for invalid account' do
7
+ expected_error = [ArgumentError, 'account required']
8
+
9
+ expect { described_class.new(nil, double(:response, code: 401)) }.to raise_error(*expected_error)
10
+ end
11
+
12
+ it 'should raise for invalid response' do
13
+ VCR.use_cassette('DomainCollection/invalid response for domain collection') do
14
+ expected_error = [ArgumentError, 'response required']
15
+
16
+ expect { described_class.new(current_account, nil) }.to raise_error(*expected_error)
17
+ end
18
+ end
19
+
20
+ context 'with not success response' do
21
+ let(:response_not_found) { double(:response_not_found, code: 404) }
22
+
23
+ let!(:result) do
24
+ VCR.use_cassette('DomainCollection/not found response for domain collection') do
25
+ described_class.new(current_account, response_not_found)
26
+ end
27
+ end
28
+
29
+ it 'should have empty content' do
30
+ expect(result).to be_empty
31
+ end
32
+
33
+ it 'should have last response' do
34
+ expect(result.last_response).to eq response_not_found
35
+ end
36
+ end
37
+
38
+ context 'with success response without data' do
39
+ let(:response_success) do
40
+ double(code: 200, body: '[]')
41
+ end
42
+
43
+ let!(:result) do
44
+ VCR.use_cassette('DomainCollection/success response without data for domain collection') do
45
+ described_class.new(current_account, response_success)
46
+ end
47
+ end
48
+
49
+ it 'should have empty content' do
50
+ expect(result).to be_empty
51
+ end
52
+
53
+ it 'should have last response' do
54
+ expect(result.last_response).to eq response_success
55
+ end
56
+ end
57
+
58
+ context 'with success response with data' do
59
+ let(:body) do
60
+ MultiJson.encode([{
61
+ supplier_name: 'supplier_name data',
62
+ address: 'address data',
63
+ city_state: 'city_state data',
64
+ zipcode: 'zipcode data',
65
+ national_identifier: 'national_identifier data',
66
+ description: 'description data',
67
+ uuid: '154932d8-66b8-4e6b-82f5-ebb1d32fe85d',
68
+ etag: 'e11877e49b4ac65b4b8d96c16012a20254312e74',
69
+ uri: 'http://sandbox.charging.financeconnect.com.br:8080/account/domains/154932d8-66b8-4e6b-82f5-ebb1d32fe85d/'
70
+ }])
71
+ end
72
+
73
+ let(:response_success) do
74
+ double(code: 200, body: body)
75
+ end
76
+
77
+ let!(:result) do
78
+ VCR.use_cassette('DomainCollection/success response with data for domain collection') do
79
+ described_class.new(current_account, response_success)
80
+ end
81
+ end
82
+
83
+ it 'should convert into an array of domain' do
84
+ expect(result.size).to eq 1
85
+ end
86
+
87
+ it 'should have last response' do
88
+ expect(result.last_response).to eq response_success
89
+ end
90
+
91
+ it 'should contain a domain' do
92
+ domain = result.first
93
+
94
+ expect(domain).to be_an_instance_of(Charging::Domain)
95
+ end
96
+
97
+ it 'should load current account for domain instance' do
98
+ expect(result.first.account).to eq current_account
99
+ end
100
+ end
101
+ end