Skip to content

Commit

Permalink
chore: add partial IT for testing context credential
Browse files Browse the repository at this point in the history
  • Loading branch information
diegomarquezp committed May 23, 2023
1 parent ea4a03c commit ea05922
Showing 1 changed file with 54 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.google.api.client.json.gson.GsonFactory;
import com.google.api.gax.core.FixedCredentialsProvider;
import com.google.api.gax.core.NoCredentialsProvider;
import com.google.api.gax.rpc.ClientContext;
import com.google.auth.Credentials;
import com.google.auth.oauth2.GdchCredentials;
import com.google.showcase.v1beta1.EchoClient;
Expand All @@ -16,6 +17,8 @@
import java.net.URISyntaxException;
import java.nio.file.Path;
import java.nio.file.Paths;

import com.google.showcase.v1beta1.stub.EchoStubSettings;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
Expand Down Expand Up @@ -83,22 +86,52 @@ public void testClientWithGdchCredential_keepsCredentials() throws IOException {
assertSame(credentials, client.getSettings().getCredentialsProvider().getCredentials());
}

/**
* Confirms setting a valid audience is successful.
*
* The provider's credentials do not get altered when adding audience.
* They are recreated and used only inside `ClientContext`
* @throws IOException
* @throws URISyntaxException
*/
@Test
public void testClientWithGdchCredentialWithValidAudience_correct()
throws IOException, URISyntaxException {
String audience = "valid-audience";
settings = settings.toBuilder().setGdchApiAudience(audience).build();
Exception unexpected = getExceptionFromClientCreation();
assertNull(unexpected);
// should have created a new credentials object with api audience
GdchCredentials fromClient =
(GdchCredentials) client.getSettings().getCredentialsProvider().getCredentials();
URI audienceFromClient = fromClient.getApiAudience();
assertNotNull(audienceFromClient);
assertTrue(audienceFromClient.equals(new URI(audience)));
(GdchCredentials) client.getSettings().getCredentialsProvider().getCredentials();

assertSame(credentials, fromClient);
}

/**
* Confirms that the context recreates and contains a different credential when `gdchApiAudience` is specified
* @throws IOException
* @throws URISyntaxException
*/
@Test
public void testClientWithGdchCredentialWithValidAudience_modifiesContextCredentials()
throws IOException, URISyntaxException {
String audience = "valid-audience";
ClientContext context = ClientContext.newBuilder()
.setCredentials(credentials)
.build();
settings = settings.newBuilder(context).build();
Exception unexpected = getExceptionFromClientCreation();
assertNull(unexpected);
GdchCredentials fromClient =
(GdchCredentials) client.getSettings().getCredentialsProvider().getCredentials();
URI audienceFromClientProvider = fromClient.getApiAudience();
assertNotNull(audienceFromClientProvider);
assertTrue(audienceFromClientProvider.equals(new URI(audience)));
assertTrue(
"GDCH credentials with audience should be GdchCredentials",
credentials instanceof GdchCredentials);
"GDCH credentials with audience should be GdchCredentials",
credentials instanceof GdchCredentials);
GdchCredentials fromContext = null;
assertNotSame(fromContext, );
}

@Test
Expand Down Expand Up @@ -133,4 +166,18 @@ private Exception getExceptionFromClientCreation() {
}
return null;
}

private class EchoStubSettingsWithAccessibleContext extends EchoStubSettings {

protected EchoStubSettingsWithAccessibleContext(Builder settingsBuilder) throws IOException {
super(settingsBuilder);
}
}

private class EchoSettingsWithAccessibleContext extends EchoSettings {

protected EchoSettingsWithAccessibleContext(Builder settingsBuilder) throws IOException {
super(settingsBuilder);
}
}
}

0 comments on commit ea05922

Please sign in to comment.