-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: integ test auth dynamic sign in (#15)
* chore: integ test auth dynamic sign in * Update Tests/IntegrationTestApp/IntegrationTestAppTests/AuthSignInHelper.swift Co-authored-by: Di Wu <[email protected]> --------- Co-authored-by: Di Wu <[email protected]>
- Loading branch information
Showing
4 changed files
with
127 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
Tests/IntegrationTestApp/IntegrationTestAppTests/AuthSignInHelper.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// | ||
// Copyright Amazon.com Inc. or its affiliates. | ||
// All Rights Reserved. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
import Amplify | ||
import XCTest | ||
|
||
enum AuthSignInHelper { | ||
|
||
static func signOut() async { | ||
let session = try? await Amplify.Auth.fetchAuthSession() | ||
if session?.isSignedIn == true { | ||
_ = await Amplify.Auth.signOut() | ||
} | ||
} | ||
|
||
static func signUpUser( | ||
username: String, | ||
password: String, | ||
email: String, | ||
phoneNumber: String? = nil) async throws -> Bool { | ||
|
||
var userAttributes = [ | ||
AuthUserAttribute(.email, value: email) | ||
] | ||
|
||
if let phoneNumber = phoneNumber { | ||
userAttributes.append(AuthUserAttribute(.phoneNumber, value: phoneNumber)) | ||
} | ||
|
||
let options = AuthSignUpRequest.Options( | ||
userAttributes: userAttributes) | ||
let result = try await Amplify.Auth.signUp(username: username, password: password, options: options) | ||
return result.isSignUpComplete | ||
} | ||
|
||
static func signInUser(username: String, password: String) async throws -> AuthSignInResult { | ||
return try await Amplify.Auth.signIn(username: username, password: password, options: nil) | ||
} | ||
|
||
static func registerAndSignInUser( | ||
username: String, | ||
password: String, | ||
email: String, | ||
phoneNumber: String? = nil) async throws -> Bool { | ||
let signedUp = try await AuthSignInHelper.signUpUser( | ||
username: username, | ||
password: password, | ||
email: email, | ||
phoneNumber: phoneNumber) | ||
guard signedUp else { | ||
throw AuthError.invalidState("Auth sign up failed", "", nil) | ||
} | ||
let result = try await AuthSignInHelper.signInUser(username: username, password: password) | ||
return result.isSignedIn | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters