Skip to content

Commit d0c0fce

Browse files
committed
chore: fix OTP integration tests
1 parent 78cd5f6 commit d0c0fce

File tree

6 files changed

+32
-14
lines changed

6 files changed

+32
-14
lines changed

AmplifyPlugins/Auth/Tests/AuthHostApp/AuthIntegrationTests/AWSAuthBaseTest.swift

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -135,13 +135,31 @@ class AWSAuthBaseTest: XCTestCase {
135135
"""
136136

137137
/// Function to create a subscription and store OTP codes in a dictionary
138-
func subscribeToOTPCreation() {
138+
func subscribeToOTPCreation() async {
139139
subscription = Amplify.API.subscribe(request: .init(document: document, responseType: [String: JSONValue].self))
140140

141+
func waitForSubscriptionConnection(
142+
subscription: AmplifyAsyncThrowingSequence<GraphQLSubscriptionEvent<[String: JSONValue]>>
143+
) async throws {
144+
for try await subscriptionEvent in subscription {
145+
if case .connection(let subscriptionConnectionState) = subscriptionEvent {
146+
print("Subscription connect state is \(subscriptionConnectionState)")
147+
if subscriptionConnectionState == .connected {
148+
return
149+
}
150+
}
151+
}
152+
}
153+
154+
guard let subscription = subscription else { return }
155+
156+
await wait(name: "Subscription Connection Waiter", timeout: 5.0) {
157+
try await waitForSubscriptionConnection(subscription: subscription)
158+
}
159+
141160
// Create the subscription and listen for OTP code events
142161
Task {
143162
do {
144-
guard let subscription = subscription else { return }
145163
for try await subscriptionEvent in subscription {
146164
switch subscriptionEvent {
147165
case .connection(let subscriptionConnectionState):
@@ -153,7 +171,7 @@ class AWSAuthBaseTest: XCTestCase {
153171
if let eventUsername = otpResult["onCreateMfaInfo"]?.asObject?["username"]?.stringValue,
154172
let code = otpResult["onCreateMfaInfo"]?.asObject?["code"]?.stringValue {
155173
// Store the code in the dictionary for the given username
156-
usernameOTPDictionary[eventUsername] = code
174+
usernameOTPDictionary[eventUsername.lowercased()] = code
157175
}
158176
case .failure(let error):
159177
print("Got failed result with \(error.errorDescription)")
@@ -168,13 +186,14 @@ class AWSAuthBaseTest: XCTestCase {
168186

169187
/// Test that waits for the OTP code using XCTestExpectation
170188
func otp(for username: String) async throws -> String? {
189+
let lowerCasedUsername = username.lowercased()
171190
let expectation = XCTestExpectation(description: "Wait for OTP")
172191
expectation.expectedFulfillmentCount = 1
173192

174193
let task = Task { () -> String? in
175194
var code: String?
176195
for _ in 0..<30 { // Poll for the code, max 30 times (once per second)
177-
if let otp = usernameOTPDictionary[username] {
196+
if let otp = usernameOTPDictionary[lowerCasedUsername] {
178197
code = otp
179198
expectation.fulfill() // Fulfill the expectation when the value is found
180199
break
@@ -190,10 +209,9 @@ class AWSAuthBaseTest: XCTestCase {
190209
if result == .timedOut {
191210
// Task cancels if timed out
192211
task.cancel()
193-
subscription?.cancel()
194212
return nil
195213
}
196-
usernameOTPDictionary.removeValue(forKey: username)
214+
usernameOTPDictionary.removeValue(forKey: lowerCasedUsername)
197215
return try await task.value
198216
}
199217
}

AmplifyPlugins/Auth/Tests/AuthHostApp/AuthIntegrationTests/MFATests/EmailMFATests/EmailMFAOnlyTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ class EmailMFARequiredTests: AWSAuthBaseTest {
6060
func testSuccessfulEmailMFASetupStep() async {
6161
do {
6262
// Step 1: Set up a subscription to receive MFA codes
63-
subscribeToOTPCreation()
63+
await subscribeToOTPCreation()
6464

6565
// Step 2: Sign up a new user
6666
let uniqueId = UUID().uuidString
@@ -144,7 +144,7 @@ class EmailMFARequiredTests: AWSAuthBaseTest {
144144
func testSuccessfulEmailMFAWithIncorrectCodeFirstAndThenValidOne() async {
145145
do {
146146
// Step 1: Set up a subscription to receive MFA codes
147-
subscribeToOTPCreation()
147+
await subscribeToOTPCreation()
148148

149149
// Step 2: Sign up a new user
150150
let uniqueId = UUID().uuidString

AmplifyPlugins/Auth/Tests/AuthHostApp/AuthIntegrationTests/MFATests/EmailMFATests/EmailMFAWithAllMFATypesRequiredTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ class EmailMFAWithAllMFATypesRequiredTests: AWSAuthBaseTest {
9494
func testSuccessfulEmailMFACodeStep() async {
9595
do {
9696
// Step 1: Set up a subscription to receive MFA codes
97-
subscribeToOTPCreation()
97+
await subscribeToOTPCreation()
9898
let uniqueId = UUID().uuidString
9999
let username = randomEmail
100100
let password = "Pp123@\(uniqueId)"
@@ -152,7 +152,7 @@ class EmailMFAWithAllMFATypesRequiredTests: AWSAuthBaseTest {
152152
func testConfirmSignInForEmailMFASetupSelectionStep() async {
153153
do {
154154
// Step 1: Set up a subscription to receive MFA codes
155-
subscribeToOTPCreation()
155+
await subscribeToOTPCreation()
156156
let uniqueId = UUID().uuidString
157157
let username = "\(uniqueId)"
158158
let password = "Pp123@\(uniqueId)"

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class PasswordlessAutoSignInTests: AWSAuthBaseTest {
6565
///
6666
func testSuccessfulPasswordlessSignUpAndAutoSignInEndtoEnd() async throws {
6767

68-
subscribeToOTPCreation()
68+
await subscribeToOTPCreation()
6969

7070
let username = "integTest\(UUID().uuidString)"
7171
let options = AuthSignUpRequest.Options(
@@ -114,7 +114,7 @@ class PasswordlessAutoSignInTests: AWSAuthBaseTest {
114114
///
115115
func testFailureMultipleAutoSignInWithSameSession() async throws {
116116

117-
subscribeToOTPCreation()
117+
await subscribeToOTPCreation()
118118

119119
let username = "integTest\(UUID().uuidString)"
120120
let options = AuthSignUpRequest.Options(

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ class PasswordlessConfirmSignUpTests: AWSAuthBaseTest {
127127
///
128128
func testSuccessfulPasswordlessSignUpAndConfirmSignUpEndtoEnd() async throws {
129129

130-
subscribeToOTPCreation()
130+
await subscribeToOTPCreation()
131131

132132
let username = "integTest\(UUID().uuidString)"
133133
let options = AuthSignUpRequest.Options(

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class PasswordlessSignInTests: AWSAuthBaseTest {
3030
try await super.setUp()
3131
AuthSessionHelper.clearSession()
3232

33-
subscribeToOTPCreation()
33+
await subscribeToOTPCreation()
3434
}
3535

3636
override func tearDown() async throws {

0 commit comments

Comments
 (0)