Skip to content

Commit ead7222

Browse files
committed
chore(auth): add passwordless integration test
1 parent a2dfdf0 commit ead7222

File tree

1 file changed

+89
-0
lines changed

1 file changed

+89
-0
lines changed

AmplifyPlugins/Auth/Tests/AuthHostApp/AuthIntegrationTests/PasswordlessTests/PasswordlessSignInTests.swift

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,95 @@ class PasswordlessSignInTests: AWSAuthBaseTest {
167167
}
168168
}
169169

170+
/// Test successful signIn of a valid user
171+
///
172+
/// - Given: A user registered in Cognito user pool
173+
/// - When:
174+
/// - I invoke Amplify.Auth.signIn with the username and password, using userAuth flow
175+
/// - Retry confirm sign in after a wrong password attempt is not supposed to work in `userAuth` flow. Cognito doesn't support this flow.
176+
/// - Re-initiation of sign in should work correctly after a incorrect attempt
177+
/// - Then:
178+
/// - I should get a completed signIn flow.
179+
///
180+
func testSignInWithPasswordSRP_givenValidUser_expectErrorOnWrongPassword() async throws {
181+
182+
let username = "integTest\(UUID().uuidString)"
183+
let password = "Pp123@\(UUID().uuidString)"
184+
185+
try await signUp(username: username, password: password)
186+
187+
do {
188+
let pluginOptions = AWSAuthSignInOptions(
189+
authFlowType: .userAuth)
190+
var signInResult = try await Amplify.Auth.signIn(
191+
username: username,
192+
password: password,
193+
options: .init(pluginOptions: pluginOptions))
194+
guard case .continueSignInWithFirstFactorSelection(let availableFactors) = signInResult.nextStep else {
195+
XCTFail("SignIn should return a .continueSignInWithFirstFactorSelection")
196+
return
197+
}
198+
XCTAssert(availableFactors.contains(.passwordSRP))
199+
var confirmSignInResult = try await Amplify.Auth.confirmSignIn(
200+
challengeResponse: AuthFactorType.passwordSRP.challengeResponse)
201+
202+
guard case .confirmSignInWithPassword = confirmSignInResult.nextStep else {
203+
XCTFail("ConfirmSignIn should return a .confirmSignInWithPassword")
204+
return
205+
}
206+
207+
// Try confirming with wrong password and it should fail
208+
209+
do {
210+
confirmSignInResult = try await Amplify.Auth.confirmSignIn(
211+
challengeResponse: "wrong-password")
212+
} catch {
213+
guard let error = error as? AuthError else {
214+
XCTFail("Error should be of type AuthError instead got: \(error)")
215+
return
216+
}
217+
guard case .notAuthorized = error else {
218+
XCTFail("Error should be .notAuthorized instead got: \(error)")
219+
return
220+
}
221+
}
222+
223+
// Try confirming with password again and it should fail saying that re-initiation is needed
224+
225+
do {
226+
confirmSignInResult = try await Amplify.Auth.confirmSignIn(
227+
challengeResponse: password)
228+
} catch {
229+
guard let error = error as? AuthError else {
230+
XCTFail("Error should be of type AuthError instead got: \(error)")
231+
return
232+
}
233+
guard case .invalidState = error else {
234+
XCTFail("Error should be .invalidState instead got: \(error)")
235+
return
236+
}
237+
}
238+
239+
// After all the errors re-initiation of sign in should work
240+
241+
// Sign in
242+
_ = try await Amplify.Auth.signIn(
243+
username: username,
244+
password: password,
245+
options: .init(pluginOptions: pluginOptions))
246+
// Select passwordSRP
247+
_ = try await Amplify.Auth.confirmSignIn(
248+
challengeResponse: AuthFactorType.passwordSRP.challengeResponse)
249+
// Complete sign in
250+
confirmSignInResult = try await Amplify.Auth.confirmSignIn(
251+
challengeResponse: password)
252+
253+
XCTAssertTrue(confirmSignInResult.isSignedIn, "SignIn should be complete")
254+
} catch {
255+
XCTFail("SignIn with a valid username/password should not fail \(error)")
256+
}
257+
}
258+
170259
/// Test successful signIn of a valid user
171260
///
172261
/// - Given: A user registered in Cognito user pool

0 commit comments

Comments
 (0)