|
7 | 7 |
|
8 | 8 | import XCTest |
9 | 9 | @testable import AWSCognitoAuthPlugin |
| 10 | +@_spi(KeychainStore) import AWSPluginsCore |
10 | 11 |
|
11 | 12 | class CredentialStoreConfigurationTests: AWSAuthBaseTest { |
12 | 13 |
|
| 14 | + private let service = "com.amplify.awsCognitoAuthPlugin" |
| 15 | + private let sharedService = "com.amplify.awsCognitoAuthPluginShared" |
| 16 | + |
13 | 17 | override func setUp() async throws { |
14 | 18 | 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") |
16 | 22 | } |
17 | 23 |
|
18 | 24 | override func tearDown() async throws { |
19 | 25 | 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 | + } |
21 | 48 | } |
22 | 49 |
|
23 | 50 | /// Test successful migration of credentials when auth configuration changes |
@@ -229,12 +256,11 @@ class CredentialStoreConfigurationTests: AWSAuthBaseTest { |
229 | 256 | } |
230 | 257 |
|
231 | 258 | // When configuration don't change changed |
232 | | - UserDefaults.standard.removeObject(forKey: "amplify_secure_storage_scopes.awsCognitoAuthPlugin.isKeychainConfigured") |
233 | 259 | let newCredentialStore = AWSCognitoAuthCredentialStore(authConfiguration: initialAuthConfig) |
234 | 260 |
|
235 | 261 | // Then credentials should be nil |
236 | 262 | let credentials = try? newCredentialStore.retrieveCredential() |
237 | | - XCTAssertNil(credentials) |
| 263 | + XCTAssertNotNil(credentials) |
238 | 264 | } |
239 | 265 |
|
240 | 266 | /// Test migrating to a shared access group keeps credentials |
@@ -679,51 +705,11 @@ class CredentialStoreConfigurationTests: AWSAuthBaseTest { |
679 | 705 | } |
680 | 706 | XCTAssertNotNil(savedCredentials) |
681 | 707 |
|
682 | | - // When: Simulate fresh install by clearing UserDefaults flag |
683 | | - UserDefaults.standard.removeObject(forKey: "amplify_secure_storage_scopes.awsCognitoAuthPlugin.isKeychainConfigured") |
684 | | - |
685 | 708 | // Initialize new credential store without access group |
686 | 709 | let newCredentialStore = AWSCognitoAuthCredentialStore(authConfiguration: authConfig) |
687 | 710 |
|
688 | 711 | // Then: Non-shared keychain credentials should be cleared |
689 | 712 | 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") |
728 | 714 | } |
729 | 715 | } |
0 commit comments