Skip to content

Commit

Permalink
allow to set a custom Telemetry instance
Browse files Browse the repository at this point in the history
  • Loading branch information
lbalmaceda committed Apr 12, 2019
1 parent db49b76 commit f54e185
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 6 deletions.
9 changes: 9 additions & 0 deletions src/main/java/com/auth0/client/auth/AuthAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,15 @@ public void doNotSendTelemetry() {
telemetry.setEnabled(false);
}

/**
* Setter for the Telemetry to send in every request to Auth0.
*
* @param telemetry to send in every request to Auth0
*/
public void setTelemetry(Telemetry telemetry) {
this.telemetry.setTelemetry(telemetry);
}

/**
* Whether to enable or not the current HTTP Logger for every Request, Response and other sensitive information.
*
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/com/auth0/client/mgmt/ManagementAPI.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.auth0.client.mgmt;

import com.auth0.net.Telemetry;
import com.auth0.net.TelemetryInterceptor;
import com.auth0.utils.Asserts;
import okhttp3.HttpUrl;
Expand Down Expand Up @@ -65,6 +66,15 @@ public void doNotSendTelemetry() {
telemetry.setEnabled(false);
}

/**
* Setter for the Telemetry to send in every request to Auth0.
*
* @param telemetry to send in every request to Auth0
*/
public void setTelemetry(Telemetry telemetry) {
this.telemetry.setTelemetry(telemetry);
}

/**
* Whether to enable or not the current HTTP Logger for every Request, Response and other sensitive information.
*
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/com/auth0/net/TelemetryInterceptor.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ public Response intercept(Chain chain) throws IOException {
return chain.proceed(request);
}

public void setTelemetry(Telemetry telemetry) {
this.telemetry = telemetry;
}

public Telemetry getTelemetry() {
return this.telemetry;
}

public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
Expand Down
39 changes: 33 additions & 6 deletions src/test/java/com/auth0/client/auth/AuthAPITest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@
import com.auth0.json.auth.CreatedUser;
import com.auth0.json.auth.TokenHolder;
import com.auth0.json.auth.UserInfo;
import com.auth0.net.AuthRequest;
import com.auth0.net.Request;
import com.auth0.net.SignUpRequest;
import com.auth0.net.TelemetryInterceptor;
import com.auth0.net.*;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import okhttp3.Interceptor;
Expand All @@ -20,6 +17,7 @@
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.mockito.Mockito;

import java.io.FileReader;
import java.util.HashMap;
Expand Down Expand Up @@ -106,6 +104,33 @@ public void shouldThrowWhenClientSecretIsNull() throws Exception {
new AuthAPI(DOMAIN, CLIENT_ID, null);
}

@Test
public void shouldUseCustomTelemetry() throws Exception {
AuthAPI api = new AuthAPI(DOMAIN, CLIENT_ID, CLIENT_SECRET);
assertThat(api.getClient().interceptors(), hasItem(isA(TelemetryInterceptor.class)));

Telemetry currentTelemetry = null;
for (Interceptor i : api.getClient().interceptors()) {
if (i instanceof TelemetryInterceptor) {
TelemetryInterceptor interceptor = (TelemetryInterceptor) i;
currentTelemetry = interceptor.getTelemetry();
}
}
assertThat(currentTelemetry, is(notNullValue()));

Telemetry newTelemetry = Mockito.mock(Telemetry.class);
api.setTelemetry(newTelemetry);

Telemetry updatedTelemetry = null;
for (Interceptor i : api.getClient().interceptors()) {
if (i instanceof TelemetryInterceptor) {
TelemetryInterceptor interceptor = (TelemetryInterceptor) i;
updatedTelemetry = interceptor.getTelemetry();
}
}
assertThat(updatedTelemetry, is(newTelemetry));
}

@Test
public void shouldAddAndEnableTelemetryInterceptor() throws Exception {
AuthAPI api = new AuthAPI(DOMAIN, CLIENT_ID, CLIENT_SECRET);
Expand Down Expand Up @@ -389,7 +414,8 @@ public void shouldThrowOnUsernameSignUpWithNullConnection() throws Exception {
public void shouldHaveNotStrongPasswordWithDetailedDescription() throws Exception {
ObjectMapper mapper = new ObjectMapper();
FileReader fr = new FileReader(PASSWORD_STRENGTH_ERROR_RESPONSE_NONE);
Map<String, Object> mapPayload = mapper.readValue(fr, new TypeReference<Map<String, Object>>() {});
Map<String, Object> mapPayload = mapper.readValue(fr, new TypeReference<Map<String, Object>>() {
});
APIException ex = new APIException(mapPayload, 400);
assertThat(ex.getError(), is("invalid_password"));
String expectedDescription = "At least 10 characters in length; Contain at least 3 of the following 4 types of characters: lower case letters (a-z), upper case letters (A-Z), numbers (i.e. 0-9), special characters (e.g. !@#$%^&*); Should contain: lower case letters (a-z), upper case letters (A-Z), numbers (i.e. 0-9), special characters (e.g. !@#$%^&*); No more than 2 identical characters in a row (e.g., \"aaa\" not allowed)";
Expand All @@ -400,7 +426,8 @@ public void shouldHaveNotStrongPasswordWithDetailedDescription() throws Exceptio
public void shouldHaveNotStrongPasswordWithShortDetailedDescription() throws Exception {
ObjectMapper mapper = new ObjectMapper();
FileReader fr = new FileReader(PASSWORD_STRENGTH_ERROR_RESPONSE_SOME);
Map<String, Object> mapPayload = mapper.readValue(fr, new TypeReference<Map<String, Object>>() {});
Map<String, Object> mapPayload = mapper.readValue(fr, new TypeReference<Map<String, Object>>() {
});
APIException ex = new APIException(mapPayload, 400);
assertThat(ex.getError(), is("invalid_password"));
String expectedDescription = "Should contain: lower case letters (a-z), upper case letters (A-Z), numbers (i.e. 0-9), special characters (e.g. !@#$%^&*)";
Expand Down
29 changes: 29 additions & 0 deletions src/test/java/com/auth0/client/mgmt/ManagementAPITest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.auth0.client.mgmt;

import com.auth0.client.MockServer;
import com.auth0.net.Telemetry;
import com.auth0.net.TelemetryInterceptor;
import okhttp3.Interceptor;
import okhttp3.logging.HttpLoggingInterceptor;
Expand All @@ -9,6 +10,7 @@
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.mockito.Mockito;

import static com.auth0.client.UrlMatcher.isUrl;
import static okhttp3.logging.HttpLoggingInterceptor.Level;
Expand Down Expand Up @@ -144,6 +146,33 @@ public void shouldAddAndEnableTelemetryInterceptor() throws Exception {
}
}

@Test
public void shouldUseCustomTelemetry() throws Exception {
ManagementAPI api = new ManagementAPI(DOMAIN, API_TOKEN);
assertThat(api.getClient().interceptors(), hasItem(isA(TelemetryInterceptor.class)));

Telemetry currentTelemetry = null;
for (Interceptor i : api.getClient().interceptors()) {
if (i instanceof TelemetryInterceptor) {
TelemetryInterceptor interceptor = (TelemetryInterceptor) i;
currentTelemetry = interceptor.getTelemetry();
}
}
assertThat(currentTelemetry, is(notNullValue()));

Telemetry newTelemetry = Mockito.mock(Telemetry.class);
api.setTelemetry(newTelemetry);

Telemetry updatedTelemetry = null;
for (Interceptor i : api.getClient().interceptors()) {
if (i instanceof TelemetryInterceptor) {
TelemetryInterceptor interceptor = (TelemetryInterceptor) i;
updatedTelemetry = interceptor.getTelemetry();
}
}
assertThat(updatedTelemetry, is(newTelemetry));
}

@Test
public void shouldDisableTelemetryInterceptor() throws Exception {
ManagementAPI api = new ManagementAPI(DOMAIN, API_TOKEN);
Expand Down
33 changes: 33 additions & 0 deletions src/test/java/com/auth0/net/TelemetryInterceptorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,39 @@ public void shouldEnable() throws Exception {
assertThat(interceptor.isEnabled(), is(true));
}

@Test
public void shouldGetTelemetry() {
TelemetryInterceptor interceptor = new TelemetryInterceptor(telemetry);
assertThat(interceptor.getTelemetry(), is(telemetry));
}

@Test
public void shouldAllowToModifyTelemetryOnceSet() throws Exception {
TelemetryInterceptor interceptor = new TelemetryInterceptor(telemetry);
Telemetry updatedTelemetry = mock(Telemetry.class);
when(updatedTelemetry.getValue()).thenReturn("UpdatedClientInfo");
interceptor.setTelemetry(updatedTelemetry);

OkHttpClient client = new OkHttpClient.Builder()
.addInterceptor(interceptor)
.build();

MockWebServer server = new MockWebServer();
server.start();
server.enqueue(new MockResponse());

Request request = new Request.Builder()
.get()
.url(server.url("/"))
.build();
client.newCall(request).execute();

RecordedRequest finalRequest = server.takeRequest();
assertThat(finalRequest.getHeader("Auth0-Client"), is("UpdatedClientInfo"));

server.shutdown();
}

@Test
public void shouldAddTelemetryHeaderIfEnabled() throws Exception {
TelemetryInterceptor interceptor = new TelemetryInterceptor(telemetry);
Expand Down

0 comments on commit f54e185

Please sign in to comment.