From ac1a26632fd7dad74273afb78cf514b9cfcd5c5d Mon Sep 17 00:00:00 2001 From: Ian Saultz <52051793+atierian@users.noreply.github.com> Date: Wed, 30 Aug 2023 16:14:50 -0400 Subject: [PATCH] feat(auth): add userID property to AuthSignUpResult (#3192) --- .../Auth/Result/AuthSignUpResult.swift | 8 ++- .../Helpers/SignUpOutputResponse+Helper.swift | 5 +- .../Task/AWSAuthConfirmSignUpTask.swift | 2 +- .../AWSAuthConfirmSignUpTaskTests.swift | 2 +- .../SignUp/AWSAuthSignUpAPITests.swift | 59 +++++++++++++++++++ .../SignUp/AWSAuthSignUpTaskTests.swift | 2 +- 6 files changed, 72 insertions(+), 6 deletions(-) diff --git a/Amplify/Categories/Auth/Result/AuthSignUpResult.swift b/Amplify/Categories/Auth/Result/AuthSignUpResult.swift index 88853771b6..012856908a 100644 --- a/Amplify/Categories/Auth/Result/AuthSignUpResult.swift +++ b/Amplify/Categories/Auth/Result/AuthSignUpResult.swift @@ -23,7 +23,13 @@ public struct AuthSignUpResult { /// public let nextStep: AuthSignUpStep - public init(_ nextStep: AuthSignUpStep) { + public let userID: String? + + public init( + _ nextStep: AuthSignUpStep, + userID: String? = nil + ) { self.nextStep = nextStep + self.userID = userID } } diff --git a/AmplifyPlugins/Auth/Sources/AWSCognitoAuthPlugin/Service/Helpers/SignUpOutputResponse+Helper.swift b/AmplifyPlugins/Auth/Sources/AWSCognitoAuthPlugin/Service/Helpers/SignUpOutputResponse+Helper.swift index c9706e53a0..c6c4742008 100644 --- a/AmplifyPlugins/Auth/Sources/AWSCognitoAuthPlugin/Service/Helpers/SignUpOutputResponse+Helper.swift +++ b/AmplifyPlugins/Auth/Sources/AWSCognitoAuthPlugin/Service/Helpers/SignUpOutputResponse+Helper.swift @@ -13,14 +13,15 @@ extension SignUpOutputResponse { var authResponse: AuthSignUpResult { if self.userConfirmed { - return .init(.done) + return .init(.done, userID: userSub) } return AuthSignUpResult( .confirmUser( codeDeliveryDetails?.toAuthCodeDeliveryDetails(), nil, userSub - ) + ), + userID: userSub ) } } diff --git a/AmplifyPlugins/Auth/Sources/AWSCognitoAuthPlugin/Task/AWSAuthConfirmSignUpTask.swift b/AmplifyPlugins/Auth/Sources/AWSCognitoAuthPlugin/Task/AWSAuthConfirmSignUpTask.swift index f33017336c..db0148eea5 100644 --- a/AmplifyPlugins/Auth/Sources/AWSCognitoAuthPlugin/Task/AWSAuthConfirmSignUpTask.swift +++ b/AmplifyPlugins/Auth/Sources/AWSCognitoAuthPlugin/Task/AWSAuthConfirmSignUpTask.swift @@ -41,7 +41,7 @@ class AWSAuthConfirmSignUpTask: AuthConfirmSignUpTask, DefaultLogger { environment: userPoolEnvironment) _ = try await client.confirmSignUp(input: input) log.verbose("Received success") - return AuthSignUpResult(.done) + return AuthSignUpResult(.done, userID: nil) } catch let error as AuthError { throw error } catch let error as AuthErrorConvertible { diff --git a/AmplifyPlugins/Auth/Tests/AWSCognitoAuthPluginUnitTests/TaskTests/ClientBehaviorTests/SignUp/AWSAuthConfirmSignUpTaskTests.swift b/AmplifyPlugins/Auth/Tests/AWSCognitoAuthPluginUnitTests/TaskTests/ClientBehaviorTests/SignUp/AWSAuthConfirmSignUpTaskTests.swift index f4a91a521d..9236e24c98 100644 --- a/AmplifyPlugins/Auth/Tests/AWSCognitoAuthPluginUnitTests/TaskTests/ClientBehaviorTests/SignUp/AWSAuthConfirmSignUpTaskTests.swift +++ b/AmplifyPlugins/Auth/Tests/AWSCognitoAuthPluginUnitTests/TaskTests/ClientBehaviorTests/SignUp/AWSAuthConfirmSignUpTaskTests.swift @@ -45,7 +45,7 @@ class AWSAuthConfirmSignUpTaskTests: XCTestCase { let task = AWSAuthConfirmSignUpTask(request, authEnvironment: authEnvironment) let confirmSignUpResult = try await task.value print("Confirm Sign Up Result: \(confirmSignUpResult)") - wait(for: [functionExpectation], timeout: 1) + await fulfillment(of: [functionExpectation], timeout: 1) } func testConfirmSignUpOperationFailure() async throws { diff --git a/AmplifyPlugins/Auth/Tests/AWSCognitoAuthPluginUnitTests/TaskTests/ClientBehaviorTests/SignUp/AWSAuthSignUpAPITests.swift b/AmplifyPlugins/Auth/Tests/AWSCognitoAuthPluginUnitTests/TaskTests/ClientBehaviorTests/SignUp/AWSAuthSignUpAPITests.swift index c43745984b..b0f514aae8 100644 --- a/AmplifyPlugins/Auth/Tests/AWSCognitoAuthPluginUnitTests/TaskTests/ClientBehaviorTests/SignUp/AWSAuthSignUpAPITests.swift +++ b/AmplifyPlugins/Auth/Tests/AWSCognitoAuthPluginUnitTests/TaskTests/ClientBehaviorTests/SignUp/AWSAuthSignUpAPITests.swift @@ -126,6 +126,65 @@ class AWSAuthSignUpAPITests: BasePluginTest { XCTAssertFalse(result.isSignUpComplete, "Signin result should be complete") } + /// Given: A response from Cognito SignUp when `userConfirmed == true` and a present `userSub` + /// When: Invoking `signUp(username:password:options:)` + /// Then: The caller should receive an `AuthSignUpResult` where `nextStep == .done` and + /// `userID` is the `userSub` returned by the service. + func test_signUp_done_withUserSub() async throws { + let sub = UUID().uuidString + mockIdentityProvider = MockIdentityProvider( + mockSignUpResponse: { _ in + return .init( + codeDeliveryDetails: nil, + userConfirmed: true, + userSub: sub + ) + } + ) + + let result = try await plugin.signUp( + username: "foo", + password: "bar", + options: nil + ) + + XCTAssertEqual(result.nextStep, .done) + XCTAssertEqual(result.userID, sub) + XCTAssertTrue(result.isSignUpComplete) + } + + /// Given: A response from Cognito SignUp that includes `codeDeliveryDetails` where `userConfirmed == false` + /// When: Invoking `signUp(username:password:options:)` + /// Then: The caller should receive an `AuthSignUpResult` where `nextStep == .confirmUser` and + /// the applicable associated value of that case and the `userID` both equal the `userSub` returned by the service. + func test_signUp_confirmUser_userIDsMatch() async throws { + let sub = UUID().uuidString + mockIdentityProvider = MockIdentityProvider( + mockSignUpResponse: { _ in + return .init( + codeDeliveryDetails: .init( + attributeName: "some attribute", + deliveryMedium: .email, + destination: "" + ), + userConfirmed: false, + userSub: sub + ) + } + ) + + let result = try await plugin.signUp( + username: "foo", + password: "bar", + options: nil + ) + + guard case .confirmUser(_, _, let userID) = result.nextStep else { + return XCTFail("expected .confirmUser nextStep") + } + XCTAssertEqual(result.userID, userID) + } + func testSignUpServiceError() async { let errorsToTest: [(signUpOutputError: SignUpOutputError, cognitoError: AWSCognitoAuthError)] = [ diff --git a/AmplifyPlugins/Auth/Tests/AWSCognitoAuthPluginUnitTests/TaskTests/ClientBehaviorTests/SignUp/AWSAuthSignUpTaskTests.swift b/AmplifyPlugins/Auth/Tests/AWSCognitoAuthPluginUnitTests/TaskTests/ClientBehaviorTests/SignUp/AWSAuthSignUpTaskTests.swift index b739ca8ef6..4368f834f5 100644 --- a/AmplifyPlugins/Auth/Tests/AWSCognitoAuthPluginUnitTests/TaskTests/ClientBehaviorTests/SignUp/AWSAuthSignUpTaskTests.swift +++ b/AmplifyPlugins/Auth/Tests/AWSCognitoAuthPluginUnitTests/TaskTests/ClientBehaviorTests/SignUp/AWSAuthSignUpTaskTests.swift @@ -41,7 +41,7 @@ class AWSAuthSignUpTaskTests: XCTestCase { let task = AWSAuthSignUpTask(request, authEnvironment: authEnvironment) let signUpResult = try await task.value print("Sign Up Result: \(signUpResult)") - wait(for: [functionExpectation], timeout: 1) + await fulfillment(of: [functionExpectation], timeout: 1) } /// Given: Configured AuthState machine