Skip to content

Commit

Permalink
feat(Auth): Add unit tests for TOTP (#3046)
Browse files Browse the repository at this point in the history
* feat(Auth): Adding TOTP support in Amplify Auth category

* feat(Auth): Adding TOTP related models to AWSCognitoPlugin

* feat(Auth): Adding TOTP service behaviour

* feat(Auth): Adding TOTP tasks and requests to AWSAuthCognitoPlugin

* feat(Auth): Adding TOTP state machine actions

* feat(Auth): Adding TOTP states, events, data models and resolvers

* feat(Auth): Add unit tests for TOTP

* feat(Auth): TOTP Unit test changes
  • Loading branch information
harsh62 authored Jul 4, 2023
1 parent a9094ad commit 3266230
Show file tree
Hide file tree
Showing 26 changed files with 4,293 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import Amplify
import AWSCognitoIdentity
import AWSCognitoIdentityProvider
import AWSPluginsCore

import ClientRuntime

extension AWSCognitoAuthPlugin {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ class VerifySignInChallengeTests: XCTestCase {
username: "usernameMock",
session: "mockSession",
parameters: [:])
let mockConfirmEvent = ConfirmSignInEventData(answer: "1233",
attributes: [:],
metadata: [:])
let mockConfirmEvent = ConfirmSignInEventData(
answer: "1233",
attributes: [:],
metadata: [:],
friendlyDeviceName: nil)

/// Test if valid input are given the service call is made
///
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ class AuthHubEventHandlerTests: XCTestCase {
private func configurePluginForConfirmSignInEvent() {
let initialState = AuthState.configured(
AuthenticationState.signingIn(.resolvingChallenge(
.waitingForAnswer(.testData, .apiBased(.userSRP)),
.waitingForAnswer(.testData(), .apiBased(.userSRP)),
.smsMfa,
.apiBased(.userSRP))),
AuthorizationState.sessionEstablished(.testData))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,32 @@ extension RespondToAuthChallengeOutputResponse {
challengeParameters: [:],
session: "session")
}

static func testData(
challenge: CognitoIdentityProviderClientTypes.ChallengeNameType = .smsMfa,
challengeParameters: [String: String] = [:]) -> RespondToAuthChallengeOutputResponse {
return RespondToAuthChallengeOutputResponse(
authenticationResult: nil,
challengeName: challenge,
challengeParameters: challengeParameters,
session: "session")
}

}

extension RespondToAuthChallenge {
static let testData = RespondToAuthChallenge(challenge: .smsMfa,
username: "username",
session: "session",
parameters: [:])

static func testData(
challenge: CognitoIdentityProviderClientTypes.ChallengeNameType = .smsMfa,
username: String = "username",
session: String = "session",
parameters: [String: String] = [:]) -> RespondToAuthChallenge {
RespondToAuthChallenge(
challenge: challenge,
username: username,
session: session,
parameters: parameters)
}
}

extension SignInEvent {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ enum Defaults {
"UserAgent": "aws-amplify/cli",
"Version": "0.1.0",
"IdentityManager": [
"Default": []
"Default": [String: String]()
],
"CredentialsProvider": [
"CognitoIdentity": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,15 @@ struct MockIdentityProvider: CognitoUserPoolBehavior {
typealias MockConfirmDeviceResponse = (ConfirmDeviceInput) async throws
-> ConfirmDeviceOutputResponse

typealias MockSetUserMFAPreferenceResponse = (SetUserMFAPreferenceInput) async throws
-> SetUserMFAPreferenceOutputResponse

typealias MockAssociateSoftwareTokenResponse = (AssociateSoftwareTokenInput) async throws
-> AssociateSoftwareTokenOutputResponse

typealias MockVerifySoftwareTokenResponse = (VerifySoftwareTokenInput) async throws
-> VerifySoftwareTokenOutputResponse

let mockSignUpResponse: MockSignUpResponse?
let mockRevokeTokenResponse: MockRevokeTokenResponse?
let mockInitiateAuthResponse: MockInitiateAuthResponse?
Expand All @@ -87,6 +96,9 @@ struct MockIdentityProvider: CognitoUserPoolBehavior {
let mockRememberDeviceResponse: MockRememberDeviceResponse?
let mockForgetDeviceResponse: MockForgetDeviceResponse?
let mockConfirmDeviceResponse: MockConfirmDeviceResponse?
let mockSetUserMFAPreferenceResponse: MockSetUserMFAPreferenceResponse?
let mockAssociateSoftwareTokenResponse: MockAssociateSoftwareTokenResponse?
let mockVerifySoftwareTokenResponse: MockVerifySoftwareTokenResponse?

init(
mockSignUpResponse: MockSignUpResponse? = nil,
Expand All @@ -107,7 +119,10 @@ struct MockIdentityProvider: CognitoUserPoolBehavior {
mockListDevicesOutputResponse: MockListDevicesOutputResponse? = nil,
mockRememberDeviceResponse: MockRememberDeviceResponse? = nil,
mockForgetDeviceResponse: MockForgetDeviceResponse? = nil,
mockConfirmDeviceResponse: MockConfirmDeviceResponse? = nil
mockConfirmDeviceResponse: MockConfirmDeviceResponse? = nil,
mockSetUserMFAPreferenceResponse: MockSetUserMFAPreferenceResponse? = nil,
mockAssociateSoftwareTokenResponse: MockAssociateSoftwareTokenResponse? = nil,
mockVerifySoftwareTokenResponse: MockVerifySoftwareTokenResponse? = nil
) {
self.mockSignUpResponse = mockSignUpResponse
self.mockRevokeTokenResponse = mockRevokeTokenResponse
Expand All @@ -128,6 +143,9 @@ struct MockIdentityProvider: CognitoUserPoolBehavior {
self.mockRememberDeviceResponse = mockRememberDeviceResponse
self.mockForgetDeviceResponse = mockForgetDeviceResponse
self.mockConfirmDeviceResponse = mockConfirmDeviceResponse
self.mockSetUserMFAPreferenceResponse = mockSetUserMFAPreferenceResponse
self.mockAssociateSoftwareTokenResponse = mockAssociateSoftwareTokenResponse
self.mockVerifySoftwareTokenResponse = mockVerifySoftwareTokenResponse
}

/// Throws InitiateAuthOutputError
Expand Down Expand Up @@ -213,4 +231,16 @@ struct MockIdentityProvider: CognitoUserPoolBehavior {
func confirmDevice(input: ConfirmDeviceInput) async throws -> ConfirmDeviceOutputResponse {
return try await mockConfirmDeviceResponse!(input)
}

func associateSoftwareToken(input: AWSCognitoIdentityProvider.AssociateSoftwareTokenInput) async throws -> AWSCognitoIdentityProvider.AssociateSoftwareTokenOutputResponse {
return try await mockAssociateSoftwareTokenResponse!(input)
}

func verifySoftwareToken(input: AWSCognitoIdentityProvider.VerifySoftwareTokenInput) async throws -> AWSCognitoIdentityProvider.VerifySoftwareTokenOutputResponse {
return try await mockVerifySoftwareTokenResponse!(input)
}

func setUserMFAPreference(input: SetUserMFAPreferenceInput) async throws -> SetUserMFAPreferenceOutputResponse {
return try await mockSetUserMFAPreferenceResponse!(input)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -704,7 +704,7 @@ class AWSAuthFetchSignInSessionOperationTests: BaseAuthorizationTests {
///
func testSessionWhenWaitingConfirmSignIn() async throws {
let signInMethod = SignInMethod.apiBased(.userSRP)
let challenge = SignInChallengeState.waitingForAnswer(.testData, signInMethod)
let challenge = SignInChallengeState.waitingForAnswer(.testData(), signInMethod)
let initialState = AuthState.configured(
AuthenticationState.signingIn(
.resolvingChallenge(challenge, .smsMfa, signInMethod)),
Expand Down
Loading

0 comments on commit 3266230

Please sign in to comment.