use of com.amazonaws.mobile.client.UserStateDetails in project amplify-android by aws-amplify.
the class AnalyticsPinpointInstrumentedTest method setUp.
/**
* Configure the Amplify framework.
*
* @throws AmplifyException From Amplify configuration.
*/
@BeforeClass
public static void setUp() throws AmplifyException {
final CountDownLatch latch = new CountDownLatch(1);
final AtomicReference<Exception> asyncException = new AtomicReference<>();
Context context = getApplicationContext();
AWSMobileClient.getInstance().initialize(context, new AWSConfiguration(context), new Callback<UserStateDetails>() {
@Override
public void onResult(UserStateDetails result) {
latch.countDown();
}
@Override
public void onError(Exception exception) {
asyncException.set(exception);
latch.countDown();
}
});
try {
if (latch.await(AUTH_TIMEOUT, TimeUnit.SECONDS)) {
if (asyncException.get() != null) {
fail("Failed to instantiate AWSMobileClient");
}
} else {
fail("Failed to instantiate AWSMobileClient within " + AUTH_TIMEOUT + " seconds");
}
} catch (InterruptedException error) {
fail("Failed to instantiate AWSMobileClient");
}
AWSPinpointAnalyticsPlugin plugin = new AWSPinpointAnalyticsPlugin((Application) context, AWSMobileClient.getInstance());
Amplify.addPlugin(plugin);
Amplify.configure(context);
analyticsClient = plugin.getAnalyticsClient();
targetingClient = plugin.getTargetingClient();
}
use of com.amazonaws.mobile.client.UserStateDetails 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.amazonaws.mobile.client.UserStateDetails in project amplify-android by aws-amplify.
the class AuthComponentTest method signInWithWebUI.
/**
* Tests that the signInWithWebUI method of the Auth wrapper of AWSMobileClient (AMC) calls AMC.showSignIn
* with the proper parameters and converts the returned result to the proper AuthSignInResult.
* @throws AuthException test fails if this gets thrown since method should succeed
*/
@Test
public void signInWithWebUI() throws AuthException {
Map<String, String> additionalInfoMap = Collections.singletonMap("testKey", "testVal");
UserStateDetails userStateResult = new UserStateDetails(UserState.SIGNED_IN, additionalInfoMap);
doAnswer(invocation -> {
Callback<UserStateDetails> callback = invocation.getArgument(2);
callback.onResult(userStateResult);
return null;
}).when(mobileClient).showSignIn(any(), any(), any());
Tokens tokensResult = new Tokens(ACCESS_TOKEN, ID_TOKEN, REFRESH_TOKEN);
doAnswer(invocation -> {
Callback<Tokens> callback = invocation.getArgument(0);
callback.onResult(tokensResult);
return null;
}).when(mobileClient).getTokens(any());
Activity activity = new Activity();
String federationProviderName = "testFedProvider";
String idpIdentifier = "testIdpID";
String browserPackage = "org.mozilla.firefox";
List<String> scopes = Collections.singletonList("scope");
Map<String, String> signInMap = Collections.singletonMap("signInKey", "signInVal");
Map<String, String> signOutMap = Collections.singletonMap("signOutKey", "signOutVal");
Map<String, String> tokensMap = Collections.singletonMap("tokensKey", "tokensVal");
AuthSignInResult result = synchronousAuth.signInWithWebUI(activity, AWSCognitoAuthWebUISignInOptions.builder().federationProviderName(federationProviderName).idpIdentifier(idpIdentifier).scopes(scopes).signInQueryParameters(signInMap).signOutQueryParameters(signOutMap).tokenQueryParameters(tokensMap).browserPackage(browserPackage).build());
assertTrue(result.isSignInComplete());
assertEquals(AuthSignInStep.DONE, result.getNextStep().getSignInStep());
assertEquals(additionalInfoMap, result.getNextStep().getAdditionalInfo());
ArgumentCaptor<SignInUIOptions> optionsCaptor = ArgumentCaptor.forClass(SignInUIOptions.class);
verify(mobileClient).showSignIn(eq(activity), optionsCaptor.capture(), any());
SignInUIOptions signInUIOptions = optionsCaptor.getValue();
HostedUIOptions hostedUIOptions = signInUIOptions.getHostedUIOptions();
assertNotNull(hostedUIOptions);
assertNull(hostedUIOptions.getIdentityProvider());
assertEquals(federationProviderName, hostedUIOptions.getFederationProviderName());
assertEquals(idpIdentifier, hostedUIOptions.getIdpIdentifier());
assertEquals(browserPackage, signInUIOptions.getBrowserPackage());
assertArrayEquals(scopes.toArray(), hostedUIOptions.getScopes());
assertEquals(signInMap, hostedUIOptions.getSignInQueryParameters());
assertEquals(signOutMap, hostedUIOptions.getSignOutQueryParameters());
assertEquals(tokensMap, hostedUIOptions.getTokenQueryParameters());
}
use of com.amazonaws.mobile.client.UserStateDetails in project amplify-android by aws-amplify.
the class AuthComponentTest method signedOutSessionWithIdentityPoolAndGuest.
/**
* Test that a signed out account which supports identity pools and has guest credentials returns the proper
* success results for identity pool fields and signed out failure results for user pool fields.
* @throws AuthException test fails if this gets thrown since method should succeed
*/
@Test
public void signedOutSessionWithIdentityPoolAndGuest() throws AuthException {
doAnswer(invocation -> IDENTITY_ID).when(mobileClient).getIdentityId();
AWSCredentials awsCredentialsResult = new BasicAWSCredentials(ACCESS_KEY, SECRET_KEY);
doAnswer(invocation -> {
Callback<AWSCredentials> callback = invocation.getArgument(0);
callback.onResult(awsCredentialsResult);
return null;
}).when(mobileClient).getAWSCredentials(any());
UserStateDetails stateResult = new UserStateDetails(UserState.GUEST, null);
doAnswer(invocation -> {
Callback<UserStateDetails> callback = invocation.getArgument(0);
callback.onResult(stateResult);
return null;
}).when(mobileClient).currentUserState(any());
AWSCognitoAuthSession authSession = (AWSCognitoAuthSession) synchronousAuth.fetchAuthSession();
verify(mobileClient).currentUserState(any());
verify(mobileClient).getIdentityId();
verify(mobileClient).getAWSCredentials(any());
AWSCognitoAuthSession expectedResult = new AWSCognitoAuthSession(false, AuthSessionResult.success(IDENTITY_ID), AuthSessionResult.success(awsCredentialsResult), AuthSessionResult.failure(new AuthException.SignedOutException()), AuthSessionResult.failure(new AuthException.SignedOutException()));
assertEquals(expectedResult, authSession);
}
use of com.amazonaws.mobile.client.UserStateDetails in project amplify-android by aws-amplify.
the class AuthComponentTest method signedOutSessionWithNoIdentityPool.
/**
* Test that a signed out account which does not support identity pools returns invalid account failures for
* identity pool fields and signed out failures for user pool fields.
* @throws AuthException test fails if this gets thrown since method should succeed
*/
@Test
public void signedOutSessionWithNoIdentityPool() throws AuthException {
AmazonClientException credentialsResult = new AmazonClientException("Cognito Identity not configured", null);
doAnswer(invocation -> {
Callback<AWSCredentials> callback = invocation.getArgument(0);
callback.onError(credentialsResult);
return null;
}).when(mobileClient).getAWSCredentials(any());
UserStateDetails stateResult = new UserStateDetails(UserState.SIGNED_OUT, null);
doAnswer(invocation -> {
Callback<UserStateDetails> callback = invocation.getArgument(0);
callback.onResult(stateResult);
return null;
}).when(mobileClient).currentUserState(any());
AWSCognitoAuthSession authSession = (AWSCognitoAuthSession) synchronousAuth.fetchAuthSession();
verify(mobileClient).currentUserState(any());
verify(mobileClient).getAWSCredentials(any());
AWSCognitoAuthSession expectedResult = new AWSCognitoAuthSession(false, AuthSessionResult.failure(new AuthException.InvalidAccountTypeException()), AuthSessionResult.failure(new AuthException.InvalidAccountTypeException()), AuthSessionResult.failure(new AuthException.SignedOutException()), AuthSessionResult.failure(new AuthException.SignedOutException()));
assertEquals(expectedResult, authSession);
}
Aggregations