[android] throw configuration exception when accessing Mapbox services with an invalid token

This commit is contained in:
Łukasz Paczos
2019-07-09 12:35:38 +02:00
committed by Łukasz Paczos
parent 1c931e4006
commit 348c437c0a
5 changed files with 105 additions and 1 deletions

View File

@@ -5,6 +5,7 @@ import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.UiThread;
import com.mapbox.mapboxsdk.constants.MapboxConstants;
import com.mapbox.mapboxsdk.exceptions.MapboxConfigurationException;
import com.mapbox.mapboxsdk.log.Logger;
@@ -87,6 +88,20 @@ public final class Mapbox {
public static void setAccessToken(String accessToken) {
validateMapbox();
INSTANCE.accessToken = accessToken;
// cleanup telemetry which is dependent on an access token
if (INSTANCE.telemetry != null) {
INSTANCE.telemetry.disableTelemetrySession();
INSTANCE.telemetry = null;
}
// initialize components dependent on a token
if (isAccessTokenValid(accessToken)) {
initializeTelemetry();
INSTANCE.accounts = new AccountsManager();
} else {
INSTANCE.accounts = null;
}
FileSource.getInstance(getApplicationContext()).setAccessToken(accessToken);
}
@@ -97,6 +112,13 @@ public final class Mapbox {
* @return the SKU token
*/
public static String getSkuToken() {
if (INSTANCE.accounts == null) {
throw new MapboxConfigurationException(
"A valid access token parameter is required when using a Mapbox service."
+ "\nPlease see https://www.mapbox.com/help/create-api-access-token/ to learn how to create one."
+ "\nMore information in this guide https://www.mapbox.com/help/first-steps-android-sdk/#access-tokens."
+ "Currently provided token is: " + INSTANCE.accessToken);
}
return INSTANCE.accounts.getSkuToken();
}

View File

@@ -1,6 +1,7 @@
package com.mapbox.mapboxsdk.exceptions;
import android.content.Context;
import android.support.annotation.NonNull;
/**
* A MapboxConfigurationException is thrown by MapboxMap when the SDK hasn't been properly initialised.
@@ -22,4 +23,11 @@ public class MapboxConfigurationException extends RuntimeException {
+ "\nPlease see https://www.mapbox.com/help/create-api-access-token/ to learn how to create one."
+ "\nMore information in this guide https://www.mapbox.com/help/first-steps-android-sdk/#access-tokens.");
}
/**
* Creates a Mapbox configuration exception thrown by MapboxMap when the SDK hasn't been properly initialised.
*/
public MapboxConfigurationException(@NonNull String message) {
super(message);
}
}

View File

@@ -32,6 +32,11 @@ public interface TelemetryDefinition {
*/
void setUserTelemetryRequestState(boolean enabled);
/**
* Disables a started telemetry service for this session only.
*/
void disableTelemetrySession();
/**
* Set the end-user selected state to participate or opt-out in telemetry collection.
*/

View File

@@ -79,6 +79,11 @@ public class TelemetryImpl implements TelemetryDefinition {
}
}
@Override
public void disableTelemetrySession() {
telemetry.disable();
}
/**
* Set the debug logging state of telemetry.
*

View File

@@ -5,10 +5,18 @@ import android.support.test.runner.AndroidJUnit4;
import com.mapbox.mapboxsdk.AppCenter;
import com.mapbox.mapboxsdk.Mapbox;
import com.mapbox.mapboxsdk.exceptions.MapboxConfigurationException;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import static junit.framework.Assert.assertFalse;
import static junit.framework.Assert.assertNotNull;
import static junit.framework.Assert.assertNull;
import static junit.framework.Assert.assertSame;
import static junit.framework.Assert.assertTrue;
@@ -18,6 +26,16 @@ public class MapboxTest extends AppCenter {
private static final String ACCESS_TOKEN = "pk.0000000001";
private static final String ACCESS_TOKEN_2 = "pk.0000000002";
@Rule
public ExpectedException expectedException = ExpectedException.none();
private String realToken;
@Before
public void setup() {
realToken = Mapbox.getAccessToken();
}
@Test
@UiThreadTest
public void testConnected() {
@@ -37,11 +55,57 @@ public class MapboxTest extends AppCenter {
@Test
@UiThreadTest
public void setAccessToken() {
String realToken = Mapbox.getAccessToken();
Mapbox.setAccessToken(ACCESS_TOKEN);
assertSame(ACCESS_TOKEN, Mapbox.getAccessToken());
Mapbox.setAccessToken(ACCESS_TOKEN_2);
assertSame(ACCESS_TOKEN_2, Mapbox.getAccessToken());
}
@Test
@UiThreadTest
public void setInvalidAccessToken() {
final String invalidAccessToken = "xyz";
expectedException.expect(MapboxConfigurationException.class);
expectedException.expectMessage(
"A valid access token parameter is required when using a Mapbox service."
+ "\nPlease see https://www.mapbox.com/help/create-api-access-token/ to learn how to create one."
+ "\nMore information in this guide https://www.mapbox.com/help/first-steps-android-sdk/#access-tokens."
+ "Currently provided token is: " + invalidAccessToken
);
Mapbox.setAccessToken(invalidAccessToken);
assertNull(Mapbox.getTelemetry());
Mapbox.getSkuToken();
}
@Test
@UiThreadTest
public void setNullAccessToken() {
expectedException.expect(MapboxConfigurationException.class);
expectedException.expectMessage(
"A valid access token parameter is required when using a Mapbox service."
+ "\nPlease see https://www.mapbox.com/help/create-api-access-token/ to learn how to create one."
+ "\nMore information in this guide https://www.mapbox.com/help/first-steps-android-sdk/#access-tokens."
+ "Currently provided token is: " + null
);
Mapbox.setAccessToken(null);
assertNull(Mapbox.getTelemetry());
Mapbox.getSkuToken();
}
@Test
@UiThreadTest
public void setValidAccessToken() {
final String invalidAccessToken = "xyz";
Mapbox.setAccessToken(invalidAccessToken);
Mapbox.setAccessToken(ACCESS_TOKEN);
assertNotNull(Mapbox.getTelemetry());
assertNotNull(Mapbox.getSkuToken());
}
@After
public void tearDown() {
Mapbox.setAccessToken(realToken);
}
}