Skip to content

Commit 6633c56

Browse files
committed
Updating Network Auth Client tests
1 parent 53fb8b3 commit 6633c56

File tree

5 files changed

+78
-40
lines changed

5 files changed

+78
-40
lines changed

lib/vonage/network_authentication.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ class NetworkAuthentication < AbstractAuthentication
66
def update(object, data)
77
return unless object.is_a?(Net::HTTPRequest)
88

9-
token = self.public_send(data[:auth_flow]).token(data).access_token
9+
token = self.public_send(data[:auth_flow]).token(data)
1010

1111
object['Authorization'] = 'Bearer ' + token
1212
end

lib/vonage/network_authentication/client.rb

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,25 @@ module Vonage
55
class NetworkAuthentication::Client < Namespace
66
extend T::Sig
77

8-
# include NetworkAuthentication::TokenRequest
9-
108
self.authentication = BearerToken
119

1210
self.host = :vonage_host
1311

1412
self.request_headers['Content-Type'] = 'application/x-www-form-urlencoded'
1513

16-
def token(**params)
14+
def token(oidc_auth_code:, redirect_uri:, **params)
1715
request(
1816
'/oauth2/token',
1917
params: {
2018
grant_type: 'authorization_code',
21-
code: params[:oidc_auth_code],
22-
redirect_uri: params[:redirect_uri]
19+
code: oidc_auth_code,
20+
redirect_uri: redirect_uri
2321
},
2422
type: Post
2523
)
2624
end
2725

28-
def self.generate_oidc_uri(purpose:, api_scope:, login_hint:, redirect_uri:, state: nil)
26+
def generate_oidc_uri(purpose:, api_scope:, login_hint:, redirect_uri:, state: nil)
2927
scope = "openid+dpv:#{purpose}##{api_scope}"
3028
uri = "https://oidc.idp.vonage.com/oauth2/auth?" +
3129
"client_id=#{@config.application_id}" +

lib/vonage/network_authentication/server.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ module Vonage
55
class NetworkAuthentication::Server < Namespace
66
extend T::Sig
77

8-
include TokenRequest
9-
108
self.authentication = BearerToken
119

1210
self.host = :vonage_host

lib/vonage/network_authentication/token_request.rb

Lines changed: 0 additions & 19 deletions
This file was deleted.

test/vonage/network_authentication/client_test.rb

Lines changed: 73 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,86 @@ def token_uri
1010
"https://api-eu.vonage.com/oauth2/token"
1111
end
1212

13-
def test_token_method
14-
grant_type = 'authorization_code'
15-
code = '0dadaeb4-7c79-4d39-b4b0-5a6cc08bf537'
16-
redirect_uri = 'https://example.com/callback'
13+
def example_oidc_auth_code
14+
'0dadaeb4-7c79-4d39-b4b0-5a6cc08bf537'
15+
end
16+
17+
def example_redirect_uri
18+
'https://example.com/callback'
19+
end
20+
21+
def example_purpose
22+
'FraudPreventionAndDetection'
23+
end
24+
25+
def example_api_scope
26+
'number-verification-verify-read'
27+
end
1728

29+
def example_login_hint
30+
'+447700900000'
31+
end
32+
33+
def example_redirect_uri
34+
'https://example.com/callback'
35+
end
36+
37+
def test_token_method
1838
request_params = {
19-
grant_type: grant_type,
20-
code: code,
21-
redirect_uri: redirect_uri
39+
grant_type: 'authorization_code',
40+
code: example_oidc_auth_code,
41+
redirect_uri: example_redirect_uri
2242
}
2343

2444
stub_request(:post, token_uri).with(request(body: request_params, headers: headers)).to_return(response)
2545

26-
response = client.token(
27-
grant_type: grant_type,
28-
oidc_auth_code: code,
29-
redirect_uri: redirect_uri
46+
assert_kind_of Vonage::Response, client.token(oidc_auth_code: example_oidc_auth_code, redirect_uri: example_redirect_uri)
47+
end
48+
49+
def test_token_method_without_oidc_auth_code
50+
assert_raises(ArgumentError) { client.token(redirect_uri: example_redirect_uri) }
51+
end
52+
53+
def test_token_method_without_redirect_uri
54+
assert_raises(ArgumentError) { client.token(oidc_auth_code: example_oidc_auth_code) }
55+
end
56+
57+
def test_generate_oidc_uri_method
58+
uri = client.generate_oidc_uri(
59+
purpose: example_purpose,
60+
api_scope: example_api_scope,
61+
login_hint: example_login_hint,
62+
redirect_uri: example_redirect_uri
63+
)
64+
65+
assert_equal "https://oidc.idp.vonage.com/oauth2/auth?client_id=#{application_id}&response_type=code&scope=openid+dpv:#{example_purpose}##{example_api_scope}&login_hint=#{example_login_hint}&redirect_uri=#{example_redirect_uri}", uri
66+
end
67+
68+
def test_generate_oidc_uri_method_with_optional_params
69+
uri = client.generate_oidc_uri(
70+
purpose: example_purpose,
71+
api_scope: example_api_scope,
72+
login_hint: example_login_hint,
73+
redirect_uri: example_redirect_uri,
74+
state: '12345'
3075
)
3176

32-
assert_kind_of Vonage::Response, response
77+
assert_equal "https://oidc.idp.vonage.com/oauth2/auth?client_id=#{application_id}&response_type=code&scope=openid+dpv:#{example_purpose}##{example_api_scope}&login_hint=#{example_login_hint}&redirect_uri=#{example_redirect_uri}&state=12345", uri
78+
end
79+
80+
def test_generate_oidc_uri_method_without_purpose
81+
assert_raises(ArgumentError) { client.generate_oidc_uri(api_scope: example_api_scope, login_hint: example_login_hint, redirect_uri: example_redirect_uri) }
82+
end
83+
84+
def test_generate_oidc_uri_method_without_api_scope
85+
assert_raises(ArgumentError) { client.generate_oidc_uri(purpose: example_purpose, login_hint: example_login_hint, redirect_uri: example_redirect_uri) }
86+
end
87+
88+
def test_generate_oidc_uri_method_without_login_hint
89+
assert_raises(ArgumentError) { client.generate_oidc_uri(purpose: example_purpose, api_scope: example_api_scope, redirect_uri: example_redirect_uri) }
90+
end
91+
92+
def test_generate_oidc_uri_method_without_redirect_uri
93+
assert_raises(ArgumentError) { client.generate_oidc_uri(purpose: example_purpose, api_scope: example_api_scope, login_hint: example_login_hint) }
3394
end
3495
end

0 commit comments

Comments
 (0)