diff --git a/aws-auth-cognito/src/main/java/com/amplifyframework/auth/cognito/RealAWSCognitoAuthPlugin.kt b/aws-auth-cognito/src/main/java/com/amplifyframework/auth/cognito/RealAWSCognitoAuthPlugin.kt index 1a32d375ee..cc4da10521 100644 --- a/aws-auth-cognito/src/main/java/com/amplifyframework/auth/cognito/RealAWSCognitoAuthPlugin.kt +++ b/aws-auth-cognito/src/main/java/com/amplifyframework/auth/cognito/RealAWSCognitoAuthPlugin.kt @@ -263,21 +263,33 @@ internal class RealAWSCognitoAuthPlugin( ) } - val authSignUpResult = AuthSignUpResult( - false, - AuthNextSignUpStep( - AuthSignUpStep.CONFIRM_SIGN_UP_STEP, - mapOf(), - AuthCodeDeliveryDetails( - deliveryDetails?.getValue("DESTINATION") ?: "", - AuthCodeDeliveryDetails.DeliveryMedium.fromString( - deliveryDetails?.getValue("MEDIUM") - ), - deliveryDetails?.getValue("ATTRIBUTE") - ) - ), - response?.userSub - ) + val authSignUpResult = if (response?.userConfirmed == true) { + AuthSignUpResult( + true, + AuthNextSignUpStep( + AuthSignUpStep.DONE, + mapOf(), + null + ), + response.userSub + ) + } else { + AuthSignUpResult( + false, + AuthNextSignUpStep( + AuthSignUpStep.CONFIRM_SIGN_UP_STEP, + mapOf(), + AuthCodeDeliveryDetails( + deliveryDetails?.getValue("DESTINATION") ?: "", + AuthCodeDeliveryDetails.DeliveryMedium.fromString( + deliveryDetails?.getValue("MEDIUM") + ), + deliveryDetails?.getValue("ATTRIBUTE") + ) + ), + response?.userSub + ) + } onSuccess.accept(authSignUpResult) logger.verbose("SignUp Execution complete") } catch (exception: Exception) { diff --git a/aws-auth-cognito/src/test/java/com/amplifyframework/auth/cognito/featuretest/generators/testcasegenerators/SignUpTestCaseGenerator.kt b/aws-auth-cognito/src/test/java/com/amplifyframework/auth/cognito/featuretest/generators/testcasegenerators/SignUpTestCaseGenerator.kt index d1c2aed6f4..0e5b26c405 100644 --- a/aws-auth-cognito/src/test/java/com/amplifyframework/auth/cognito/featuretest/generators/testcasegenerators/SignUpTestCaseGenerator.kt +++ b/aws-auth-cognito/src/test/java/com/amplifyframework/auth/cognito/featuretest/generators/testcasegenerators/SignUpTestCaseGenerator.kt @@ -42,6 +42,12 @@ object SignUpTestCaseGenerator : SerializableProvider { "attributeName" to "attributeName" ) + private val emptyCodeDeliveryDetails = mapOf( + "destination" to "", + "deliveryMedium" to "", + "attributeName" to "" + ) + val baseCase = FeatureTestCase( description = "Test that signup invokes proper cognito request and returns success", preConditions = PreConditions( @@ -98,5 +104,45 @@ object SignUpTestCaseGenerator : SerializableProvider { ) ) - override val serializables: List = listOf(baseCase) + val signupSuccessCase = baseCase.copy( + description = "Sign up finishes if user is confirmed in the first step", + preConditions = baseCase.preConditions.copy( + mockedResponses = listOf( + MockResponse( + CognitoType.CognitoIdentityProvider, + "signUp", + ResponseType.Success, + mapOf("codeDeliveryDetails" to emptyCodeDeliveryDetails, "userConfirmed" to true).toJsonElement() + ) + ) + ), + validations = listOf( + ExpectationShapes.Cognito.CognitoIdentityProvider( + apiName = "signUp", + // see [https://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/API_SignUp.html] + request = mapOf( + "clientId" to "testAppClientId", // This should be pulled from configuration + "username" to username, + "password" to password, + "userAttributes" to listOf(mapOf("name" to "email", "value" to email)) + ).toJsonElement() + ), + ExpectationShapes.Amplify( + apiName = AuthAPI.signUp, + responseType = ResponseType.Success, + response = + AuthSignUpResult( + true, + AuthNextSignUpStep( + AuthSignUpStep.DONE, + emptyMap(), + null + ), + null + ).toJsonElement() + ) + ) + ) + + override val serializables: List = listOf(baseCase, signupSuccessCase) } diff --git a/aws-auth-cognito/src/test/java/featureTest/utilities/CognitoMockFactory.kt b/aws-auth-cognito/src/test/java/featureTest/utilities/CognitoMockFactory.kt index a3cb1f065d..de657cb440 100644 --- a/aws-auth-cognito/src/test/java/featureTest/utilities/CognitoMockFactory.kt +++ b/aws-auth-cognito/src/test/java/featureTest/utilities/CognitoMockFactory.kt @@ -42,6 +42,7 @@ import io.mockk.coEvery import kotlinx.serialization.json.Json import kotlinx.serialization.json.JsonObject import kotlinx.serialization.json.JsonPrimitive +import kotlinx.serialization.json.boolean /** * Factory to mock aws sdk's cognito API calls and responses. @@ -67,6 +68,9 @@ class CognitoMockFactory( setupError(mockResponse, responseObject) SignUpResponse.invoke { this.codeDeliveryDetails = parseCodeDeliveryDetails(responseObject) + this.userConfirmed = if (responseObject.containsKey("userConfirmed")) { + (responseObject["userConfirmed"] as? JsonPrimitive)?.boolean ?: false + } else false } } } diff --git a/aws-auth-cognito/src/test/resources/feature-test/testsuites/fetchAuthSession/AuthSession_object_is_successfully_returned_after_refresh.json b/aws-auth-cognito/src/test/resources/feature-test/testsuites/fetchAuthSession/AuthSession_object_is_successfully_returned_after_refresh.json new file mode 100644 index 0000000000..b10484548e --- /dev/null +++ b/aws-auth-cognito/src/test/resources/feature-test/testsuites/fetchAuthSession/AuthSession_object_is_successfully_returned_after_refresh.json @@ -0,0 +1,53 @@ +{ + "description": "AuthSession object is successfully returned after refresh", + "preConditions": { + "amplify-configuration": "authconfiguration.json", + "state": "SignedIn_SessionEstablished.json", + "mockedResponses": [ + { + "type": "cognitoIdentityProvider", + "apiName": "initiateAuth", + "responseType": "success", + "response": { + "authenticationResult": { + "idToken": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJ1c2VySWQiLCJ1c2VybmFtZSI6InVzZXJuYW1lIiwiZXhwIjoxNTE2MjM5MDIyLCJvcmlnaW5fanRpIjoib3JpZ2luX2p0aSJ9.Xqa-vjJe5wwwsqeRAdHf8kTBn_rYSkDn2lB7xj9Z1xU", + "accessToken": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJ1c2VySWQiLCJ1c2VybmFtZSI6InVzZXJuYW1lIiwiZXhwIjoxNTE2MjM5MDIyLCJvcmlnaW5fanRpIjoib3JpZ2luX2p0aSJ9.Xqa-vjJe5wwwsqeRAdHf8kTBn_rYSkDn2lB7xj9Z1xU", + "refreshToken": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJ1c2VySWQiLCJ1c2VybmFtZSI6InVzZXJuYW1lIiwiZXhwIjoxNTE2MjM5MDIyLCJvcmlnaW5fanRpIjoib3JpZ2luX2p0aSJ9.Xqa-vjJe5wwwsqeRAdHf8kTBn_rYSkDn2lB7xj9Z1xU", + "expiresIn": 300 + } + } + } + ] + }, + "api": { + "name": "fetchAuthSession", + "params": { + }, + "options": { + "forceRefresh": true + } + }, + "validations": [ + { + "type": "amplify", + "apiName": "fetchAuthSession", + "responseType": "success", + "response": { + "awsCredentialsResult": { + "accessKeyId": "someAccessKey", + "expiration": 2342134, + "secretAccessKey": "someSecretKey", + "sessionToken": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJ1c2VySWQiLCJ1c2VybmFtZSI6InVzZXJuYW1lIiwiZXhwIjoxNTE2MjM5MDIyLCJvcmlnaW5fanRpIjoib3JpZ2luX2p0aSJ9.Xqa-vjJe5wwwsqeRAdHf8kTBn_rYSkDn2lB7xj9Z1xU" + }, + "identityIdResult": "someIdentityId", + "isSignedIn": true, + "userPoolTokensResult": { + "accessToken": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJ1c2VySWQiLCJ1c2VybmFtZSI6InVzZXJuYW1lIiwiZXhwIjoxNTE2MjM5MDIyLCJvcmlnaW5fanRpIjoib3JpZ2luX2p0aSJ9.Xqa-vjJe5wwwsqeRAdHf8kTBn_rYSkDn2lB7xj9Z1xU", + "idToken": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJ1c2VySWQiLCJ1c2VybmFtZSI6InVzZXJuYW1lIiwiZXhwIjoxNTE2MjM5MDIyLCJvcmlnaW5fanRpIjoib3JpZ2luX2p0aSJ9.Xqa-vjJe5wwwsqeRAdHf8kTBn_rYSkDn2lB7xj9Z1xU", + "refreshToken": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJ1c2VySWQiLCJ1c2VybmFtZSI6InVzZXJuYW1lIiwiZXhwIjoxNTE2MjM5MDIyLCJvcmlnaW5fanRpIjoib3JpZ2luX2p0aSJ9.Xqa-vjJe5wwwsqeRAdHf8kTBn_rYSkDn2lB7xj9Z1xU" + }, + "userSubResult": "userId" + } + } + ] +} \ No newline at end of file diff --git a/aws-auth-cognito/src/test/resources/feature-test/testsuites/fetchAuthSession/AuthSession_object_is_successfully_returned_for_Identity_Pool.json b/aws-auth-cognito/src/test/resources/feature-test/testsuites/fetchAuthSession/AuthSession_object_is_successfully_returned_for_Identity_Pool.json new file mode 100644 index 0000000000..98d7623119 --- /dev/null +++ b/aws-auth-cognito/src/test/resources/feature-test/testsuites/fetchAuthSession/AuthSession_object_is_successfully_returned_for_Identity_Pool.json @@ -0,0 +1,45 @@ +{ + "description": "AuthSession object is successfully returned for Identity Pool", + "preConditions": { + "amplify-configuration": "authconfiguration.json", + "state": "SignedOut_IdentityPoolConfigured.json", + "mockedResponses": [ + ] + }, + "api": { + "name": "fetchAuthSession", + "params": { + }, + "options": { + } + }, + "validations": [ + { + "type": "amplify", + "apiName": "fetchAuthSession", + "responseType": "success", + "response": { + "awsCredentialsResult": { + "accessKeyId": "someAccessKey", + "expiration": 2342134, + "secretAccessKey": "someSecretKey", + "sessionToken": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJ1c2VySWQiLCJ1c2VybmFtZSI6InVzZXJuYW1lIiwiZXhwIjoxNTE2MjM5MDIyLCJvcmlnaW5fanRpIjoib3JpZ2luX2p0aSJ9.Xqa-vjJe5wwwsqeRAdHf8kTBn_rYSkDn2lB7xj9Z1xU" + }, + "identityIdResult": "someIdentityId", + "isSignedIn": false, + "userPoolTokensResult": { + "errorType": "SignedOutException", + "errorMessage": "You are currently signed out.", + "recoverySuggestion": "Please sign in and reattempt the operation.", + "cause": null + }, + "userSubResult": { + "errorType": "SignedOutException", + "errorMessage": "You are currently signed out.", + "recoverySuggestion": "Please sign in and reattempt the operation.", + "cause": null + } + } + } + ] +} \ No newline at end of file diff --git a/aws-auth-cognito/src/test/resources/feature-test/testsuites/fetchAuthSession/AuthSession_object_is_successfully_returned_for_UserAndIdentity_Pool.json b/aws-auth-cognito/src/test/resources/feature-test/testsuites/fetchAuthSession/AuthSession_object_is_successfully_returned_for_UserAndIdentity_Pool.json new file mode 100644 index 0000000000..5c840db23c --- /dev/null +++ b/aws-auth-cognito/src/test/resources/feature-test/testsuites/fetchAuthSession/AuthSession_object_is_successfully_returned_for_UserAndIdentity_Pool.json @@ -0,0 +1,39 @@ +{ + "description": "AuthSession object is successfully returned for UserAndIdentity Pool", + "preConditions": { + "amplify-configuration": "authconfiguration.json", + "state": "SignedIn_SessionEstablished.json", + "mockedResponses": [ + ] + }, + "api": { + "name": "fetchAuthSession", + "params": { + }, + "options": { + } + }, + "validations": [ + { + "type": "amplify", + "apiName": "fetchAuthSession", + "responseType": "success", + "response": { + "awsCredentialsResult": { + "accessKeyId": "someAccessKey", + "expiration": 2342134, + "secretAccessKey": "someSecretKey", + "sessionToken": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJ1c2VySWQiLCJ1c2VybmFtZSI6InVzZXJuYW1lIiwiZXhwIjoxNTE2MjM5MDIyLCJvcmlnaW5fanRpIjoib3JpZ2luX2p0aSJ9.Xqa-vjJe5wwwsqeRAdHf8kTBn_rYSkDn2lB7xj9Z1xU" + }, + "identityIdResult": "someIdentityId", + "isSignedIn": true, + "userPoolTokensResult": { + "accessToken": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJ1c2VySWQiLCJ1c2VybmFtZSI6InVzZXJuYW1lIiwiZXhwIjoxNTE2MjM5MDIyLCJvcmlnaW5fanRpIjoib3JpZ2luX2p0aSJ9.Xqa-vjJe5wwwsqeRAdHf8kTBn_rYSkDn2lB7xj9Z1xU", + "idToken": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJ1c2VySWQiLCJ1c2VybmFtZSI6InVzZXJuYW1lIiwiZXhwIjoxNTE2MjM5MDIyLCJvcmlnaW5fanRpIjoib3JpZ2luX2p0aSJ9.Xqa-vjJe5wwwsqeRAdHf8kTBn_rYSkDn2lB7xj9Z1xU", + "refreshToken": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJ1c2VySWQiLCJ1c2VybmFtZSI6InVzZXJuYW1lIiwiZXhwIjoxNTE2MjM5MDIyLCJvcmlnaW5fanRpIjoib3JpZ2luX2p0aSJ9.Xqa-vjJe5wwwsqeRAdHf8kTBn_rYSkDn2lB7xj9Z1xU" + }, + "userSubResult": "userId" + } + } + ] +} \ No newline at end of file diff --git a/aws-auth-cognito/src/test/resources/feature-test/testsuites/fetchAuthSession/AuthSession_object_is_successfully_returned_for_User_Pool.json b/aws-auth-cognito/src/test/resources/feature-test/testsuites/fetchAuthSession/AuthSession_object_is_successfully_returned_for_User_Pool.json new file mode 100644 index 0000000000..58db064009 --- /dev/null +++ b/aws-auth-cognito/src/test/resources/feature-test/testsuites/fetchAuthSession/AuthSession_object_is_successfully_returned_for_User_Pool.json @@ -0,0 +1,44 @@ +{ + "description": "AuthSession object is successfully returned for User Pool", + "preConditions": { + "amplify-configuration": "authconfiguration.json", + "state": "SignedIn_UserPoolSessionEstablished.json", + "mockedResponses": [ + ] + }, + "api": { + "name": "fetchAuthSession", + "params": { + }, + "options": { + } + }, + "validations": [ + { + "type": "amplify", + "apiName": "fetchAuthSession", + "responseType": "success", + "response": { + "awsCredentialsResult": { + "errorType": "ConfigurationException", + "errorMessage": "Could not fetch AWS Cognito credentials", + "recoverySuggestion": "Cognito Identity not configured. Please check amplifyconfiguration.json file.", + "cause": null + }, + "identityIdResult": { + "errorType": "ConfigurationException", + "errorMessage": "Could not retrieve Identity ID", + "recoverySuggestion": "Cognito Identity not configured. Please check amplifyconfiguration.json file.", + "cause": null + }, + "isSignedIn": true, + "userPoolTokensResult": { + "accessToken": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJ1c2VySWQiLCJ1c2VybmFtZSI6InVzZXJuYW1lIiwiZXhwIjoxNTE2MjM5MDIyLCJvcmlnaW5fanRpIjoib3JpZ2luX2p0aSJ9.Xqa-vjJe5wwwsqeRAdHf8kTBn_rYSkDn2lB7xj9Z1xU", + "idToken": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJ1c2VySWQiLCJ1c2VybmFtZSI6InVzZXJuYW1lIiwiZXhwIjoxNTE2MjM5MDIyLCJvcmlnaW5fanRpIjoib3JpZ2luX2p0aSJ9.Xqa-vjJe5wwwsqeRAdHf8kTBn_rYSkDn2lB7xj9Z1xU", + "refreshToken": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJ1c2VySWQiLCJ1c2VybmFtZSI6InVzZXJuYW1lIiwiZXhwIjoxNTE2MjM5MDIyLCJvcmlnaW5fanRpIjoib3JpZ2luX2p0aSJ9.Xqa-vjJe5wwwsqeRAdHf8kTBn_rYSkDn2lB7xj9Z1xU" + }, + "userSubResult": "userId" + } + } + ] +} \ No newline at end of file diff --git a/aws-auth-cognito/src/test/resources/feature-test/testsuites/signUp/Sign_up_finishes_if_user_is_confirmed_in_the_first_step.json b/aws-auth-cognito/src/test/resources/feature-test/testsuites/signUp/Sign_up_finishes_if_user_is_confirmed_in_the_first_step.json new file mode 100644 index 0000000000..156fa4a187 --- /dev/null +++ b/aws-auth-cognito/src/test/resources/feature-test/testsuites/signUp/Sign_up_finishes_if_user_is_confirmed_in_the_first_step.json @@ -0,0 +1,64 @@ +{ + "description": "Sign up finishes if user is confirmed in the first step", + "preConditions": { + "amplify-configuration": "authconfiguration.json", + "state": "SignedOut_Configured.json", + "mockedResponses": [ + { + "type": "cognitoIdentityProvider", + "apiName": "signUp", + "responseType": "success", + "response": { + "codeDeliveryDetails": { + "destination": "", + "deliveryMedium": "", + "attributeName": "" + }, + "userConfirmed": true + } + } + ] + }, + "api": { + "name": "signUp", + "params": { + "username": "user", + "password": "password" + }, + "options": { + "userAttributes": { + "email": "user@domain.com" + } + } + }, + "validations": [ + { + "type": "cognitoIdentityProvider", + "apiName": "signUp", + "request": { + "clientId": "testAppClientId", + "username": "user", + "password": "password", + "userAttributes": [ + { + "name": "email", + "value": "user@domain.com" + } + ] + } + }, + { + "type": "amplify", + "apiName": "signUp", + "responseType": "success", + "response": { + "isSignUpComplete": true, + "nextStep": { + "signUpStep": "DONE", + "additionalInfo": { + } + } + } + } + ] +} \ No newline at end of file