From 01bb8dd5304b8b7122cedd2af155079393a480d2 Mon Sep 17 00:00:00 2001 From: henryhl22321 <130406709+henryhl22321@users.noreply.github.com> Date: Wed, 19 Apr 2023 15:39:53 -0700 Subject: [PATCH 1/6] Fix the restorePreviousSignIn when completion is nil --- GoogleSignIn/Sources/GIDSignIn.m | 3 +++ GoogleSignIn/Tests/Unit/GIDSignInTest.m | 12 ++++++++++++ 2 files changed, 15 insertions(+) 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..f4d99c43 100644 --- a/GoogleSignIn/Tests/Unit/GIDSignInTest.m +++ b/GoogleSignIn/Tests/Unit/GIDSignInTest.m @@ -503,6 +503,18 @@ - (void)testRestorePreviousSignInWhenSignedOut { [_authState verify]; } +- (void)testNotRestorePreviousSignWhenCompletionIsNil { + [[[_authorization expect] andReturn:_authState] authState]; + [[[_authState expect] andReturnValue:[NSNumber numberWithBool:NO]] isAuthorized]; + _completionCalled = NO; + _authError = nil; + + [_signIn restorePreviousSignInWithCompletion:nil]; + [NSThread sleepForTimeInterval:1.0]; + + XCTAssertNil(_signIn.currentUser); +} + - (void)testOAuthLogin { [self OAuthLoginWithAddScopesFlow:NO authError:nil From 7d35a42fdeab33f70a481c688c3ff9320b0af3ca Mon Sep 17 00:00:00 2001 From: henryhl22321 <130406709+henryhl22321@users.noreply.github.com> Date: Tue, 25 Apr 2023 15:30:16 -0700 Subject: [PATCH 2/6] Add restorePreviousSignIn test case when completion is nil --- GoogleSignIn/Tests/Unit/GIDSignInTest.m | 35 ++++++++++++++++++++++--- 1 file changed, 31 insertions(+), 4 deletions(-) diff --git a/GoogleSignIn/Tests/Unit/GIDSignInTest.m b/GoogleSignIn/Tests/Unit/GIDSignInTest.m index f4d99c43..d0e7684c 100644 --- a/GoogleSignIn/Tests/Unit/GIDSignInTest.m +++ b/GoogleSignIn/Tests/Unit/GIDSignInTest.m @@ -503,18 +503,45 @@ - (void)testRestorePreviousSignInWhenSignedOut { [_authState verify]; } -- (void)testNotRestorePreviousSignWhenCompletionIsNil { +- (void)testNotRestorePreviousSignInWhenSignedOutAndCompletionIsNil { [[[_authorization expect] andReturn:_authState] authState]; [[[_authState expect] andReturnValue:[NSNumber numberWithBool:NO]] isAuthorized]; - _completionCalled = NO; - _authError = nil; [_signIn restorePreviousSignInWithCompletion:nil]; - [NSThread sleepForTimeInterval:1.0]; XCTAssertNil(_signIn.currentUser); } +- (void)testRestorePreviousSignInWhenCompletionIsNil { + [[[_authorization expect] andReturn:_authState] authState]; + [[[_authState expect] andReturnValue:[NSNumber numberWithBool:YES]] isAuthorized]; + + OIDAuthorizationResponse *authResponse = + [OIDAuthorizationResponse testInstanceWithAdditionalParameters:nil + errorString:nil]; + OIDTokenResponse *tokenResponse = + [OIDTokenResponse testInstanceWithIDToken:[OIDTokenResponse fatIDToken] + accessToken:kAccessToken + expiresIn: nil + refreshToken:kRefreshToken + tokenRequest:nil]; + + [[[_authState stub] andReturn:tokenResponse] lastTokenResponse]; + + // SaveAuthCallback + __block OIDAuthState *authState; + __block GIDProfileData *profileData; + [[[_user stub] andReturn:_user] alloc]; + (void)[[[_user expect] andReturn:_user] initWithAuthState:SAVE_TO_ARG_BLOCK(authState) + profileData:SAVE_TO_ARG_BLOCK(profileData)]; + XCTAssertNil(_signIn.currentUser); + + [_signIn restorePreviousSignInWithCompletion:nil]; + + XCTAssertNotNil(_signIn.currentUser); + [_authState verify]; +} + - (void)testOAuthLogin { [self OAuthLoginWithAddScopesFlow:NO authError:nil From b6210ac63bd11ed113e8796a114a2567011df13f Mon Sep 17 00:00:00 2001 From: henryhl22321 <130406709+henryhl22321@users.noreply.github.com> Date: Thu, 27 Apr 2023 00:03:51 -0700 Subject: [PATCH 3/6] Fixed the test problem from comments. --- GoogleSignIn/Tests/Unit/GIDSignInTest.m | 22 ++++++++-------------- 1 file changed, 8 insertions(+), 14 deletions(-) diff --git a/GoogleSignIn/Tests/Unit/GIDSignInTest.m b/GoogleSignIn/Tests/Unit/GIDSignInTest.m index d0e7684c..fd93704d 100644 --- a/GoogleSignIn/Tests/Unit/GIDSignInTest.m +++ b/GoogleSignIn/Tests/Unit/GIDSignInTest.m @@ -516,24 +516,18 @@ - (void)testRestorePreviousSignInWhenCompletionIsNil { [[[_authorization expect] andReturn:_authState] authState]; [[[_authState expect] andReturnValue:[NSNumber numberWithBool:YES]] isAuthorized]; - OIDAuthorizationResponse *authResponse = - [OIDAuthorizationResponse testInstanceWithAdditionalParameters:nil - errorString:nil]; OIDTokenResponse *tokenResponse = - [OIDTokenResponse testInstanceWithIDToken:[OIDTokenResponse fatIDToken] - accessToken:kAccessToken - expiresIn: nil - refreshToken:kRefreshToken - tokenRequest:nil]; + [OIDTokenResponse testInstanceWithIDToken:[OIDTokenResponse fatIDToken] + accessToken:kAccessToken + expiresIn:nil + refreshToken:kRefreshToken + tokenRequest:nil]; [[[_authState stub] andReturn:tokenResponse] lastTokenResponse]; - // SaveAuthCallback - __block OIDAuthState *authState; - __block GIDProfileData *profileData; [[[_user stub] andReturn:_user] alloc]; - (void)[[[_user expect] andReturn:_user] initWithAuthState:SAVE_TO_ARG_BLOCK(authState) - profileData:SAVE_TO_ARG_BLOCK(profileData)]; + (void)[[[_user expect] andReturn:_user] initWithAuthState:OCMOCK_ANY + profileData:OCMOCK_ANY]; XCTAssertNil(_signIn.currentUser); [_signIn restorePreviousSignInWithCompletion:nil]; @@ -1278,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]; From bbb0cd4b2188eb5f74725b13d0e8b88a16ebfb3d Mon Sep 17 00:00:00 2001 From: henryhl22321 <130406709+henryhl22321@users.noreply.github.com> Date: Fri, 28 Apr 2023 16:33:24 -0700 Subject: [PATCH 4/6] Add the value to verify the _signIn.currentUser --- GoogleSignIn/Tests/Unit/GIDSignInTest.m | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/GoogleSignIn/Tests/Unit/GIDSignInTest.m b/GoogleSignIn/Tests/Unit/GIDSignInTest.m index fd93704d..b65464ea 100644 --- a/GoogleSignIn/Tests/Unit/GIDSignInTest.m +++ b/GoogleSignIn/Tests/Unit/GIDSignInTest.m @@ -517,14 +517,19 @@ - (void)testRestorePreviousSignInWhenCompletionIsNil { [[[_authState expect] andReturnValue:[NSNumber numberWithBool:YES]] isAuthorized]; OIDTokenResponse *tokenResponse = - [OIDTokenResponse testInstanceWithIDToken:[OIDTokenResponse fatIDToken] - accessToken:kAccessToken - expiresIn:nil - refreshToken:kRefreshToken - tokenRequest:nil]; + [OIDTokenResponse testInstanceWithIDToken:[OIDTokenResponse fatIDToken] + accessToken:kAccessToken + expiresIn:nil + refreshToken:kRefreshToken + tokenRequest:nil]; [[[_authState stub] andReturn:tokenResponse] lastTokenResponse]; + id profile = OCMStrictClassMock([GIDProfileData class]); + OCMStub([profile email]).andReturn(kUserEmail); + OCMStub([_user profile]).andReturn(profile); + OCMStub([_user userID]).andReturn(kUserID); + [[[_user stub] andReturn:_user] alloc]; (void)[[[_user expect] andReturn:_user] initWithAuthState:OCMOCK_ANY profileData:OCMOCK_ANY]; @@ -533,6 +538,8 @@ - (void)testRestorePreviousSignInWhenCompletionIsNil { [_signIn restorePreviousSignInWithCompletion:nil]; XCTAssertNotNil(_signIn.currentUser); + XCTAssertEqualObjects(_signIn.currentUser.userID, kUserID); + XCTAssertEqualObjects(_signIn.currentUser.profile.email, kUserEmail); [_authState verify]; } From fa25c23e202edc5fb879a8b866866b960165d431 Mon Sep 17 00:00:00 2001 From: henryhl22321 <130406709+henryhl22321@users.noreply.github.com> Date: Tue, 2 May 2023 14:21:22 -0700 Subject: [PATCH 5/6] Removed the mocked user data comparison, add TODO for using the real GIDGoogleUser on GIDSignInTest --- GoogleSignIn/Tests/Unit/GIDSignInTest.m | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/GoogleSignIn/Tests/Unit/GIDSignInTest.m b/GoogleSignIn/Tests/Unit/GIDSignInTest.m index b65464ea..539f13ea 100644 --- a/GoogleSignIn/Tests/Unit/GIDSignInTest.m +++ b/GoogleSignIn/Tests/Unit/GIDSignInTest.m @@ -38,6 +38,9 @@ #import "GoogleSignIn/Tests/Unit/GIDFakeFetcher.h" #import "GoogleSignIn/Tests/Unit/GIDFakeFetcherService.h" #import "GoogleSignIn/Tests/Unit/GIDFakeMainBundle.h" +#import "GoogleSignIn/Tests/Unit/GIDGoogleUser+Testing.h" +#import "GoogleSignIn/Tests/Unit/GIDProfileData+Testing.h" +#import "GoogleSignIn/Tests/Unit/OIDAuthState+Testing.h" #import "GoogleSignIn/Tests/Unit/OIDAuthorizationResponse+Testing.h" #import "GoogleSignIn/Tests/Unit/OIDTokenResponse+Testing.h" @@ -525,11 +528,7 @@ - (void)testRestorePreviousSignInWhenCompletionIsNil { [[[_authState stub] andReturn:tokenResponse] lastTokenResponse]; - id profile = OCMStrictClassMock([GIDProfileData class]); - OCMStub([profile email]).andReturn(kUserEmail); - OCMStub([_user profile]).andReturn(profile); - OCMStub([_user userID]).andReturn(kUserID); - + // 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]; @@ -538,9 +537,6 @@ - (void)testRestorePreviousSignInWhenCompletionIsNil { [_signIn restorePreviousSignInWithCompletion:nil]; XCTAssertNotNil(_signIn.currentUser); - XCTAssertEqualObjects(_signIn.currentUser.userID, kUserID); - XCTAssertEqualObjects(_signIn.currentUser.profile.email, kUserEmail); - [_authState verify]; } - (void)testOAuthLogin { From a8ce59e809aa3e6ac7b5833d6b182224211ed8ca Mon Sep 17 00:00:00 2001 From: henryhl22321 <130406709+henryhl22321@users.noreply.github.com> Date: Tue, 2 May 2023 15:38:33 -0700 Subject: [PATCH 6/6] Removed the unused import --- GoogleSignIn/Tests/Unit/GIDSignInTest.m | 3 --- 1 file changed, 3 deletions(-) diff --git a/GoogleSignIn/Tests/Unit/GIDSignInTest.m b/GoogleSignIn/Tests/Unit/GIDSignInTest.m index 539f13ea..b778501c 100644 --- a/GoogleSignIn/Tests/Unit/GIDSignInTest.m +++ b/GoogleSignIn/Tests/Unit/GIDSignInTest.m @@ -38,9 +38,6 @@ #import "GoogleSignIn/Tests/Unit/GIDFakeFetcher.h" #import "GoogleSignIn/Tests/Unit/GIDFakeFetcherService.h" #import "GoogleSignIn/Tests/Unit/GIDFakeMainBundle.h" -#import "GoogleSignIn/Tests/Unit/GIDGoogleUser+Testing.h" -#import "GoogleSignIn/Tests/Unit/GIDProfileData+Testing.h" -#import "GoogleSignIn/Tests/Unit/OIDAuthState+Testing.h" #import "GoogleSignIn/Tests/Unit/OIDAuthorizationResponse+Testing.h" #import "GoogleSignIn/Tests/Unit/OIDTokenResponse+Testing.h"