Skip to content

Commit aa7bfbf

Browse files
committed
add unit tests for authenticate
1 parent e5f4505 commit aa7bfbf

File tree

1 file changed

+96
-2
lines changed

1 file changed

+96
-2
lines changed

core/src/test/java/cloud/stackit/sdk/core/KeyFlowAuthenticatorTest.java

Lines changed: 96 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@
1616
import java.security.spec.InvalidKeySpecException;
1717
import java.time.temporal.ChronoUnit;
1818
import java.util.Date;
19-
import okhttp3.HttpUrl;
20-
import okhttp3.OkHttpClient;
19+
import okhttp3.*;
2120
import okhttp3.mockwebserver.MockResponse;
2221
import okhttp3.mockwebserver.MockWebServer;
2322
import org.junit.jupiter.api.AfterEach;
@@ -62,6 +61,9 @@ class KeyFlowAuthenticatorTest {
6261
+ "h/9afEtu5aUE/m+1vGBoH8z1\n"
6362
+ "-----END PRIVATE KEY-----\n";
6463

64+
private static final Request mockRequest =
65+
new Request.Builder().url("https://stackit.com").get().build();
66+
6567
private ServiceAccountKey createDummyServiceAccount() {
6668
ServiceAccountCredentials credentials =
6769
new ServiceAccountCredentials("aud", "iss", "kid", PRIVATE_KEY, "sub");
@@ -270,4 +272,96 @@ void createAccessTokenWithRefreshTokenResponse200WithEmptyBodyThrowsException()
270272
assertThrows(
271273
JsonSyntaxException.class, keyFlowAuthenticator::createAccessTokenWithRefreshToken);
272274
}
275+
276+
@Test
277+
@DisplayName("authenticator sets Authorization header")
278+
void authenticatorSetsAuthorizationHeaderIfNotAuthenticated()
279+
throws NoSuchAlgorithmException, InvalidKeySpecException, IOException {
280+
// Setup mockServer
281+
final String authorizationHeader = "Authorization";
282+
KeyFlowAuthenticator.KeyFlowTokenResponse mockResponse = mockResponseBody(false);
283+
MockResponse mockedResponse =
284+
new MockResponse()
285+
.setResponseCode(200)
286+
.setBody(new Gson().toJson(mockResponse))
287+
.addHeader(
288+
"Content-type",
289+
"application/json"); // mock response for KeyFlow authentication
290+
// with mocked access token
291+
mockWebServer.enqueue(mockedResponse);
292+
HttpUrl url = mockWebServer.url(MOCK_WEBSERVER_PATH);
293+
294+
// Set unauthorized request
295+
Response unauthorizedRequest =
296+
new Response.Builder()
297+
.request(mockRequest)
298+
.code(401)
299+
.message("Unauthorized")
300+
.protocol(Protocol.HTTP_2)
301+
.build();
302+
303+
// Config
304+
CoreConfiguration cfg =
305+
new CoreConfiguration().tokenCustomUrl(url.toString()); // Use mockWebServer
306+
307+
// Check if "Authorization" header is unset
308+
assertNull(unauthorizedRequest.request().header(authorizationHeader));
309+
310+
// Prepare keyFlowAuthenticator
311+
KeyFlowAuthenticator keyFlowAuthenticator =
312+
new KeyFlowAuthenticator(httpClient, cfg, createDummyServiceAccount());
313+
// authenticator creates new access token and sets it the Authorization header
314+
Request newRequest = keyFlowAuthenticator.authenticate(null, unauthorizedRequest);
315+
316+
// Check if new request is not null
317+
assertNotNull(newRequest);
318+
// Check if the "Authorization" Header is set
319+
assertNotNull(newRequest.header(authorizationHeader));
320+
}
321+
322+
@Test
323+
@DisplayName("Authenticator returns null when already authenticated")
324+
void authenticatorReturnsNullWhenAlreadyAuthenticated()
325+
throws NoSuchAlgorithmException, InvalidKeySpecException, IOException {
326+
// Setup mockServer
327+
final String authorizationHeader = "Authorization";
328+
KeyFlowAuthenticator.KeyFlowTokenResponse mockResponse = mockResponseBody(false);
329+
MockResponse mockedResponse =
330+
new MockResponse()
331+
.setResponseCode(200)
332+
.setBody(new Gson().toJson(mockResponse))
333+
.addHeader(
334+
"Content-type",
335+
"application/json"); // mock response for KeyFlow authentication
336+
// with mocked access token
337+
mockWebServer.enqueue(mockedResponse);
338+
HttpUrl url = mockWebServer.url(MOCK_WEBSERVER_PATH);
339+
340+
// Set unauthorized request
341+
Response unauthorizedRequest =
342+
new Response.Builder()
343+
.request(
344+
mockRequest
345+
.newBuilder()
346+
.addHeader(authorizationHeader, "<my-access-token>")
347+
.build())
348+
.code(401)
349+
.message("Unauthorized")
350+
.protocol(Protocol.HTTP_2)
351+
.build(); // Unauthorized request
352+
353+
// Config
354+
CoreConfiguration cfg =
355+
new CoreConfiguration().tokenCustomUrl(url.toString()); // Use mockWebServer
356+
357+
// Check if "Authorization" header is set
358+
assertNotNull(unauthorizedRequest.request().header(authorizationHeader));
359+
360+
// Prepare keyFlowAuthenticator
361+
KeyFlowAuthenticator keyFlowAuthenticator =
362+
new KeyFlowAuthenticator(httpClient, cfg, createDummyServiceAccount());
363+
364+
// Authenticator returns no new request, because "Authorization" header was already set
365+
assertNull(keyFlowAuthenticator.authenticate(null, unauthorizedRequest));
366+
}
273367
}

0 commit comments

Comments
 (0)