Skip to content

Commit 361ba1d

Browse files
authored
test(auth): Fixes HostedUI signIn flow and flaky test (#2456)
1 parent 6e4124c commit 361ba1d

File tree

5 files changed

+33
-18
lines changed

5 files changed

+33
-18
lines changed

AmplifyPlugins/Auth/Sources/AWSCognitoAuthPlugin/Operations/Helpers/HostedUISignInHelper.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ struct HostedUISignInHelper {
3737
continue
3838
}
3939
switch authenticationState {
40+
case .signingIn:
41+
continue
4042
case .signedIn:
4143
throw AuthError.invalidState(
4244
"There is already a user in signedIn state. SignOut the user first before calling signIn",

AmplifyPlugins/Auth/Tests/AWSCognitoAuthPluginUnitTests/Support/DefaultConfig.swift

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -256,14 +256,15 @@ struct MockCredentialStoreOperationClient: CredentialStoreStateBehavior {
256256

257257
class MockAmplifyStore: AmplifyAuthCredentialStoreBehavior {
258258
let credentialsKey = "amplifyCredentials"
259-
static var dict: [String: Data] = [:]
259+
static var dict = AtomicDictionary<String, Data>()
260260

261261
func saveCredential(_ credential: AmplifyCredentials) throws {
262-
Self.dict[credentialsKey] = try? JSONEncoder().encode(credential)
262+
let value = (try? JSONEncoder().encode(credential)) ?? Data()
263+
Self.dict.set(value: value, forKey: credentialsKey)
263264
}
264265

265266
func retrieveCredential() throws -> AmplifyCredentials {
266-
guard let data = Self.dict[credentialsKey],
267+
guard let data = Self.dict.getValue(forKey: credentialsKey),
267268
let cred = (try? JSONDecoder().decode(AmplifyCredentials.self, from: data)) else {
268269
return AmplifyCredentials.noCredentials
269270
}
@@ -279,11 +280,12 @@ class MockAmplifyStore: AmplifyAuthCredentialStoreBehavior {
279280
}
280281

281282
func saveDevice(_ deviceMetadata: DeviceMetadata, for username: String) throws {
282-
Self.dict[username] = try? JSONEncoder().encode(deviceMetadata)
283+
let value = (try? JSONEncoder().encode(deviceMetadata)) ?? Data()
284+
Self.dict.set(value: value, forKey: username)
283285
}
284286

285287
func retrieveDevice(for username: String) throws -> DeviceMetadata {
286-
guard let data = Self.dict[username],
288+
guard let data = Self.dict.getValue(forKey: username),
287289
let device = (try? JSONDecoder().decode(DeviceMetadata.self, from: data)) else {
288290
return .noData
289291
}
@@ -295,12 +297,13 @@ class MockAmplifyStore: AmplifyAuthCredentialStoreBehavior {
295297
}
296298

297299
func saveASFDevice(_ deviceId: String, for username: String) throws {
298-
Self.dict[username] = try? JSONEncoder().encode(deviceId)
300+
let value = (try? JSONEncoder().encode(deviceId)) ?? Data()
301+
Self.dict.set(value: value, forKey: username)
299302

300303
}
301304

302305
func retrieveASFDevice(for username: String) throws -> String {
303-
guard let data = Self.dict[username],
306+
guard let data = Self.dict.getValue(forKey: username),
304307
let device = (try? JSONDecoder().decode(String.self, from: data)) else {
305308
throw KeychainStoreError.itemNotFound
306309
}

AmplifyPlugins/Auth/Tests/AWSCognitoAuthPluginUnitTests/TaskTests/HostedUITests/AWSAuthHostedUISignInTests.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ class AWSAuthHostedUISignInTests: XCTestCase {
9999
}
100100

101101
@MainActor
102-
func testRestartAfterError() async {
102+
func testRestartAfterError() async throws {
103103
mockHostedUIResult = .failure(.cancelled)
104104
let errorExpectation = expectation(description: "SignIn operation should complete")
105105
do {
@@ -114,7 +114,7 @@ class AWSAuthHostedUISignInTests: XCTestCase {
114114
errorExpectation.fulfill()
115115
}
116116

117-
wait(for: [errorExpectation], timeout: networkTimeout)
117+
waitForExpectations(timeout: networkTimeout)
118118
mockHostedUIResult = .success([
119119
.init(name: "state", value: mockState),
120120
.init(name: "code", value: mockProof)
@@ -127,7 +127,7 @@ class AWSAuthHostedUISignInTests: XCTestCase {
127127
} catch {
128128
XCTFail("Should not fail with error = \(error)")
129129
}
130-
wait(for: [signInExpectation], timeout: networkTimeout)
130+
waitForExpectations(timeout: networkTimeout)
131131
}
132132

133133
@MainActor

AmplifyPlugins/Auth/Tests/AWSCognitoAuthPluginUnitTests/hierarchical-state-machine-swiftTests/StateMachineListenerTests.swift

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -123,22 +123,28 @@ class StateMachineListenerTests: XCTestCase {
123123
let task = Task {
124124
for try await _ in seq {
125125
if Task.isCancelled {
126-
notified.fulfill()
127-
break
126+
print("Cancelled")
128127
}
129128
}
130-
129+
notified.fulfill()
131130
}
132131

133-
Task {
134-
for _ in 1...100 {
135-
let event = Counter.Event(id: "test", eventType: .adjustBy(0))
132+
let task2 = Task {
133+
134+
for index in 1...100 {
135+
136+
let event = Counter.Event(id: "test", eventType: .adjustBy(index))
136137
await stateMachine.send(event)
138+
139+
if (index == 30) {
140+
task.cancel()
141+
await Task.yield()
142+
}
137143
}
138144
}
139145

140-
task.cancel()
141-
await waitForExpectations(timeout: 0.1)
146+
await waitForExpectations(timeout: 1)
147+
task2.cancel()
142148
}
143149

144150
}

AmplifyPlugins/Auth/Tests/AWSCognitoAuthPluginUnitTests/hierarchical-state-machine-swiftTests/StateMachineTests.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,19 @@ class StateMachineTests: XCTestCase {
3737
let increment = Counter.Event(id: "increment", eventType: .increment)
3838
let decrement = Counter.Event(id: "decrement", eventType: .decrement)
3939

40+
let taskCompletion = expectation(description: "Completed sending event")
41+
taskCompletion.expectedFulfillmentCount = 1_000
4042
Task {
4143
for i in 1...1_000 {
4244
if i.isMultiple(of: 2) {
4345
await testMachine.send(increment)
4446
} else {
4547
await testMachine.send(decrement)
4648
}
49+
taskCompletion.fulfill()
4750
}
4851
}
52+
await waitForExpectations(timeout: 1)
4953
let state = await testMachine.currentState
5054
XCTAssertEqual(state.value, 0)
5155
}

0 commit comments

Comments
 (0)