Search in sources :

Example 1 with UserStateDetails

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();
}
Also used : Context(android.content.Context) ApplicationProvider.getApplicationContext(androidx.test.core.app.ApplicationProvider.getApplicationContext) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) AWSConfiguration(com.amazonaws.mobile.config.AWSConfiguration) UserStateDetails(com.amazonaws.mobile.client.UserStateDetails) AmplifyException(com.amplifyframework.AmplifyException) JSONException(org.json.JSONException) BeforeClass(org.junit.BeforeClass)

Example 2 with UserStateDetails

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

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());
}
Also used : HostedUIOptions(com.amazonaws.mobile.client.HostedUIOptions) SignInUIOptions(com.amazonaws.mobile.client.SignInUIOptions) Activity(android.app.Activity) RandomString(com.amplifyframework.testutils.random.RandomString) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) UserStateDetails(com.amazonaws.mobile.client.UserStateDetails) AuthSignInResult(com.amplifyframework.auth.result.AuthSignInResult) Tokens(com.amazonaws.mobile.client.results.Tokens) Test(org.junit.Test)

Example 4 with UserStateDetails

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);
}
Also used : UserStateDetails(com.amazonaws.mobile.client.UserStateDetails) AWSCredentials(com.amazonaws.auth.AWSCredentials) BasicAWSCredentials(com.amazonaws.auth.BasicAWSCredentials) BasicAWSCredentials(com.amazonaws.auth.BasicAWSCredentials) Test(org.junit.Test)

Example 5 with UserStateDetails

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);
}
Also used : AmazonClientException(com.amazonaws.AmazonClientException) UserStateDetails(com.amazonaws.mobile.client.UserStateDetails) AWSCredentials(com.amazonaws.auth.AWSCredentials) BasicAWSCredentials(com.amazonaws.auth.BasicAWSCredentials) Test(org.junit.Test)

Aggregations

UserStateDetails (com.amazonaws.mobile.client.UserStateDetails)16 Test (org.junit.Test)11 AWSCredentials (com.amazonaws.auth.AWSCredentials)6 BasicAWSCredentials (com.amazonaws.auth.BasicAWSCredentials)6 Tokens (com.amazonaws.mobile.client.results.Tokens)6 AmplifyException (com.amplifyframework.AmplifyException)6 JSONException (org.json.JSONException)6 Context (android.content.Context)5 AuthException (com.amplifyframework.auth.AuthException)5 AmazonClientException (com.amazonaws.AmazonClientException)4 HostedUIOptions (com.amazonaws.mobile.client.HostedUIOptions)4 SignInUIOptions (com.amazonaws.mobile.client.SignInUIOptions)4 AWSConfiguration (com.amazonaws.mobile.config.AWSConfiguration)4 AuthSignInResult (com.amplifyframework.auth.result.AuthSignInResult)4 JSONObject (org.json.JSONObject)4 Activity (android.app.Activity)3 ApplicationProvider.getApplicationContext (androidx.test.core.app.ApplicationProvider.getApplicationContext)3 AWSMobileClient (com.amazonaws.mobile.client.AWSMobileClient)3 AuthCategoryConfiguration (com.amplifyframework.auth.AuthCategoryConfiguration)3 NonNull (androidx.annotation.NonNull)2