Search in sources :

Example 1 with AuthCategoryConfiguration

use of com.amplifyframework.auth.AuthCategoryConfiguration in project amplify-android by aws-amplify.

the class AmplifyConfiguration method configsFromJson.

private static Map<String, CategoryConfiguration> configsFromJson(JSONObject json) throws AmplifyException {
    final List<CategoryConfiguration> possibleConfigs = Arrays.asList(new AnalyticsCategoryConfiguration(), new ApiCategoryConfiguration(), new AuthCategoryConfiguration(), new DataStoreCategoryConfiguration(), new GeoCategoryConfiguration(), new HubCategoryConfiguration(), new LoggingCategoryConfiguration(), new PredictionsCategoryConfiguration(), new StorageCategoryConfiguration());
    final Map<String, CategoryConfiguration> actualConfigs = new HashMap<>();
    try {
        for (CategoryConfiguration possibleConfig : possibleConfigs) {
            String categoryJsonKey = possibleConfig.getCategoryType().getConfigurationKey();
            if (json.has(categoryJsonKey)) {
                possibleConfig.populateFromJSON(json.getJSONObject(categoryJsonKey));
                actualConfigs.put(categoryJsonKey, possibleConfig);
            }
        }
    } catch (JSONException error) {
        throw new AmplifyException("Could not parse amplifyconfiguration.json ", error, "Check any modifications made to the file.");
    }
    return Immutable.of(actualConfigs);
}
Also used : DataStoreCategoryConfiguration(com.amplifyframework.datastore.DataStoreCategoryConfiguration) StorageCategoryConfiguration(com.amplifyframework.storage.StorageCategoryConfiguration) AmplifyException(com.amplifyframework.AmplifyException) PredictionsCategoryConfiguration(com.amplifyframework.predictions.PredictionsCategoryConfiguration) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) AuthCategoryConfiguration(com.amplifyframework.auth.AuthCategoryConfiguration) LoggingCategoryConfiguration(com.amplifyframework.logging.LoggingCategoryConfiguration) AnalyticsCategoryConfiguration(com.amplifyframework.analytics.AnalyticsCategoryConfiguration) AuthCategoryConfiguration(com.amplifyframework.auth.AuthCategoryConfiguration) CategoryConfiguration(com.amplifyframework.core.category.CategoryConfiguration) GeoCategoryConfiguration(com.amplifyframework.geo.GeoCategoryConfiguration) DataStoreCategoryConfiguration(com.amplifyframework.datastore.DataStoreCategoryConfiguration) HubCategoryConfiguration(com.amplifyframework.hub.HubCategoryConfiguration) PredictionsCategoryConfiguration(com.amplifyframework.predictions.PredictionsCategoryConfiguration) StorageCategoryConfiguration(com.amplifyframework.storage.StorageCategoryConfiguration) ApiCategoryConfiguration(com.amplifyframework.api.ApiCategoryConfiguration) EmptyCategoryConfiguration(com.amplifyframework.core.category.EmptyCategoryConfiguration) JSONException(org.json.JSONException) GeoCategoryConfiguration(com.amplifyframework.geo.GeoCategoryConfiguration) AnalyticsCategoryConfiguration(com.amplifyframework.analytics.AnalyticsCategoryConfiguration) ApiCategoryConfiguration(com.amplifyframework.api.ApiCategoryConfiguration) HubCategoryConfiguration(com.amplifyframework.hub.HubCategoryConfiguration) LoggingCategoryConfiguration(com.amplifyframework.logging.LoggingCategoryConfiguration)

Example 2 with AuthCategoryConfiguration

use of com.amplifyframework.auth.AuthCategoryConfiguration in project amplify-android by aws-amplify.

the class AuthComponentConfigureTest method testConfigureExceptionHandling.

/**
 * If {@link AWSMobileClient} emits an error during initialization, the
 * {@link com.amplifyframework.auth.AuthPlugin#configure(JSONObject, Context)} method should wrap that exception
 * in an {@link AuthException} and throw it on its calling thread.
 * @throws AmplifyException the exception expected to be thrown when configuration fails.
 * @throws JSONException has to be declared as part of creating a test JSON object
 */
@Test(expected = AuthException.class)
public void testConfigureExceptionHandling() throws AmplifyException, JSONException {
    JSONObject pluginConfig = new JSONObject().put("TestKey", "TestVal");
    JSONObject json = new JSONObject().put("plugins", new JSONObject().put(PLUGIN_KEY, pluginConfig));
    AuthCategoryConfiguration authConfig = new AuthCategoryConfiguration();
    authConfig.populateFromJSON(json);
    doAnswer(invocation -> {
        Callback<UserStateDetails> callback = invocation.getArgument(2);
        callback.onError(new Exception());
        return null;
    }).when(mobileClient).initialize(any(), any(), any());
    authCategory.configure(authConfig, getApplicationContext());
}
Also used : JSONObject(org.json.JSONObject) AuthCategoryConfiguration(com.amplifyframework.auth.AuthCategoryConfiguration) UserStateDetails(com.amazonaws.mobile.client.UserStateDetails) AmplifyException(com.amplifyframework.AmplifyException) AuthException(com.amplifyframework.auth.AuthException) JSONException(org.json.JSONException) Test(org.junit.Test)

Example 3 with AuthCategoryConfiguration

use of com.amplifyframework.auth.AuthCategoryConfiguration in project amplify-android by aws-amplify.

the class AuthComponentConfigureTest method testConfigure.

/**
 * Tests that the configure method of the Auth wrapper of AWSMobileClient (AMC) calls AMC.initialize() with the
 * passed in context, a new AWSConfiguration object containing the passed in JSONObject, and a callback object
 * which causes configure to complete successfully in the onResult case.
 * @throws AmplifyException an exception wrapping the exception returned in onError of AMC.initialize()
 * @throws JSONException has to be declared as part of creating a test JSON object
 */
@Test
public void testConfigure() throws AmplifyException, JSONException {
    UserStateDetails userStateDetails = new UserStateDetails(UserState.SIGNED_OUT, null);
    Context context = getApplicationContext();
    JSONObject pluginConfig = new JSONObject().put("TestKey", "TestVal");
    pluginConfig.put("UserAgentOverride", UserAgent.string());
    JSONObject json = new JSONObject().put("plugins", new JSONObject().put(PLUGIN_KEY, pluginConfig));
    AuthCategoryConfiguration authConfig = new AuthCategoryConfiguration();
    authConfig.populateFromJSON(json);
    doAnswer(invocation -> {
        Callback<UserStateDetails> callback = invocation.getArgument(2);
        callback.onResult(userStateDetails);
        return null;
    }).when(mobileClient).initialize(any(), any(), any());
    authCategory.configure(authConfig, context);
    ArgumentCaptor<AWSConfiguration> awsConfigCaptor = ArgumentCaptor.forClass(AWSConfiguration.class);
    verify(mobileClient).initialize(eq(context), awsConfigCaptor.capture(), any());
    String returnedConfig = awsConfigCaptor.getValue().toString();
    String inputConfig = pluginConfig.toString();
    // Strip the opening and closing braces from the test input and ensure that the key/value pair is included
    // in the returned aws config.
    assertTrue(returnedConfig.contains(inputConfig.substring(1, inputConfig.length() - 1)));
}
Also used : Context(android.content.Context) ApplicationProvider.getApplicationContext(androidx.test.core.app.ApplicationProvider.getApplicationContext) JSONObject(org.json.JSONObject) AuthCategoryConfiguration(com.amplifyframework.auth.AuthCategoryConfiguration) UserStateDetails(com.amazonaws.mobile.client.UserStateDetails) AWSConfiguration(com.amazonaws.mobile.config.AWSConfiguration) Test(org.junit.Test)

Example 4 with AuthCategoryConfiguration

use of com.amplifyframework.auth.AuthCategoryConfiguration in project amplify-android by aws-amplify.

the class AuthComponentTest method setup.

/**
 * Get all setup for the future tests with mocks and a standard response for tokens.
 * @throws AmplifyException If add plugin fails
 * @throws JSONException has to be declared as part of creating a test JSON object
 */
@Before
public void setup() throws AmplifyException, JSONException {
    mobileClient = mock(AWSMobileClient.class);
    authCategory = new AuthCategory();
    authCategory.addPlugin(new AWSCognitoAuthPlugin(mobileClient, USER_SUB));
    Map<String, String> additionalInfoMap = Collections.singletonMap("token", ACCESS_TOKEN);
    UserStateDetails userStateDetails = new UserStateDetails(UserState.SIGNED_IN, additionalInfoMap);
    Context context = getApplicationContext();
    JSONObject pluginConfig = new JSONObject().put("TestKey", "TestVal");
    pluginConfig.put("UserAgentOverride", UserAgent.string());
    JSONObject json = new JSONObject().put("plugins", new JSONObject().put(PLUGIN_KEY, pluginConfig));
    AuthCategoryConfiguration authConfig = new AuthCategoryConfiguration();
    authConfig.populateFromJSON(json);
    doAnswer(invocation -> {
        Callback<UserStateDetails> callback = invocation.getArgument(2);
        callback.onResult(userStateDetails);
        return null;
    }).when(mobileClient).initialize(any(), any(), any());
    authCategory.configure(authConfig, context);
    authCategory.initialize(context);
    synchronousAuth = SynchronousAuth.delegatingTo(authCategory);
}
Also used : ApplicationProvider.getApplicationContext(androidx.test.core.app.ApplicationProvider.getApplicationContext) Context(android.content.Context) AuthCategory(com.amplifyframework.auth.AuthCategory) JSONObject(org.json.JSONObject) AuthCategoryConfiguration(com.amplifyframework.auth.AuthCategoryConfiguration) AWSMobileClient(com.amazonaws.mobile.client.AWSMobileClient) RandomString(com.amplifyframework.testutils.random.RandomString) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) UserStateDetails(com.amazonaws.mobile.client.UserStateDetails) Before(org.junit.Before)

Aggregations

AuthCategoryConfiguration (com.amplifyframework.auth.AuthCategoryConfiguration)4 UserStateDetails (com.amazonaws.mobile.client.UserStateDetails)3 JSONObject (org.json.JSONObject)3 Context (android.content.Context)2 ApplicationProvider.getApplicationContext (androidx.test.core.app.ApplicationProvider.getApplicationContext)2 AmplifyException (com.amplifyframework.AmplifyException)2 JSONException (org.json.JSONException)2 Test (org.junit.Test)2 AWSMobileClient (com.amazonaws.mobile.client.AWSMobileClient)1 AWSConfiguration (com.amazonaws.mobile.config.AWSConfiguration)1 AnalyticsCategoryConfiguration (com.amplifyframework.analytics.AnalyticsCategoryConfiguration)1 ApiCategoryConfiguration (com.amplifyframework.api.ApiCategoryConfiguration)1 AuthCategory (com.amplifyframework.auth.AuthCategory)1 AuthException (com.amplifyframework.auth.AuthException)1 CategoryConfiguration (com.amplifyframework.core.category.CategoryConfiguration)1 EmptyCategoryConfiguration (com.amplifyframework.core.category.EmptyCategoryConfiguration)1 DataStoreCategoryConfiguration (com.amplifyframework.datastore.DataStoreCategoryConfiguration)1 GeoCategoryConfiguration (com.amplifyframework.geo.GeoCategoryConfiguration)1 HubCategoryConfiguration (com.amplifyframework.hub.HubCategoryConfiguration)1 LoggingCategoryConfiguration (com.amplifyframework.logging.LoggingCategoryConfiguration)1