4040#import " GoogleSignIn/Sources/GIDEMMErrorHandler.h"
4141#endif // TARGET_OS_IOS && !TARGET_OS_MACCATALYST
4242
43+ #import " GoogleSignIn/Sources/GIDAuthStateMigration/Fake/GIDFakeAuthStateMigration.h"
4344#import " GoogleSignIn/Tests/Unit/GIDFakeFetcher.h"
4445#import " GoogleSignIn/Tests/Unit/GIDFakeFetcherService.h"
4546#import " GoogleSignIn/Tests/Unit/GIDFakeMainBundle.h"
@@ -221,6 +222,9 @@ @interface GIDSignInTest : XCTestCase {
221222 // Whether callback block has been called.
222223 BOOL _completionCalled;
223224
225+ // Fake for |GIDAuthStateMigration|.
226+ GIDFakeAuthStateMigration *_authStateMigrationService;
227+
224228 // Fake fetcher service to emulate network requests.
225229 GIDFakeFetcherService *_fetcherService;
226230
@@ -331,6 +335,7 @@ - (void)setUp {
331335 callback: COPY_TO_ARG_BLOCK (self ->_savedTokenCallback)]);
332336
333337 // Fakes
338+ _authStateMigrationService = [[GIDFakeAuthStateMigration alloc ] init ];
334339 _fetcherService = [[GIDFakeFetcherService alloc ] init ];
335340 _fakeMainBundle = [[GIDFakeMainBundle alloc ] init ];
336341 [_fakeMainBundle startFakingWithClientID: kClientId ];
@@ -339,7 +344,8 @@ - (void)setUp {
339344 // Object under test
340345 [[NSUserDefaults standardUserDefaults ] setBool: YES forKey: kAppHasRunBeforeKey ];
341346
342- _signIn = [[GIDSignIn alloc ] initWithKeychainStore: _keychainStore];
347+ _signIn = [[GIDSignIn alloc ] initWithKeychainStore: _keychainStore
348+ authStateMigrationService: _authStateMigrationService];
343349 _hint = nil ;
344350
345351#if TARGET_OS_IOS && !TARGET_OS_MACCATALYST
@@ -398,6 +404,7 @@ - (void)testConfigureSucceeds {
398404 userDefaults: _testUserDefaults];
399405
400406 GIDSignIn *signIn = [[GIDSignIn alloc ] initWithKeychainStore: _keychainStore
407+ authStateMigrationService: _authStateMigrationService
401408 appCheck: appCheck];
402409 [signIn configureWithCompletion: ^(NSError * _Nullable error) {
403410 XCTAssertNil (error);
@@ -421,6 +428,7 @@ - (void)testConfigureFailsNoTokenOrError {
421428 userDefaults: _testUserDefaults];
422429
423430 GIDSignIn *signIn = [[GIDSignIn alloc ] initWithKeychainStore: _keychainStore
431+ authStateMigrationService: _authStateMigrationService
424432 appCheck: appCheck];
425433
426434 // Should fail if missing both token and error
@@ -439,7 +447,8 @@ - (void)testConfigureFailsNoTokenOrError {
439447- (void )testInitWithKeychainStore {
440448 GTMKeychainStore *store = [[GTMKeychainStore alloc ] initWithItemName: @" foo" ];
441449 GIDSignIn *signIn;
442- signIn = [[GIDSignIn alloc ] initWithKeychainStore: store];
450+ signIn = [[GIDSignIn alloc ] initWithKeychainStore: store
451+ authStateMigrationService: _authStateMigrationService];
443452 XCTAssertNotNil (signIn.configuration );
444453 XCTAssertEqual (signIn.configuration .clientID , kClientId );
445454 XCTAssertNil (signIn.configuration .serverClientID );
@@ -454,7 +463,8 @@ - (void)testInitWithKeychainStore_noConfig {
454463 openIDRealm: nil ];
455464 GTMKeychainStore *store = [[GTMKeychainStore alloc ] initWithItemName: @" foo" ];
456465 GIDSignIn *signIn;
457- signIn = [[GIDSignIn alloc ] initWithKeychainStore: store];
466+ signIn = [[GIDSignIn alloc ] initWithKeychainStore: store
467+ authStateMigrationService: _authStateMigrationService];
458468 XCTAssertNil (signIn.configuration );
459469}
460470
@@ -466,7 +476,8 @@ - (void)testInitWithKeychainStore_fullConfig {
466476
467477 GTMKeychainStore *store = [[GTMKeychainStore alloc ] initWithItemName: @" foo" ];
468478 GIDSignIn *signIn;
469- signIn = [[GIDSignIn alloc ] initWithKeychainStore: store];
479+ signIn = [[GIDSignIn alloc ] initWithKeychainStore: store
480+ authStateMigrationService: _authStateMigrationService];
470481 XCTAssertNotNil (signIn.configuration );
471482 XCTAssertEqual (signIn.configuration .clientID , kClientId );
472483 XCTAssertEqual (signIn.configuration .serverClientID , kServerClientId );
@@ -481,10 +492,29 @@ - (void)testInitWithKeychainStore_invalidConfig {
481492 openIDRealm: nil ];
482493 GTMKeychainStore *store = [[GTMKeychainStore alloc ] initWithItemName: @" foo" ];
483494 GIDSignIn *signIn;
484- signIn = [[GIDSignIn alloc ] initWithKeychainStore: store];
495+ signIn = [[GIDSignIn alloc ] initWithKeychainStore: store
496+ authStateMigrationService: _authStateMigrationService];
485497 XCTAssertNil (signIn.configuration );
486498}
487499
500+ - (void )testInitWithKeychainStore_attemptsMigration {
501+ NSString *expectedKeychainName = @" foo" ;
502+
503+ XCTestExpectation *expectation = [self expectationWithDescription: @" Callback should be called." ];
504+ _authStateMigrationService.migrationInvokedCallback =
505+ ^(NSURL *tokenURL, NSString *callbackPath, NSString *keychainName, BOOL isFreshInstall) {
506+ XCTAssertFalse (isFreshInstall);
507+ [expectation fulfill ];
508+ };
509+
510+ GTMKeychainStore *store = [[GTMKeychainStore alloc ] initWithItemName: expectedKeychainName];
511+ GIDSignIn *signIn = [[GIDSignIn alloc ] initWithKeychainStore: store
512+ authStateMigrationService: _authStateMigrationService];
513+
514+ XCTAssertNotNil (signIn.configuration );
515+ [self waitForExpectationsWithTimeout: 1 handler: nil ];
516+ }
517+
488518- (void )testRestorePreviousSignInNoRefresh_hasPreviousUser {
489519 [[[_authorization stub ] andReturn: _authState] authState ];
490520#if TARGET_OS_IOS && !TARGET_OS_MACCATALYST
0 commit comments