diff --git a/GoogleSignIn/Sources/GIDSignIn.m b/GoogleSignIn/Sources/GIDSignIn.m index 429c1666..f4cdf1af 100644 --- a/GoogleSignIn/Sources/GIDSignIn.m +++ b/GoogleSignIn/Sources/GIDSignIn.m @@ -202,6 +202,9 @@ - (void)restorePreviousSignInWithCompletion:(nullable void (^)(GIDGoogleUser *_N NSError *_Nullable error))completion { [self signInWithOptions:[GIDSignInInternalOptions silentOptionsWithCompletion: ^(GIDSignInResult *signInResult, NSError *error) { + if (!completion) { + return; + } if (signInResult) { completion(signInResult.user, nil); } else { diff --git a/GoogleSignIn/Tests/Unit/GIDSignInTest.m b/GoogleSignIn/Tests/Unit/GIDSignInTest.m index 7a81ede6..b778501c 100644 --- a/GoogleSignIn/Tests/Unit/GIDSignInTest.m +++ b/GoogleSignIn/Tests/Unit/GIDSignInTest.m @@ -503,6 +503,39 @@ - (void)testRestorePreviousSignInWhenSignedOut { [_authState verify]; } +- (void)testNotRestorePreviousSignInWhenSignedOutAndCompletionIsNil { + [[[_authorization expect] andReturn:_authState] authState]; + [[[_authState expect] andReturnValue:[NSNumber numberWithBool:NO]] isAuthorized]; + + [_signIn restorePreviousSignInWithCompletion:nil]; + + XCTAssertNil(_signIn.currentUser); +} + +- (void)testRestorePreviousSignInWhenCompletionIsNil { + [[[_authorization expect] andReturn:_authState] authState]; + [[[_authState expect] andReturnValue:[NSNumber numberWithBool:YES]] isAuthorized]; + + OIDTokenResponse *tokenResponse = + [OIDTokenResponse testInstanceWithIDToken:[OIDTokenResponse fatIDToken] + accessToken:kAccessToken + expiresIn:nil + refreshToken:kRefreshToken + tokenRequest:nil]; + + [[[_authState stub] andReturn:tokenResponse] lastTokenResponse]; + + // TODO: Create a real GIDGoogleUser to verify the signed in user value(#306). + [[[_user stub] andReturn:_user] alloc]; + (void)[[[_user expect] andReturn:_user] initWithAuthState:OCMOCK_ANY + profileData:OCMOCK_ANY]; + XCTAssertNil(_signIn.currentUser); + + [_signIn restorePreviousSignInWithCompletion:nil]; + + XCTAssertNotNil(_signIn.currentUser); +} + - (void)testOAuthLogin { [self OAuthLoginWithAddScopesFlow:NO authError:nil @@ -1239,7 +1272,7 @@ - (void)OAuthLoginWithAddScopesFlow:(BOOL)addScopesFlow [[[_authState expect] andReturn:tokenResponse] lastTokenResponse]; if (oldAccessToken) { #if TARGET_OS_IOS && !TARGET_OS_MACCATALYST - // Corresponds to EMM support + // Corresponds to EMM support [[[_authState expect] andReturn:authResponse] lastAuthorizationResponse]; #endif // TARGET_OS_IOS && !TARGET_OS_MACCATALYST [[[_authState expect] andReturn:tokenResponse] lastTokenResponse];