11
11
12
12
import io .restassured .RestAssured ;
13
13
import io .restassured .config .SSLConfig ;
14
+ import io .restassured .response .Validatable ;
15
+ import io .restassured .specification .RequestSpecification ;
14
16
import org .apache .commons .lang3 .StringUtils ;
15
- import org .junit .jupiter .api .*;
17
+ import org .junit .jupiter .api .BeforeAll ;
18
+ import org .junit .jupiter .api .BeforeEach ;
19
+ import org .junit .jupiter .api .Nested ;
20
+ import org .junit .jupiter .api .Test ;
16
21
import org .junit .jupiter .params .ParameterizedTest ;
17
22
import org .junit .jupiter .params .provider .Arguments ;
18
23
import org .junit .jupiter .params .provider .MethodSource ;
19
24
import org .springframework .http .HttpHeaders ;
20
25
import org .springframework .http .HttpStatus ;
21
26
import org .zowe .apiml .util .categories .GeneralAuthenticationTest ;
22
- import org .zowe .apiml .util .config .*;
27
+ import org .zowe .apiml .util .config .ConfigReader ;
28
+ import org .zowe .apiml .util .config .ItSslConfigFactory ;
29
+ import org .zowe .apiml .util .config .SslContext ;
23
30
import org .zowe .apiml .util .service .DiscoveryUtils ;
24
31
25
32
import java .util .List ;
@@ -37,11 +44,12 @@ class ApiCatalogAuthenticationTest {
37
44
private final static String PASSWORD = ConfigReader .environmentConfiguration ().getCredentials ().getPassword ();
38
45
private final static String USERNAME = ConfigReader .environmentConfiguration ().getCredentials ().getUser ();
39
46
40
- private static final String CATALOG_PREFIX = "/api/v1" ;
41
47
private static final String CATALOG_SERVICE_ID = "apicatalog" ;
42
48
private static final String CATALOG_SERVICE_ID_PATH = "/" + CATALOG_SERVICE_ID ;
49
+ private static final String CATALOG_PREFIX = "/api/v1" ;
43
50
44
51
private static final String CATALOG_APIDOC_ENDPOINT = "/apidoc/discoverableclient/v1" ;
52
+ private static final String CATALOG_STATIC_REFRESH_ENDPOINT = "/static-api/refresh" ;
45
53
private static final String CATALOG_ACTUATOR_ENDPOINT = "/application" ;
46
54
47
55
private final static String COOKIE = "apimlAuthenticationToken" ;
@@ -51,10 +59,23 @@ class ApiCatalogAuthenticationTest {
51
59
52
60
private static String apiCatalogServiceUrl = ConfigReader .environmentConfiguration ().getApiCatalogServiceConfiguration ().getUrl ();
53
61
54
- static Stream <Arguments > urlsToTest () {
62
+ @ FunctionalInterface
63
+ private interface Request {
64
+ Validatable <?, ?> execute (RequestSpecification when , String endpoint );
65
+ }
66
+
67
+ static Stream <Arguments > requestsToTest () {
55
68
return Stream .of (
56
- Arguments .of (CATALOG_APIDOC_ENDPOINT ),
57
- Arguments .of (CATALOG_ACTUATOR_ENDPOINT )
69
+ Arguments .of (CATALOG_APIDOC_ENDPOINT , (Request ) (when , endpoint ) -> when .get (getUriFromGateway (CATALOG_SERVICE_ID_PATH + CATALOG_PREFIX + endpoint ))),
70
+ Arguments .of (CATALOG_STATIC_REFRESH_ENDPOINT , (Request ) (when , endpoint ) -> when .post (getUriFromGateway (CATALOG_SERVICE_ID_PATH + CATALOG_PREFIX + endpoint ))),
71
+ Arguments .of (CATALOG_ACTUATOR_ENDPOINT , (Request ) (when , endpoint ) -> when .get (getUriFromGateway (CATALOG_SERVICE_ID_PATH + CATALOG_PREFIX + endpoint )))
72
+ );
73
+ }
74
+
75
+ static Stream <Arguments > requestsToTestWithCertificate () {
76
+ return Stream .of (
77
+ Arguments .of (CATALOG_SERVICE_ID_PATH + CATALOG_APIDOC_ENDPOINT , (Request ) (when , endpoint ) -> when .get (apiCatalogServiceUrl + endpoint )),
78
+ Arguments .of (CATALOG_SERVICE_ID_PATH + CATALOG_STATIC_REFRESH_ENDPOINT , (Request ) (when , endpoint ) -> when .post (apiCatalogServiceUrl + endpoint ))
58
79
);
59
80
}
60
81
@@ -82,24 +103,28 @@ class WhenAccessingCatalog {
82
103
@ Nested
83
104
class ReturnOk {
84
105
@ ParameterizedTest (name = "givenValidBasicAuthentication {index} {0} " )
85
- @ MethodSource ("org.zowe.apiml.functional.apicatalog.ApiCatalogAuthenticationTest#urlsToTest" )
86
- void givenValidBasicAuthentication (String endpoint ) {
87
- given ()
88
- .auth ().preemptive ().basic (USERNAME , PASSWORD ) // Isn't this kind of strange behavior?
89
- .when ()
90
- .get (getUriFromGateway (CATALOG_PREFIX + CATALOG_SERVICE_ID_PATH + endpoint ))
106
+ @ MethodSource ("org.zowe.apiml.functional.apicatalog.ApiCatalogAuthenticationTest#requestsToTest" )
107
+ void givenValidBasicAuthentication (String endpoint , Request request ) {
108
+ request .execute (
109
+ given ()
110
+ .auth ().preemptive ().basic (USERNAME , PASSWORD ) // Isn't this kind of strange behavior?
111
+ .when (),
112
+ endpoint
113
+ )
91
114
.then ()
92
115
.statusCode (is (SC_OK ));
93
116
}
94
117
95
- @ ParameterizedTest (name = "givenValidBasicAuthentication {index} {0} " )
96
- @ MethodSource ("org.zowe.apiml.functional.apicatalog.ApiCatalogAuthenticationTest#urlsToTest" )
97
- void givenValidBasicAuthenticationAndCertificate (String endpoint ) {
98
- given ()
99
- .config (SslContext .clientCertApiml )
100
- .auth ().preemptive ().basic (USERNAME , PASSWORD ) // Isn't this kind of strange behavior?
101
- .when ()
102
- .get (getUriFromGateway (CATALOG_PREFIX + CATALOG_SERVICE_ID_PATH + endpoint ))
118
+ @ ParameterizedTest (name = "givenValidBasicAuthenticationAndCertificate {index} {0} " )
119
+ @ MethodSource ("org.zowe.apiml.functional.apicatalog.ApiCatalogAuthenticationTest#requestsToTest" )
120
+ void givenValidBasicAuthenticationAndCertificate (String endpoint , Request request ) {
121
+ request .execute (
122
+ given ()
123
+ .config (SslContext .clientCertApiml )
124
+ .auth ().preemptive ().basic (USERNAME , PASSWORD ) // Isn't this kind of strange behavior?
125
+ .when (),
126
+ endpoint
127
+ )
103
128
.then ()
104
129
.statusCode (is (SC_OK ));
105
130
}
@@ -108,14 +133,16 @@ void givenValidBasicAuthenticationAndCertificate(String endpoint) {
108
133
@ Nested
109
134
class ReturnUnauthorized {
110
135
@ ParameterizedTest (name = "givenNoAuthentication {index} {0}" )
111
- @ MethodSource ("org.zowe.apiml.functional.apicatalog.ApiCatalogAuthenticationTest#urlsToTest " )
112
- void givenNoAuthentication (String endpoint ) {
136
+ @ MethodSource ("org.zowe.apiml.functional.apicatalog.ApiCatalogAuthenticationTest#requestsToTest " )
137
+ void givenNoAuthentication (String endpoint , Request request ) {
113
138
String expectedMessage = "Authentication is required for URL '" + CATALOG_SERVICE_ID_PATH + endpoint + "'" ;
114
139
115
- given ()
116
- .config (SslContext .tlsWithoutCert )
117
- .when ()
118
- .get (getUriFromGateway (CATALOG_PREFIX + CATALOG_SERVICE_ID_PATH + endpoint ))
140
+ request .execute (
141
+ given ()
142
+ .config (SslContext .tlsWithoutCert )
143
+ .when (),
144
+ endpoint
145
+ )
119
146
.then ()
120
147
.statusCode (is (SC_UNAUTHORIZED ))
121
148
.header (HttpHeaders .WWW_AUTHENTICATE , BASIC_AUTHENTICATION_PREFIX )
@@ -125,30 +152,34 @@ void givenNoAuthentication(String endpoint) {
125
152
}
126
153
127
154
@ ParameterizedTest (name = "givenInvalidBasicAuthentication {index} {0}" )
128
- @ MethodSource ("org.zowe.apiml.functional.apicatalog.ApiCatalogAuthenticationTest#urlsToTest " )
129
- void givenInvalidBasicAuthentication (String endpoint ) {
155
+ @ MethodSource ("org.zowe.apiml.functional.apicatalog.ApiCatalogAuthenticationTest#requestsToTest " )
156
+ void givenInvalidBasicAuthentication (String endpoint , Request request ) {
130
157
String expectedMessage = "Invalid username or password for URL '" + CATALOG_SERVICE_ID_PATH + endpoint + "'" ;
131
158
132
- given ()
133
- .auth ().preemptive ().basic (INVALID_USERNAME , INVALID_PASSWORD )
134
- .when ()
135
- .get (getUriFromGateway (CATALOG_PREFIX + CATALOG_SERVICE_ID_PATH + endpoint ))
159
+ request .execute (
160
+ given ()
161
+ .auth ().preemptive ().basic (INVALID_USERNAME , INVALID_PASSWORD )
162
+ .when (),
163
+ endpoint
164
+ )
136
165
.then ()
137
166
.body (
138
167
"messages.find { it.messageNumber == 'ZWEAS120E' }.messageContent" , equalTo (expectedMessage )
139
168
).statusCode (is (SC_UNAUTHORIZED ));
140
169
}
141
170
142
171
@ ParameterizedTest (name = "givenInvalidTokenInCookie {index} {0}" )
143
- @ MethodSource ("org.zowe.apiml.functional.apicatalog.ApiCatalogAuthenticationTest#urlsToTest " )
144
- void givenInvalidTokenInCookie (String endpoint ) {
172
+ @ MethodSource ("org.zowe.apiml.functional.apicatalog.ApiCatalogAuthenticationTest#requestsToTest " )
173
+ void givenInvalidTokenInCookie (String endpoint , Request request ) {
145
174
String expectedMessage = "Token is not valid for URL '" + CATALOG_SERVICE_ID_PATH + endpoint + "'" ;
146
175
String invalidToken = "nonsense" ;
147
176
148
- given ()
149
- .cookie (COOKIE , invalidToken )
150
- .when ()
151
- .get (getUriFromGateway (CATALOG_PREFIX + CATALOG_SERVICE_ID_PATH + endpoint ))
177
+ request .execute (
178
+ given ()
179
+ .cookie (COOKIE , invalidToken )
180
+ .when (),
181
+ endpoint
182
+ )
152
183
.then ()
153
184
.statusCode (is (SC_UNAUTHORIZED ))
154
185
.body (
@@ -166,53 +197,65 @@ class WhenAccessApiDocRoute {
166
197
167
198
@ Nested
168
199
class ThenReturnOk {
169
- @ Test
170
- void givenValidCertificate () {
171
- given ()
172
- .config (SslContext .clientCertUser )
173
- .when ()
174
- .get (apiCatalogServiceUrl + CATALOG_SERVICE_ID_PATH + CATALOG_APIDOC_ENDPOINT )
200
+ @ ParameterizedTest (name = "givenValidCertificate {index} {0} " )
201
+ @ MethodSource ("org.zowe.apiml.functional.apicatalog.ApiCatalogAuthenticationTest#requestsToTestWithCertificate" )
202
+ void givenValidCertificate (String endpoint , Request request ) {
203
+ request .execute (
204
+ given ()
205
+ .config (SslContext .clientCertUser )
206
+ .when (),
207
+ endpoint
208
+ )
175
209
.then ()
176
210
.statusCode (HttpStatus .OK .value ());
177
211
}
178
212
179
- @ Test
180
- void givenValidCertificateAndBasicAuth () {
181
- given ()
182
- .config (SslContext .clientCertApiml )
183
- .auth ().preemptive ().basic (USERNAME , PASSWORD ) // Isn't this kind of strange behavior?
184
- .when ()
185
- .get (apiCatalogServiceUrl + CATALOG_SERVICE_ID_PATH + CATALOG_APIDOC_ENDPOINT )
213
+ @ ParameterizedTest (name = "givenValidCertificateAndBasicAuth {index} {0} " )
214
+ @ MethodSource ("org.zowe.apiml.functional.apicatalog.ApiCatalogAuthenticationTest#requestsToTestWithCertificate" )
215
+ void givenValidCertificateAndBasicAuth (String endpoint , Request request ) {
216
+ request .execute (
217
+ given ()
218
+ .config (SslContext .clientCertApiml )
219
+ .auth ().preemptive ().basic (USERNAME , PASSWORD ) // Isn't this kind of strange behavior?
220
+ .when (),
221
+ endpoint
222
+ )
186
223
.then ()
187
224
.statusCode (is (SC_OK ));
188
225
}
189
226
}
190
227
191
228
@ Nested
192
229
class ThenReturnUnauthorized {
193
- @ Test
194
- void givenUnTrustedCertificateAndNoBasicAuth_thenReturnUnauthorized () {
195
- given ()
196
- .config (SslContext .selfSignedUntrusted )
197
- .when ()
198
- .get (apiCatalogServiceUrl + CATALOG_SERVICE_ID_PATH + CATALOG_APIDOC_ENDPOINT )
230
+ @ ParameterizedTest (name = "givenUnTrustedCertificateAndNoBasicAuth_thenReturnUnauthorized {index} {0} " )
231
+ @ MethodSource ("org.zowe.apiml.functional.apicatalog.ApiCatalogAuthenticationTest#requestsToTestWithCertificate" )
232
+ void givenUnTrustedCertificateAndNoBasicAuth_thenReturnUnauthorized (String endpoint , Request request ) {
233
+ request .execute (
234
+ given ()
235
+ .config (SslContext .selfSignedUntrusted )
236
+ .when (),
237
+ endpoint
238
+ )
199
239
.then ()
200
240
.statusCode (HttpStatus .UNAUTHORIZED .value ());
201
241
}
202
242
203
- @ Test
204
- void givenNoCertificateAndNoBasicAuth_thenReturnUnauthorized () {
205
- given ()
206
- .when ()
207
- .get (apiCatalogServiceUrl + CATALOG_SERVICE_ID_PATH + CATALOG_APIDOC_ENDPOINT )
243
+ @ ParameterizedTest (name = "givenNoCertificateAndNoBasicAuth_thenReturnUnauthorized {index} {0} " )
244
+ @ MethodSource ("org.zowe.apiml.functional.apicatalog.ApiCatalogAuthenticationTest#requestsToTestWithCertificate" )
245
+ void givenNoCertificateAndNoBasicAuth_thenReturnUnauthorized (String endpoint , Request request ) {
246
+ request .execute (
247
+ given ()
248
+ .when (),
249
+ endpoint
250
+ )
208
251
.then ()
209
252
.statusCode (HttpStatus .UNAUTHORIZED .value ());
210
253
}
211
254
}
212
255
}
213
256
214
257
@ Test
215
- void givenOnlyValidCertificate_whenAccessNotApiDocRoute_thenReturnUnauthorized () {
258
+ void givenOnlyValidCertificate_whenAccessNotCertificateAuthedRoute_thenReturnUnauthorized () {
216
259
given ()
217
260
.config (SslContext .clientCertApiml )
218
261
.when ()
0 commit comments