Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
[android] - fix accesstoken validation
Browse files Browse the repository at this point in the history
  • Loading branch information
tobrun committed Jul 23, 2018
1 parent bbc2529 commit bfe714e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.UiThread;
import android.text.TextUtils;
import com.mapbox.mapboxsdk.constants.MapboxConstants;
import com.mapbox.mapboxsdk.exceptions.MapboxConfigurationException;
import com.mapbox.mapboxsdk.maps.Telemetry;
Expand Down Expand Up @@ -138,8 +137,12 @@ private static void validateMapbox() {
* @param accessToken the access token to validate
* @return true is valid, false otherwise
*/
private static boolean isAccessTokenValid(String accessToken) {
return !(TextUtils.isEmpty(accessToken)
|| (!accessToken.toLowerCase(MapboxConstants.MAPBOX_LOCALE).startsWith("pk.")));
static boolean isAccessTokenValid(String accessToken) {
if (accessToken == null) {
return false;
}

accessToken = accessToken.trim().toLowerCase(MapboxConstants.MAPBOX_LOCALE);
return accessToken.length() != 0 && (accessToken.startsWith("pk.") || accessToken.startsWith("sk."));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

import org.junit.Before;
import org.junit.Test;

Expand Down Expand Up @@ -45,6 +44,31 @@ public void testApplicationContext() {
assertEquals(appContext, appContext);
}

@Test
public void testPkTokenValid() {
assertTrue(Mapbox.isAccessTokenValid("pk.0000000001"));
}

@Test
public void testSkTokenValid() {
assertTrue(Mapbox.isAccessTokenValid("sk.0000000001"));
}

@Test
public void testEmptyToken() {
assertFalse(Mapbox.isAccessTokenValid(""));
}

@Test
public void testNullToken() {
assertFalse(Mapbox.isAccessTokenValid(null));
}

@Test
public void testBlaBlaToken() {
assertFalse(Mapbox.isAccessTokenValid("blabla"));
}

@Test
public void testConnected() {
injectMapboxSingleton("dummy");
Expand Down

0 comments on commit bfe714e

Please sign in to comment.