Skip to content

Commit 93310c9

Browse files
authored
chore: fix integration tests (#4130)
* fix(auth): remove is keychain configured check * Update CredentialStoreConfigurationTests.swift * Revert "Update CredentialStoreConfigurationTests.swift" This reverts commit dbb7da8. * Update CredentialStoreConfigurationTests.swift
1 parent cc3bbaf commit 93310c9

1 file changed

Lines changed: 31 additions & 45 deletions

File tree

AmplifyPlugins/Auth/Tests/AuthHostApp/AuthIntegrationTests/CredentialStore/CredentialStoreConfigurationTests.swift

Lines changed: 31 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,44 @@
77

88
import XCTest
99
@testable import AWSCognitoAuthPlugin
10+
@_spi(KeychainStore) import AWSPluginsCore
1011

1112
class CredentialStoreConfigurationTests: AWSAuthBaseTest {
1213

14+
private let service = "com.amplify.awsCognitoAuthPlugin"
15+
private let sharedService = "com.amplify.awsCognitoAuthPluginShared"
16+
1317
override func setUp() async throws {
1418
try await super.setUp()
15-
AuthSessionHelper.clearSession()
19+
clearAllKeychains()
20+
// Clear access group UserDefaults to ensure clean state for migration tests
21+
UserDefaults.standard.removeObject(forKey: "amplify_secure_storage_scopes.awsCognitoAuthPlugin.accessGroup")
1622
}
1723

1824
override func tearDown() async throws {
1925
try await super.tearDown()
20-
AuthSessionHelper.clearSession()
26+
clearAllKeychains()
27+
// Clear access group UserDefaults
28+
UserDefaults.standard.removeObject(forKey: "amplify_secure_storage_scopes.awsCognitoAuthPlugin.accessGroup")
29+
}
30+
31+
/// Clears all keychain items (both shared and non-shared) to ensure clean test state
32+
private func clearAllKeychains() {
33+
// Clear non-shared keychain
34+
let nonSharedKeychain = KeychainStore(service: service)
35+
try? nonSharedKeychain._removeAll()
36+
37+
// Clear shared keychains for all access groups used in tests
38+
#if os(watchOS)
39+
let accessGroups = [keychainAccessGroupWatch, keychainAccessGroupWatch2]
40+
#else
41+
let accessGroups = [keychainAccessGroup, keychainAccessGroup2]
42+
#endif
43+
44+
for accessGroup in accessGroups {
45+
let sharedKeychain = KeychainStore(service: sharedService, accessGroup: accessGroup)
46+
try? sharedKeychain._removeAll()
47+
}
2148
}
2249

2350
/// Test successful migration of credentials when auth configuration changes
@@ -229,12 +256,11 @@ class CredentialStoreConfigurationTests: AWSAuthBaseTest {
229256
}
230257

231258
// When configuration don't change changed
232-
UserDefaults.standard.removeObject(forKey: "amplify_secure_storage_scopes.awsCognitoAuthPlugin.isKeychainConfigured")
233259
let newCredentialStore = AWSCognitoAuthCredentialStore(authConfiguration: initialAuthConfig)
234260

235261
// Then credentials should be nil
236262
let credentials = try? newCredentialStore.retrieveCredential()
237-
XCTAssertNil(credentials)
263+
XCTAssertNotNil(credentials)
238264
}
239265

240266
/// Test migrating to a shared access group keeps credentials
@@ -679,51 +705,11 @@ class CredentialStoreConfigurationTests: AWSAuthBaseTest {
679705
}
680706
XCTAssertNotNil(savedCredentials)
681707

682-
// When: Simulate fresh install by clearing UserDefaults flag
683-
UserDefaults.standard.removeObject(forKey: "amplify_secure_storage_scopes.awsCognitoAuthPlugin.isKeychainConfigured")
684-
685708
// Initialize new credential store without access group
686709
let newCredentialStore = AWSCognitoAuthCredentialStore(authConfiguration: authConfig)
687710

688711
// Then: Non-shared keychain credentials should be cleared
689712
let retrievedCredentials = try? newCredentialStore.retrieveCredential()
690-
XCTAssertNil(retrievedCredentials, "Non-shared keychain credentials should be cleared on fresh install")
691-
}
692-
693-
/// Test that UserDefaults flag is properly set regardless of access group usage
694-
///
695-
/// - Given: Fresh UserDefaults state
696-
/// - When: Credential store is initialized with or without access group
697-
/// - Then: UserDefaults flag should be set in both cases
698-
///
699-
func testUserDefaultsFlagSetRegardlessOfAccessGroup() {
700-
let authConfig = AuthConfiguration.userPoolsAndIdentityPools(
701-
Defaults.makeDefaultUserPoolConfigData(),
702-
Defaults.makeIdentityConfigData()
703-
)
704-
let userDefaultsKey = "amplify_secure_storage_scopes.awsCognitoAuthPlugin.isKeychainConfigured"
705-
706-
// Test without access group
707-
UserDefaults.standard.removeObject(forKey: userDefaultsKey)
708-
XCTAssertFalse(UserDefaults.standard.bool(forKey: userDefaultsKey))
709-
710-
_ = AWSCognitoAuthCredentialStore(authConfiguration: authConfig)
711-
XCTAssertTrue(UserDefaults.standard.bool(forKey: userDefaultsKey))
712-
713-
// Test with access group
714-
UserDefaults.standard.removeObject(forKey: userDefaultsKey)
715-
XCTAssertFalse(UserDefaults.standard.bool(forKey: userDefaultsKey))
716-
717-
#if os(watchOS)
718-
let accessGroup = keychainAccessGroupWatch
719-
#else
720-
let accessGroup = keychainAccessGroup
721-
#endif
722-
723-
_ = AWSCognitoAuthCredentialStore(
724-
authConfiguration: authConfig,
725-
accessGroup: accessGroup
726-
)
727-
XCTAssertTrue(UserDefaults.standard.bool(forKey: userDefaultsKey))
713+
XCTAssertNotNil(retrievedCredentials, "Non-shared keychain credentials should NOT be cleared on fresh install")
728714
}
729715
}

0 commit comments

Comments
 (0)