Skip to content

Commit 634c7fe

Browse files
committed
fix tests for GIDAuthStateMigration
1 parent 2d1e82f commit 634c7fe

1 file changed

Lines changed: 34 additions & 9 deletions

File tree

GoogleSignIn/Tests/Unit/GIDAuthStateMigrationTest.m

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,9 @@ @implementation GIDAuthStateMigrationTest {
8080
id _mockNSBundle;
8181
id _mockGIDSignInCallbackSchemes;
8282
id _mockGTMOAuth2Compatibility;
83+
#if TARGET_OS_OSX || TARGET_OS_MACCATALYST
84+
id _realLegacyGTMKeychainStore;
85+
#endif
8386
}
8487

8588
- (void)setUp {
@@ -93,6 +96,12 @@ - (void)setUp {
9396
_mockNSBundle = OCMStrictClassMock([NSBundle class]);
9497
_mockGIDSignInCallbackSchemes = OCMStrictClassMock([GIDSignInCallbackSchemes class]);
9598
_mockGTMOAuth2Compatibility = OCMStrictClassMock([GTMOAuth2Compatibility class]);
99+
#if TARGET_OS_OSX || TARGET_OS_MACCATALYST
100+
GTMKeychainAttribute *fileBasedKeychain = [GTMKeychainAttribute useFileBasedKeychain];
101+
NSSet *attributes = [NSSet setWithArray:@[fileBasedKeychain]];
102+
_realLegacyGTMKeychainStore = [[GTMKeychainStore alloc] initWithItemName:kKeychainName
103+
keychainAttributes:attributes];
104+
#endif
96105
}
97106

98107
- (void)tearDown {
@@ -112,6 +121,9 @@ - (void)tearDown {
112121
[_mockGIDSignInCallbackSchemes stopMocking];
113122
[_mockGTMOAuth2Compatibility verify];
114123
[_mockGTMOAuth2Compatibility stopMocking];
124+
#if TARGET_OS_OSX || TARGET_OS_MACCATALYST
125+
[_realLegacyGTMKeychainStore removeAuthSessionWithError:nil];
126+
#endif
115127

116128
[super tearDown];
117129
}
@@ -124,26 +136,27 @@ - (void)testMigrateIfNeeded_NoPreviousMigration {
124136
[[_mockUserDefaults expect] setBool:YES forKey:kMigrationCheckPerformedKey];
125137

126138
#if TARGET_OS_OSX || TARGET_OS_MACCATALYST
127-
[[[_mockGTMKeychainStore expect] andReturn:@"auth"] itemName];
128-
[[[_mockGTMKeychainStore expect] andReturn:_mockGTMKeychainStore] initWithItemName:OCMOCK_ANY
129-
keychainAttributes:OCMOCK_ANY];
139+
[[[_mockGTMKeychainStore expect] andReturn:kKeychainName] itemName];
130140
OIDAuthState *authState = [OIDAuthState testInstance];
131141
GTMAuthSession *authSession = [[GTMAuthSession alloc] initWithAuthState:authState];
132-
[[[_mockGTMKeychainStore expect] andReturn:authSession] retrieveAuthSessionWithError:nil];
133-
// [[[_mockGTMKeychainStore initWithKeychainStore:OCMOCK_ANY] andReturn:_mockGTMKeychainStore];
134-
// [[OCMStub([_mockGTMKeychainStore retrieveAuthSessionWithError:OCMOCK_ANY]) andReturn:(id)];
142+
NSError *err;
143+
[_realLegacyGTMKeychainStore saveAuthSession:authSession error:&err];
144+
XCTAssertNil(err);
145+
#else
146+
[self setUpCommonExtractAuthorizationMocksWithFingerPrint:kSavedFingerprint];
135147
#endif
136148

137149
[[_mockGTMKeychainStore expect] saveAuthSession:OCMOCK_ANY error:OCMArg.anyObjectRef];
138150

139-
// [self setUpCommonExtractAuthorizationMocksWithFingerPrint:kSavedFingerprint];
140-
141151
GIDAuthStateMigration *migration =
142152
[[GIDAuthStateMigration alloc] initWithKeychainStore:_mockGTMKeychainStore];
143153
[migration migrateIfNeededWithTokenURL:[NSURL URLWithString:kTokenURL]
144154
callbackPath:kCallbackPath
145155
keychainName:kKeychainName
146156
isFreshInstall:NO];
157+
#if TARGET_OS_OSX || TARGET_OS_MACCATALYST
158+
XCTAssertNil([_realLegacyGTMKeychainStore retrieveAuthSessionWithError:nil]);
159+
#endif
147160
}
148161

149162
- (void)testMigrateIfNeeded_HasPreviousMigration {
@@ -166,14 +179,26 @@ - (void)testMigrateIfNeeded_KeychainFailure {
166179
NSError *keychainSaveError = [NSError new];
167180
OCMStub([_mockGTMKeychainStore saveAuthSession:OCMOCK_ANY error:[OCMArg setTo:keychainSaveError]]);
168181

169-
[self setUpCommonExtractAuthorizationMocksWithFingerPrint:kSavedFingerprint];
182+
#if TARGET_OS_OSX || TARGET_OS_MACCATALYST
183+
[[[_mockGTMKeychainStore expect] andReturn:kKeychainName] itemName];
184+
OIDAuthState *authState = [OIDAuthState testInstance];
185+
GTMAuthSession *authSession = [[GTMAuthSession alloc] initWithAuthState:authState];
186+
NSError *err;
187+
[_realLegacyGTMKeychainStore saveAuthSession:authSession error:&err];
188+
XCTAssertNil(err);
189+
#else
190+
[self setUpCommonExtractAuthorizationMocksWithFingerPrint:kSavedFingerprint];
191+
#endif
170192

171193
GIDAuthStateMigration *migration =
172194
[[GIDAuthStateMigration alloc] initWithKeychainStore:_mockGTMKeychainStore];
173195
[migration migrateIfNeededWithTokenURL:[NSURL URLWithString:kTokenURL]
174196
callbackPath:kCallbackPath
175197
keychainName:kKeychainName
176198
isFreshInstall:NO];
199+
#if TARGET_OS_OSX || TARGET_OS_MACCATALYST
200+
XCTAssertNotNil([_realLegacyGTMKeychainStore retrieveAuthSessionWithError:nil]);
201+
#endif
177202
}
178203

179204
- (void)testMigrateIfNeeded_isFreshInstall {

0 commit comments

Comments
 (0)