ibm_cloud_sdk_core 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative("./../test_helper.rb")
4
+ require("webmock/minitest")
5
+ require("http")
6
+
7
+ WebMock.disable_net_connect!(allow_localhost: true)
8
+
9
+ # Unit tests for the IAM Token Manager
10
+ class DetailedResponseTest < Minitest::Test
11
+ def test_detailed_response
12
+ res = IBMCloudSdkCore::DetailedResponse.new(status: 200, headers: {}, body: {}, response: { code: 401 })
13
+ assert_equal(res.status, 200)
14
+ end
15
+
16
+ def test_detailed_response_when_no_arguments
17
+ assert_raises do
18
+ IBMCloudSdkCore::DetailedResponse.new
19
+ end
20
+ end
21
+
22
+ def test_detailed_response_when_only_response
23
+ response = IBMCloudSdkCore::DetailedResponse.new(response: HTTP::Response.new(status: 401, version: 1, body: {}))
24
+ assert_equal(response.status, 401)
25
+ end
26
+ end
@@ -0,0 +1,228 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative("./../test_helper.rb")
4
+ require("webmock/minitest")
5
+
6
+ WebMock.disable_net_connect!(allow_localhost: true)
7
+
8
+ # Unit tests for the IAM Token Manager
9
+ class IAMTokenManagerTest < Minitest::Test
10
+ def test_request_token
11
+ iam_url = "https://iam.bluemix.net/identity/token"
12
+ response = {
13
+ "access_token" => "oAeisG8yqPY7sFR_x66Z15",
14
+ "token_type" => "Bearer",
15
+ "expires_in" => 3600,
16
+ "expiration" => 1_524_167_011,
17
+ "refresh_token" => "jy4gl91BQ"
18
+ }
19
+
20
+ token_manager = IBMCloudSdkCore::IAMTokenManager.new(
21
+ iam_apikey: "iam_apikey",
22
+ iam_access_token: "iam_access_token",
23
+ iam_url: iam_url
24
+ )
25
+ stub_request(:post, "https://iam.bluemix.net/identity/token")
26
+ .with(
27
+ body: { "apikey" => "iam_apikey", "grant_type" => "urn:ibm:params:oauth:grant-type:apikey", "response_type" => "cloud_iam" },
28
+ headers: {
29
+ "Accept" => "application/json",
30
+ "Authorization" => "Basic Yng6Yng=",
31
+ "Content-Type" => "application/x-www-form-urlencoded",
32
+ "Host" => "iam.bluemix.net"
33
+ }
34
+ ).to_return(status: 200, body: response.to_json, headers: {})
35
+ token_response = token_manager.send(:request_token)
36
+ assert_equal(response, token_response)
37
+ end
38
+
39
+ def test_request_token_fails
40
+ iam_url = "https://iam.bluemix.net/identity/token"
41
+ token_manager = IBMCloudSdkCore::IAMTokenManager.new(
42
+ iam_apikey: "iam_apikey",
43
+ iam_access_token: "iam_access_token",
44
+ iam_url: iam_url
45
+ )
46
+ response = {
47
+ "code" => "500",
48
+ "error" => "Oh no"
49
+ }
50
+ stub_request(:post, "https://iam.bluemix.net/identity/token")
51
+ .with(
52
+ body: { "apikey" => "iam_apikey", "grant_type" => "urn:ibm:params:oauth:grant-type:apikey", "response_type" => "cloud_iam" },
53
+ headers: {
54
+ "Accept" => "application/json",
55
+ "Authorization" => "Basic Yng6Yng=",
56
+ "Content-Type" => "application/x-www-form-urlencoded",
57
+ "Host" => "iam.bluemix.net"
58
+ }
59
+ ).to_return(status: 500, body: response.to_json, headers: {})
60
+ assert_raises do
61
+ token_manager.send(:request_token)
62
+ end
63
+ end
64
+
65
+ def test_request_token_fails_catch_exception
66
+ iam_url = "https://iam.bluemix.net/identity/token"
67
+ token_manager = IBMCloudSdkCore::IAMTokenManager.new(
68
+ iam_apikey: "iam_apikey",
69
+ iam_access_token: "iam_access_token",
70
+ iam_url: iam_url
71
+ )
72
+ response = {
73
+ "code" => "500",
74
+ "error" => "Oh no"
75
+ }
76
+ stub_request(:post, "https://iam.bluemix.net/identity/token")
77
+ .with(
78
+ body: { "apikey" => "iam_apikey", "grant_type" => "urn:ibm:params:oauth:grant-type:apikey", "response_type" => "cloud_iam" },
79
+ headers: {
80
+ "Accept" => "application/json",
81
+ "Authorization" => "Basic Yng6Yng=",
82
+ "Content-Type" => "application/x-www-form-urlencoded",
83
+ "Host" => "iam.bluemix.net"
84
+ }
85
+ ).to_return(status: 401, body: response.to_json, headers: {})
86
+ begin
87
+ token_manager.send(:request_token)
88
+ rescue IBMCloudSdkCore::ApiException => e
89
+ assert(e.to_s.instance_of?(String))
90
+ end
91
+ end
92
+
93
+ def test_refresh_token
94
+ iam_url = "https://iam.bluemix.net/identity/token"
95
+ response = {
96
+ "access_token" => "oAeisG8yqPY7sFR_x66Z15",
97
+ "token_type" => "Bearer",
98
+ "expires_in" => 3600,
99
+ "expiration" => 1_524_167_011,
100
+ "refresh_token" => "jy4gl91BQ"
101
+ }
102
+ token_manager = IBMCloudSdkCore::IAMTokenManager.new(
103
+ iam_apikey: "iam_apikey",
104
+ iam_access_token: "iam_access_token",
105
+ iam_url: iam_url
106
+ )
107
+ stub_request(:post, "https://iam.bluemix.net/identity/token")
108
+ .with(
109
+ body: { "grant_type" => "refresh_token", "refresh_token" => "" },
110
+ headers: {
111
+ "Accept" => "application/json",
112
+ "Authorization" => "Basic Yng6Yng=",
113
+ "Content-Type" => "application/x-www-form-urlencoded",
114
+ "Host" => "iam.bluemix.net"
115
+ }
116
+ ).to_return(status: 200, body: response.to_json, headers: {})
117
+ token_response = token_manager.send(:refresh_token)
118
+ assert_equal(response, token_response)
119
+ end
120
+
121
+ def test_is_token_expired
122
+ token_manager = IBMCloudSdkCore::IAMTokenManager.new(
123
+ iam_apikey: "iam_apikey",
124
+ iam_access_token: "iam_access_token",
125
+ iam_url: "iam_url"
126
+ )
127
+ token_manager.token_info = {
128
+ "access_token" => "oAeisG8yqPY7sFR_x66Z15",
129
+ "token_type" => "Bearer",
130
+ "expires_in" => 3600,
131
+ "expiration" => Time.now.to_i + 6000,
132
+ "refresh_token" => "jy4gl91BQ"
133
+ }
134
+
135
+ refute(token_manager.send(:token_expired?))
136
+ token_manager.token_info["expiration"] = Time.now.to_i - 3600
137
+ assert(token_manager.send(:token_expired?))
138
+ end
139
+
140
+ def test_is_refresh_token_expired
141
+ token_manager = IBMCloudSdkCore::IAMTokenManager.new(
142
+ iam_apikey: "iam_apikey",
143
+ iam_access_token: "iam_access_token",
144
+ iam_url: "iam_url"
145
+ )
146
+ token_manager.token_info = {
147
+ "access_token" => "oAeisG8yqPY7sFR_x66Z15",
148
+ "token_type" => "Bearer",
149
+ "expires_in" => 3600,
150
+ "expiration" => Time.now.to_i,
151
+ "refresh_token" => "jy4gl91BQ"
152
+ }
153
+
154
+ refute(token_manager.send(:refresh_token_expired?))
155
+ token_manager.token_info["expiration"] = Time.now.to_i - (8 * 24 * 3600)
156
+ assert(token_manager.send(:token_expired?))
157
+ end
158
+
159
+ def test_get_token
160
+ iam_url = "https://iam.bluemix.net/identity/token"
161
+ token_manager = IBMCloudSdkCore::IAMTokenManager.new(
162
+ iam_apikey: "iam_apikey",
163
+ iam_url: iam_url
164
+ )
165
+ token_manager.user_access_token = "user_access_token"
166
+
167
+ token = token_manager.token
168
+ assert_equal(token_manager.user_access_token, token)
169
+
170
+ response = {
171
+ "access_token" => "hellohello",
172
+ "token_type" => "Bearer",
173
+ "expires_in" => 3600,
174
+ "expiration" => 1_524_167_011,
175
+ "refresh_token" => "jy4gl91BQ"
176
+ }
177
+ stub_request(:post, "https://iam.bluemix.net/identity/token")
178
+ .with(
179
+ body: { "apikey" => "iam_apikey", "grant_type" => "urn:ibm:params:oauth:grant-type:apikey", "response_type" => "cloud_iam" },
180
+ headers: {
181
+ "Accept" => "application/json",
182
+ "Authorization" => "Basic Yng6Yng=",
183
+ "Content-Type" => "application/x-www-form-urlencoded",
184
+ "Host" => "iam.bluemix.net"
185
+ }
186
+ ).to_return(status: 200, body: response.to_json, headers: {})
187
+ token_manager.user_access_token = ""
188
+ token = token_manager.token
189
+ assert_equal("hellohello", token)
190
+
191
+ token_manager.token_info["expiration"] = Time.now.to_i - (20 * 24 * 3600)
192
+ token = token_manager.token
193
+ assert_equal("hellohello", token)
194
+
195
+ stub_request(:post, "https://iam.bluemix.net/identity/token")
196
+ .with(
197
+ headers: {
198
+ "Accept" => "application/json",
199
+ "Authorization" => "Basic Yng6Yng=",
200
+ "Content-Type" => "application/x-www-form-urlencoded",
201
+ "Host" => "iam.bluemix.net"
202
+ }
203
+ ).to_return(status: 200, body: response.to_json, headers: {})
204
+ token_manager.token_info["expiration"] = Time.now.to_i - 4000
205
+ token = token_manager.token
206
+ assert_equal("hellohello", token)
207
+
208
+ token_manager.token_info = {
209
+ "access_token" => "dummy",
210
+ "token_type" => "Bearer",
211
+ "expires_in" => 3600,
212
+ "expiration" => Time.now.to_i + 3600,
213
+ "refresh_token" => "jy4gl91BQ"
214
+ }
215
+ token = token_manager.token
216
+ assert_equal("dummy", token)
217
+ end
218
+
219
+ def test_dont_leak_constants
220
+ assert_nil(defined? DEFAULT_IAM_URL)
221
+ assert_nil(defined? CONTENT_TYPE)
222
+ assert_nil(defined? ACCEPT)
223
+ assert_nil(defined? DEFAULT_AUTHORIZATION)
224
+ assert_nil(defined? REQUEST_TOKEN_GRANT_TYPE)
225
+ assert_nil(defined? REQUEST_TOKEN_RESPONSE_TYPE)
226
+ assert_nil(defined? REFRESH_TOKEN_GRANT_TYPE)
227
+ end
228
+ end
metadata ADDED
@@ -0,0 +1,254 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ibm_cloud_sdk_core
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Mamoon Raja
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-03-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: concurrent-ruby
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: http
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 4.1.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 4.1.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.16'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.16'
55
+ - !ruby/object:Gem::Dependency
56
+ name: codecov
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.1'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.1'
69
+ - !ruby/object:Gem::Dependency
70
+ name: dotenv
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '2.4'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '2.4'
83
+ - !ruby/object:Gem::Dependency
84
+ name: httplog
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '1.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '1.0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: minitest
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '5.11'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '5.11'
111
+ - !ruby/object:Gem::Dependency
112
+ name: minitest-hooks
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '1.5'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '1.5'
125
+ - !ruby/object:Gem::Dependency
126
+ name: minitest-reporters
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: '1.3'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: '1.3'
139
+ - !ruby/object:Gem::Dependency
140
+ name: minitest-retry
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - "~>"
144
+ - !ruby/object:Gem::Version
145
+ version: '0.1'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - "~>"
151
+ - !ruby/object:Gem::Version
152
+ version: '0.1'
153
+ - !ruby/object:Gem::Dependency
154
+ name: rake
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - "~>"
158
+ - !ruby/object:Gem::Version
159
+ version: '12.3'
160
+ type: :development
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - "~>"
165
+ - !ruby/object:Gem::Version
166
+ version: '12.3'
167
+ - !ruby/object:Gem::Dependency
168
+ name: rubocop
169
+ requirement: !ruby/object:Gem::Requirement
170
+ requirements:
171
+ - - '='
172
+ - !ruby/object:Gem::Version
173
+ version: '0.62'
174
+ type: :development
175
+ prerelease: false
176
+ version_requirements: !ruby/object:Gem::Requirement
177
+ requirements:
178
+ - - '='
179
+ - !ruby/object:Gem::Version
180
+ version: '0.62'
181
+ - !ruby/object:Gem::Dependency
182
+ name: simplecov
183
+ requirement: !ruby/object:Gem::Requirement
184
+ requirements:
185
+ - - "~>"
186
+ - !ruby/object:Gem::Version
187
+ version: '0.16'
188
+ type: :development
189
+ prerelease: false
190
+ version_requirements: !ruby/object:Gem::Requirement
191
+ requirements:
192
+ - - "~>"
193
+ - !ruby/object:Gem::Version
194
+ version: '0.16'
195
+ - !ruby/object:Gem::Dependency
196
+ name: webmock
197
+ requirement: !ruby/object:Gem::Requirement
198
+ requirements:
199
+ - - "~>"
200
+ - !ruby/object:Gem::Version
201
+ version: '3.4'
202
+ type: :development
203
+ prerelease: false
204
+ version_requirements: !ruby/object:Gem::Requirement
205
+ requirements:
206
+ - - "~>"
207
+ - !ruby/object:Gem::Version
208
+ version: '3.4'
209
+ description:
210
+ email:
211
+ executables: []
212
+ extensions: []
213
+ extra_rdoc_files: []
214
+ files:
215
+ - README.md
216
+ - lib/ibm_cloud_sdk_core.rb
217
+ - lib/ibm_cloud_sdk_core/api_exception.rb
218
+ - lib/ibm_cloud_sdk_core/base_service.rb
219
+ - lib/ibm_cloud_sdk_core/detailed_response.rb
220
+ - lib/ibm_cloud_sdk_core/iam_token_manager.rb
221
+ - lib/ibm_cloud_sdk_core/version.rb
222
+ - rakefile
223
+ - test/test_helper.rb
224
+ - test/unit/test_base_service.rb
225
+ - test/unit/test_configure_http_proxy.rb
226
+ - test/unit/test_detailed_response.rb
227
+ - test/unit/test_iam_token_manager.rb
228
+ homepage: https://www.github.com/IBM
229
+ licenses:
230
+ - Apache-2.0
231
+ metadata:
232
+ allowed_push_host: https://rubygems.org
233
+ source_code_uri: https://github.com/IBM/ruby-sdk-core
234
+ documentation_uri: https://github.com/IBM/ruby-sdk-core
235
+ post_install_message:
236
+ rdoc_options: []
237
+ require_paths:
238
+ - lib
239
+ required_ruby_version: !ruby/object:Gem::Requirement
240
+ requirements:
241
+ - - ">="
242
+ - !ruby/object:Gem::Version
243
+ version: '2.3'
244
+ required_rubygems_version: !ruby/object:Gem::Requirement
245
+ requirements:
246
+ - - ">="
247
+ - !ruby/object:Gem::Version
248
+ version: '0'
249
+ requirements: []
250
+ rubygems_version: 3.0.3
251
+ signing_key:
252
+ specification_version: 4
253
+ summary: Official IBM Cloud SDK core library
254
+ test_files: []