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);
}
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());
}
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)));
}
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);
}
Aggregations