19
19
import org .cloudfoundry .AbstractIntegrationTest ;
20
20
import org .cloudfoundry .reactor .ConnectionContext ;
21
21
import org .cloudfoundry .reactor .TokenProvider ;
22
+ import org .cloudfoundry .uaa .authorizations .AuthorizeByAuthorizationCodeGrantApiRequest ;
22
23
import org .cloudfoundry .uaa .tokens .CheckTokenRequest ;
23
24
import org .cloudfoundry .uaa .tokens .GetTokenByAuthorizationCodeRequest ;
25
+ import org .cloudfoundry .uaa .tokens .GetTokenByAuthorizationCodeResponse ;
24
26
import org .cloudfoundry .uaa .tokens .GetTokenByClientCredentialsRequest ;
25
27
import org .cloudfoundry .uaa .tokens .GetTokenByClientCredentialsResponse ;
26
28
import org .cloudfoundry .uaa .tokens .GetTokenByOneTimePasscodeRequest ;
33
35
import org .cloudfoundry .uaa .tokens .GetTokenKeyResponse ;
34
36
import org .cloudfoundry .uaa .tokens .ListTokenKeysRequest ;
35
37
import org .cloudfoundry .uaa .tokens .RefreshTokenRequest ;
38
+ import org .cloudfoundry .uaa .tokens .RefreshTokenResponse ;
36
39
import org .cloudfoundry .uaa .tokens .TokenFormat ;
37
40
import org .cloudfoundry .uaa .tokens .TokenKey ;
38
41
import org .junit .Ignore ;
43
46
import reactor .test .StepVerifier ;
44
47
45
48
import java .time .Duration ;
46
- import java .util .concurrent .TimeoutException ;
47
49
48
50
import static org .assertj .core .api .Assertions .assertThat ;
49
51
@@ -58,12 +60,18 @@ public final class TokensTest extends AbstractIntegrationTest {
58
60
@ Autowired
59
61
private ConnectionContext connectionContext ;
60
62
63
+ @ Autowired
64
+ private String password ;
65
+
61
66
@ Autowired
62
67
private TokenProvider tokenProvider ;
63
68
64
69
@ Autowired
65
70
private UaaClient uaaClient ;
66
71
72
+ @ Autowired
73
+ private String username ;
74
+
67
75
@ Test
68
76
public void checkTokenNotAuthorized () {
69
77
this .tokenProvider .getToken (this .connectionContext )
@@ -72,26 +80,25 @@ public void checkTokenNotAuthorized() {
72
80
.token (token )
73
81
.clientId (this .clientId )
74
82
.clientSecret (this .clientSecret )
75
- .scope ("password.write" )
76
- .scope ("scim.userids" )
83
+ .scope ("password.write" , "scim.userids" )
77
84
.build ()))
78
85
.as (StepVerifier ::create )
79
86
.consumeErrorWith (t -> assertThat (t ).isInstanceOf (UaaException .class ).hasMessage ("access_denied: Access is denied" ))
80
87
.verify (Duration .ofMinutes (5 ));
81
88
}
82
89
83
- //TODO: Ready to Implement - use test authorizationCode
84
- @ Ignore ("Ready to Implement - use test authorizationCode" )
85
90
@ Test
86
91
public void getTokenByAuthorizationCode () {
87
- this .uaaClient .tokens ()
88
- .getByAuthorizationCode (GetTokenByAuthorizationCodeRequest .builder ()
89
- .authorizationCode ("some auth code" )
90
- .clientId (this .clientId )
91
- .clientSecret (this .clientSecret )
92
- .build ())
92
+ requestGetAuthorizationCode (this .uaaClient , this .clientId )
93
+ .then (authorizationCode -> this .uaaClient .tokens ()
94
+ .getByAuthorizationCode (GetTokenByAuthorizationCodeRequest .builder ()
95
+ .authorizationCode (authorizationCode )
96
+ .clientId (this .clientId )
97
+ .clientSecret (this .clientSecret )
98
+ .build ()))
99
+ .map (GetTokenByAuthorizationCodeResponse ::getTokenType )
93
100
.as (StepVerifier ::create )
94
- .expectNextCount ( 1 )
101
+ .expectNext ( "bearer" )
95
102
.expectComplete ()
96
103
.verify (Duration .ofMinutes (5 ));
97
104
}
@@ -111,52 +118,50 @@ public void getTokenByClientCredentials() {
111
118
.verify (Duration .ofMinutes (5 ));
112
119
}
113
120
114
- //TODO: Ready to Implement - use test one-time passcode
115
- @ Ignore ("Ready to Implement - use test one-time passcode" )
121
+ //TODO: Ready to Implement - Await https://github.com/cloudfoundry/cf-java-client/issues/862 to get passcode
122
+ @ Ignore ("Ready to Implement - Await https://github.com/cloudfoundry/cf-java-client/issues/862 to get passcode" )
116
123
@ Test
117
124
public void getTokenByOneTimePasscode () {
118
125
this .uaaClient .tokens ()
119
126
.getByOneTimePasscode (GetTokenByOneTimePasscodeRequest .builder ()
120
- .passcode ("Some passcode" )
127
+ .passcode ("some passcode" )
121
128
.clientId (this .clientId )
122
129
.clientSecret (this .clientSecret )
123
130
.tokenFormat (TokenFormat .OPAQUE )
124
131
.build ())
125
- .map (GetTokenByOneTimePasscodeResponse ::getTokenType )
132
+ .map (GetTokenByOneTimePasscodeResponse ::getAccessToken )
126
133
.as (StepVerifier ::create )
127
134
.expectNext ("bearer" )
128
- .expectComplete ();
135
+ .expectComplete ()
136
+ .verify (Duration .ofMinutes (5 ));
129
137
}
130
138
131
- //TODO: Ready to Implement - use test openid authorizationCode
132
- @ Ignore ("Ready to Implement - use test openid authorizationCode" )
133
139
@ Test
134
140
public void getTokenByOpenId () {
135
- this .uaaClient .tokens ()
136
- .getByOpenId (GetTokenByOpenIdRequest .builder ()
137
- .authorizationCode ("Some authorization code" )
138
- .clientId (this .clientId )
139
- .clientSecret (this .clientSecret )
140
- .tokenFormat (TokenFormat .OPAQUE )
141
- .build ())
141
+ requestGetAuthorizationCode (this .uaaClient , this .clientId )
142
+ .then (authorizationCode -> this .uaaClient .tokens ()
143
+ .getByOpenId (GetTokenByOpenIdRequest .builder ()
144
+ .authorizationCode (authorizationCode )
145
+ .clientId (this .clientId )
146
+ .clientSecret (this .clientSecret )
147
+ .tokenFormat (TokenFormat .OPAQUE )
148
+ .build ()))
142
149
.map (GetTokenByOpenIdResponse ::getTokenType )
143
150
.as (StepVerifier ::create )
144
151
.expectNext ("bearer" )
145
152
.expectComplete ()
146
153
.verify (Duration .ofMinutes (5 ));
147
154
}
148
155
149
- //TODO: Ready to Implement - use test username and password
150
- @ Ignore ("Ready to Implement - use test username and password" )
151
156
@ Test
152
157
public void getTokenByPassword () {
153
158
this .uaaClient .tokens ()
154
159
.getByPassword (GetTokenByPasswordRequest .builder ()
155
- .password ("a-password" )
156
- .username ("a-username" )
157
160
.clientId (this .clientId )
158
161
.clientSecret (this .clientSecret )
162
+ .password (this .password )
159
163
.tokenFormat (TokenFormat .OPAQUE )
164
+ .username (this .username )
160
165
.build ())
161
166
.map (GetTokenByPasswordResponse ::getTokenType )
162
167
.as (StepVerifier ::create )
@@ -199,21 +204,40 @@ public void listTokenKeys() {
199
204
.verify (Duration .ofMinutes (5 ));
200
205
}
201
206
202
- //TODO: Ready to Implement - use test refresh token
203
- @ Ignore ("Ready to Implement - use test refresh token" )
204
207
@ Test
205
208
public void refreshToken () {
206
- this .uaaClient .tokens ()
207
- .refresh (RefreshTokenRequest .builder ()
208
- .tokenFormat (TokenFormat .OPAQUE )
209
- .clientId (this .clientId )
210
- .clientSecret (this .clientSecret )
211
- .refreshToken ("a-refresh-token" )
212
- .build ())
209
+ getRequestToken (this .uaaClient , this .clientId , this .clientSecret , this .password , this .username )
210
+ .then (refreshToken -> this .uaaClient .tokens ()
211
+ .refresh (RefreshTokenRequest .builder ()
212
+ .tokenFormat (TokenFormat .OPAQUE )
213
+ .clientId (this .clientId )
214
+ .clientSecret (this .clientSecret )
215
+ .refreshToken (refreshToken )
216
+ .build ()))
217
+ .map (RefreshTokenResponse ::getTokenType )
213
218
.as (StepVerifier ::create )
214
- .expectNextCount ( 1 )
219
+ .expectNext ( "bearer" )
215
220
.expectComplete ()
216
221
.verify (Duration .ofMinutes (5 ));
217
222
}
218
223
224
+ private static Mono <String > getRequestToken (UaaClient uaaClient , String clientId , String clientSecret , String password , String username ) {
225
+ return uaaClient .tokens ()
226
+ .getByPassword (GetTokenByPasswordRequest .builder ()
227
+ .clientId (clientId )
228
+ .clientSecret (clientSecret )
229
+ .password (password )
230
+ .tokenFormat (TokenFormat .OPAQUE )
231
+ .username (username )
232
+ .build ())
233
+ .map (GetTokenByPasswordResponse ::getRefreshToken );
234
+ }
235
+
236
+ private static Mono <String > requestGetAuthorizationCode (UaaClient uaaClient , String clientId ) {
237
+ return uaaClient .authorizations ()
238
+ .authorizationCodeGrantApi (AuthorizeByAuthorizationCodeGrantApiRequest .builder ()
239
+ .clientId (clientId )
240
+ .build ());
241
+ }
242
+
219
243
}
0 commit comments