/* * Copyright 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"). * You may not use this file except in compliance with the License. * A copy of the License is located at * * http://aws.amazon.com/apache2.0 * * or in the "license" file accompanying this file. This file is distributed * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either * express or implied. See the License for the specific language governing * permissions and limitations under the License. */ package featureTest.utilities import com.amplifyframework.auth.AuthException import com.amplifyframework.auth.AuthSession import com.amplifyframework.auth.AuthUser import com.amplifyframework.auth.cognito.featuretest.AuthAPI import com.amplifyframework.auth.cognito.featuretest.ExpectationShapes import com.amplifyframework.auth.cognito.featuretest.ResponseType import com.amplifyframework.auth.result.AuthResetPasswordResult import com.amplifyframework.auth.result.AuthSignInResult import com.amplifyframework.auth.result.AuthSignOutResult import com.amplifyframework.auth.result.AuthSignUpResult import com.amplifyframework.core.Action import com.amplifyframework.core.Consumer import io.mockk.CapturingSlot import io.mockk.every import io.mockk.mockk import io.mockk.slot import java.util.concurrent.CountDownLatch /** * Factory with association of results captor to top level APIs */ class APICaptorFactory( private val authApi: ExpectationShapes.Amplify, private val latch: CountDownLatch, // ToDo: Remove this param ) { companion object { val onSuccess = mapOf( AuthAPI.resetPassword to mockk>(), AuthAPI.signUp to mockk>(), AuthAPI.signIn to mockk>(), AuthAPI.deleteUser to mockk(), AuthAPI.fetchAuthSession to mockk(), AuthAPI.getCurrentUser to mockk(), AuthAPI.rememberDevice to mockk(), AuthAPI.forgetDevice to mockk() ) val onError = mockk>() val onComplete = mapOf( AuthAPI.signOut to mockk>() ) val successCaptors: MutableMap> = mutableMapOf() val completeCaptors: MutableMap> = mutableMapOf() val errorCaptor = slot() val actionCaptor = slot>().apply { captured = emptyMap() isCaptured = true } } init { successCaptors.clear() completeCaptors.clear() if (authApi.responseType == ResponseType.Success) setupOnSuccess() if (authApi.responseType == ResponseType.Complete) setupOnComplete() else setupOnError() } private fun setupOnSuccess() { when (val apiName = authApi.apiName) { AuthAPI.resetPassword -> { val resultCaptor = slot() val consumer = onSuccess[apiName] as Consumer every { consumer.accept(capture(resultCaptor)) } answers { latch.countDown() } successCaptors[apiName] = resultCaptor } AuthAPI.signUp -> { val resultCaptor = slot() val consumer = onSuccess[apiName] as Consumer every { consumer.accept(capture(resultCaptor)) } answers { latch.countDown() } successCaptors[apiName] = resultCaptor } AuthAPI.signIn -> { val resultCaptor = slot() val consumer = onSuccess[apiName] as Consumer every { consumer.accept(capture(resultCaptor)) } answers { latch.countDown() } successCaptors[apiName] = resultCaptor } AuthAPI.deleteUser -> { val consumer = onSuccess[apiName] as Action every { consumer.call() } answers { latch.countDown() } successCaptors[apiName] = actionCaptor } AuthAPI.fetchAuthSession -> { val consumer = onSuccess[apiName] as Action every { consumer.call() } answers { latch.countDown() } successCaptors[apiName] = actionCaptor } AuthAPI.getCurrentUser -> { val consumer = onSuccess[apiName] as Action every { consumer.call() } answers { latch.countDown() } successCaptors[apiName] = actionCaptor } AuthAPI.rememberDevice -> { val consumer = onSuccess[apiName] as Action every { consumer.call() } answers { latch.countDown() } successCaptors[apiName] = actionCaptor } AuthAPI.forgetDevice -> { val consumer = onSuccess[apiName] as Action every { consumer.call() } answers { latch.countDown() } successCaptors[apiName] = actionCaptor } AuthAPI.fetchDevices -> { val consumer = onSuccess[apiName] as Action every { consumer.call() } answers { latch.countDown() } successCaptors[apiName] = actionCaptor } AuthAPI.fetchUserAttributes -> { val consumer = onSuccess[apiName] as Action every { consumer.call() } answers { latch.countDown() } successCaptors[apiName] = actionCaptor } else -> throw Error("onSuccess for $authApi is not defined!") } } private fun setupOnComplete() { when (val apiName = authApi.apiName) { AuthAPI.signOut -> { val resultCaptor = slot() val consumer = onComplete[apiName] as Consumer every { consumer.accept(capture(resultCaptor)) } answers { latch.countDown() } completeCaptors[apiName] = resultCaptor } else -> throw Error("onComplete for $authApi is not defined!") } } private fun setupOnError() { every { onError.accept(capture(errorCaptor)) } answers { latch.countDown() } } }