minty 1.0.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.
- checksums.yaml +7 -0
- data/.bundle/config +4 -0
- data/.devcontainer/Dockerfile +19 -0
- data/.devcontainer/devcontainer.json +37 -0
- data/.env.example +2 -0
- data/.gemrelease +2 -0
- data/.github/PULL_REQUEST_TEMPLATE.md +33 -0
- data/.github/dependabot.yml +10 -0
- data/.github/stale.yml +20 -0
- data/.gitignore +18 -0
- data/.rspec +3 -0
- data/.rubocop.yml +9 -0
- data/CODE_OF_CONDUCT.md +3 -0
- data/DEPLOYMENT.md +61 -0
- data/DEVELOPMENT.md +35 -0
- data/Dockerfile +5 -0
- data/EXAMPLES.md +195 -0
- data/Gemfile +21 -0
- data/Gemfile.lock +250 -0
- data/Guardfile +39 -0
- data/LICENSE +21 -0
- data/Makefile +5 -0
- data/README.md +88 -0
- data/RUBYGEM.md +9 -0
- data/Rakefile +33 -0
- data/codecov.yml +22 -0
- data/lib/minty/algorithm.rb +7 -0
- data/lib/minty/api/authentication_endpoints.rb +30 -0
- data/lib/minty/api/v2.rb +8 -0
- data/lib/minty/client.rb +7 -0
- data/lib/minty/exception.rb +50 -0
- data/lib/minty/mixins/api_token_struct.rb +4 -0
- data/lib/minty/mixins/headers.rb +19 -0
- data/lib/minty/mixins/httpproxy.rb +125 -0
- data/lib/minty/mixins/initializer.rb +38 -0
- data/lib/minty/mixins/validation.rb +113 -0
- data/lib/minty/mixins.rb +23 -0
- data/lib/minty/version.rb +5 -0
- data/lib/minty.rb +11 -0
- data/lib/minty_client.rb +4 -0
- data/minty.gemspec +39 -0
- data/publish_rubygem.sh +10 -0
- data/spec/integration/lib/minty/api/api_authentication_spec.rb +122 -0
- data/spec/integration/lib/minty/minty_client_spec.rb +92 -0
- data/spec/lib/minty/client_spec.rb +223 -0
- data/spec/lib/minty/mixins/httpproxy_spec.rb +658 -0
- data/spec/lib/minty/mixins/initializer_spec.rb +121 -0
- data/spec/lib/minty/mixins/token_management_spec.rb +129 -0
- data/spec/lib/minty/mixins/validation_spec.rb +559 -0
- data/spec/spec_helper.rb +75 -0
- data/spec/support/credentials.rb +14 -0
- data/spec/support/dummy_class.rb +20 -0
- data/spec/support/dummy_class_for_proxy.rb +6 -0
- data/spec/support/dummy_class_for_restclient.rb +4 -0
- data/spec/support/dummy_class_for_tokens.rb +18 -0
- data/spec/support/import_users.json +13 -0
- data/spec/support/stub_response.rb +3 -0
- metadata +366 -0
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'spec_helper'
|
|
4
|
+
require 'timecop'
|
|
5
|
+
|
|
6
|
+
class MockClass
|
|
7
|
+
attr_reader :token
|
|
8
|
+
|
|
9
|
+
include Minty::Mixins::Initializer
|
|
10
|
+
include Minty::Mixins::HTTPProxy
|
|
11
|
+
include Minty::Mixins::Headers
|
|
12
|
+
include Minty::Mixins::TokenManagement
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
describe Minty::Mixins::Initializer do
|
|
16
|
+
let(:params) { { namespace: 'samples.minty.page' } }
|
|
17
|
+
let(:instance) { DummyClassForProxy.send(:include, described_class).new(params) }
|
|
18
|
+
let(:time_now) { Time.zone.now }
|
|
19
|
+
|
|
20
|
+
context 'api v2' do
|
|
21
|
+
it 'sets retry_count when passed' do
|
|
22
|
+
params[:token] = '123'
|
|
23
|
+
params[:retry_count] = 10
|
|
24
|
+
|
|
25
|
+
expect(instance.instance_variable_get('@retry_count')).to eq(10)
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
context 'token initialization' do
|
|
30
|
+
before do
|
|
31
|
+
params[:api_version] = 2
|
|
32
|
+
Timecop.freeze(time_now)
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
after do
|
|
36
|
+
Timecop.return
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
it 'sets token when access_token is passed' do
|
|
40
|
+
params[:access_token] = '123'
|
|
41
|
+
|
|
42
|
+
expect(instance.instance_variable_get('@token')).to eq('123')
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
it 'sets token when token is passed' do
|
|
46
|
+
params[:token] = '123'
|
|
47
|
+
|
|
48
|
+
expect(instance.instance_variable_get('@token')).to eq('123')
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
it 'fetches a token if none was given' do
|
|
52
|
+
params[:client_id] = client_id = 'test_client_id'
|
|
53
|
+
params[:client_secret] = client_secret = 'test_client_secret'
|
|
54
|
+
params[:api_identifier] = api_identifier = 'test'
|
|
55
|
+
|
|
56
|
+
payload = {
|
|
57
|
+
grant_type: 'client_credentials',
|
|
58
|
+
client_id: client_id,
|
|
59
|
+
client_secret: client_secret,
|
|
60
|
+
audience: api_identifier
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
expect(RestClient::Request).to receive(:execute).with(hash_including(
|
|
64
|
+
method: :post,
|
|
65
|
+
url: 'https://samples.minty.page/oauth/token',
|
|
66
|
+
payload: payload.to_json
|
|
67
|
+
))
|
|
68
|
+
.and_return(StubResponse.new({
|
|
69
|
+
'access_token' => 'test',
|
|
70
|
+
'expires_in' => 86_400
|
|
71
|
+
},
|
|
72
|
+
true,
|
|
73
|
+
200))
|
|
74
|
+
|
|
75
|
+
expect(instance.instance_variable_get('@token')).to eq('test')
|
|
76
|
+
expect(instance.instance_variable_get('@token_expires_at')).to eq(time_now.to_i + 86_400)
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
it "doesn't get a new token if one was supplied using 'token'" do
|
|
80
|
+
params[:token] = 'access-token'
|
|
81
|
+
|
|
82
|
+
expect(RestClient::Request).not_to receive(:execute).with(hash_including(
|
|
83
|
+
method: :post,
|
|
84
|
+
url: 'https://samples.minty.page/oauth/token'
|
|
85
|
+
))
|
|
86
|
+
|
|
87
|
+
expect(instance.instance_variable_get('@token')).to eq('access-token')
|
|
88
|
+
expect(instance.instance_variable_get('@token_expires_at')).to eq(Time.now.to_i + 3600)
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
it "doesn't get a new token if one was supplied using 'access_token'" do
|
|
92
|
+
params[:access_token] = 'access-token'
|
|
93
|
+
|
|
94
|
+
expect(RestClient::Request).not_to receive(:execute).with(hash_including(
|
|
95
|
+
method: :post,
|
|
96
|
+
url: 'https://samples.minty.page/oauth/token'
|
|
97
|
+
))
|
|
98
|
+
|
|
99
|
+
expect(instance.instance_variable_get('@token')).to eq('access-token')
|
|
100
|
+
expect(instance.instance_variable_get('@token_expires_at')).to eq(Time.now.to_i + 3600)
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
it 'can supply token_expires_at option' do
|
|
104
|
+
params[:token] = 'access-token'
|
|
105
|
+
params[:token_expires_at] = time_now.to_i + 300
|
|
106
|
+
|
|
107
|
+
expect(RestClient::Request).not_to receive(:execute).with(hash_including(
|
|
108
|
+
method: :post,
|
|
109
|
+
url: 'https://samples.minty.page/oauth/token'
|
|
110
|
+
))
|
|
111
|
+
|
|
112
|
+
expect(instance.instance_variable_get('@token')).to eq('access-token')
|
|
113
|
+
expect(instance.instance_variable_get('@token_expires_at')).to eq(time_now.to_i + 300)
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
it 'throws if no token or credentials were given' do
|
|
117
|
+
params[:client_id] = 'test-client-id'
|
|
118
|
+
expect { instance }.to raise_error(Minty::InvalidCredentials)
|
|
119
|
+
end
|
|
120
|
+
end
|
|
121
|
+
end
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'spec_helper'
|
|
4
|
+
require 'timecop'
|
|
5
|
+
|
|
6
|
+
describe Minty::Mixins::TokenManagement do
|
|
7
|
+
let(:client_id) { 'test-client-id' }
|
|
8
|
+
let(:client_secret) { 'test-client-secret' }
|
|
9
|
+
let(:api_identifier) { 'test-audience' }
|
|
10
|
+
let(:domain) { 'samples.minty.page' }
|
|
11
|
+
|
|
12
|
+
let(:payload) do
|
|
13
|
+
{
|
|
14
|
+
grant_type: 'client_credentials',
|
|
15
|
+
client_id: client_id,
|
|
16
|
+
client_secret: client_secret,
|
|
17
|
+
audience: api_identifier
|
|
18
|
+
}
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
let(:params) do
|
|
22
|
+
{
|
|
23
|
+
domain: domain,
|
|
24
|
+
client_id: client_id,
|
|
25
|
+
client_secret: client_secret,
|
|
26
|
+
api_identifier: api_identifier
|
|
27
|
+
}
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
let(:instance) { DummyClassForTokens.send(:include, described_class).new(params) }
|
|
31
|
+
let(:time_now) { Time.zone.now }
|
|
32
|
+
|
|
33
|
+
before :each do
|
|
34
|
+
Timecop.freeze(time_now)
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
after :each do
|
|
38
|
+
Timecop.return
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
context 'get_token' do
|
|
42
|
+
it 'renews the token if there is no token set' do
|
|
43
|
+
expect(RestClient::Request).to receive(:execute).with(hash_including(
|
|
44
|
+
method: :post,
|
|
45
|
+
url: 'https://samples.minty.page/oauth/token',
|
|
46
|
+
payload: payload.to_json
|
|
47
|
+
))
|
|
48
|
+
.and_return(StubResponse.new({
|
|
49
|
+
'access_token' => 'test',
|
|
50
|
+
'expires_in' => 86_400
|
|
51
|
+
},
|
|
52
|
+
true,
|
|
53
|
+
200))
|
|
54
|
+
|
|
55
|
+
instance.send(:get_token)
|
|
56
|
+
|
|
57
|
+
expect(instance.instance_variable_get('@token')).to eq('test')
|
|
58
|
+
expect(instance.instance_variable_get('@token_expires_at')).to eq(time_now.to_i + 86_400)
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
it 'does not renew the token if the expiry time has not been reached' do
|
|
62
|
+
params[:token] = 'test-token'
|
|
63
|
+
params[:token_expires_at] = time_now.to_i + 86_400
|
|
64
|
+
|
|
65
|
+
expect(RestClient::Request).not_to receive(:execute).with(hash_including(
|
|
66
|
+
method: :post,
|
|
67
|
+
url: 'https://samples.minty.page/oauth/token'
|
|
68
|
+
))
|
|
69
|
+
|
|
70
|
+
instance.send(:get_token)
|
|
71
|
+
|
|
72
|
+
expect(instance.instance_variable_get('@token')).to eq('test-token')
|
|
73
|
+
expect(instance.instance_variable_get('@token_expires_at')).to eq(time_now.to_i + 86_400)
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
it 'renews the token if within 10 seconds of the expiry' do
|
|
77
|
+
params[:token] = 'test-token'
|
|
78
|
+
params[:token_expires_at] = time_now.to_i + 5
|
|
79
|
+
|
|
80
|
+
expect(RestClient::Request).to receive(:execute).with(hash_including(
|
|
81
|
+
method: :post,
|
|
82
|
+
url: 'https://samples.minty.page/oauth/token',
|
|
83
|
+
payload: payload.to_json
|
|
84
|
+
))
|
|
85
|
+
.and_return(StubResponse.new({
|
|
86
|
+
'access_token' => 'renewed_token',
|
|
87
|
+
'expires_in' => 86_400
|
|
88
|
+
},
|
|
89
|
+
true,
|
|
90
|
+
200))
|
|
91
|
+
|
|
92
|
+
instance.send(:get_token)
|
|
93
|
+
|
|
94
|
+
expect(instance.instance_variable_get('@token')).to eq('renewed_token')
|
|
95
|
+
expect(instance.instance_variable_get('@token_expires_at')).to eq(time_now.to_i + 86_400)
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
it 'renews the token if past the expiry' do
|
|
99
|
+
params[:token] = 'test-token'
|
|
100
|
+
params[:token_expires_at] = time_now.to_i - 10
|
|
101
|
+
|
|
102
|
+
expect(RestClient::Request).to receive(:execute).with(hash_including(
|
|
103
|
+
method: :post,
|
|
104
|
+
url: 'https://samples.minty.page/oauth/token',
|
|
105
|
+
payload: payload.to_json
|
|
106
|
+
))
|
|
107
|
+
.and_return(StubResponse.new({
|
|
108
|
+
'access_token' => 'renewed_token',
|
|
109
|
+
'expires_in' => 86_400
|
|
110
|
+
},
|
|
111
|
+
true,
|
|
112
|
+
200))
|
|
113
|
+
|
|
114
|
+
instance.send(:get_token)
|
|
115
|
+
|
|
116
|
+
expect(instance.instance_variable_get('@token')).to eq('renewed_token')
|
|
117
|
+
expect(instance.instance_variable_get('@token_expires_at')).to eq(time_now.to_i + 86_400)
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
it 'does not renew existing token if no token_expires_at' do
|
|
121
|
+
params[:token] = 'test-token'
|
|
122
|
+
instance.instance_variable_set '@token_expires_at', nil
|
|
123
|
+
|
|
124
|
+
expect(RestClient::Request).not_to receive(:execute)
|
|
125
|
+
|
|
126
|
+
instance.send(:get_token)
|
|
127
|
+
end
|
|
128
|
+
end
|
|
129
|
+
end
|