Skip to content

Commit

Permalink
feat(auth): Added parity test for fetchDevices,rememberDevice,forgetD…
Browse files Browse the repository at this point in the history
…evice and fetchUserAttributes (#2174)

Co-authored-by: Sunil Timalsina <sunil.timalsina@gmail.com>
Co-authored-by: Banji Jolaoso <banjij@amazon.com>
Co-authored-by: Divyesh Chitroda <div5yesh@gmail.com>
Co-authored-by: Matt Creaser <mattwcc@amazon.com>
Co-authored-by: Saijad Dhuka <83975678+sdhuka@users.noreply.github.com>
Co-authored-by: Tyler Roach <tjroach@amazon.com>
Co-authored-by: Thomas Leing <bluezebragames@gmail.com>
Co-authored-by: Thomas Leing <leint@amazon.com>
Co-authored-by: gpanshu <91897496+gpanshu@users.noreply.github.com>
Co-authored-by: AWS Mobile SDK Bot <46607340+awsmobilesdk@users.noreply.github.com>
  • Loading branch information
11 people committed Feb 1, 2023
1 parent c467c4d commit 9e368fe
Show file tree
Hide file tree
Showing 20 changed files with 984 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ import com.amplifyframework.auth.cognito.featuretest.generators.authstategenerat
import com.amplifyframework.auth.cognito.featuretest.generators.testcasegenerators.ConfirmSignInTestCaseGenerator
import com.amplifyframework.auth.cognito.featuretest.generators.testcasegenerators.DeleteUserTestCaseGenerator
import com.amplifyframework.auth.cognito.featuretest.generators.testcasegenerators.FetchAuthSessionTestCaseGenerator
import com.amplifyframework.auth.cognito.featuretest.generators.testcasegenerators.FetchDevicesTestCaseGenerator
import com.amplifyframework.auth.cognito.featuretest.generators.testcasegenerators.FetchUserAttributesTestCaseGenerator
import com.amplifyframework.auth.cognito.featuretest.generators.testcasegenerators.ForgetDeviceTestCaseGenerator
import com.amplifyframework.auth.cognito.featuretest.generators.testcasegenerators.RememberDeviceTestCaseGenerator
import com.amplifyframework.auth.cognito.featuretest.generators.testcasegenerators.ResetPasswordTestCaseGenerator
import com.amplifyframework.auth.cognito.featuretest.generators.testcasegenerators.SignInTestCaseGenerator
import com.amplifyframework.auth.cognito.featuretest.generators.testcasegenerators.SignOutTestCaseGenerator
Expand All @@ -43,6 +47,10 @@ object JsonGenerator {
ConfirmSignInTestCaseGenerator,
DeleteUserTestCaseGenerator,
FetchAuthSessionTestCaseGenerator,
RememberDeviceTestCaseGenerator,
ForgetDeviceTestCaseGenerator,
FetchDevicesTestCaseGenerator,
FetchUserAttributesTestCaseGenerator,
)

fun generate() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
/*
* Copyright 2023 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 com.amplifyframework.auth.cognito.featuretest.generators.testcasegenerators

import com.amplifyframework.auth.AWSCredentials
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/*
* Copyright 2023 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 com.amplifyframework.auth.cognito.featuretest.generators.testcasegenerators

import aws.sdk.kotlin.services.cognitoidentityprovider.model.AttributeType
import aws.sdk.kotlin.services.cognitoidentityprovider.model.DeviceType
import aws.smithy.kotlin.runtime.time.Instant
import com.amplifyframework.auth.AuthDevice
import com.amplifyframework.auth.cognito.featuretest.API
import com.amplifyframework.auth.cognito.featuretest.AuthAPI
import com.amplifyframework.auth.cognito.featuretest.CognitoType
import com.amplifyframework.auth.cognito.featuretest.ExpectationShapes
import com.amplifyframework.auth.cognito.featuretest.FeatureTestCase
import com.amplifyframework.auth.cognito.featuretest.MockResponse
import com.amplifyframework.auth.cognito.featuretest.PreConditions
import com.amplifyframework.auth.cognito.featuretest.ResponseType
import com.amplifyframework.auth.cognito.featuretest.generators.SerializableProvider
import com.amplifyframework.auth.cognito.featuretest.generators.toJsonElement
import com.amplifyframework.auth.exceptions.SignedOutException
import kotlinx.serialization.json.JsonObject

object FetchDevicesTestCaseGenerator : SerializableProvider {

private val expectedSuccess = listOf<AuthDevice>(AuthDevice.fromId("deviceKey")).toJsonElement()

private val mockCognitoResponse = MockResponse(
CognitoType.CognitoIdentityProvider,
"listDevices",
ResponseType.Success,
mapOf(
"devices" to listOf<DeviceType>(
DeviceType.invoke {
deviceAttributes = listOf<AttributeType>(
AttributeType.invoke {
name = "name"
value = "value"
}
)
deviceKey = "deviceKey"
deviceCreateDate = Instant.now()
deviceLastAuthenticatedDate = Instant.now()
deviceLastModifiedDate = Instant.now()
}
)
).toJsonElement()
)

private val apiReturnValidation = ExpectationShapes.Amplify(
AuthAPI.fetchDevices,
ResponseType.Success,
expectedSuccess,
)

private val baseCase = FeatureTestCase(
description = "Test that Cognito is called with given payload and returns successful data",
preConditions = PreConditions(
"authconfiguration.json",
"SignedIn_SessionEstablished.json",
mockedResponses = listOf(mockCognitoResponse)
),
api = API(
AuthAPI.fetchDevices,
JsonObject(emptyMap()),
JsonObject(emptyMap()),
),
validations = listOf(apiReturnValidation)
)

private val successCase: FeatureTestCase = baseCase.copy(
description = "List of devices returned when fetch devices API succeeds",
preConditions = baseCase.preConditions.copy(mockedResponses = listOf(mockCognitoResponse)),
validations = baseCase.validations.plus(apiReturnValidation)
)

private val errorCase: FeatureTestCase
get() {
val errorResponse = SignedOutException()
return baseCase.copy(
description = "AuthException is thrown when forgetDevice API is called without signing in",
preConditions = baseCase.preConditions.copy(
state = "SignedOut_Configured.json",
mockedResponses = listOf(
MockResponse(
CognitoType.CognitoIdentityProvider,
"forgetDevice",
ResponseType.Failure,
errorResponse.toJsonElement()
)
)
),
validations = listOf(
ExpectationShapes.Amplify(
AuthAPI.forgetDevice,
ResponseType.Failure,
com.amplifyframework.auth.exceptions.SignedOutException().toJsonElement(),
)
)
)
}

override val serializables: List<Any> = listOf(baseCase, errorCase, successCase)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/*
* Copyright 2023 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 com.amplifyframework.auth.cognito.featuretest.generators.testcasegenerators
import aws.sdk.kotlin.services.cognitoidentityprovider.model.AttributeType
import com.amplifyframework.auth.AuthUserAttribute
import com.amplifyframework.auth.AuthUserAttributeKey
import com.amplifyframework.auth.cognito.featuretest.API
import com.amplifyframework.auth.cognito.featuretest.AuthAPI
import com.amplifyframework.auth.cognito.featuretest.CognitoType
import com.amplifyframework.auth.cognito.featuretest.ExpectationShapes
import com.amplifyframework.auth.cognito.featuretest.FeatureTestCase
import com.amplifyframework.auth.cognito.featuretest.MockResponse
import com.amplifyframework.auth.cognito.featuretest.PreConditions
import com.amplifyframework.auth.cognito.featuretest.ResponseType
import com.amplifyframework.auth.cognito.featuretest.generators.SerializableProvider
import com.amplifyframework.auth.cognito.featuretest.generators.toJsonElement
import com.amplifyframework.auth.exceptions.SignedOutException
import kotlinx.serialization.json.JsonObject

object FetchUserAttributesTestCaseGenerator : SerializableProvider {

private val expectedSuccess = listOf<AuthUserAttribute>(
AuthUserAttribute(AuthUserAttributeKey.email(), "email@email.com"),
AuthUserAttribute(AuthUserAttributeKey.phoneNumber(), "000-000-0000")
).toJsonElement()

private val mockCognitoResponse = MockResponse(
CognitoType.CognitoIdentityProvider,
"getUser",
ResponseType.Success,
mapOf(
"userAttributes" to listOf<AttributeType>(
AttributeType.invoke {
name = "email"
value = "email@email.com"
},
AttributeType.invoke {
name = "phone"
value = "000-000-0000"
}
)
).toJsonElement()
)

private val apiReturnValidation = ExpectationShapes.Amplify(
AuthAPI.fetchUserAttributes,
ResponseType.Success,
expectedSuccess,
)

private val baseCase = FeatureTestCase(
description = "Test that Cognito is called with given payload and returns successful data",
preConditions = PreConditions(
"authconfiguration.json",
"SignedIn_SessionEstablished.json",
mockedResponses = listOf(mockCognitoResponse)
),
api = API(
AuthAPI.fetchUserAttributes,
JsonObject(emptyMap()),
JsonObject(emptyMap()),
),
validations = listOf(apiReturnValidation)
)

private val successCase: FeatureTestCase = baseCase.copy(
description = "List of user attributes returned when fetch user attributes API succeeds",
preConditions = baseCase.preConditions.copy(mockedResponses = listOf(mockCognitoResponse)),
validations = baseCase.validations.plus(apiReturnValidation)
)

private val errorCase: FeatureTestCase
get() {
val errorResponse = SignedOutException()
return baseCase.copy(
description = "AuthException is thrown when fetchUserAttributes API is called without signing in",
preConditions = baseCase.preConditions.copy(
state = "SignedOut_Configured.json",
mockedResponses = listOf(
MockResponse(
CognitoType.CognitoIdentityProvider,
"getUser",
ResponseType.Failure,
errorResponse.toJsonElement()
)
)
),
validations = listOf(
ExpectationShapes.Amplify(
AuthAPI.fetchUserAttributes,
ResponseType.Failure,
SignedOutException().toJsonElement(),
)
)
)
}

override val serializables: List<Any> = listOf(baseCase, errorCase, successCase)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
* Copyright 2023 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 com.amplifyframework.auth.cognito.featuretest.generators.testcasegenerators

import com.amplifyframework.auth.AuthDevice
import com.amplifyframework.auth.cognito.featuretest.API
import com.amplifyframework.auth.cognito.featuretest.AuthAPI
import com.amplifyframework.auth.cognito.featuretest.CognitoType
import com.amplifyframework.auth.cognito.featuretest.ExpectationShapes
import com.amplifyframework.auth.cognito.featuretest.FeatureTestCase
import com.amplifyframework.auth.cognito.featuretest.MockResponse
import com.amplifyframework.auth.cognito.featuretest.PreConditions
import com.amplifyframework.auth.cognito.featuretest.ResponseType
import com.amplifyframework.auth.cognito.featuretest.generators.SerializableProvider
import com.amplifyframework.auth.cognito.featuretest.generators.toJsonElement
import com.amplifyframework.auth.exceptions.SignedOutException
import kotlinx.serialization.json.JsonObject

object ForgetDeviceTestCaseGenerator : SerializableProvider {
private val mockCognitoResponse = MockResponse(
CognitoType.CognitoIdentityProvider,
"updateDeviceStatus",
ResponseType.Success,
JsonObject(emptyMap())
)

private val apiReturnValidation = ExpectationShapes.Amplify(
AuthAPI.forgetDevice,
ResponseType.Success,
JsonObject(emptyMap()),
)

private val baseCase = FeatureTestCase(
description = "Test that Cognito is called with given payload and returns successful data",
preConditions = PreConditions(
"authconfiguration.json",
"SignedIn_SessionEstablished.json",
mockedResponses = listOf(mockCognitoResponse)
),
api = API(
AuthAPI.forgetDevice,
mapOf(
"device" to AuthDevice.fromId("id", "test")
).toJsonElement(),
JsonObject(emptyMap()),
),
validations = listOf(apiReturnValidation)
)

private val successCase: FeatureTestCase = baseCase.copy(
description = "Nothing is returned when forget device succeeds",
preConditions = baseCase.preConditions.copy(mockedResponses = listOf(mockCognitoResponse)),
validations = baseCase.validations.plus(apiReturnValidation)
)

private val errorCase: FeatureTestCase
get() {
val errorResponse = SignedOutException()
return baseCase.copy(
description = "AuthException is thrown when forgetDevice API is called without signing in",
preConditions = baseCase.preConditions.copy(
state = "SignedOut_Configured.json",
mockedResponses = listOf(
MockResponse(
CognitoType.CognitoIdentityProvider,
"forgetDevice",
ResponseType.Failure,
errorResponse.toJsonElement()
)
)
),
validations = listOf(
ExpectationShapes.Amplify(
AuthAPI.forgetDevice,
ResponseType.Failure,
SignedOutException().toJsonElement(),
)
)
)
}

override val serializables: List<Any> = listOf(baseCase, errorCase, successCase)
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
/*
* Copyright 2023 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 com.amplifyframework.auth.cognito.featuretest.generators.testcasegenerators

import aws.sdk.kotlin.services.cognitoidentityprovider.model.NotAuthorizedException
Expand Down
Loading

0 comments on commit 9e368fe

Please sign in to comment.