ibm_watson 0.13.0 → 0.14.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.
@@ -1,175 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative("./../../lib/ibm_watson/iam_token_manager.rb")
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 IAM Token Manager
10
- class IAMTokenManagerTest < Minitest::Test
11
- def test_request_token
12
- iam_url = "https://iam.bluemix.net/identity/token"
13
- response = {
14
- "access_token" => "oAeisG8yqPY7sFR_x66Z15",
15
- "token_type" => "Bearer",
16
- "expires_in" => 3600,
17
- "expiration" => 1_524_167_011,
18
- "refresh_token" => "jy4gl91BQ"
19
- }
20
-
21
- token_manager = IAMTokenManager.new(
22
- iam_apikey: "iam_apikey",
23
- iam_access_token: "iam_access_token",
24
- iam_url: iam_url
25
- )
26
- stub_request(:post, "https://iam.bluemix.net/identity/token")
27
- .with(
28
- body: { "apikey" => "iam_apikey", "grant_type" => "urn:ibm:params:oauth:grant-type:apikey", "response_type" => "cloud_iam" },
29
- headers: {
30
- "Accept" => "application/json",
31
- "Authorization" => "Basic Yng6Yng=",
32
- "Content-Type" => "application/x-www-form-urlencoded",
33
- "Host" => "iam.bluemix.net"
34
- }
35
- ).to_return(status: 200, body: response.to_json, headers: {})
36
- token_response = token_manager.send(:request_token)
37
- assert_equal(response, token_response)
38
- end
39
-
40
- def test_refresh_token
41
- iam_url = "https://iam.bluemix.net/identity/token"
42
- response = {
43
- "access_token" => "oAeisG8yqPY7sFR_x66Z15",
44
- "token_type" => "Bearer",
45
- "expires_in" => 3600,
46
- "expiration" => 1_524_167_011,
47
- "refresh_token" => "jy4gl91BQ"
48
- }
49
- token_manager = IAMTokenManager.new(
50
- iam_apikey: "iam_apikey",
51
- iam_access_token: "iam_access_token",
52
- iam_url: iam_url
53
- )
54
- stub_request(:post, "https://iam.bluemix.net/identity/token")
55
- .with(
56
- body: { "grant_type" => "refresh_token", "refresh_token" => "" },
57
- headers: {
58
- "Accept" => "application/json",
59
- "Authorization" => "Basic Yng6Yng=",
60
- "Content-Type" => "application/x-www-form-urlencoded",
61
- "Host" => "iam.bluemix.net"
62
- }
63
- ).to_return(status: 200, body: response.to_json, headers: {})
64
- token_response = token_manager.send(:refresh_token)
65
- assert_equal(response, token_response)
66
- end
67
-
68
- def test_is_token_expired
69
- token_manager = IAMTokenManager.new(
70
- iam_apikey: "iam_apikey",
71
- iam_access_token: "iam_access_token",
72
- iam_url: "iam_url"
73
- )
74
- token_manager.token_info = {
75
- "access_token" => "oAeisG8yqPY7sFR_x66Z15",
76
- "token_type" => "Bearer",
77
- "expires_in" => 3600,
78
- "expiration" => Time.now.to_i + 6000,
79
- "refresh_token" => "jy4gl91BQ"
80
- }
81
-
82
- refute(token_manager.send(:token_expired?))
83
- token_manager.token_info["expiration"] = Time.now.to_i - 3600
84
- assert(token_manager.send(:token_expired?))
85
- end
86
-
87
- def test_is_refresh_token_expired
88
- token_manager = IAMTokenManager.new(
89
- iam_apikey: "iam_apikey",
90
- iam_access_token: "iam_access_token",
91
- iam_url: "iam_url"
92
- )
93
- token_manager.token_info = {
94
- "access_token" => "oAeisG8yqPY7sFR_x66Z15",
95
- "token_type" => "Bearer",
96
- "expires_in" => 3600,
97
- "expiration" => Time.now.to_i,
98
- "refresh_token" => "jy4gl91BQ"
99
- }
100
-
101
- refute(token_manager.send(:refresh_token_expired?))
102
- token_manager.token_info["expiration"] = Time.now.to_i - (8 * 24 * 3600)
103
- assert(token_manager.send(:token_expired?))
104
- end
105
-
106
- def test_get_token
107
- iam_url = "https://iam.bluemix.net/identity/token"
108
- token_manager = IAMTokenManager.new(
109
- iam_apikey: "iam_apikey",
110
- iam_url: iam_url
111
- )
112
- token_manager.user_access_token = "user_access_token"
113
-
114
- token = token_manager.token
115
- assert_equal(token_manager.user_access_token, token)
116
-
117
- response = {
118
- "access_token" => "hellohello",
119
- "token_type" => "Bearer",
120
- "expires_in" => 3600,
121
- "expiration" => 1_524_167_011,
122
- "refresh_token" => "jy4gl91BQ"
123
- }
124
- stub_request(:post, "https://iam.bluemix.net/identity/token")
125
- .with(
126
- body: { "apikey" => "iam_apikey", "grant_type" => "urn:ibm:params:oauth:grant-type:apikey", "response_type" => "cloud_iam" },
127
- headers: {
128
- "Accept" => "application/json",
129
- "Authorization" => "Basic Yng6Yng=",
130
- "Content-Type" => "application/x-www-form-urlencoded",
131
- "Host" => "iam.bluemix.net"
132
- }
133
- ).to_return(status: 200, body: response.to_json, headers: {})
134
- token_manager.user_access_token = ""
135
- token = token_manager.token
136
- assert_equal("hellohello", token)
137
-
138
- token_manager.token_info["expiration"] = Time.now.to_i - (20 * 24 * 3600)
139
- token = token_manager.token
140
- assert_equal("hellohello", token)
141
-
142
- stub_request(:post, "https://iam.bluemix.net/identity/token")
143
- .with(
144
- headers: {
145
- "Accept" => "application/json",
146
- "Authorization" => "Basic Yng6Yng=",
147
- "Content-Type" => "application/x-www-form-urlencoded",
148
- "Host" => "iam.bluemix.net"
149
- }
150
- ).to_return(status: 200, body: response.to_json, headers: {})
151
- token_manager.token_info["expiration"] = Time.now.to_i - 4000
152
- token = token_manager.token
153
- assert_equal("hellohello", token)
154
-
155
- token_manager.token_info = {
156
- "access_token" => "dummy",
157
- "token_type" => "Bearer",
158
- "expires_in" => 3600,
159
- "expiration" => Time.now.to_i + 3600,
160
- "refresh_token" => "jy4gl91BQ"
161
- }
162
- token = token_manager.token
163
- assert_equal("dummy", token)
164
- end
165
-
166
- def test_dont_leak_constants
167
- assert_nil(defined? DEFAULT_IAM_URL)
168
- assert_nil(defined? CONTENT_TYPE)
169
- assert_nil(defined? ACCEPT)
170
- assert_nil(defined? DEFAULT_AUTHORIZATION)
171
- assert_nil(defined? REQUEST_TOKEN_GRANT_TYPE)
172
- assert_nil(defined? REQUEST_TOKEN_RESPONSE_TYPE)
173
- assert_nil(defined? REFRESH_TOKEN_GRANT_TYPE)
174
- end
175
- end