@@ -18,25 +18,24 @@ package com.amplifyframework.auth.cognito
1818import android.content.Context
1919import androidx.test.core.app.ApplicationProvider
2020import com.amplifyframework.api.aws.AWSApiPlugin
21- import com.amplifyframework.api.graphql.GraphQLOperation
22- import com.amplifyframework.api.graphql.SimpleGraphQLRequest
2321import com.amplifyframework.auth.AuthUserAttribute
2422import com.amplifyframework.auth.AuthUserAttributeKey
2523import com.amplifyframework.auth.MFAType
2624import com.amplifyframework.auth.cognito.exceptions.service.CodeMismatchException
2725import com.amplifyframework.auth.cognito.test.R
26+ import com.amplifyframework.auth.cognito.testutils.blockForCode
27+ import com.amplifyframework.auth.cognito.testutils.blockUntilEstablished
28+ import com.amplifyframework.auth.cognito.testutils.createMfaSubscription
2829import com.amplifyframework.auth.options.AuthSignUpOptions
2930import com.amplifyframework.auth.result.AuthSignUpResult
3031import com.amplifyframework.auth.result.step.AuthSignInStep
3132import com.amplifyframework.core.configuration.AmplifyOutputs
3233import com.amplifyframework.core.configuration.AmplifyOutputsData
3334import com.amplifyframework.datastore.generated.model.MfaInfo
34- import com.amplifyframework.testutils.Assets
35+ import com.amplifyframework.testutils.api.SubscriptionHolder
3536import com.amplifyframework.testutils.sync.SynchronousAuth
3637import java.util.Random
3738import java.util.UUID
38- import java.util.concurrent.CountDownLatch
39- import java.util.concurrent.TimeUnit
4039import kotlin.test.assertEquals
4140import kotlin.test.assertFailsWith
4241import org.junit.After
@@ -46,15 +45,13 @@ import org.junit.Test
4645class AWSCognitoAuthPluginEmailMFATests {
4746
4847 private val password = " ${UUID .randomUUID()} BleepBloop1234!"
49- private val userName = " test${Random ().nextInt()} "
50- private val email = " $userName @amplify-swift-gamma.awsapps.com"
48+ private val username = " test${Random ().nextInt()} "
49+ private val email = " $username @amplify-swift-gamma.awsapps.com"
5150
5251 private var authPlugin = AWSCognitoAuthPlugin ()
5352 private var apiPlugin = AWSApiPlugin ()
5453 private lateinit var synchronousAuth: SynchronousAuth
55- private var subscription: GraphQLOperation <MfaInfo >? = null
56- private var mfaCode = " "
57- private var latch: CountDownLatch ? = null
54+ private lateinit var subscription: SubscriptionHolder <MfaInfo >
5855
5956 @Before
6057 fun initializePlugin () {
@@ -66,29 +63,12 @@ class AWSCognitoAuthPluginEmailMFATests {
6663 apiPlugin.configure(config, context)
6764 synchronousAuth = SynchronousAuth .delegatingTo(authPlugin)
6865
69- subscription = apiPlugin.subscribe(
70- SimpleGraphQLRequest (
71- Assets .readAsString(" create-mfa-subscription.graphql" ),
72- MfaInfo ::class .java,
73- null
74- ),
75- { println (" ====== Subscription Established ======" ) },
76- {
77- println (" ====== Received some MFA Info ======" )
78- if (it.data.username == userName) {
79- mfaCode = it.data.code
80- latch?.countDown()
81- }
82- },
83- { println (" ====== Subscription Failed $it ======" ) },
84- { }
85- )
66+ subscription = apiPlugin.createMfaSubscription()
8667 }
8768
8869 @After
8970 fun tearDown () {
90- subscription?.cancel()
91- mfaCode = " "
71+ subscription.cancel()
9272 synchronousAuth.deleteUser()
9373 }
9474
@@ -98,7 +78,7 @@ class AWSCognitoAuthPluginEmailMFATests {
9878 signUpNewUser()
9979
10080 // Step 2: Attempt to sign in with the newly created user
101- var signInResult = synchronousAuth.signIn(userName , password)
81+ var signInResult = synchronousAuth.signIn(username , password)
10282
10383 // Validation 1: Validate that the next step is MFA Setup Selection
10484 assertEquals(AuthSignInStep .CONTINUE_SIGN_IN_WITH_MFA_SETUP_SELECTION , signInResult.nextStep.signInStep)
@@ -112,15 +92,17 @@ class AWSCognitoAuthPluginEmailMFATests {
11292 // Validation 2: Validate that the next step is to input the user's email address
11393 assertEquals(AuthSignInStep .CONTINUE_SIGN_IN_WITH_EMAIL_MFA_SETUP , signInResult.nextStep.signInStep)
11494
95+ // Ensure subscription is ready to receive MFA code
96+ subscription.blockUntilEstablished()
97+
11598 // Step 4: Input the email address to send the code to then wait for the MFA code
116- latch = CountDownLatch (1 )
11799 signInResult = synchronousAuth.confirmSignIn(email)
118100
119101 // Validation 3: Validate that the next step is to confirm the emailed MFA code
120102 assertEquals(AuthSignInStep .CONFIRM_SIGN_IN_WITH_OTP , signInResult.nextStep.signInStep)
121103
122104 // Wait until the MFA code has been received
123- latch?.await( 20 , TimeUnit . SECONDS )
105+ val mfaCode = subscription.blockForCode(username )
124106
125107 // Step 5: Input the emailed MFA code for confirmation
126108 signInResult = synchronousAuth.confirmSignIn(mfaCode)
@@ -134,15 +116,17 @@ class AWSCognitoAuthPluginEmailMFATests {
134116 // Step 1: Sign up a new user with an existing email address
135117 signUpNewUser(email)
136118
119+ // Ensure subscription is ready to receive MFA code
120+ subscription.blockUntilEstablished()
121+
137122 // Step 2: Attempt to sign in with the newly created user
138- latch = CountDownLatch (1 )
139- var signInResult = synchronousAuth.signIn(userName, password)
123+ var signInResult = synchronousAuth.signIn(username, password)
140124
141125 // Validation 1: Validate that the next step is to confirm the emailed MFA code
142126 assertEquals(AuthSignInStep .CONFIRM_SIGN_IN_WITH_OTP , signInResult.nextStep.signInStep)
143127
144128 // Wait until the MFA code has been received
145- latch?.await( 20 , TimeUnit . SECONDS )
129+ val mfaCode = subscription.blockForCode(username )
146130
147131 // Step 4: Input the emailed MFA code for confirmation
148132 signInResult = synchronousAuth.confirmSignIn(mfaCode)
@@ -156,20 +140,22 @@ class AWSCognitoAuthPluginEmailMFATests {
156140 // Step 1: Sign up a new user with an existing email address
157141 signUpNewUser(email)
158142
143+ // Ensure subscription is ready to receive MFA code
144+ subscription.blockUntilEstablished()
145+
159146 // Step 2: Attempt to sign in with the newly created user
160- latch = CountDownLatch (1 )
161- var signInResult = synchronousAuth.signIn(userName, password)
147+ var signInResult = synchronousAuth.signIn(username, password)
162148
163149 // Validation 1: Validate that the next step is to confirm the emailed MFA code
164150 assertEquals(AuthSignInStep .CONFIRM_SIGN_IN_WITH_OTP , signInResult.nextStep.signInStep)
165151
166152 // Wait until the MFA code has been received
167- latch?.await( 20 , TimeUnit . SECONDS )
153+ val mfaCode = subscription.blockForCode(username )
168154
169155 // Step 4: Input the an incorrect MFA code
170156 // Validation 2: Validate that an incorrect MFA code throws a CodeMismatchException
171157 assertFailsWith<CodeMismatchException > {
172- signInResult = synchronousAuth.confirmSignIn(mfaCode.reversed())
158+ synchronousAuth.confirmSignIn(mfaCode.reversed())
173159 }
174160
175161 // Step 5: Input the correct MFA code for validation
@@ -189,6 +175,6 @@ class AWSCognitoAuthPluginEmailMFATests {
189175 .userAttributes(
190176 attributes
191177 ).build()
192- return synchronousAuth.signUp(userName , password, options)
178+ return synchronousAuth.signUp(username , password, options)
193179 }
194180}
0 commit comments