Skip to content

Commit

Permalink
chore: migrate tests in oauth2_http module from JUnit4 to JUnit5 - fo…
Browse files Browse the repository at this point in the history
…urth iteration (googleapis#756) (googleapis#766)
  • Loading branch information
Captain1653 authored Oct 12, 2021
1 parent ec29abc commit d7edc45
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 92 deletions.
24 changes: 12 additions & 12 deletions oauth2_http/javatests/com/google/auth/oauth2/ITDownscopingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@

package com.google.auth.oauth2;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import com.google.api.client.http.GenericUrl;
import com.google.api.client.http.HttpRequest;
Expand All @@ -46,7 +46,7 @@
import com.google.auth.Credentials;
import com.google.auth.http.HttpCredentialsAdapter;
import java.io.IOException;
import org.junit.Test;
import org.junit.jupiter.api.Test;

/**
* Integration tests for Downscoping with Credential Access Boundaries via {@link
Expand All @@ -56,7 +56,7 @@
* GOOGLE_APPLICATION_CREDENTIALS to point to the same service account configured in the setup
* script (downscoping-with-cab-setup.sh).
*/
public final class ITDownscopingTest {
class ITDownscopingTest {

// Output copied from the setup script (downscoping-with-cab-setup.sh).
private static final String GCS_BUCKET_NAME = "cab-int-bucket-cbi3qrv5";
Expand Down Expand Up @@ -93,7 +93,7 @@ public final class ITDownscopingTest {
* in the same bucket.
*/
@Test
public void downscoping_serviceAccountSourceWithRefresh() throws IOException {
void downscoping_serviceAccountSourceWithRefresh() throws IOException {
OAuth2CredentialsWithRefresh.OAuth2RefreshHandler refreshHandler =
new OAuth2CredentialsWithRefresh.OAuth2RefreshHandler() {
@Override
Expand Down Expand Up @@ -122,12 +122,12 @@ public AccessToken refreshAccessToken() throws IOException {

// Attempt to retrieve the object that the downscoped token does not have access to. This should
// fail.
try {
retrieveObjectFromGcs(credentials, GCS_OBJECT_NAME_WITHOUT_PERMISSION);
fail("Call to GCS should have failed.");
} catch (HttpResponseException e) {
assertEquals(403, e.getStatusCode());
}
HttpResponseException exception =
assertThrows(
HttpResponseException.class,
() -> retrieveObjectFromGcs(credentials, GCS_OBJECT_NAME_WITHOUT_PERMISSION),
"Call to GCS should have failed.");
assertEquals(403, exception.getStatusCode());
}

private void retrieveObjectFromGcs(Credentials credentials, String objectName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@

package com.google.auth.oauth2;

import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import com.google.api.client.http.GenericUrl;
import com.google.api.client.http.HttpRequest;
Expand All @@ -51,8 +51,8 @@
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

/**
* Integration tests for Workload Identity Federation.
Expand All @@ -62,7 +62,7 @@
* (workloadidentityfederation-setup). These tests call GCS to get bucket information. The bucket
* name must be provided through the GCS_BUCKET environment variable.
*/
public final class ITWorkloadIdentityFederationTest {
class ITWorkloadIdentityFederationTest {

// Copy output from workloadidentityfederation-setup.
private static final String AUDIENCE_PREFIX =
Expand All @@ -75,8 +75,8 @@ public final class ITWorkloadIdentityFederationTest {

private String clientEmail;

@Before
public void setup() throws IOException {
@BeforeEach
void setup() throws IOException {
GenericJson keys = getServiceAccountKeyFileAsJson();
clientEmail = (String) keys.get("client_email");
}
Expand All @@ -89,7 +89,7 @@ public void setup() throws IOException {
* service account key.
*/
@Test
public void identityPoolCredentials() throws IOException {
void identityPoolCredentials() throws IOException {
IdentityPoolCredentials identityPoolCredentials =
(IdentityPoolCredentials)
ExternalAccountCredentials.fromJson(
Expand All @@ -108,7 +108,7 @@ public void identityPoolCredentials() throws IOException {
* service account key.
*/
@Test
public void awsCredentials() throws Exception {
void awsCredentials() throws Exception {
String idToken = generateGoogleIdToken(AWS_AUDIENCE);

String url =
Expand Down Expand Up @@ -202,9 +202,7 @@ private GenericJson buildAwsCredentialConfig() {

private void callGcs(GoogleCredentials credentials) throws IOException {
String bucketName = System.getenv("GCS_BUCKET");
if (bucketName == null) {
fail("GCS bucket name not set through GCS_BUCKET env variable.");
}
assertNotNull(bucketName, "GCS bucket name not set through GCS_BUCKET env variable.");

String url = "https://storage.googleapis.com/storage/v1/b/" + bucketName;

Expand Down
24 changes: 12 additions & 12 deletions oauth2_http/javatests/com/google/auth/oauth2/JwtClaimsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,19 @@

package com.google.auth.oauth2;

import static org.junit.Assert.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.Collections;
import java.util.Map;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.junit.jupiter.api.Test;

@RunWith(JUnit4.class)
public class JwtClaimsTest {

@Test
public void testMergeOverwritesFields() {
void testMergeOverwritesFields() {
JwtClaims claims1 =
JwtClaims.newBuilder()
.setAudience("audience-1")
Expand All @@ -64,7 +64,7 @@ public void testMergeOverwritesFields() {
}

@Test
public void testMergeDefaultValues() {
void testMergeDefaultValues() {
JwtClaims claims1 =
JwtClaims.newBuilder()
.setAudience("audience-1")
Expand All @@ -80,7 +80,7 @@ public void testMergeDefaultValues() {
}

@Test
public void testMergeNull() {
void testMergeNull() {
JwtClaims claims1 = JwtClaims.newBuilder().build();
JwtClaims claims2 = JwtClaims.newBuilder().build();
JwtClaims merged = claims1.merge(claims2);
Expand All @@ -93,7 +93,7 @@ public void testMergeNull() {
}

@Test
public void testEquals() {
void testEquals() {
JwtClaims claims1 =
JwtClaims.newBuilder()
.setAudience("audience-1")
Expand All @@ -111,14 +111,14 @@ public void testEquals() {
}

@Test
public void testAdditionalClaimsDefaults() {
void testAdditionalClaimsDefaults() {
JwtClaims claims = JwtClaims.newBuilder().build();
assertNotNull(claims.getAdditionalClaims());
assertTrue(claims.getAdditionalClaims().isEmpty());
}

@Test
public void testMergeAdditionalClaims() {
void testMergeAdditionalClaims() {
JwtClaims claims1 =
JwtClaims.newBuilder().setAdditionalClaims(Collections.singletonMap("foo", "bar")).build();
JwtClaims claims2 =
Expand All @@ -138,7 +138,7 @@ public void testMergeAdditionalClaims() {
}

@Test
public void testIsComplete() {
void testIsComplete() {
// Test JwtClaim is complete if audience is not set but scope is provided.
JwtClaims claims =
JwtClaims.newBuilder()
Expand Down
Loading

0 comments on commit d7edc45

Please sign in to comment.