Skip to content

Commit

Permalink
feat(auth): add userID property to AuthSignUpResult (#3179)
Browse files Browse the repository at this point in the history
  • Loading branch information
atierian authored Aug 30, 2023
1 parent fac13ac commit 115407a
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 6 deletions.
8 changes: 7 additions & 1 deletion Amplify/Categories/Auth/Result/AuthSignUpResult.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)] = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 115407a

Please sign in to comment.