Ver código fonte

Blocked by another PR to create and test OIDAuthorizationRequest.

pinlu 3 anos atrás
pai
commit
69aa8208bc

+ 11 - 4
GoogleSignIn/Sources/GIDAuthorizationFlowProcessor/Implementations/Fakes/GIDFakeAuthorizationFlowProcessor.h

@@ -18,16 +18,22 @@
 
 #import "GoogleSignIn/Sources/GIDAuthorizationFlowProcessor/API/GIDAuthorizationFlowProcessor.h"
 
+#ifdef SWIFT_PACKAGE
+@import AppAuth;
+#else
+#import <AppAuth/AppAuth.h>
+#endif
+
 NS_ASSUME_NONNULL_BEGIN
 
 /// The block which provides the response for the method `startWithOptions:emmSupport:completion:`.
 ///
-/// @param data The OIDAuthorizationResponse object returned if succeed,
+/// @param authorizationResponse The OIDAuthorizationResponse object returned if succeeded.
 /// @param error The error returned if failed.
 typedef void(^GIDAuthorizationFlowProcessorFakeResponseProviderBlock)
     (OIDAuthorizationResponse *_Nullable authorizationResponse, NSError *_Nullable error);
 
-/// The block to set up data based on the input request for the method
+/// The block to set up response value for the method
 /// `startWithOptions:emmSupport:completion:`.
 ///
 /// @param responseProvider The block which provides the response.
@@ -36,8 +42,9 @@ typedef void (^GIDAuthorizationFlowProcessorTestBlock)
 
 @interface GIDFakeAuthorizationFlowProcessor : NSObject <GIDAuthorizationFlowProcessor>
 
-/// Set the test block which provides the response value.
-- (void)setTestBlock:(GIDAuthorizationFlowProcessorTestBlock)block;
+/// The test block which provides the response value.
+@property(nonatomic) GIDAuthorizationFlowProcessorTestBlock testBlock;
+
 
 @end
 

+ 7 - 10
GoogleSignIn/Sources/GIDAuthorizationFlowProcessor/Implementations/Fakes/GIDFakeAuthorizationFlowProcessor.m

@@ -1,11 +1,5 @@
 #import "GoogleSignIn/Sources/GIDAuthorizationFlowProcessor/Implementations/Fakes/GIDFakeAuthorizationFlowProcessor.h"
 
-@interface GIDFakeAuthorizationFlowProcessor ()
-
-@property(nonatomic) GIDAuthorizationFlowProcessorTestBlock testBlock;
-
-@end
-
 @implementation GIDFakeAuthorizationFlowProcessor
 
 - (void)startWithOptions:(GIDSignInInternalOptions *)options
@@ -13,22 +7,25 @@
               completion:(void (^)(OIDAuthorizationResponse *_Nullable authorizationResponse,
                                    NSError *_Nullable error))completion {
   NSAssert(self.testBlock != nil, @"Set the test block before invoking this method.");
+  
+  
   self.testBlock(^(OIDAuthorizationResponse *authorizationResponse, NSError *error) {
     completion(authorizationResponse, error);
   });
 }
 
-/// Not used in test for now.
 - (BOOL)isStarted {
+  NSAssert(NO, @"Not implemented.");
   return YES;
 }
 
-/// Not used in test for now.
 - (BOOL)resumeExternalUserAgentFlowWithURL:(NSURL *)url {
+  NSAssert(NO, @"Not implemented.");
   return YES;
 }
 
-/// Not used in test for now.
-- (void)cancelAuthenticationFlow {}
+- (void)cancelAuthenticationFlow {
+  NSAssert(NO, @"Not implemented.");
+}
 
 @end

+ 0 - 1
GoogleSignIn/Tests/Unit/GIDAuthorizationFlowProcessorTest.m

@@ -92,7 +92,6 @@ static NSInteger const kTimeout = 1;
                                      emmSupport:nil
                                      completion:^(OIDAuthorizationResponse *authorizationResponse,
                                                   NSError *error) {
-<
     XCTAssertEqualObjects(authorizationResponse.accessToken,
                           self->_fakeResponse.accessToken);
     XCTAssertEqualObjects(authorizationResponse.authorizationCode,

+ 44 - 36
GoogleSignIn/Tests/Unit/GIDSignInTest.m

@@ -26,7 +26,7 @@
 // Test module imports
 @import GoogleSignIn;
 
-#import "GoogleSignIn/Sources/GIDAuthorizationFlowProcessor/Implementations/GIDAuthorizationFlowProcessor.h"
+#import "GoogleSignIn/Sources/GIDAuthorizationFlowProcessor/Implementations/Fakes/GIDFakeAuthorizationFlowProcessor.h"
 #import "GoogleSignIn/Sources/GIDEMMSupport.h"
 #import "GoogleSignIn/Sources/GIDGoogleUser_Private.h"
 #import "GoogleSignIn/Sources/GIDSignIn_Private.h"
@@ -36,7 +36,6 @@
 #import "GoogleSignIn/Sources/GIDHTTPFetcher/Implementations/GIDHTTPFetcher.h"
 #import "GoogleSignIn/Sources/GIDProfileDataFetcher/Implementations/GIDProfileDataFetcher.h"
 
-
 #if TARGET_OS_IOS && !TARGET_OS_MACCATALYST
 #import "GoogleSignIn/Sources/GIDEMMErrorHandler.h"
 #endif // TARGET_OS_IOS && !TARGET_OS_MACCATALYST
@@ -203,6 +202,9 @@ static NSString *const kNewScope = @"newScope";
   // Fake for |GIDHTTPFetcher|.
   GIDFakeHTTPFetcher *_httpFetcher;
   
+  // Fake for |GIDAuthorizationFlowProcessor|
+  GIDFakeAuthorizationFlowProcessor *_authorizationFlowProcessor;
+  
 #if TARGET_OS_IOS || TARGET_OS_MACCATALYST
   // Mock |UIViewController|.
   id _presentingViewController;
@@ -289,14 +291,14 @@ static NSString *const kNewScope = @"newScope";
   OCMStub([_authorization initWithAuthState:OCMOCK_ANY]).andReturn(_authorization);
   _user = OCMStrictClassMock([GIDGoogleUser class]);
   _oidAuthorizationService = OCMStrictClassMock([OIDAuthorizationService class]);
-  OCMStub([_oidAuthorizationService
-      presentAuthorizationRequest:SAVE_TO_ARG_BLOCK(self->_savedAuthorizationRequest)
-#if TARGET_OS_IOS || TARGET_OS_MACCATALYST
-           presentingViewController:SAVE_TO_ARG_BLOCK(self->_savedPresentingViewController)
-#elif TARGET_OS_OSX
-           presentingWindow:SAVE_TO_ARG_BLOCK(self->_savedPresentingWindow)
-#endif // TARGET_OS_IOS || TARGET_OS_MACCATALYST
-                         callback:COPY_TO_ARG_BLOCK(self->_savedAuthorizationCallback)]);
+//  OCMStub([_oidAuthorizationService
+//      presentAuthorizationRequest:SAVE_TO_ARG_BLOCK(self->_savedAuthorizationRequest)
+//#if TARGET_OS_IOS || TARGET_OS_MACCATALYST
+//           presentingViewController:SAVE_TO_ARG_BLOCK(self->_savedPresentingViewController)
+//#elif TARGET_OS_OSX
+//           presentingWindow:SAVE_TO_ARG_BLOCK(self->_savedPresentingWindow)
+//#endif // TARGET_OS_IOS || TARGET_OS_MACCATALYST
+//                         callback:COPY_TO_ARG_BLOCK(self->_savedAuthorizationCallback)]);
   OCMStub([self->_oidAuthorizationService
       performTokenRequest:SAVE_TO_ARG_BLOCK(self->_savedTokenRequest)
                  callback:COPY_TO_ARG_BLOCK(self->_savedTokenCallback)]);
@@ -314,15 +316,14 @@ static NSString *const kNewScope = @"newScope";
   
   _httpFetcher = [[GIDFakeHTTPFetcher alloc] init];
   
-  GIDAuthorizationFlowProcessor * authorizationFlowProcessor =
-      [[GIDAuthorizationFlowProcessor alloc] init];
+  _authorizationFlowProcessor = [[GIDFakeAuthorizationFlowProcessor alloc] init];
   
   id<GIDProfileDataFetcher> profileDataFetcher = [[GIDProfileDataFetcher alloc] init];
 
   _signIn = [[GIDSignIn alloc] initWithKeychainHandler:_keychainHandler
                                            httpFetcher:_httpFetcher
                                     profileDataFetcher:profileDataFetcher
-                            authorizationFlowProcessor:authorizationFlowProcessor];
+                            authorizationFlowProcessor:_authorizationFlowProcessor];
 
   _hint = nil;
 
@@ -343,7 +344,7 @@ static NSString *const kNewScope = @"newScope";
   OCMVerifyAll(_tokenResponse);
   OCMVerifyAll(_tokenRequest);
   OCMVerifyAll(_user);
-  OCMVerifyAll(_oidAuthorizationService);
+//  OCMVerifyAll(_oidAuthorizationService);
 
 #if TARGET_OS_IOS || TARGET_OS_MACCATALYST
   OCMVerifyAll(_presentingViewController);
@@ -1215,6 +1216,34 @@ static NSString *const kNewScope = @"newScope";
                refreshToken:kRefreshToken
                codeVerifier:nil
        additionalParameters:tokenResponse.request.additionalParameters];
+  
+  // Simulate auth endpoint response
+  GIDAuthorizationFlowProcessorTestBlock testBlock;
+  if (modalCancel) {
+    NSError *error = [NSError errorWithDomain:OIDGeneralErrorDomain
+                                         code:OIDErrorCodeUserCanceledAuthorizationFlow
+                                     userInfo:nil];
+    testBlock = ^(GIDAuthorizationFlowProcessorFakeResponseProviderBlock responseProvider) {
+      responseProvider(nil, error);
+    };
+  } else {
+    testBlock = ^(GIDAuthorizationFlowProcessorFakeResponseProviderBlock responseProvider) {
+      responseProvider(authResponse, nil);
+    };
+  }
+  _authorizationFlowProcessor.testBlock = testBlock;
+  
+  // maybeFetchToken
+  if (!(authError || modalCancel)) {
+    [[[_authState expect] andReturn:nil] lastTokenResponse];
+#if TARGET_OS_IOS && !TARGET_OS_MACCATALYST
+    // Corresponds to EMM support
+    [[[_authState expect] andReturn:authResponse] lastAuthorizationResponse];
+#endif // TARGET_OS_IOS && !TARGET_OS_MACCATALYST
+    [[[_authState expect] andReturn:nil] lastTokenResponse];
+    [[[_authState expect] andReturn:authResponse] lastAuthorizationResponse];
+    [[[_authState expect] andReturn:authResponse] lastAuthorizationResponse];
+  }
 
   if (restoredSignIn) {
     // maybeFetchToken
@@ -1280,34 +1309,13 @@ static NSString *const kNewScope = @"newScope";
     XCTAssertEqualObjects(params[@"include_granted_scopes"], @"true");
     XCTAssertEqualObjects(params[kSDKVersionLoggingParameter], GIDVersion());
     XCTAssertEqualObjects(params[kEnvironmentLoggingParameter], GIDEnvironment());
-    XCTAssertNotNil(_savedAuthorizationCallback);
+//    XCTAssertNotNil(_savedAuthorizationCallback);
 #if TARGET_OS_IOS || TARGET_OS_MACCATALYST
     XCTAssertEqual(_savedPresentingViewController, _presentingViewController);
 #elif TARGET_OS_OSX
     XCTAssertEqual(_savedPresentingWindow, _presentingWindow);
 #endif // TARGET_OS_IOS || TARGET_OS_MACCATALYST
 
-    // maybeFetchToken
-    if (!(authError || modalCancel)) {
-      [[[_authState expect] andReturn:nil] lastTokenResponse];
-#if TARGET_OS_IOS && !TARGET_OS_MACCATALYST
-      // Corresponds to EMM support
-      [[[_authState expect] andReturn:authResponse] lastAuthorizationResponse];
-#endif // TARGET_OS_IOS && !TARGET_OS_MACCATALYST
-      [[[_authState expect] andReturn:nil] lastTokenResponse];
-      [[[_authState expect] andReturn:authResponse] lastAuthorizationResponse];
-      [[[_authState expect] andReturn:authResponse] lastAuthorizationResponse];
-    }
-
-    // Simulate auth endpoint response
-    if (modalCancel) {
-      NSError *error = [NSError errorWithDomain:OIDGeneralErrorDomain
-                                           code:OIDErrorCodeUserCanceledAuthorizationFlow
-                                       userInfo:nil];
-      _savedAuthorizationCallback(nil, error);
-    } else {
-      _savedAuthorizationCallback(authResponse, nil);
-    }
 
     if (authError || modalCancel) {
       return;