peat 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/peat/token_manager.rb +8 -4
- data/lib/peat/version.rb +1 -1
- data/lib/peat.rb +2 -0
- data/spec/token_manager_spec.rb +69 -10
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 41c65100d648379520dc5052704759855a56fa65
|
4
|
+
data.tar.gz: 85cb807ffd5ec5e054e11e21337b364b34923df1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: aa237a24c04a8d10f5d63afe3ab9c2d72db5f3d861b5478b8e4aa6e6e474a8d12b681daa580ff789273ee2ac70a13ae27e46fac15630f3bf1ca75fe60fd16bbf
|
7
|
+
data.tar.gz: 25fdde4566320c272e6f60d2cd67ebb82c01718376f853a7ef6284f54c9a318cc0ec3cafe429aa604a72e4701f8a5da141735586be5e716d79b2f02a78a8d9cb
|
data/lib/peat/token_manager.rb
CHANGED
@@ -2,21 +2,25 @@ module Peat
|
|
2
2
|
module TokenManager
|
3
3
|
module_function
|
4
4
|
|
5
|
-
def token
|
5
|
+
def token(fuel_client_id: nil, fuel_secret: nil)
|
6
6
|
fetch_token do
|
7
|
+
client_id = fuel_client_id || ENV['FUEL_CLIENT_ID'] || $fuel_client_id
|
8
|
+
secret = fuel_secret || ENV['FUEL_SECRET'] || $fuel_secret
|
9
|
+
raise MissingConfiguration, 'You must set values for fuel client id and secret' unless client_id && secret
|
10
|
+
|
7
11
|
connection.post do |req|
|
8
12
|
req.url 'requestToken'
|
9
13
|
req.headers['Content-Type'] = 'application/json'
|
10
14
|
req.body = {
|
11
|
-
'clientId' =>
|
12
|
-
'clientSecret' =>
|
15
|
+
'clientId' => client_id,
|
16
|
+
'clientSecret' => secret,
|
13
17
|
}.to_json
|
14
18
|
end.body.merge('created_at' => Time.now)
|
15
19
|
end
|
16
20
|
end
|
17
21
|
|
18
22
|
def connection
|
19
|
-
@connection
|
23
|
+
@connection ||= Faraday.new(url: 'https://auth.exacttargetapis.com/v1/') do |conn|
|
20
24
|
conn.request :json
|
21
25
|
|
22
26
|
conn.response :logger
|
data/lib/peat/version.rb
CHANGED
data/lib/peat.rb
CHANGED
data/spec/token_manager_spec.rb
CHANGED
@@ -4,27 +4,86 @@ require 'active_support/core_ext'
|
|
4
4
|
|
5
5
|
module Peat
|
6
6
|
describe TokenManager do
|
7
|
+
before do
|
8
|
+
test = Faraday.new(url: 'https://auth.exacttargetapis.com/v1/') do |builder|
|
9
|
+
builder.adapter :test do |stub|
|
10
|
+
stub.post('v1/requestToken') { |env|
|
11
|
+
[ 200, {}, {'accessToken' => 'foobar123' }]
|
12
|
+
}
|
13
|
+
end
|
14
|
+
end
|
15
|
+
allow(TokenManager).to receive(:connection).and_return(test)
|
16
|
+
end
|
17
|
+
|
18
|
+
after do
|
19
|
+
TokenManager.instance_variable_set('@token', nil)
|
20
|
+
end
|
21
|
+
|
22
|
+
context 'configuring fuel client id and secret' do
|
23
|
+
context 'no creds set' do
|
24
|
+
it 'raises MissingConfiguration if no fuel credentials set' do
|
25
|
+
expect { Peat::TokenManager.token }.to raise_error(MissingConfiguration, 'You must set values for fuel client id and secret')
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
context 'client and secret passed in' do
|
30
|
+
it 'uses passed in client and secret' do
|
31
|
+
expect { Peat::TokenManager.token(fuel_client_id: 'foo', fuel_secret: 'bar') }.not_to raise_error
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
context 'client and secret set in env' do
|
36
|
+
around(:each) do |example|
|
37
|
+
ENV['FUEL_CLIENT_ID'] = 'foo'
|
38
|
+
ENV['FUEL_SECRET'] = 'bar'
|
39
|
+
example.run
|
40
|
+
ENV['FUEL_CLIENT_ID'] = nil
|
41
|
+
ENV['FUEL_SECRET'] = nil
|
42
|
+
end
|
43
|
+
|
44
|
+
it 'uses FUEL_CLIENT_ID and FUEL_SECRET from env if not passed in values' do
|
45
|
+
expect { Peat::TokenManager.token }.not_to raise_error
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
context 'client and secret set globally' do
|
50
|
+
around(:each) do |example|
|
51
|
+
$fuel_client_id, $fuel_secret = %w(foo bar)
|
52
|
+
example.run
|
53
|
+
$fuel_client_id, $fuel_secret = %w(nil nil)
|
54
|
+
end
|
55
|
+
|
56
|
+
it 'uses global $fuel_client_id and $fuel_secret if no values passed in or set in env' do
|
57
|
+
expect { Peat::TokenManager.token }.not_to raise_error
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
7
62
|
context 'token is nil' do
|
8
63
|
it 'fetches token' do
|
9
|
-
expect(TokenManager).to
|
10
|
-
TokenManager.token
|
64
|
+
expect(TokenManager.token).to eq('foobar123')
|
11
65
|
end
|
12
66
|
end
|
13
67
|
|
14
68
|
context 'token is expired' do
|
69
|
+
before do
|
70
|
+
TokenManager.instance_variable_set('@token', { 'accessToken' => 'expired', 'created_at' => 1.hour.ago })
|
71
|
+
end
|
72
|
+
|
15
73
|
it 'fetches token' do
|
16
|
-
TokenManager.
|
17
|
-
expect(TokenManager).to receive(:fetch_token)
|
18
|
-
TokenManager.token
|
74
|
+
expect(TokenManager.token).to eq('foobar123')
|
19
75
|
end
|
20
76
|
end
|
21
77
|
|
22
78
|
context 'token is present and not expired' do
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
79
|
+
before
|
80
|
+
token = { 'accessToken' => 'valid', 'created_at' => 1.day.from_now }
|
81
|
+
TokenManager.instance_variable_set('@token', token)
|
82
|
+
end
|
83
|
+
|
84
|
+
it 'returns token' do
|
85
|
+
expect(TokenManager.token).to eql('valid')
|
28
86
|
end
|
29
87
|
end
|
88
|
+
|
30
89
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: peat
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Hubert Huang
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-07-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: faraday
|