1
+ /*
2
+ * OpenFGA
3
+ * A high performance and flexible authorization/permission engine built for developers and inspired by Google Zanzibar.
4
+ *
5
+ * The version of the OpenAPI document: 0.1
6
+
7
+ *
8
+ * NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
9
+ * https://openapi-generator.tech
10
+ * Do not edit the class manually.
11
+ */
12
+
1
13
package dev .openfga .sdk .api .auth ;
2
14
3
- import static org .hamcrest .Matchers .is ;
15
+ import static org .hamcrest .Matchers .allOf ;
16
+ import static org .hamcrest .core .StringContains .containsString ;
4
17
import static org .junit .jupiter .api .Assertions .*;
5
18
import static org .mockito .Mockito .mock ;
6
19
import static org .mockito .Mockito .when ;
10
23
import dev .openfga .sdk .api .client .ApiClient ;
11
24
import dev .openfga .sdk .api .configuration .*;
12
25
import dev .openfga .sdk .errors .FgaInvalidParameterException ;
26
+ import java .net .URLEncoder ;
27
+ import java .nio .charset .StandardCharsets ;
13
28
import java .util .stream .Stream ;
14
29
import org .junit .jupiter .api .Test ;
15
30
import org .junit .jupiter .params .ParameterizedTest ;
@@ -41,16 +56,25 @@ private static Stream<Arguments> apiTokenIssuers() {
41
56
"https://issuer.fga.example:8080/some_endpoint" ));
42
57
}
43
58
59
+ private String urlEncode (String value ) {
60
+ return URLEncoder .encode (value , StandardCharsets .UTF_8 );
61
+ }
62
+
44
63
@ ParameterizedTest
45
64
@ MethodSource ("apiTokenIssuers" )
46
65
public void exchangeAuth0Token (String apiTokenIssuer , String tokenEndpointUrl ) throws Exception {
47
66
// Given
48
67
OAuth2Client auth0 = newAuth0Client (apiTokenIssuer );
49
- String expectedPostBody = String .format (
50
- "{\" client_id\" :\" %s\" ,\" client_secret\" :\" %s\" ,\" audience\" :\" %s\" ,\" grant_type\" :\" %s\" }" ,
51
- CLIENT_ID , CLIENT_SECRET , AUDIENCE , GRANT_TYPE );
68
+
52
69
String responseBody = String .format ("{\" access_token\" :\" %s\" }" , ACCESS_TOKEN );
53
- mockHttpClient .onPost (tokenEndpointUrl ).withBody (is (expectedPostBody )).doReturn (200 , responseBody );
70
+ mockHttpClient
71
+ .onPost (tokenEndpointUrl )
72
+ .withBody (allOf (
73
+ containsString (String .format ("client_id=%s" , CLIENT_ID )),
74
+ containsString (String .format ("client_secret=%s" , CLIENT_SECRET )),
75
+ containsString (String .format ("audience=%s" , AUDIENCE )),
76
+ containsString (String .format ("grant_type=%s" , GRANT_TYPE ))))
77
+ .doReturn (200 , responseBody );
54
78
55
79
// When
56
80
String result = auth0 .getAccessToken ().get ();
@@ -59,7 +83,12 @@ public void exchangeAuth0Token(String apiTokenIssuer, String tokenEndpointUrl) t
59
83
mockHttpClient
60
84
.verify ()
61
85
.post (tokenEndpointUrl )
62
- .withBody (is (expectedPostBody ))
86
+ .withBody (allOf (
87
+ containsString (String .format ("client_id=%s" , CLIENT_ID )),
88
+ containsString (String .format ("client_secret=%s" , CLIENT_SECRET )),
89
+ containsString (String .format ("audience=%s" , AUDIENCE )),
90
+ containsString (String .format ("grant_type=%s" , GRANT_TYPE ))))
91
+ .withHeader ("Content-Type" , "application/x-www-form-urlencoded" )
63
92
.called ();
64
93
assertEquals (ACCESS_TOKEN , result );
65
94
}
@@ -68,21 +97,31 @@ public void exchangeAuth0Token(String apiTokenIssuer, String tokenEndpointUrl) t
68
97
@ MethodSource ("apiTokenIssuers" )
69
98
public void exchangeOAuth2Token (String apiTokenIssuer , String tokenEndpointUrl ) throws Exception {
70
99
// Given
71
- OAuth2Client oAuth2 = newOAuth2Client (apiTokenIssuer );
72
- String expectedPostBody = String .format (
73
- "{\" client_id\" :\" %s\" ,\" client_secret\" :\" %s\" ,\" scope\" :\" %s\" ,\" grant_type\" :\" %s\" }" ,
74
- CLIENT_ID , CLIENT_SECRET , SCOPES , GRANT_TYPE );
100
+ OAuth2Client auth0 = newOAuth2Client (apiTokenIssuer );
101
+
75
102
String responseBody = String .format ("{\" access_token\" :\" %s\" }" , ACCESS_TOKEN );
76
- mockHttpClient .onPost (tokenEndpointUrl ).withBody (is (expectedPostBody )).doReturn (200 , responseBody );
103
+ mockHttpClient
104
+ .onPost (tokenEndpointUrl )
105
+ .withBody (allOf (
106
+ containsString (String .format ("client_id=%s" , CLIENT_ID )),
107
+ containsString (String .format ("client_secret=%s" , CLIENT_SECRET )),
108
+ containsString (String .format ("scope=%s" , urlEncode (SCOPES ))),
109
+ containsString (String .format ("grant_type=%s" , GRANT_TYPE ))))
110
+ .doReturn (200 , responseBody );
77
111
78
112
// When
79
- String result = oAuth2 .getAccessToken ().get ();
113
+ String result = auth0 .getAccessToken ().get ();
80
114
81
115
// Then
82
116
mockHttpClient
83
117
.verify ()
84
118
.post (tokenEndpointUrl )
85
- .withBody (is (expectedPostBody ))
119
+ .withBody (allOf (
120
+ containsString (String .format ("client_id=%s" , CLIENT_ID )),
121
+ containsString (String .format ("client_secret=%s" , CLIENT_SECRET )),
122
+ containsString (String .format ("scope=%s" , urlEncode (SCOPES ))),
123
+ containsString (String .format ("grant_type=%s" , GRANT_TYPE ))))
124
+ .withHeader ("Content-Type" , "application/x-www-form-urlencoded" )
86
125
.called ();
87
126
assertEquals (ACCESS_TOKEN , result );
88
127
}
0 commit comments