Skip to content

Commit 174e351

Browse files
committed
feat(iamTokenManager): keep internal methods private and expose method to get token
1 parent 0dd91d5 commit 174e351

File tree

2 files changed

+14
-23
lines changed

2 files changed

+14
-23
lines changed

lib/ibm_watson/iam_token_manager.rb

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def request(method:, url:, headers: nil, params: nil, data: nil)
5454
# 2. If this class is managing tokens and does not yet have one, make a request for one
5555
# 3. If this class is managing tokens and the token has expired refresh it. In case the refresh token is expired, get a new one
5656
# If this class is managing tokens and has a valid token stored, send it
57-
def _token
57+
def token
5858
return @user_access_token unless @user_access_token.nil? || (@user_access_token.respond_to?(:empty?) && @user_access_token.empty?)
5959
if @token_info.all? { |_k, v| v.nil? }
6060
token_info = _request_token
@@ -73,6 +73,8 @@ def _token
7373
end
7474
end
7575

76+
private
77+
7678
# Request an IAM token using an API key
7779
def _request_token
7880
headers = {
@@ -114,17 +116,6 @@ def _refresh_token
114116
response
115117
end
116118

117-
# Set a self-managed IAM access token.
118-
# The access token should be valid and not yet expired.
119-
def _access_token(iam_access_token:)
120-
@user_access_token = iam_access_token
121-
end
122-
123-
# Set the IAM api key
124-
def _iam_apikey(iam_apikey:)
125-
@iam_apikey = iam_apikey
126-
end
127-
128119
# Check if currently stored token is expired.
129120
# Using a buffer to prevent the edge case of the
130121
# token expiring before the request could be made.

test/unit/test_iam_token_manager.rb

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def test_request_token
3333
"Host" => "iam.bluemix.net"
3434
}
3535
).to_return(status: 200, body: response.to_json, headers: {})
36-
token_response = token_manager._request_token
36+
token_response = token_manager.send(:_request_token)
3737
assert_equal(response, token_response)
3838
end
3939

@@ -61,7 +61,7 @@ def test_refresh_token
6161
"Host" => "iam.bluemix.net"
6262
}
6363
).to_return(status: 200, body: response.to_json, headers: {})
64-
token_response = token_manager._refresh_token
64+
token_response = token_manager.send(:_refresh_token)
6565
assert_equal(response, token_response)
6666
end
6767

@@ -79,9 +79,9 @@ def test_is_token_expired
7979
"refresh_token" => "jy4gl91BQ"
8080
}
8181

82-
refute(token_manager._is_token_expired?)
82+
refute(token_manager.send(:_is_token_expired?))
8383
token_manager.token_info["expiration"] = Time.now.to_i - 3600
84-
assert(token_manager._is_token_expired?)
84+
assert(token_manager.send(:_is_token_expired?))
8585
end
8686

8787
def test_is_refresh_token_expired
@@ -98,9 +98,9 @@ def test_is_refresh_token_expired
9898
"refresh_token" => "jy4gl91BQ"
9999
}
100100

101-
refute(token_manager._is_refresh_token_expired?)
101+
refute(token_manager.send(:_is_refresh_token_expired?))
102102
token_manager.token_info["expiration"] = Time.now.to_i - (8 * 24 * 3600)
103-
assert(token_manager._is_token_expired?)
103+
assert(token_manager.send(:_is_token_expired?))
104104
end
105105

106106
def test_get_token
@@ -111,7 +111,7 @@ def test_get_token
111111
)
112112
token_manager.user_access_token = "user_access_token"
113113

114-
token = token_manager._token
114+
token = token_manager.token
115115
assert_equal(token_manager.user_access_token, token)
116116

117117
response = {
@@ -132,11 +132,11 @@ def test_get_token
132132
}
133133
).to_return(status: 200, body: response.to_json, headers: {})
134134
token_manager.user_access_token = ""
135-
token = token_manager._token
135+
token = token_manager.token
136136
assert_equal("hellohello", token)
137137

138138
token_manager.token_info["expiration"] = Time.now.to_i - (20 * 24 * 3600)
139-
token = token_manager._token
139+
token = token_manager.token
140140
assert_equal("hellohello", token)
141141

142142
stub_request(:post, "https://iam.bluemix.net/identity/token")
@@ -149,7 +149,7 @@ def test_get_token
149149
}
150150
).to_return(status: 200, body: response.to_json, headers: {})
151151
token_manager.token_info["expiration"] = Time.now.to_i - 4000
152-
token = token_manager._token
152+
token = token_manager.token
153153
assert_equal("hellohello", token)
154154

155155
token_manager.token_info = {
@@ -159,7 +159,7 @@ def test_get_token
159159
"expiration" => Time.now.to_i + 3600,
160160
"refresh_token" => "jy4gl91BQ"
161161
}
162-
token = token_manager._token
162+
token = token_manager.token
163163
assert_equal("dummy", token)
164164
end
165165

0 commit comments

Comments
 (0)