|
| 1 | +/* |
| 2 | + * Copyright 2019-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"). |
| 5 | + * You may not use this file except in compliance with the License. |
| 6 | + * A copy of the License is located at |
| 7 | + * |
| 8 | + * http://aws.amazon.com/apache2.0 |
| 9 | + * |
| 10 | + * or in the "license" file accompanying this file. This file is distributed |
| 11 | + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 12 | + * express or implied. See the License for the specific language governing |
| 13 | + * permissions and limitations under the License. |
| 14 | + */ |
| 15 | + |
| 16 | +package com.amazonaws.mobileconnectors.cognitoidentityprovider; |
| 17 | + |
| 18 | +import android.content.Context; |
| 19 | +import android.os.Build; |
| 20 | +import android.support.test.InstrumentationRegistry; |
| 21 | +import android.util.Log; |
| 22 | + |
| 23 | +import com.amazonaws.mobileconnectors.cognitoidentityprovider.continuations.AuthenticationContinuation; |
| 24 | +import com.amazonaws.mobileconnectors.cognitoidentityprovider.continuations.AuthenticationDetails; |
| 25 | +import com.amazonaws.mobileconnectors.cognitoidentityprovider.continuations.ChallengeContinuation; |
| 26 | +import com.amazonaws.mobileconnectors.cognitoidentityprovider.continuations.MultiFactorAuthenticationContinuation; |
| 27 | +import com.amazonaws.mobileconnectors.cognitoidentityprovider.handlers.AuthenticationHandler; |
| 28 | +import com.amazonaws.testutils.AWSTestBase; |
| 29 | + |
| 30 | +import org.json.JSONObject; |
| 31 | + |
| 32 | +import java.security.KeyStore; |
| 33 | +import java.security.KeyStoreException; |
| 34 | +import java.util.ArrayList; |
| 35 | +import java.util.Enumeration; |
| 36 | +import java.util.concurrent.CountDownLatch; |
| 37 | +import java.util.concurrent.TimeUnit; |
| 38 | + |
| 39 | +import static junit.framework.Assert.assertFalse; |
| 40 | +import static junit.framework.Assert.assertTrue; |
| 41 | +import static org.junit.Assert.*; |
| 42 | + |
| 43 | +public abstract class CognitoUserPoolsIntegrationTestBase extends AWSTestBase { |
| 44 | + |
| 45 | + private Context appContext; |
| 46 | + private CognitoUserPool cognitoUserPool; |
| 47 | + private String userName; |
| 48 | + private String password; |
| 49 | + private String userEmail; |
| 50 | + |
| 51 | + private JSONObject getPackageConfigure() { |
| 52 | + return getPackageConfigure("CognitoUserPools"); |
| 53 | + } |
| 54 | + |
| 55 | + void setUp() { |
| 56 | + try { |
| 57 | + appContext = InstrumentationRegistry.getTargetContext(); |
| 58 | + userName = getPackageConfigure().getString("UserName"); |
| 59 | + password = getPackageConfigure().getString("Password"); |
| 60 | + userEmail = getPackageConfigure().getString("UserEmail"); |
| 61 | + |
| 62 | + cognitoUserPool = new CognitoUserPool(appContext, |
| 63 | + getPackageConfigure().getString("UserPoolId"), |
| 64 | + getPackageConfigure().getString("AppClientId"), |
| 65 | + getPackageConfigure().getString("AppClientSecret")); |
| 66 | + } catch (Exception ex) { |
| 67 | + fail("Error in reading CognitoUserPools test configuration. Please check " + super.TEST_CONFIGURATION_FILENAME + " file." |
| 68 | + + ex); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + void tearDown() { |
| 73 | + try { |
| 74 | + cognitoUserPool.awsKeyValueStore.clear(); |
| 75 | + } catch (Exception ex) { |
| 76 | + ex.printStackTrace(); |
| 77 | + fail ("Error in wiping off data stored on disk by CognitoUserPools." + ex); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + CognitoUserSession signIn() { |
| 82 | + final CountDownLatch signInLatch = new CountDownLatch(1); |
| 83 | + final ArrayList<CognitoUserSession> listSessions = new ArrayList<CognitoUserSession>(); |
| 84 | + |
| 85 | + cognitoUserPool.getUser(userName).getSession(new AuthenticationHandler() { |
| 86 | + @Override |
| 87 | + public void onSuccess(CognitoUserSession userSession, CognitoDevice newDevice) { |
| 88 | + listSessions.add(userSession); |
| 89 | + signInLatch.countDown(); |
| 90 | + } |
| 91 | + |
| 92 | + @Override |
| 93 | + public void getAuthenticationDetails(AuthenticationContinuation authenticationContinuation, String userId) { |
| 94 | + AuthenticationDetails authenticationDetails = new AuthenticationDetails(userName, password, null); |
| 95 | + authenticationContinuation.setAuthenticationDetails(authenticationDetails); |
| 96 | + authenticationContinuation.continueTask(); |
| 97 | + } |
| 98 | + |
| 99 | + @Override |
| 100 | + public void getMFACode(MultiFactorAuthenticationContinuation continuation) { |
| 101 | + fail("Tests are not configured to work with MFA. " + |
| 102 | + "Either create a CognitoUserPool without MFA or update the test."); |
| 103 | + signInLatch.countDown(); |
| 104 | + } |
| 105 | + |
| 106 | + @Override |
| 107 | + public void authenticationChallenge(ChallengeContinuation continuation) { |
| 108 | + fail("Tests are not configured to work with additional challenges. " + |
| 109 | + "Either create a CognitoUserPool without additional challenges or update the test."); |
| 110 | + signInLatch.countDown(); |
| 111 | + } |
| 112 | + |
| 113 | + @Override |
| 114 | + public void onFailure(Exception exception) { |
| 115 | + fail("Error while signing-in. " + exception.getLocalizedMessage()); |
| 116 | + signInLatch.countDown(); |
| 117 | + } |
| 118 | + }); |
| 119 | + |
| 120 | + try { |
| 121 | + signInLatch.await(60, TimeUnit.SECONDS); |
| 122 | + } catch (InterruptedException e) { |
| 123 | + e.printStackTrace(); |
| 124 | + } |
| 125 | + |
| 126 | + assertEquals(1, listSessions.size()); |
| 127 | + |
| 128 | + return listSessions.get(0); |
| 129 | + } |
| 130 | + |
| 131 | + void signOut() { |
| 132 | + getUser().signOut(); |
| 133 | + } |
| 134 | + |
| 135 | + CognitoUserPool createCognitoUserPool() { |
| 136 | + try { |
| 137 | + return new CognitoUserPool(appContext, |
| 138 | + getPackageConfigure().getString("UserPoolId"), |
| 139 | + getPackageConfigure().getString("AppClientId"), |
| 140 | + getPackageConfigure().getString("AppClientSecret")); |
| 141 | + } catch (Exception ex) { |
| 142 | + fail("Error in constructing a CognitoUserPool object."); |
| 143 | + return null; |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + public CognitoUserPool getUserPool() { |
| 148 | + return cognitoUserPool; |
| 149 | + } |
| 150 | + |
| 151 | + public CognitoUser getUser() { |
| 152 | + return cognitoUserPool.getUser(userName); |
| 153 | + } |
| 154 | + |
| 155 | + public String getUserName() { |
| 156 | + return userName; |
| 157 | + } |
| 158 | + |
| 159 | + public String getPassword() { |
| 160 | + return password; |
| 161 | + } |
| 162 | + |
| 163 | + public String getUserEmail() { |
| 164 | + return userEmail; |
| 165 | + } |
| 166 | +} |
0 commit comments