Skip to content

Commit

Permalink
Merge pull request #384 from auth0/add-auth0parcelable-tests
Browse files Browse the repository at this point in the history
Fix Auth0 parcel that was losing telemetry and OIDC flag
  • Loading branch information
hzalaz authored Jan 2, 2017
2 parents 0265dfb + 3fb8751 commit 7695af4
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 15 deletions.
25 changes: 23 additions & 2 deletions lib/src/main/java/com/auth0/android/lock/Auth0Parcelable.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,17 @@
import android.os.Parcelable;

import com.auth0.android.Auth0;
import com.auth0.android.util.Telemetry;


/**
* This class wraps a {@link Auth0} to make it Parcelable
*/
public class Auth0Parcelable implements Parcelable {

Auth0 auth0;
private static final double WITHOUT_DATA = 0x00;
private static final double WITH_DATA = 0x01;
private Auth0 auth0;

public Auth0Parcelable(Auth0 auth0) {
this.auth0 = auth0;
Expand All @@ -56,6 +59,13 @@ public void writeToParcel(Parcel dest, int flags) {
dest.writeString(auth0.getClientId());
dest.writeString(auth0.getDomainUrl());
dest.writeString(auth0.getConfigurationUrl());
dest.writeByte((byte) (auth0.isOIDCConformant() ? WITH_DATA : WITHOUT_DATA));
dest.writeByte((byte) (auth0.getTelemetry() != null ? WITH_DATA : WITHOUT_DATA));
if (auth0.getTelemetry() != null) {
dest.writeString(auth0.getTelemetry().getName());
dest.writeString(auth0.getTelemetry().getVersion());
dest.writeString(auth0.getTelemetry().getLibraryVersion());
}
}

public static final Parcelable.Creator<Auth0Parcelable> CREATOR
Expand All @@ -73,6 +83,17 @@ private Auth0Parcelable(Parcel in) {
String clientId = in.readString();
String domain = in.readString();
String configurationDomain = in.readString();
this.auth0 = new Auth0(clientId, domain, configurationDomain);
boolean isOIDCConformant = in.readByte() != WITHOUT_DATA;
boolean hasTelemetry = in.readByte() != WITHOUT_DATA;
String telemetryName = in.readString();
String telemetryVersion = in.readString();
String telemetryLibraryVersion = in.readString();

auth0 = new Auth0(clientId, domain, configurationDomain);
auth0.setOIDCConformant(isOIDCConformant);
if (hasTelemetry) {
Telemetry telemetry = new Telemetry(telemetryName, telemetryVersion, telemetryLibraryVersion);
auth0.setTelemetry(telemetry);
}
}
}
80 changes: 67 additions & 13 deletions lib/src/test/java/com/auth0/android/lock/Auth0ParcelableTest.java
Original file line number Diff line number Diff line change
@@ -1,42 +1,96 @@
package com.auth0.android.lock;

import android.os.Bundle;

import android.os.Parcel;

import com.auth0.android.Auth0;
import com.auth0.android.util.Telemetry;
import com.squareup.okhttp.HttpUrl;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricGradleTestRunner;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;

import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
import static org.junit.Assert.assertThat;


@RunWith(RobolectricGradleTestRunner.class)
@RunWith(RobolectricTestRunner.class)
@Config(constants = BuildConfig.class, sdk = 21, manifest = Config.NONE)
public class Auth0ParcelableTest {

private static final String CLIENT_ID = "CLIENT_ID";
private static final String DOMAIN = "https://my-domain.auth0.com";
private static final String CONFIG_DOMAIN = "https://my-cdn.auth0.com";

private static final String AUTH0_KEY = "AUTH0_KEY";
@Test
public void shouldSaveClientId() throws Exception {
Auth0 auth0 = new Auth0(CLIENT_ID, DOMAIN, CONFIG_DOMAIN);
Auth0Parcelable auth0Parcelable = new Auth0Parcelable(auth0);
Parcel parcel = Parcel.obtain();
auth0Parcelable.writeToParcel(parcel, 0);
parcel.setDataPosition(0);

Auth0Parcelable parceledAuth0 = Auth0Parcelable.CREATOR.createFromParcel(parcel);
assertThat(auth0.getClientId(), is(equalTo(CLIENT_ID)));
assertThat(parceledAuth0.getAuth0().getClientId(), is(equalTo(CLIENT_ID)));
}

@Test
public void shouldSaveDomainUrl() throws Exception {
Auth0 auth0 = new Auth0(CLIENT_ID, DOMAIN, CONFIG_DOMAIN);
Auth0Parcelable auth0Parcelable = new Auth0Parcelable(auth0);
Parcel parcel = Parcel.obtain();
auth0Parcelable.writeToParcel(parcel, 0);
parcel.setDataPosition(0);

Auth0Parcelable parceledAuth0 = Auth0Parcelable.CREATOR.createFromParcel(parcel);
assertThat(HttpUrl.parse(auth0.getDomainUrl()), is(equalTo(HttpUrl.parse(DOMAIN))));
assertThat(HttpUrl.parse(parceledAuth0.getAuth0().getDomainUrl()), is(equalTo(HttpUrl.parse(DOMAIN))));
}

@Test
public void testParcelable() throws Exception {
public void shouldSaveConfigurationUrl() throws Exception {
Auth0 auth0 = new Auth0(CLIENT_ID, DOMAIN, CONFIG_DOMAIN);
Bundle bundle = new Bundle();
bundle.putParcelable(AUTH0_KEY, new Auth0Parcelable(auth0));
Auth0Parcelable auth0Parcelable = new Auth0Parcelable(auth0);
Parcel parcel = Parcel.obtain();
auth0Parcelable.writeToParcel(parcel, 0);
parcel.setDataPosition(0);

Auth0Parcelable auth0Parcelable = bundle.getParcelable(AUTH0_KEY);
Auth0 auth0bundle = auth0Parcelable.getAuth0();
Auth0Parcelable parceledAuth0 = Auth0Parcelable.CREATOR.createFromParcel(parcel);
assertThat(HttpUrl.parse(auth0.getConfigurationUrl()), is(equalTo(HttpUrl.parse(CONFIG_DOMAIN))));
assertThat(HttpUrl.parse(parceledAuth0.getAuth0().getConfigurationUrl()), is(equalTo(HttpUrl.parse(CONFIG_DOMAIN))));
}

@Test
public void shouldSaveTelemetry() throws Exception {
Telemetry telemetry = new Telemetry("name", "version", "libraryVersion");
Auth0 auth0 = new Auth0(CLIENT_ID, DOMAIN, CONFIG_DOMAIN);
auth0.setTelemetry(telemetry);
Auth0Parcelable auth0Parcelable = new Auth0Parcelable(auth0);
Parcel parcel = Parcel.obtain();
auth0Parcelable.writeToParcel(parcel, 0);
parcel.setDataPosition(0);

Auth0Parcelable parceledAuth0 = Auth0Parcelable.CREATOR.createFromParcel(parcel);
assertThat(telemetry.getValue(), is(notNullValue()));
assertThat(auth0.getTelemetry().getValue(), is(equalTo(telemetry.getValue())));
assertThat(parceledAuth0.getAuth0().getTelemetry().getValue(), is(equalTo(telemetry.getValue())));
}

@Test
public void shouldSaveOIDCConformantFlag() throws Exception {
Auth0 auth0 = new Auth0(CLIENT_ID, DOMAIN, CONFIG_DOMAIN);
auth0.setOIDCConformant(true);
Auth0Parcelable auth0Parcelable = new Auth0Parcelable(auth0);
Parcel parcel = Parcel.obtain();
auth0Parcelable.writeToParcel(parcel, 0);
parcel.setDataPosition(0);

assertThat(auth0bundle.getClientId(), is(equalTo(CLIENT_ID)));
assertThat(HttpUrl.parse(auth0bundle.getDomainUrl()), is(equalTo(HttpUrl.parse(DOMAIN))));
assertThat(HttpUrl.parse(auth0bundle.getConfigurationUrl()), is(equalTo(HttpUrl.parse(CONFIG_DOMAIN))));
Auth0Parcelable parceledAuth0 = Auth0Parcelable.CREATOR.createFromParcel(parcel);
assertThat(auth0.isOIDCConformant(), is(equalTo(true)));
assertThat(parceledAuth0.getAuth0().isOIDCConformant(), is(equalTo(true)));
}
}

0 comments on commit 7695af4

Please sign in to comment.