ibm_cloud_sdk_core 0.1.1

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,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module IBMCloudSdkCore
4
+ VERSION = "0.1.1"
5
+ end
data/rakefile ADDED
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "dotenv/tasks"
4
+ require "rake/testtask"
5
+ require "rubocop/rake_task"
6
+
7
+ task default: %w[def]
8
+
9
+ RuboCop::RakeTask.new
10
+
11
+ namespace :test do
12
+ Rake::TestTask.new do |t|
13
+ t.name = "unit"
14
+ t.description = "Run unit tests"
15
+ t.libs << "test"
16
+ t.test_files = FileList["test/unit/*.rb"]
17
+ t.verbose = true
18
+ t.warning = true
19
+ t.deps = [:rubocop]
20
+ end
21
+
22
+ Rake::TestTask.new do |t|
23
+ t.name = "appveyor_status"
24
+ t.description = "Checks to ensure that AppVeyor tests pass before deploying from Travis"
25
+ t.libs << "test"
26
+ t.test_files = FileList["test/appveyor_status.rb"]
27
+ t.verbose = false
28
+ t.warning = false
29
+ end
30
+ end
31
+
32
+ desc "Run unit tests"
33
+ task :test do
34
+ Rake::Task["test:unit"].invoke
35
+ end
36
+
37
+ desc "Run tests and generate a code coverage report"
38
+ task :coverage do
39
+ ENV["COVERAGE"] = "true" if ENV["TRAVIS_RUBY_VERSION"] == "2.5.1" || ENV["CI"].nil?
40
+ Rake::Task["test"].execute
41
+ end
42
+
43
+ task def: %i[coverage] do
44
+ end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ require("simplecov")
4
+ require("codecov")
5
+ require("minitest/reporters")
6
+
7
+ if ENV["COVERAGE"]
8
+ SimpleCov.formatter = SimpleCov::Formatter::Codecov if ENV["CI"]
9
+ unless SimpleCov.running
10
+ SimpleCov.start do
11
+ add_filter "/test/"
12
+ add_filter do |src_file|
13
+ File.basename(src_file.filename) == "version.rb"
14
+ end
15
+
16
+ command_name "Minitest"
17
+ end
18
+ end
19
+ end
20
+
21
+ require("minitest/autorun")
22
+ require_relative("./../lib/ibm_cloud_sdk_core.rb")
23
+ require("minitest/retry")
24
+
25
+ Minitest::Retry.use!
26
+
27
+ Minitest::Reporters.use! [Minitest::Reporters::DefaultReporter.new(color: true, slow_count: 10), Minitest::Reporters::SpecReporter.new, Minitest::Reporters::HtmlReporter.new] if ENV["CI"].nil?
28
+ Minitest::Reporters.use! [Minitest::Reporters::DefaultReporter.new(color: true, slow_count: 10), Minitest::Reporters::SpecReporter.new] if ENV["CI"]
@@ -0,0 +1,304 @@
1
+ # frozen_string_literal: true
2
+
3
+ require("json")
4
+ require_relative("./../test_helper.rb")
5
+ require("webmock/minitest")
6
+
7
+ WebMock.disable_net_connect!(allow_localhost: true)
8
+
9
+ # Unit tests for the base service
10
+ class BaseServiceTest < Minitest::Test
11
+ def test_wrong_username
12
+ assert_raises do
13
+ IBMCloudSdkCore::BaseService.new(
14
+ username: "\"username",
15
+ password: "password"
16
+ )
17
+ end
18
+ end
19
+
20
+ def test_wrong_apikey
21
+ assert_raises do
22
+ IBMCloudSdkCore::BaseService.new(
23
+ iam_apikey: "{apikey"
24
+ )
25
+ end
26
+ end
27
+
28
+ def test_wrong_url
29
+ assert_raises do
30
+ IBMCloudSdkCore::BaseService.new(
31
+ iam_apikey: "apikey",
32
+ url: "url}"
33
+ )
34
+ end
35
+ end
36
+
37
+ def test_correct_creds_and_headers
38
+ service = IBMCloudSdkCore::BaseService.new(
39
+ username: "username",
40
+ password: "password"
41
+ )
42
+ service.add_default_headers(
43
+ headers: {
44
+ "X-Watson-Learning-Opt-Out" => "1",
45
+ "X-Watson-Test" => "1"
46
+ }
47
+ )
48
+ service.headers("analytics" => "example")
49
+ refute_nil(service)
50
+ end
51
+
52
+ def test_iam_access_token
53
+ token = "new$token"
54
+ service = IBMCloudSdkCore::BaseService.new(
55
+ url: "https://we.the.best"
56
+ )
57
+ response = service.iam_access_token(iam_access_token: token)
58
+ assert_equal(response, token)
59
+ end
60
+
61
+ def test_set_credentials_from_path_in_env
62
+ file_path = File.join(File.dirname(__FILE__), "../../resources/ibm-credentials.env")
63
+ ENV["IBM_CREDENTIALS_FILE"] = file_path
64
+ service = IBMCloudSdkCore::BaseService.new(display_name: "Visual Recognition")
65
+ assert_equal(service.url, "https://gateway.ronaldo.com")
66
+ refute_nil(service)
67
+ ENV.delete("IBM_CREDENTIALS_FILE")
68
+ end
69
+
70
+ def test_vcap_services
71
+ ENV["VCAP_SERVICES"] = JSON.parse(File.read(Dir.getwd + "/resources/vcap-testing.json")).to_json
72
+ service = IBMCloudSdkCore::BaseService.new(vcap_services_name: "salah", use_vcap_services: true)
73
+ assert_equal(service.username, "mo")
74
+ end
75
+
76
+ def test_dummy_request
77
+ ENV["VCAP_SERVICES"] = JSON.parse(File.read(Dir.getwd + "/resources/vcap-testing.json")).to_json
78
+ stub_request(:get, "https://we.the.best/music")
79
+ .with(
80
+ headers: {
81
+ "Host" => "we.the.best"
82
+ }
83
+ ).to_return(status: 200, body: "", headers: {})
84
+ service = IBMCloudSdkCore::BaseService.new(display_name: "Salah", url: "https://we.the.best")
85
+ service_response = service.request(method: "GET", url: "/music", headers: {})
86
+ assert_equal("", service_response.result)
87
+ end
88
+
89
+ def test_dummy_request_form_data
90
+ service = IBMCloudSdkCore::BaseService.new(
91
+ iam_apikey: "apikey",
92
+ iam_access_token: "token",
93
+ url: "https://gateway.watsonplatform.net/"
94
+ )
95
+ form_data = {}
96
+ file = File.open(Dir.getwd + "/resources/cnc_test.pdf")
97
+ filename = file.path if filename.nil? && file.respond_to?(:path)
98
+ form_data[:file] = HTTP::FormData::File.new(file, content_type: "application/octet-stream", filename: filename)
99
+
100
+ stub_request(:post, "https://gateway.watsonplatform.net/").with do |req|
101
+ # Test the headers.
102
+ assert_equal(req.headers["Accept"], "application/json")
103
+ assert_match(%r{\Amultipart/form-data}, req.headers["Content-Type"])
104
+ end
105
+ service.request(
106
+ method: "POST",
107
+ form: form_data,
108
+ headers: { "Accept" => "application/json" },
109
+ url: ""
110
+ )
111
+ end
112
+
113
+ def test_dummy_request_fails
114
+ ENV["VCAP_SERVICES"] = JSON.parse(File.read(Dir.getwd + "/resources/vcap-testing.json")).to_json
115
+ response = {
116
+ "code" => "500",
117
+ "error" => "Oh no"
118
+ }
119
+ stub_request(:get, "https://we.the.best/music")
120
+ .with(
121
+ headers: {
122
+ "Host" => "we.the.best"
123
+ }
124
+ ).to_return(status: 500, body: response.to_json, headers: {})
125
+ service = IBMCloudSdkCore::BaseService.new(display_name: "Salah", url: "https://we.the.best")
126
+ assert_raises do
127
+ service.request(method: "GET", url: "/music", headers: {})
128
+ end
129
+ end
130
+
131
+ def test_dummy_request_icp
132
+ response = {
133
+ "text" => "I want financial advice today.",
134
+ "created" => "2016-07-11T16:39:01.774Z",
135
+ "updated" => "2015-12-07T18:53:59.153Z"
136
+ }
137
+ headers = {
138
+ "Content-Type" => "application/json"
139
+ }
140
+ stub_request(:get, "https://we.the.best/music")
141
+ .with(
142
+ headers: {
143
+ "Authorization" => "Basic YXBpa2V5OmljcC14eXo=",
144
+ "Host" => "we.the.best"
145
+ }
146
+ ).to_return(status: 200, body: response.to_json, headers: headers)
147
+ service = IBMCloudSdkCore::BaseService.new(
148
+ url: "https://we.the.best",
149
+ username: "apikey",
150
+ password: "icp-xyz"
151
+ )
152
+ service_response = service.request(method: "GET", url: "/music", headers: {})
153
+ assert_equal(response, service_response.result)
154
+ end
155
+
156
+ def test_dummy_request_icp_iam_apikey
157
+ response = {
158
+ "text" => "I want financial advice today.",
159
+ "created" => "2016-07-11T16:39:01.774Z",
160
+ "updated" => "2015-12-07T18:53:59.153Z"
161
+ }
162
+ headers = {
163
+ "Content-Type" => "application/json"
164
+ }
165
+ stub_request(:get, "https://we.the.best/music")
166
+ .with(
167
+ headers: {
168
+ "Authorization" => "Basic YXBpa2V5OmljcC14eXo=",
169
+ "Host" => "we.the.best"
170
+ }
171
+ ).to_return(status: 200, body: response.to_json, headers: headers)
172
+ service = IBMCloudSdkCore::BaseService.new(
173
+ url: "https://we.the.best",
174
+ iam_apikey: "icp-xyz"
175
+ )
176
+ service_response = service.request(method: "GET", url: "/music", headers: {})
177
+ assert_equal(response, service_response.result)
178
+ end
179
+
180
+ def test_dummy_request_icp_iam_apikey_cred_file
181
+ response = {
182
+ "text" => "I want financial advice today.",
183
+ "created" => "2016-07-11T16:39:01.774Z",
184
+ "updated" => "2015-12-07T18:53:59.153Z"
185
+ }
186
+ headers = {
187
+ "Content-Type" => "application/json"
188
+ }
189
+ stub_request(:get, "https://we.the.best/music")
190
+ .with(
191
+ headers: {
192
+ "Authorization" => "Basic YXBpa2V5OmljcC14eXo=",
193
+ "Host" => "we.the.best"
194
+ }
195
+ ).to_return(status: 200, body: response.to_json, headers: headers)
196
+ file_path = File.join(File.dirname(__FILE__), "../../resources/ibm-credentials.env")
197
+ ENV["IBM_CREDENTIALS_FILE"] = file_path
198
+ service = IBMCloudSdkCore::BaseService.new(
199
+ url: "https://we.the.best",
200
+ display_name: "messi"
201
+ )
202
+ service_response = service.request(method: "GET", url: "/music", headers: {})
203
+ assert_equal(response, service_response.result)
204
+ end
205
+
206
+ def test_dummy_request_username_apikey
207
+ response = {
208
+ "text" => "I want financial advice today.",
209
+ "created" => "2016-07-11T16:39:01.774Z",
210
+ "updated" => "2015-12-07T18:53:59.153Z"
211
+ }
212
+ token_response = {
213
+ "access_token" => "oAeisG8yqPY7sFR_x66Z15",
214
+ "token_type" => "Bearer",
215
+ "expires_in" => 3600,
216
+ "expiration" => 1_524_167_011,
217
+ "refresh_token" => "jy4gl91BQ"
218
+ }
219
+
220
+ headers = {
221
+ "Content-Type" => "application/json"
222
+ }
223
+ stub_request(:post, "https://iam.bluemix.net/identity/token")
224
+ .with(
225
+ body: {
226
+ "apikey" => "xyz",
227
+ "grant_type" => "urn:ibm:params:oauth:grant-type:apikey",
228
+ "response_type" => "cloud_iam"
229
+ },
230
+ headers: {
231
+ "Accept" => "application/json",
232
+ "Authorization" => "Basic Yng6Yng=",
233
+ "Connection" => "close",
234
+ "Content-Type" => "application/x-www-form-urlencoded",
235
+ "Host" => "iam.bluemix.net",
236
+ "User-Agent" => "http.rb/4.1.1"
237
+ }
238
+ ).to_return(status: 200, body: token_response.to_json, headers: {})
239
+ stub_request(:get, "https://we.the.best/music")
240
+ .with(
241
+ headers: {
242
+ "Authorization" => "Bearer oAeisG8yqPY7sFR_x66Z15",
243
+ "Host" => "we.the.best"
244
+ }
245
+ ).to_return(status: 200, body: response.to_json, headers: headers)
246
+ service = IBMCloudSdkCore::BaseService.new(
247
+ url: "https://we.the.best",
248
+ username: "apikey",
249
+ password: "xyz"
250
+ )
251
+ service_response = service.request(method: "GET", url: "/music", headers: {})
252
+ assert_equal(response, service_response.result)
253
+ end
254
+
255
+ def test_dummy_request_username_apikey_cred_file
256
+ response = {
257
+ "text" => "I want financial advice today.",
258
+ "created" => "2016-07-11T16:39:01.774Z",
259
+ "updated" => "2015-12-07T18:53:59.153Z"
260
+ }
261
+ token_response = {
262
+ "access_token" => "oAeisG8yqPY7sFR_x66Z15",
263
+ "token_type" => "Bearer",
264
+ "expires_in" => 3600,
265
+ "expiration" => 1_524_167_011,
266
+ "refresh_token" => "jy4gl91BQ"
267
+ }
268
+
269
+ headers = {
270
+ "Content-Type" => "application/json"
271
+ }
272
+ stub_request(:post, "https://iam.bluemix.net/identity/token")
273
+ .with(
274
+ body: {
275
+ "apikey" => "xyz",
276
+ "grant_type" => "urn:ibm:params:oauth:grant-type:apikey",
277
+ "response_type" => "cloud_iam"
278
+ },
279
+ headers: {
280
+ "Accept" => "application/json",
281
+ "Authorization" => "Basic Yng6Yng=",
282
+ "Connection" => "close",
283
+ "Content-Type" => "application/x-www-form-urlencoded",
284
+ "Host" => "iam.bluemix.net",
285
+ "User-Agent" => "http.rb/4.1.1"
286
+ }
287
+ ).to_return(status: 200, body: token_response.to_json, headers: {})
288
+ stub_request(:get, "https://we.the.best/music")
289
+ .with(
290
+ headers: {
291
+ "Authorization" => "Bearer oAeisG8yqPY7sFR_x66Z15",
292
+ "Host" => "we.the.best"
293
+ }
294
+ ).to_return(status: 200, body: response.to_json, headers: headers)
295
+ file_path = File.join(File.dirname(__FILE__), "../../resources/ibm-credentials.env")
296
+ ENV["IBM_CREDENTIALS_FILE"] = file_path
297
+ service = IBMCloudSdkCore::BaseService.new(
298
+ display_name: "ronaldo",
299
+ url: "https://we.the.best"
300
+ )
301
+ service_response = service.request(method: "GET", url: "/music", headers: {})
302
+ assert_equal(response, service_response.result)
303
+ end
304
+ end
@@ -0,0 +1,152 @@
1
+ # frozen_string_literal: true
2
+
3
+ require("json")
4
+ require_relative("./../test_helper.rb")
5
+ require("webmock/minitest")
6
+
7
+ WebMock.disable_net_connect!(allow_localhost: true)
8
+
9
+ # Unit tests for the configure_http_client customizations, such as proxies and timeouts
10
+ class HTTPConfigTest < Minitest::Test
11
+ def test_proxy_address_port
12
+ service = IBMCloudSdkCore::BaseService.new(
13
+ version: "2018-03-16",
14
+ username: "username",
15
+ password: "password"
16
+ )
17
+ service.configure_http_client(
18
+ proxy: {
19
+ address: "bogus_address.com",
20
+ port: 9999
21
+ }
22
+ )
23
+ proxy = service.conn.default_options.proxy
24
+ assert_equal("bogus_address.com", proxy[:proxy_address])
25
+ assert_equal(9999, proxy[:proxy_port])
26
+ end
27
+
28
+ def test_proxy_username_password
29
+ service = IBMCloudSdkCore::BaseService.new(
30
+ version: "2018-03-16",
31
+ username: "username",
32
+ password: "password"
33
+ )
34
+ service.configure_http_client(
35
+ proxy: {
36
+ address: "bogus_address.com",
37
+ port: 9999,
38
+ username: "username",
39
+ password: "password"
40
+ }
41
+ )
42
+ proxy = service.conn.default_options.proxy
43
+ assert_equal("bogus_address.com", proxy[:proxy_address])
44
+ assert_equal(9999, proxy[:proxy_port])
45
+ assert_equal("username", proxy[:proxy_username])
46
+ assert_equal("password", proxy[:proxy_password])
47
+ end
48
+
49
+ def test_proxy_headers
50
+ service = IBMCloudSdkCore::BaseService.new(
51
+ version: "2018-03-16",
52
+ username: "username",
53
+ password: "password"
54
+ )
55
+ service.configure_http_client(
56
+ proxy: {
57
+ address: "bogus_address.com",
58
+ port: 9999,
59
+ headers: {
60
+ bogus_header: true
61
+ }
62
+ }
63
+ )
64
+ proxy = service.conn.default_options.proxy
65
+ assert_equal("bogus_address.com", proxy[:proxy_address])
66
+ assert_equal(9999, proxy[:proxy_port])
67
+ assert_equal({ bogus_header: true }, proxy[:proxy_headers])
68
+ end
69
+
70
+ def test_proxy_username_password_headers
71
+ service = IBMCloudSdkCore::BaseService.new(
72
+ version: "2018-03-16",
73
+ username: "username",
74
+ password: "password"
75
+ )
76
+ service.configure_http_client(
77
+ proxy: {
78
+ address: "bogus_address.com",
79
+ port: 9999,
80
+ username: "username",
81
+ password: "password",
82
+ headers: {
83
+ bogus_header: true
84
+ }
85
+ }
86
+ )
87
+ proxy = service.conn.default_options.proxy
88
+ assert_equal("bogus_address.com", proxy[:proxy_address])
89
+ assert_equal(9999, proxy[:proxy_port])
90
+ assert_equal("username", proxy[:proxy_username])
91
+ assert_equal("password", proxy[:proxy_password])
92
+ assert_equal({ bogus_header: true }, proxy[:proxy_headers])
93
+ end
94
+
95
+ def test_timeout_per_operation
96
+ service = IBMCloudSdkCore::BaseService.new(
97
+ version: "2018-03-16",
98
+ username: "username",
99
+ password: "password"
100
+ )
101
+ service.configure_http_client(
102
+ timeout: {
103
+ per_operation: {
104
+ read: 5,
105
+ write: 7,
106
+ connect: 10
107
+ }
108
+ }
109
+ )
110
+ timeout_class = service.conn.default_options.timeout_class
111
+ assert_equal(HTTP::Timeout::PerOperation, timeout_class)
112
+
113
+ expected_timeouts = {
114
+ read_timeout: 5,
115
+ write_timeout: 7,
116
+ connect_timeout: 10
117
+ }
118
+ timeout = service.conn.default_options.timeout_options
119
+ assert_equal(expected_timeouts, timeout)
120
+ end
121
+
122
+ def test_timeout_global
123
+ service = IBMCloudSdkCore::BaseService.new(
124
+ version: "2018-03-16",
125
+ username: "username",
126
+ password: "password"
127
+ )
128
+ service.configure_http_client(
129
+ timeout: {
130
+ global: 20
131
+ }
132
+ )
133
+ timeout_class = service.conn.default_options.timeout_class
134
+ assert_equal(HTTP::Timeout::Global, timeout_class)
135
+
136
+ expected_timeouts = {
137
+ global_timeout: 20
138
+ }
139
+ timeout = service.conn.default_options.timeout_options
140
+ assert_equal(expected_timeouts, timeout)
141
+ end
142
+
143
+ def test_disable_ssl_verification
144
+ service = IBMCloudSdkCore::BaseService.new(
145
+ version: "2018-03-16",
146
+ username: "username",
147
+ password: "password"
148
+ )
149
+ service.configure_http_client(disable_ssl_verification: true)
150
+ refute_nil(service.conn.default_options.ssl_context)
151
+ end
152
+ end