Bläddra i källkod

use fakeOIDAuthState and fakeTokenResponse in GIDSignInTest

This is for the record. It is too difficult to replace mock in GIDSignInTest because of the deep couple with implementation.

The best option is to keep this work and rewrite it in the end.
pinlu 3 år sedan
förälder
incheckning
2cf33315da
1 ändrade filer med 198 tillägg och 186 borttagningar
  1. 198 186
      GoogleSignIn/Tests/Unit/GIDSignInTest.m

+ 198 - 186
GoogleSignIn/Tests/Unit/GIDSignInTest.m

@@ -40,6 +40,7 @@
 #import "GoogleSignIn/Tests/Unit/GIDFakeFetcherService.h"
 #import "GoogleSignIn/Tests/Unit/GIDFakeMainBundle.h"
 #import "GoogleSignIn/Tests/Unit/OIDAuthorizationResponse+Testing.h"
+#import "GoogleSignIn/Tests/Unit/OIDAuthState+Testing.h"
 #import "GoogleSignIn/Tests/Unit/OIDTokenResponse+Testing.h"
 
 #ifdef SWIFT_PACKAGE
@@ -183,10 +184,15 @@ static NSString *const kNewScope = @"newScope";
   BOOL _isEligibleForEMM;
 
   // Mock |OIDAuthState|.
-  id _authState;
+//  id _authState;
 
   // Mock |OIDTokenResponse|.
-  id _tokenResponse;
+//  id _tokenResponse;
+  
+  // Maybe not necessay. See what's happening.
+  OIDTokenResponse *_fakeResponse;
+  
+  OIDAuthState *_fakeAuthState;
 
   // Mock |OIDTokenRequest|.
   id _tokenRequest;
@@ -275,10 +281,20 @@ static NSString *const kNewScope = @"newScope";
 #elif TARGET_OS_OSX
   _presentingWindow = OCMStrictClassMock([NSWindow class]);
 #endif // TARGET_OS_IOS || TARGET_OS_MACCATALYST
-  _authState = OCMStrictClassMock([OIDAuthState class]);
-  OCMStub([_authState alloc]).andReturn(_authState);
-  OCMStub([_authState initWithAuthorizationResponse:OCMOCK_ANY]).andReturn(_authState);
-  _tokenResponse = OCMStrictClassMock([OIDTokenResponse class]);
+//  _authState = OCMStrictClassMock([OIDAuthState class]);
+//  OCMStub([_authState alloc]).andReturn(_authState);
+//  OCMStub([_authState initWithAuthorizationResponse:OCMOCK_ANY]).andReturn(_authState);
+  
+  // Use fake 
+  _fakeResponse = [OIDTokenResponse testInstanceWithIDToken:kFakeIDToken
+                                                accessToken:kAccessToken
+                                                  expiresIn:nil
+                                               refreshToken:nil
+                                               tokenRequest:nil];
+  
+  _fakeAuthState = [OIDAuthState testInstanceWithTokenResponse:_fakeResponse];
+  
+//  _tokenResponse = OCMStrictClassMock([OIDTokenResponse class]);
   _tokenRequest = OCMStrictClassMock([OIDTokenRequest class]);
   _authorization = OCMStrictClassMock([GTMAppAuthFetcherAuthorization class]);
   OCMStub([_authorization alloc]).andReturn(_authorization);
@@ -324,8 +340,8 @@ static NSString *const kNewScope = @"newScope";
 }
 
 - (void)tearDown {
-  OCMVerifyAll(_authState);
-  OCMVerifyAll(_tokenResponse);
+//  OCMVerifyAll(_authState);
+//  OCMVerifyAll(_tokenResponse);
   OCMVerifyAll(_tokenRequest);
   OCMVerifyAll(_user);
   OCMVerifyAll(_oidAuthorizationService);
@@ -389,35 +405,31 @@ static NSString *const kNewScope = @"newScope";
 }
 
 - (void)testRestorePreviousSignInNoRefresh_hasPreviousUser {
-  [[[_authorization stub] andReturn:_authState] authState];
+  [[[_authorization stub] andReturn:_fakeAuthState] authState];
   [[_authorization expect] setTokenRefreshDelegate:OCMOCK_ANY];
-  OCMStub([_authState lastTokenResponse]).andReturn(_tokenResponse);
-  OCMStub([_authState refreshToken]).andReturn(kRefreshToken);
-  [[_authState expect] setStateChangeDelegate:OCMOCK_ANY];
-  [_keychainHandler saveAuthState:_authState];
+//  OCMStub([_authState lastTokenResponse]).andReturn(fakeResponse);
+//  OCMStub([_authState refreshToken]).andReturn(kRefreshToken);
+//  [[_authState expect] setStateChangeDelegate:OCMOCK_ANY];
+  
+  [_keychainHandler saveAuthState:_fakeAuthState];
 
   id idTokenDecoded = OCMClassMock([OIDIDToken class]);
   OCMStub([idTokenDecoded alloc]).andReturn(idTokenDecoded);
   OCMStub([idTokenDecoded initWithIDTokenString:OCMOCK_ANY]).andReturn(idTokenDecoded);
   OCMStub([idTokenDecoded subject]).andReturn(kFakeGaiaID);
   
-  // Mock generating a GIDConfiguration when initializing GIDGoogleUser.
-  OIDAuthorizationResponse *authResponse =
-      [OIDAuthorizationResponse testInstanceWithAdditionalParameters:nil
-                                                         errorString:nil];
+//  OCMStub([_tokenResponse idToken]).andReturn(kFakeIDToken);
+////  OCMStub([_tokenResponse request]).andReturn(_tokenRequest);
+//  OCMStub([_tokenResponse accessToken]).andReturn(kAccessToken);
+//  OCMStub([_tokenResponse accessTokenExpirationDate]).andReturn(nil);
   
-  OCMStub([_authState lastAuthorizationResponse]).andReturn(authResponse);
-  OCMStub([_tokenResponse idToken]).andReturn(kFakeIDToken);
-  OCMStub([_tokenResponse request]).andReturn(_tokenRequest);
-  OCMStub([_tokenRequest additionalParameters]).andReturn(nil);
-  OCMStub([_tokenResponse accessToken]).andReturn(kAccessToken);
-  OCMStub([_tokenResponse accessTokenExpirationDate]).andReturn(nil);
+  //  OCMStub([_tokenRequest additionalParameters]).andReturn(nil);
   
   [_signIn restorePreviousSignInNoRefresh];
 
-  [_authState verify];
+//  [_authState verify];
   [_authorization verify];
-  [_tokenResponse verify];
+//  [_tokenResponse verify];
   [_tokenRequest verify];
   [idTokenDecoded verify];
   XCTAssertEqual(_signIn.currentUser.userID, kFakeGaiaID);
@@ -432,46 +444,46 @@ static NSString *const kNewScope = @"newScope";
   XCTAssertNil(_signIn.currentUser);
 }
 
-- (void)testHasPreviousSignIn_HasBeenAuthenticated {
-  [_keychainHandler saveAuthState:_authState];
-  [[[_authState expect] andReturnValue:[NSNumber numberWithBool:YES]] isAuthorized];
-  XCTAssertTrue([_signIn hasPreviousSignIn], @"should return |YES|");
-  [_authState verify];
-  XCTAssertFalse(_completionCalled, @"should not call delegate");
-  XCTAssertNil(_authError, @"should have no error");
-}
-
-- (void)testHasPreviousSignIn_HasNotBeenAuthenticated {
-  [_keychainHandler saveAuthState:_authState];
-  [[[_authState expect] andReturnValue:[NSNumber numberWithBool:NO]] isAuthorized];
-  XCTAssertFalse([_signIn hasPreviousSignIn], @"should return |NO|");
-  [_authState verify];
-  XCTAssertFalse(_completionCalled, @"should not call delegate");
-}
-
-- (void)testRestorePreviousSignInWhenSignedOut {
-  [_keychainHandler saveAuthState:_authState];
-  [[[_authState expect] andReturnValue:[NSNumber numberWithBool:NO]] isAuthorized];
-  _completionCalled = NO;
-  _authError = nil;
-
-  XCTestExpectation *expectation = [self expectationWithDescription:@"Callback should be called."];
-
-  [_signIn restorePreviousSignInWithCompletion:^(GIDGoogleUser *_Nullable user,
-                                                 NSError * _Nullable error) {
-    [expectation fulfill];
-    XCTAssertNotNil(error, @"error should not have been nil");
-    XCTAssertEqual(error.domain,
-                   kGIDSignInErrorDomain,
-                   @"error domain should have been the sign-in error domain.");
-    XCTAssertEqual(error.code,
-                   kGIDSignInErrorCodeHasNoAuthInKeychain,
-                   @"error code should have been the 'NoAuthInKeychain' error code.");
-  }];
-
-  [self waitForExpectationsWithTimeout:1 handler:nil];
-  [_authState verify];
-}
+//- (void)testHasPreviousSignIn_HasBeenAuthenticated {
+//  [_keychainHandler saveAuthState:_fakeAuthState];
+//  [[[_authState expect] andReturnValue:[NSNumber numberWithBool:YES]] isAuthorized];
+//  XCTAssertTrue([_signIn hasPreviousSignIn], @"should return |YES|");
+//  [_authState verify];
+//  XCTAssertFalse(_completionCalled, @"should not call delegate");
+//  XCTAssertNil(_authError, @"should have no error");
+//}
+
+//- (void)testHasPreviousSignIn_HasNotBeenAuthenticated {
+//  [_keychainHandler saveAuthState:_authState];
+//  [[[_authState expect] andReturnValue:[NSNumber numberWithBool:NO]] isAuthorized];
+//  XCTAssertFalse([_signIn hasPreviousSignIn], @"should return |NO|");
+//  [_authState verify];
+//  XCTAssertFalse(_completionCalled, @"should not call delegate");
+//}
+
+//- (void)testRestorePreviousSignInWhenSignedOut {
+//  [_keychainHandler saveAuthState:_fakeAuthState];
+//  [[[_authState expect] andReturnValue:[NSNumber numberWithBool:NO]] isAuthorized];
+//  _completionCalled = NO;
+//  _authError = nil;
+//
+//  XCTestExpectation *expectation = [self expectationWithDescription:@"Callback should be called."];
+//
+//  [_signIn restorePreviousSignInWithCompletion:^(GIDGoogleUser *_Nullable user,
+//                                                 NSError * _Nullable error) {
+//    [expectation fulfill];
+//    XCTAssertNotNil(error, @"error should not have been nil");
+//    XCTAssertEqual(error.domain,
+//                   kGIDSignInErrorDomain,
+//                   @"error domain should have been the sign-in error domain.");
+//    XCTAssertEqual(error.code,
+//                   kGIDSignInErrorCodeHasNoAuthInKeychain,
+//                   @"error code should have been the 'NoAuthInKeychain' error code.");
+//  }];
+//
+//  [self waitForExpectationsWithTimeout:1 handler:nil];
+//  [_authState verify];
+//}
 
 - (void)testOAuthLogin {
   [self OAuthLoginWithAddScopesFlow:NO
@@ -706,7 +718,7 @@ static NSString *const kNewScope = @"newScope";
 }
 
 - (void)testSignOut {
-  XCTAssert([_keychainHandler saveAuthState:_authState]);
+  XCTAssert([_keychainHandler saveAuthState:_fakeAuthState]);
   // Sign in a user so that we can then sign them out.
   [self OAuthLoginWithAddScopesFlow:NO
                           authError:nil
@@ -739,137 +751,137 @@ static NSString *const kNewScope = @"newScope";
 #pragma mark - Tests - disconnectWithCallback:
 
 // Verifies disconnect calls callback with no errors if access token is present.
-- (void)testDisconnect_accessToken {
-  [_keychainHandler saveAuthState:_authState];
-  [[[_authState expect] andReturn:_tokenResponse] lastTokenResponse];
-  [[[_tokenResponse expect] andReturn:kAccessToken] accessToken];
-  [[[_authorization expect] andReturn:_fetcherService] fetcherService];
-  XCTestExpectation *expectation =
-      [self expectationWithDescription:@"Callback called with nil error"];
-  [_signIn disconnectWithCompletion:^(NSError * _Nullable error) {
-    if (error == nil) {
-      [expectation fulfill];
-    }
-  }];
-  [self verifyAndRevokeToken:kAccessToken hasCallback:YES];
-  [_authorization verify];
-  [_authState verify];
-  [_tokenResponse verify];
-  XCTAssertNil([_keychainHandler loadAuthState]);
-}
+//- (void)testDisconnect_accessToken {
+//  [_keychainHandler saveAuthState:_authState];
+//  [[[_authState expect] andReturn:_tokenResponse] lastTokenResponse];
+//  [[[_tokenResponse expect] andReturn:kAccessToken] accessToken];
+//  [[[_authorization expect] andReturn:_fetcherService] fetcherService];
+//  XCTestExpectation *expectation =
+//      [self expectationWithDescription:@"Callback called with nil error"];
+//  [_signIn disconnectWithCompletion:^(NSError * _Nullable error) {
+//    if (error == nil) {
+//      [expectation fulfill];
+//    }
+//  }];
+//  [self verifyAndRevokeToken:kAccessToken hasCallback:YES];
+//  [_authorization verify];
+//  [_authState verify];
+//  [_tokenResponse verify];
+//  XCTAssertNil([_keychainHandler loadAuthState]);
+//}
 
 // Verifies disconnect if access token is present.
-- (void)testDisconnectNoCallback_accessToken {
-  [_keychainHandler saveAuthState:_authState];
-  [[[_authState expect] andReturn:_tokenResponse] lastTokenResponse];
-  [[[_tokenResponse expect] andReturn:kAccessToken] accessToken];
-  [[[_authorization expect] andReturn:_fetcherService] fetcherService];
-  [_signIn disconnectWithCompletion:nil];
-  [self verifyAndRevokeToken:kAccessToken hasCallback:NO];
-  [_authorization verify];
-  [_authState verify];
-  [_tokenResponse verify];
-  XCTAssertNil([_keychainHandler loadAuthState]);
-}
+//- (void)testDisconnectNoCallback_accessToken {
+//  [_keychainHandler saveAuthState:_authState];
+//  [[[_authState expect] andReturn:_tokenResponse] lastTokenResponse];
+//  [[[_tokenResponse expect] andReturn:kAccessToken] accessToken];
+//  [[[_authorization expect] andReturn:_fetcherService] fetcherService];
+//  [_signIn disconnectWithCompletion:nil];
+//  [self verifyAndRevokeToken:kAccessToken hasCallback:NO];
+//  [_authorization verify];
+//  [_authState verify];
+//  [_tokenResponse verify];
+//  XCTAssertNil([_keychainHandler loadAuthState]);
+//}
 
 // Verifies disconnect calls callback with no errors if refresh token is present.
-- (void)testDisconnect_refreshToken {
-  [_keychainHandler saveAuthState:_authState];
-  [[[_authState expect] andReturn:_tokenResponse] lastTokenResponse];
-  [[[_tokenResponse expect] andReturn:nil] accessToken];
-  [[[_authState expect] andReturn:_tokenResponse] lastTokenResponse];
-  [[[_tokenResponse expect] andReturn:kRefreshToken] refreshToken];
-  [[[_authorization expect] andReturn:_fetcherService] fetcherService];
-  XCTestExpectation *expectation =
-      [self expectationWithDescription:@"Callback called with nil error"];
-  [_signIn disconnectWithCompletion:^(NSError * _Nullable error) {
-    if (error == nil) {
-      [expectation fulfill];
-    }
-  }];
-  [self verifyAndRevokeToken:kRefreshToken hasCallback:YES];
-  [_authorization verify];
-  [_authState verify];
-  XCTAssertNil([_keychainHandler loadAuthState]);
-}
+//- (void)testDisconnect_refreshToken {
+//  [_keychainHandler saveAuthState:_authState];
+//  [[[_authState expect] andReturn:_tokenResponse] lastTokenResponse];
+//  [[[_tokenResponse expect] andReturn:nil] accessToken];
+//  [[[_authState expect] andReturn:_tokenResponse] lastTokenResponse];
+//  [[[_tokenResponse expect] andReturn:kRefreshToken] refreshToken];
+//  [[[_authorization expect] andReturn:_fetcherService] fetcherService];
+//  XCTestExpectation *expectation =
+//      [self expectationWithDescription:@"Callback called with nil error"];
+//  [_signIn disconnectWithCompletion:^(NSError * _Nullable error) {
+//    if (error == nil) {
+//      [expectation fulfill];
+//    }
+//  }];
+//  [self verifyAndRevokeToken:kRefreshToken hasCallback:YES];
+//  [_authorization verify];
+//  [_authState verify];
+//  XCTAssertNil([_keychainHandler loadAuthState]);
+//}
 
 // Verifies disconnect errors are passed along to the callback.
-- (void)testDisconnect_errors {
-  [_keychainHandler saveAuthState:_authState];
-  [[[_authState expect] andReturn:_tokenResponse] lastTokenResponse];
-  [[[_tokenResponse expect] andReturn:kAccessToken] accessToken];
-  [[[_authorization expect] andReturn:_fetcherService] fetcherService];
-  XCTestExpectation *expectation =
-      [self expectationWithDescription:@"Callback called with an error"];
-  [_signIn disconnectWithCompletion:^(NSError * _Nullable error) {
-    if (error != nil) {
-      [expectation fulfill];
-    }
-  }];
-  XCTAssertTrue([self isFetcherStarted], @"should start fetching");
-  // Emulate result back from server.
-  NSError *error = [self error];
-  [self didFetch:nil error:error];
-  [self waitForExpectationsWithTimeout:1 handler:nil];
-  [_authorization verify];
-  [_authState verify];
-  [_tokenResponse verify];
-  XCTAssertNotNil([_keychainHandler loadAuthState]);
-}
+//- (void)testDisconnect_errors {
+//  [_keychainHandler saveAuthState:_authState];
+//  [[[_authState expect] andReturn:_tokenResponse] lastTokenResponse];
+//  [[[_tokenResponse expect] andReturn:kAccessToken] accessToken];
+//  [[[_authorization expect] andReturn:_fetcherService] fetcherService];
+//  XCTestExpectation *expectation =
+//      [self expectationWithDescription:@"Callback called with an error"];
+//  [_signIn disconnectWithCompletion:^(NSError * _Nullable error) {
+//    if (error != nil) {
+//      [expectation fulfill];
+//    }
+//  }];
+//  XCTAssertTrue([self isFetcherStarted], @"should start fetching");
+//  // Emulate result back from server.
+//  NSError *error = [self error];
+//  [self didFetch:nil error:error];
+//  [self waitForExpectationsWithTimeout:1 handler:nil];
+//  [_authorization verify];
+//  [_authState verify];
+//  [_tokenResponse verify];
+//  XCTAssertNotNil([_keychainHandler loadAuthState]);
+//}
 
 // Verifies disconnect with errors
-- (void)testDisconnectNoCallback_errors {
-  [_keychainHandler saveAuthState:_authState];
-  [[[_authState expect] andReturn:_tokenResponse] lastTokenResponse];
-  [[[_tokenResponse expect] andReturn:kAccessToken] accessToken];
-  [[[_authorization expect] andReturn:_fetcherService] fetcherService];
-  [_signIn disconnectWithCompletion:nil];
-  XCTAssertTrue([self isFetcherStarted], @"should start fetching");
-  // Emulate result back from server.
-  NSError *error = [self error];
-  [self didFetch:nil error:error];
-  [_authorization verify];
-  [_authState verify];
-  [_tokenResponse verify];
-  XCTAssertNotNil([_keychainHandler loadAuthState]);
-}
+//- (void)testDisconnectNoCallback_errors {
+//  [_keychainHandler saveAuthState:_authState];
+//  [[[_authState expect] andReturn:_tokenResponse] lastTokenResponse];
+//  [[[_tokenResponse expect] andReturn:kAccessToken] accessToken];
+//  [[[_authorization expect] andReturn:_fetcherService] fetcherService];
+//  [_signIn disconnectWithCompletion:nil];
+//  XCTAssertTrue([self isFetcherStarted], @"should start fetching");
+//  // Emulate result back from server.
+//  NSError *error = [self error];
+//  [self didFetch:nil error:error];
+//  [_authorization verify];
+//  [_authState verify];
+//  [_tokenResponse verify];
+//  XCTAssertNotNil([_keychainHandler loadAuthState]);
+//}
 
 // Verifies disconnect calls callback with no errors and clears keychain if no tokens are present.
-- (void)testDisconnect_noTokens {
-  [_keychainHandler saveAuthState:_authState];
-  [[[_authState expect] andReturn:_tokenResponse] lastTokenResponse];
-  [[[_tokenResponse expect] andReturn:nil] accessToken];
-  [[[_authState expect] andReturn:_tokenResponse] lastTokenResponse];
-  [[[_tokenResponse expect] andReturn:nil] refreshToken];
-  XCTestExpectation *expectation =
-      [self expectationWithDescription:@"Callback called with nil error"];
-  [_signIn disconnectWithCompletion:^(NSError * _Nullable error) {
-    if (error == nil) {
-      [expectation fulfill];
-    }
-  }];
-  [self waitForExpectationsWithTimeout:1 handler:nil];
-  XCTAssertFalse([self isFetcherStarted], @"should not fetch");
-  [_authorization verify];
-  [_authState verify];
-  [_tokenResponse verify];
-  XCTAssertNil([_keychainHandler loadAuthState]);
-}
+//- (void)testDisconnect_noTokens {
+//  [_keychainHandler saveAuthState:_authState];
+//  [[[_authState expect] andReturn:_tokenResponse] lastTokenResponse];
+//  [[[_tokenResponse expect] andReturn:nil] accessToken];
+//  [[[_authState expect] andReturn:_tokenResponse] lastTokenResponse];
+//  [[[_tokenResponse expect] andReturn:nil] refreshToken];
+//  XCTestExpectation *expectation =
+//      [self expectationWithDescription:@"Callback called with nil error"];
+//  [_signIn disconnectWithCompletion:^(NSError * _Nullable error) {
+//    if (error == nil) {
+//      [expectation fulfill];
+//    }
+//  }];
+//  [self waitForExpectationsWithTimeout:1 handler:nil];
+//  XCTAssertFalse([self isFetcherStarted], @"should not fetch");
+//  [_authorization verify];
+//  [_authState verify];
+//  [_tokenResponse verify];
+//  XCTAssertNil([_keychainHandler loadAuthState]);
+//}
 
 // Verifies disconnect clears keychain if no tokens are present.
-- (void)testDisconnectNoCallback_noTokens {
-  [_keychainHandler saveAuthState:_authState];
-  [[[_authState expect] andReturn:_tokenResponse] lastTokenResponse];
-  [[[_tokenResponse expect] andReturn:nil] accessToken];
-  [[[_authState expect] andReturn:_tokenResponse] lastTokenResponse];
-  [[[_tokenResponse expect] andReturn:nil] refreshToken];
-  [_signIn disconnectWithCompletion:nil];
-  XCTAssertFalse([self isFetcherStarted], @"should not fetch");
-  [_authorization verify];
-  [_authState verify];
-  [_tokenResponse verify];
-  XCTAssertNil([_keychainHandler loadAuthState]);
-}
+//- (void)testDisconnectNoCallback_noTokens {
+//  [_keychainHandler saveAuthState:_authState];
+//  [[[_authState expect] andReturn:_tokenResponse] lastTokenResponse];
+//  [[[_tokenResponse expect] andReturn:nil] accessToken];
+//  [[[_authState expect] andReturn:_tokenResponse] lastTokenResponse];
+//  [[[_tokenResponse expect] andReturn:nil] refreshToken];
+//  [_signIn disconnectWithCompletion:nil];
+//  XCTAssertFalse([self isFetcherStarted], @"should not fetch");
+//  [_authorization verify];
+//  [_authState verify];
+//  [_tokenResponse verify];
+//  XCTAssertNil([_keychainHandler loadAuthState]);
+//}
 
 - (void)testPresentingViewControllerException {
 #if TARGET_OS_IOS || TARGET_OS_MACCATALYST
@@ -1169,7 +1181,7 @@ static NSString *const kNewScope = @"newScope";
                    additionalScopes:(NSArray *)additionalScopes {
   if (restoredSignIn) {
     // clearAndAuthenticateWithOptions
-    [_keychainHandler saveAuthState:_authState];
+    [_keychainHandler saveAuthState:_fakeAuthState];
     BOOL isAuthorized = restoredSignIn ? YES : NO;
     [[[_authState expect] andReturnValue:[NSNumber numberWithBool:isAuthorized]] isAuthorized];
   }