pactas_itero 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +39 -0
- data/.hound.yml +14 -0
- data/.rubocop.yml +2 -0
- data/.ruby-style.yml +268 -0
- data/.travis.yml +13 -0
- data/CHANGELOG.md +46 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +33 -0
- data/Rakefile +16 -0
- data/lib/pactas_itero/api/contracts.rb +30 -0
- data/lib/pactas_itero/api/customers.rb +20 -0
- data/lib/pactas_itero/api/invoices.rb +25 -0
- data/lib/pactas_itero/api/oauth.rb +12 -0
- data/lib/pactas_itero/api/orders.rb +19 -0
- data/lib/pactas_itero/api/rated_items.rb +16 -0
- data/lib/pactas_itero/api.rb +18 -0
- data/lib/pactas_itero/client.rb +108 -0
- data/lib/pactas_itero/configurable.rb +59 -0
- data/lib/pactas_itero/default.rb +77 -0
- data/lib/pactas_itero/error.rb +217 -0
- data/lib/pactas_itero/ext/hash/camelize_keys.rb +32 -0
- data/lib/pactas_itero/response/raise_error.rb +21 -0
- data/lib/pactas_itero/version.rb +3 -0
- data/lib/pactas_itero.rb +31 -0
- data/pactas_itero.gemspec +32 -0
- data/spec/fixtures/bearer_token.json +5 -0
- data/spec/fixtures/commit_order_response.json +50 -0
- data/spec/fixtures/contract.json +46 -0
- data/spec/fixtures/contract_cancellation_preview_response.json +73 -0
- data/spec/fixtures/contracts.json +48 -0
- data/spec/fixtures/create_order_response.json +17 -0
- data/spec/fixtures/create_rated_item.json +8 -0
- data/spec/fixtures/customer.json +23 -0
- data/spec/fixtures/customers.json +40 -0
- data/spec/fixtures/invoice.json +22 -0
- data/spec/fixtures/invoices.json +46 -0
- data/spec/fixtures/order.json +15 -0
- data/spec/pactas_itero/api/contracts_spec.rb +118 -0
- data/spec/pactas_itero/api/customers_spec.rb +188 -0
- data/spec/pactas_itero/api/invoices_spec.rb +87 -0
- data/spec/pactas_itero/api/oauth_spec.rb +40 -0
- data/spec/pactas_itero/api/orders_spec.rb +102 -0
- data/spec/pactas_itero/api/rated_items_spec.rb +74 -0
- data/spec/pactas_itero/client_spec.rb +316 -0
- data/spec/pactas_itero_spec.rb +32 -0
- data/spec/spec_helper.rb +100 -0
- metadata +218 -0
@@ -0,0 +1,87 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe PactasItero::Api::Invoices do
|
4
|
+
describe '.invoices' do
|
5
|
+
it 'requests the correct resource' do
|
6
|
+
client = PactasItero::Client.new(bearer_token: 'bt')
|
7
|
+
request = stub_get('/api/v1/invoices').
|
8
|
+
to_return(
|
9
|
+
body: fixture('invoices.json'),
|
10
|
+
headers: { content_type: 'application/json; charset=utf-8'}
|
11
|
+
)
|
12
|
+
|
13
|
+
client.invoices
|
14
|
+
|
15
|
+
expect(request).to have_been_made
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'returns an array of contracts' do
|
19
|
+
client = PactasItero::Client.new(bearer_token: 'bt')
|
20
|
+
request = stub_get('/api/v1/invoices').
|
21
|
+
to_return(
|
22
|
+
body: fixture('invoices.json'),
|
23
|
+
headers: { content_type: 'application/json; charset=utf-8'}
|
24
|
+
)
|
25
|
+
|
26
|
+
invoices = client.invoices
|
27
|
+
|
28
|
+
expect(invoices).to be_a Array
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
describe '.invoices_from' do
|
33
|
+
it 'requests the correct resource' do
|
34
|
+
client = PactasItero::Client.new(bearer_token: 'bt')
|
35
|
+
request = stub_get('/api/v1/invoices?from=54b67d9e995ec90bc8f37718').
|
36
|
+
to_return(
|
37
|
+
body: fixture('invoices.json'),
|
38
|
+
headers: { content_type: 'application/json; charset=utf-8'}
|
39
|
+
)
|
40
|
+
|
41
|
+
client.invoices_from('54b67d9e995ec90bc8f37718')
|
42
|
+
|
43
|
+
expect(request).to have_been_made
|
44
|
+
end
|
45
|
+
|
46
|
+
it 'requests the correct resource, if from parameter is nil' do
|
47
|
+
client = PactasItero::Client.new(bearer_token: 'bt')
|
48
|
+
request = stub_get('/api/v1/invoices').
|
49
|
+
to_return(
|
50
|
+
body: fixture('invoices.json'),
|
51
|
+
headers: { content_type: 'application/json; charset=utf-8'}
|
52
|
+
)
|
53
|
+
|
54
|
+
client.invoices_from(nil)
|
55
|
+
|
56
|
+
expect(request).to have_been_made
|
57
|
+
end
|
58
|
+
|
59
|
+
it 'returns an array of contracts' do
|
60
|
+
client = PactasItero::Client.new(bearer_token: 'bt')
|
61
|
+
request = stub_get('/api/v1/invoices?from=54b67d9e995ec90bc8f37718').
|
62
|
+
to_return(
|
63
|
+
body: fixture('invoices.json'),
|
64
|
+
headers: { content_type: 'application/json; charset=utf-8'}
|
65
|
+
)
|
66
|
+
|
67
|
+
invoices = client.invoices_from('54b67d9e995ec90bc8f37718')
|
68
|
+
|
69
|
+
expect(invoices).to be_a Array
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
describe '.invoice' do
|
74
|
+
it 'requests the correct resource' do
|
75
|
+
client = PactasItero::Client.new(bearer_token: 'bt')
|
76
|
+
request = stub_get('/api/v1/invoices/54b67d9e995ec90bc8f37718').
|
77
|
+
to_return(
|
78
|
+
body: fixture('invoice.json'),
|
79
|
+
headers: { content_type: 'application/json; charset=utf-8'}
|
80
|
+
)
|
81
|
+
|
82
|
+
client.invoice('54b67d9e995ec90bc8f37718')
|
83
|
+
|
84
|
+
expect(request).to have_been_made
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe PactasItero::Api::OAuth do
|
4
|
+
|
5
|
+
describe '#token' do
|
6
|
+
before do
|
7
|
+
stub_post("https://a_client_id:a_client_secret@sandbox.billwerk.com/oauth/token").with(
|
8
|
+
:body => { :grant_type => 'client_credentials' }
|
9
|
+
).to_return(
|
10
|
+
:status => 200,
|
11
|
+
:body => fixture('bearer_token.json'),
|
12
|
+
:headers => {
|
13
|
+
:content_type => 'application/json; charset=utf-8'
|
14
|
+
}
|
15
|
+
)
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'requests the correct resource' do
|
19
|
+
client = PactasItero::Client.new(client_id: 'a_client_id', client_secret: 'a_client_secret')
|
20
|
+
|
21
|
+
client.token
|
22
|
+
|
23
|
+
expect(a_post("https://a_client_id:a_client_secret@sandbox.billwerk.com/oauth/token").with(
|
24
|
+
:body => "grant_type=client_credentials",
|
25
|
+
:headers => {
|
26
|
+
'Content-Type'=>'application/x-www-form-urlencoded; charset=UTF-8'
|
27
|
+
}
|
28
|
+
)).to have_been_made
|
29
|
+
end
|
30
|
+
|
31
|
+
it 'returns the bearer token' do
|
32
|
+
client = PactasItero::Client.new(client_id: 'a_client_id', client_secret: 'a_client_secret')
|
33
|
+
bearer_token = client.token
|
34
|
+
|
35
|
+
expect(bearer_token.access_token).to eq('top_secret_access_token')
|
36
|
+
expect(bearer_token.token_type).to eq('bearer')
|
37
|
+
expect(bearer_token.expires).to eq(0)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,102 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe PactasItero::Api::Orders do
|
4
|
+
describe ".create_order" do
|
5
|
+
it "requests the correct resource" do
|
6
|
+
client = PactasItero::Client.new(bearer_token: 'bt')
|
7
|
+
post_data = {
|
8
|
+
ContractId: "5370e5ab9e40071fd01e01e0",
|
9
|
+
Cart: {
|
10
|
+
PlanVariantId: "525bf9089e40073a58590fd5"
|
11
|
+
}
|
12
|
+
}.to_json
|
13
|
+
request = stub_post('/api/v1/orders').
|
14
|
+
with(body: post_data).
|
15
|
+
to_return(
|
16
|
+
body: fixture('create_order_response.json'),
|
17
|
+
headers: { content_type: 'application/json; charset=utf-8'}
|
18
|
+
)
|
19
|
+
|
20
|
+
client.create_order(
|
21
|
+
contract_id: "5370e5ab9e40071fd01e01e0",
|
22
|
+
cart: {
|
23
|
+
plan_variant_id: "525bf9089e40073a58590fd5"
|
24
|
+
}
|
25
|
+
)
|
26
|
+
|
27
|
+
expect(request).to have_been_made
|
28
|
+
end
|
29
|
+
|
30
|
+
it 'returns the created order' do
|
31
|
+
client = PactasItero::Client.new(bearer_token: 'bt')
|
32
|
+
post_data = {
|
33
|
+
ContractId: "5370e5ab9e40071fd01e01e0",
|
34
|
+
Cart: {
|
35
|
+
PlanVariantId: "525bf9089e40073a58590fd5"
|
36
|
+
}
|
37
|
+
}.to_json
|
38
|
+
request = stub_post('/api/v1/orders').
|
39
|
+
with(body: post_data).
|
40
|
+
to_return(
|
41
|
+
body: fixture('create_order_response.json'),
|
42
|
+
headers: { content_type: 'application/json; charset=utf-8'}
|
43
|
+
)
|
44
|
+
|
45
|
+
order = client.create_order(
|
46
|
+
contract_id: "5370e5ab9e40071fd01e01e0",
|
47
|
+
cart: {
|
48
|
+
plan_variant_id: "525bf9089e40073a58590fd5"
|
49
|
+
}
|
50
|
+
)
|
51
|
+
|
52
|
+
expect(order.id).to eq '537dfcab9e400760b4cd6347'
|
53
|
+
expect(order.contract_id).to eq '5370e5ab9e40071fd01e01e0'
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
describe '.order' do
|
58
|
+
it 'requests the correct resource' do
|
59
|
+
client = PactasItero::Client.new(bearer_token: 'bt')
|
60
|
+
request = stub_get('/api/v1/orders/5357bc4f1d8dd00fa0db6c31').
|
61
|
+
to_return(
|
62
|
+
body: fixture('order.json'),
|
63
|
+
headers: { content_type: 'application/json; charset=utf-8'}
|
64
|
+
)
|
65
|
+
|
66
|
+
client.order('5357bc4f1d8dd00fa0db6c31')
|
67
|
+
|
68
|
+
expect(request).to have_been_made
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
describe ".commit_order" do
|
73
|
+
it "requests the correct resource" do
|
74
|
+
client = PactasItero::Client.new(bearer_token: 'bt')
|
75
|
+
request = stub_post('/api/v1/orders/537dfcab9e400760b4cd6347/commit').
|
76
|
+
with(body: {}.to_json).
|
77
|
+
to_return(
|
78
|
+
body: fixture('commit_order_response.json'),
|
79
|
+
headers: { content_type: 'application/json; charset=utf-8'}
|
80
|
+
)
|
81
|
+
|
82
|
+
client.commit_order('537dfcab9e400760b4cd6347')
|
83
|
+
|
84
|
+
expect(request).to have_been_made
|
85
|
+
end
|
86
|
+
|
87
|
+
it 'returns the updated contract' do
|
88
|
+
client = PactasItero::Client.new(bearer_token: 'bt')
|
89
|
+
request = stub_post('/api/v1/orders/537dfcab9e400760b4cd6347/commit').
|
90
|
+
with(body: {}.to_json).
|
91
|
+
to_return(
|
92
|
+
body: fixture('commit_order_response.json'),
|
93
|
+
headers: { content_type: 'application/json; charset=utf-8'}
|
94
|
+
)
|
95
|
+
|
96
|
+
contract = client.commit_order('537dfcab9e400760b4cd6347')
|
97
|
+
|
98
|
+
expect(contract.id).to eq '5370e5ab9e40071fd01e01e0'
|
99
|
+
expect(contract.current_phase.plan_variant_id).to eq '525bf9089e40073a58590fd5'
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
@@ -0,0 +1,74 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe PactasItero::Api::RatedItems do
|
4
|
+
describe ".create_rated_item" do
|
5
|
+
it "requests the correct resource" do
|
6
|
+
client = PactasItero::Client.new(bearer_token: 'bt')
|
7
|
+
post_data = {
|
8
|
+
PeriodStart: '2014-09-19T08:18:51.907Z',
|
9
|
+
PeriodEnd: '2014-09-19T08:19:51.907Z',
|
10
|
+
Quantity: 2,
|
11
|
+
Description: 'the description',
|
12
|
+
PricePerUnit: 123.45,
|
13
|
+
TaxPolicyId: 'tax-policy-id',
|
14
|
+
}.to_json
|
15
|
+
request = stub_post('/api/v1/contracts/contract-id/ratedItems').
|
16
|
+
with(body: post_data).
|
17
|
+
to_return(
|
18
|
+
body: fixture('create_rated_item.json'),
|
19
|
+
headers: { content_type: 'application/json; charset=utf-8'}
|
20
|
+
)
|
21
|
+
|
22
|
+
client.create_rated_item(
|
23
|
+
'contract-id',
|
24
|
+
2,
|
25
|
+
'the description',
|
26
|
+
123.45,
|
27
|
+
'tax-policy-id',
|
28
|
+
{
|
29
|
+
period_start: "2014-09-19T08:18:51.907Z",
|
30
|
+
period_end: "2014-09-19T08:19:51.907Z"
|
31
|
+
}
|
32
|
+
)
|
33
|
+
|
34
|
+
expect(request).to have_been_made
|
35
|
+
end
|
36
|
+
|
37
|
+
it 'returns the created rated item' do
|
38
|
+
client = PactasItero::Client.new(bearer_token: 'bt')
|
39
|
+
post_data = {
|
40
|
+
PeriodStart: '2014-09-19T08:18:51.907Z',
|
41
|
+
PeriodEnd: '2014-09-19T08:19:51.907Z',
|
42
|
+
Quantity: 2,
|
43
|
+
Description: 'the description',
|
44
|
+
PricePerUnit: 123.45,
|
45
|
+
TaxPolicyId: 'tax-policy-id',
|
46
|
+
}.to_json
|
47
|
+
request = stub_post('/api/v1/contracts/contract-id/ratedItems').
|
48
|
+
with(body: post_data).
|
49
|
+
to_return(
|
50
|
+
body: fixture('create_rated_item.json'),
|
51
|
+
headers: { content_type: 'application/json; charset=utf-8'}
|
52
|
+
)
|
53
|
+
|
54
|
+
rated_item = client.create_rated_item(
|
55
|
+
'contract-id',
|
56
|
+
2,
|
57
|
+
'the description',
|
58
|
+
123.45,
|
59
|
+
'tax-policy-id',
|
60
|
+
{
|
61
|
+
period_start: "2014-09-19T08:18:51.907Z",
|
62
|
+
period_end: "2014-09-19T08:19:51.907Z"
|
63
|
+
}
|
64
|
+
)
|
65
|
+
|
66
|
+
expect(rated_item.description).to eq 'the description'
|
67
|
+
expect(rated_item.period_end).to eq '2014-09-19T08:18:51.907Z'
|
68
|
+
expect(rated_item.period_start).to eq '2014-09-19T08:18:51.907Z'
|
69
|
+
expect(rated_item.price_per_unit).to eq 123.45
|
70
|
+
expect(rated_item.quantity).to eq 2
|
71
|
+
expect(rated_item.tax_policy_id).to eq 'tax-policy-id'
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
@@ -0,0 +1,316 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'json'
|
3
|
+
|
4
|
+
describe PactasItero::Client do
|
5
|
+
|
6
|
+
before do
|
7
|
+
PactasItero.reset!
|
8
|
+
end
|
9
|
+
|
10
|
+
describe "module configuration" do
|
11
|
+
|
12
|
+
before do
|
13
|
+
PactasItero.configure do |config|
|
14
|
+
PactasItero::Configurable.keys.each do |key|
|
15
|
+
config.send("#{key}=", "the #{key} value")
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
it "inherits the module configuration" do
|
21
|
+
client = PactasItero::Client.new
|
22
|
+
|
23
|
+
PactasItero::Configurable.keys.each do |key|
|
24
|
+
expect(client.instance_variable_get(:"@#{key}")).to eq("the #{key} value")
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
describe "with class level configuration" do
|
29
|
+
|
30
|
+
before do
|
31
|
+
@opts = {
|
32
|
+
#connection_options: {ssl: {verify: false}},
|
33
|
+
client_id: "the_client_id",
|
34
|
+
client_secret: "the_client_secret"
|
35
|
+
}
|
36
|
+
end
|
37
|
+
|
38
|
+
it "overrides module configuration" do
|
39
|
+
client = PactasItero::Client.new(@opts)
|
40
|
+
|
41
|
+
expect(client.client_id).to eq("the_client_id")
|
42
|
+
expect(client.client_secret).to eq("the_client_secret")
|
43
|
+
expect(client.user_agent).to eq(PactasItero.user_agent)
|
44
|
+
end
|
45
|
+
|
46
|
+
it "can set configuration after initialization" do
|
47
|
+
client = PactasItero::Client.new
|
48
|
+
client.configure do |config|
|
49
|
+
@opts.each do |key, value|
|
50
|
+
config.send("#{key}=", value)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
expect(client.client_id).to eq("the_client_id")
|
55
|
+
expect(client.client_secret).to eq("the_client_secret")
|
56
|
+
expect(client.user_agent).to eq(PactasItero.user_agent)
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
describe ".get" do
|
62
|
+
it "requests the given resource using get" do
|
63
|
+
client = PactasItero::Client.new(bearer_token: 'bt')
|
64
|
+
request = stub_get("/something?foo=bar")
|
65
|
+
|
66
|
+
client.get "/something", foo: "bar"
|
67
|
+
|
68
|
+
expect(request).to have_been_made
|
69
|
+
end
|
70
|
+
|
71
|
+
it "passes along request headers" do
|
72
|
+
client = PactasItero::Client.new(bearer_token: 'bt')
|
73
|
+
request = stub_get("/").
|
74
|
+
with(query: { foo: "bar" }, headers: { accept: "text/plain" })
|
75
|
+
client.get "/", foo: "bar", accept: "text/plain"
|
76
|
+
|
77
|
+
expect(request).to have_been_made
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
describe ".head" do
|
82
|
+
it "requests the given resource using head" do
|
83
|
+
client = PactasItero::Client.new(bearer_token: 'bt')
|
84
|
+
request = stub_head("/something?foo=bar")
|
85
|
+
|
86
|
+
client.head "/something", foo: "bar"
|
87
|
+
|
88
|
+
expect(request).to have_been_made
|
89
|
+
end
|
90
|
+
|
91
|
+
it "passes along request headers" do
|
92
|
+
client = PactasItero::Client.new(bearer_token: 'bt')
|
93
|
+
request = stub_head("/").
|
94
|
+
with(query: { foo: "bar" }, headers: { accept: "text/plain" })
|
95
|
+
|
96
|
+
client.head "/", foo: "bar", accept: "text/plain"
|
97
|
+
|
98
|
+
expect(request).to have_been_made
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
describe ".post" do
|
103
|
+
it "requests the given resource using post" do
|
104
|
+
client = PactasItero::Client.new(bearer_token: 'bt')
|
105
|
+
request = stub_post("/something").
|
106
|
+
with(body: { foo: "bar" }.to_json)
|
107
|
+
|
108
|
+
client.post "/something", foo: "bar"
|
109
|
+
|
110
|
+
expect(request).to have_been_made
|
111
|
+
end
|
112
|
+
|
113
|
+
it "passes along request headers" do
|
114
|
+
client = PactasItero::Client.new(bearer_token: 'bt')
|
115
|
+
headers = { "X-Foo" => "bar" }
|
116
|
+
request = stub_post("/").
|
117
|
+
with(body: { foo: "bar" }.to_json, headers: headers)
|
118
|
+
|
119
|
+
client.post "/", foo: "bar", headers: headers
|
120
|
+
|
121
|
+
expect(request).to have_been_made
|
122
|
+
end
|
123
|
+
end
|
124
|
+
|
125
|
+
describe ".put" do
|
126
|
+
it "requests the given resource using put" do
|
127
|
+
client = PactasItero::Client.new(bearer_token: 'bt')
|
128
|
+
request = stub_put("/something").
|
129
|
+
with(body: { foo: "bar" }.to_json)
|
130
|
+
|
131
|
+
client.put "/something", foo: "bar"
|
132
|
+
|
133
|
+
expect(request).to have_been_made
|
134
|
+
end
|
135
|
+
|
136
|
+
it "passes along request headers" do
|
137
|
+
client = PactasItero::Client.new(bearer_token: 'bt')
|
138
|
+
headers = { "X-Foo" => "bar" }
|
139
|
+
request = stub_put("/").
|
140
|
+
with(body: { foo: "bar" }.to_json, headers: headers)
|
141
|
+
|
142
|
+
client.put "/", foo: "bar", headers: headers
|
143
|
+
|
144
|
+
expect(request).to have_been_made
|
145
|
+
end
|
146
|
+
end
|
147
|
+
|
148
|
+
describe ".patch" do
|
149
|
+
it "requests the given resource using patch" do
|
150
|
+
client = PactasItero::Client.new(bearer_token: 'bt')
|
151
|
+
request = stub_patch("/something").
|
152
|
+
with(body: { foo: "bar" }.to_json)
|
153
|
+
|
154
|
+
client.patch "/something", foo: "bar"
|
155
|
+
|
156
|
+
expect(request).to have_been_made
|
157
|
+
end
|
158
|
+
|
159
|
+
it "passes along request headers" do
|
160
|
+
client = PactasItero::Client.new(bearer_token: 'bt')
|
161
|
+
headers = { "X-Foo" => "bar" }
|
162
|
+
request = stub_patch("/").
|
163
|
+
with(body: { foo: "bar" }.to_json, headers: headers)
|
164
|
+
|
165
|
+
client.patch "/", foo: "bar", headers: headers
|
166
|
+
|
167
|
+
expect(request).to have_been_made
|
168
|
+
end
|
169
|
+
end
|
170
|
+
|
171
|
+
describe ".delete" do
|
172
|
+
it "requests the given resource using delete" do
|
173
|
+
client = PactasItero::Client.new(bearer_token: 'bt')
|
174
|
+
request = stub_delete("/something")
|
175
|
+
|
176
|
+
client.delete "/something"
|
177
|
+
|
178
|
+
expect(request).to have_been_made
|
179
|
+
end
|
180
|
+
|
181
|
+
it "passes along request headers" do
|
182
|
+
client = PactasItero::Client.new(bearer_token: 'bt')
|
183
|
+
headers = { "X-Foo" => "bar" }
|
184
|
+
request = stub_delete("/").with(headers: headers)
|
185
|
+
|
186
|
+
client.delete "/", headers: headers
|
187
|
+
|
188
|
+
expect(request).to have_been_made
|
189
|
+
end
|
190
|
+
end
|
191
|
+
|
192
|
+
describe "when making requests" do
|
193
|
+
|
194
|
+
it 'uses the sandbox endpoint by default' do
|
195
|
+
client = PactasItero::Client.new
|
196
|
+
|
197
|
+
expect(client.api_endpoint).to eq "https://sandbox.billwerk.com/"
|
198
|
+
end
|
199
|
+
|
200
|
+
it "uses the production endpoint when production is set to true" do
|
201
|
+
client = PactasItero::Client.new(production: true)
|
202
|
+
|
203
|
+
expect(client.api_endpoint).to eq 'https://itero.pactas.com/'
|
204
|
+
end
|
205
|
+
|
206
|
+
it "sets a default user agent" do
|
207
|
+
client = PactasItero::Client.new(bearer_token: 'bt')
|
208
|
+
request = stub_get("/").
|
209
|
+
with(headers: { user_agent: PactasItero::Default.user_agent })
|
210
|
+
|
211
|
+
client.get "/"
|
212
|
+
|
213
|
+
expect(request).to have_been_made
|
214
|
+
end
|
215
|
+
|
216
|
+
it "accepts a custom user agent" do
|
217
|
+
user_agent = "Mozilla/1.0 (Win3.1)"
|
218
|
+
client = PactasItero::Client.new(user_agent: user_agent, bearer_token: 'bt')
|
219
|
+
request = stub_get("/").with(headers: { user_agent: user_agent })
|
220
|
+
|
221
|
+
client.get "/"
|
222
|
+
|
223
|
+
expect(request).to have_been_made
|
224
|
+
end
|
225
|
+
|
226
|
+
it 'creates the correct auth headers with supplied bearer_token' do
|
227
|
+
token = 'the_bearer_token'
|
228
|
+
request = stub_get("/").with(headers: { authorization: "Bearer #{token}" })
|
229
|
+
client = PactasItero::Client.new(bearer_token: token)
|
230
|
+
|
231
|
+
client.get "/"
|
232
|
+
|
233
|
+
expect(request).to have_been_made
|
234
|
+
end
|
235
|
+
|
236
|
+
it 'creates the correct auth headers with supplied bearer_token' do
|
237
|
+
token = OpenStruct.new(access_token: 'the_bearer_token')
|
238
|
+
client = PactasItero::Client.new(bearer_token: token)
|
239
|
+
|
240
|
+
request = stub_get("/").with(headers: { authorization: "Bearer #{token.access_token}" })
|
241
|
+
|
242
|
+
client.get "/"
|
243
|
+
|
244
|
+
expect(request).to have_been_made
|
245
|
+
end
|
246
|
+
end
|
247
|
+
|
248
|
+
context "error handling" do
|
249
|
+
it "raises on 404" do
|
250
|
+
client = PactasItero::Client.new(bearer_token: 'bt')
|
251
|
+
stub_get('/four_oh_four').to_return(status: 404)
|
252
|
+
|
253
|
+
expect { client.get('/four_oh_four') }.to raise_error PactasItero::NotFound
|
254
|
+
end
|
255
|
+
|
256
|
+
it "raises on 500" do
|
257
|
+
client = PactasItero::Client.new(bearer_token: 'bt')
|
258
|
+
stub_get('/five_oh_oh').to_return(status: 500)
|
259
|
+
|
260
|
+
expect { client.get('/five_oh_oh') }.to raise_error PactasItero::InternalServerError
|
261
|
+
end
|
262
|
+
|
263
|
+
it "includes a message" do
|
264
|
+
client = PactasItero::Client.new(bearer_token: 'bt')
|
265
|
+
stub_get('/with_message').to_return(
|
266
|
+
status: 422,
|
267
|
+
headers: { content_type: "application/json" },
|
268
|
+
body: { Message: "'Something' is not a valid ObjectId. Expected a 24 digit hex string." }.to_json
|
269
|
+
)
|
270
|
+
|
271
|
+
expect { client.get('/with_message') }.to raise_error(
|
272
|
+
PactasItero::UnprocessableEntity,
|
273
|
+
"GET https://sandbox.billwerk.com/with_message: " \
|
274
|
+
"422 - 'Something' is not a valid ObjectId. Expected a 24 digit hex string.",
|
275
|
+
)
|
276
|
+
end
|
277
|
+
|
278
|
+
it "raises a ClientError on unknown client errors" do
|
279
|
+
client = PactasItero::Client.new(bearer_token: 'bt')
|
280
|
+
stub_get('/something').to_return(
|
281
|
+
status: 418,
|
282
|
+
headers: { content_type: "application/json" },
|
283
|
+
body: { message: "I'm a teapot" }.to_json
|
284
|
+
)
|
285
|
+
|
286
|
+
expect { client.get('/something') }.to raise_error PactasItero::ClientError
|
287
|
+
end
|
288
|
+
|
289
|
+
it "raises a ServerError on unknown server errors" do
|
290
|
+
client = PactasItero::Client.new(bearer_token: 'bt')
|
291
|
+
stub_get('/something').to_return(
|
292
|
+
status: 509,
|
293
|
+
headers: { content_type: "application/json" },
|
294
|
+
body: { message: "Bandwidth exceeded" }.to_json
|
295
|
+
)
|
296
|
+
|
297
|
+
expect { client.get('/something') }.to raise_error PactasItero::ServerError
|
298
|
+
end
|
299
|
+
end
|
300
|
+
|
301
|
+
describe '.sandbox_api_endpoint' do
|
302
|
+
it 'returns url of the sandbox endpoint' do
|
303
|
+
client = PactasItero::Client.new
|
304
|
+
|
305
|
+
expect(client.sandbox_api_endpoint).to eq "https://sandbox.billwerk.com"
|
306
|
+
end
|
307
|
+
end
|
308
|
+
|
309
|
+
describe '.production_api_endpoint' do
|
310
|
+
it 'returns url of the sandbox endpoint' do
|
311
|
+
client = PactasItero::Client.new
|
312
|
+
|
313
|
+
expect(client.production_api_endpoint).to eq 'https://itero.pactas.com'
|
314
|
+
end
|
315
|
+
end
|
316
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe PactasItero do
|
4
|
+
|
5
|
+
it "sets defaults" do
|
6
|
+
PactasItero::Configurable.keys.each do |key|
|
7
|
+
expect(PactasItero.instance_variable_get(:"@#{key}")).to eq(PactasItero::Default.send(key))
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
describe ".client" do
|
12
|
+
it "creates an PactasItero::Client" do
|
13
|
+
expect(PactasItero.client).to be_kind_of PactasItero::Client
|
14
|
+
end
|
15
|
+
|
16
|
+
it "creates new client everytime" do
|
17
|
+
expect(PactasItero.client).to_not eq(PactasItero.client)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
describe ".configure" do
|
22
|
+
PactasItero::Configurable.keys.each do |key|
|
23
|
+
it "sets the #{key.to_s.gsub('_', ' ')}" do
|
24
|
+
PactasItero.configure do |config|
|
25
|
+
config.send("#{key}=", key)
|
26
|
+
end
|
27
|
+
expect(PactasItero.instance_variable_get(:"@#{key}")).to eq(key)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|