Răsfoiți Sursa

Add fake for GIDProfileDataFetcher.

pinlu 3 ani în urmă
părinte
comite
af9bdae887

+ 38 - 0
GoogleSignIn/Sources/GIDProfileDataFetcher/Implementations/Fakes/GIDFakeProfileDataFetcher.h

@@ -0,0 +1,38 @@
+/*
+ * Copyright 2023 Google LLC
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#import <Foundation/Foundation.h>
+
+#import "GoogleSignIn/Sources/GIDProfileDataFetcher/API/GIDProfileDataFetcher.h"
+
+@class GIDProfileData;
+
+NS_ASSUME_NONNULL_BEGIN
+
+typedef void (^GIDProfileDataFetcherFakeResponseProvider)(GIDProfileData *_Nullable profileData,
+                                                          NSError *_Nullable error);
+
+typedef void (^GIDProfileDataFetcherTestBlock)(GIDProfileDataFetcherFakeResponseProvider
+                                               responseProvider);
+
+@interface GIDFakeProfileDataFetcher : NSObject <GIDProfileDataFetcher>
+
+/// Set the test block which provides the response value.
+- (void)setTestBlock:(GIDProfileDataFetcherTestBlock)block;
+
+@end
+
+NS_ASSUME_NONNULL_END

+ 32 - 0
GoogleSignIn/Sources/GIDProfileDataFetcher/Implementations/Fakes/GIDFakeProfileDataFetcher.m

@@ -0,0 +1,32 @@
+#import "GoogleSignIn/Sources/GIDProfileDataFetcher/Implementations/Fakes/GIDFakeProfileDataFetcher.h"
+
+#import "GoogleSignIn/Sources/Public/GoogleSignIn/GIDProfileData.h"
+
+#ifdef SWIFT_PACKAGE
+@import AppAuth;
+#else
+#import <AppAuth/AppAuth.h>
+#endif
+
+NS_ASSUME_NONNULL_BEGIN
+
+@interface GIDFakeProfileDataFetcher ()
+
+@property(nonatomic) GIDProfileDataFetcherTestBlock testBlock;
+
+@end
+
+@implementation GIDFakeProfileDataFetcher
+
+- (void)fetchProfileDataWithAuthState:(OIDAuthState *)authState
+                           completion:(void (^)(GIDProfileData *_Nullable profileData,
+                                                NSError *_Nullable error))completion {
+  NSAssert(self.testBlock != nil, @"Set the test block before invoking this method.");
+  self.testBlock(^(GIDProfileData *_Nullable profileData, NSError *_Nullable error){
+    completion(profileData,error);
+  });
+}
+
+@end
+
+NS_ASSUME_NONNULL_END

+ 18 - 11
GoogleSignIn/Tests/Unit/GIDSignInTest.m

@@ -33,14 +33,14 @@
 #import "GoogleSignIn/Sources/GIDKeychainHandler/Implementations/Fakes/GIDFakeKeychainHandler.h"
 #import "GoogleSignIn/Sources/GIDHTTPFetcher/Implementations/Fakes/GIDFakeHTTPFetcher.h"
 #import "GoogleSignIn/Sources/GIDHTTPFetcher/Implementations/GIDHTTPFetcher.h"
-#import "GoogleSignIn/Sources/GIDProfileDataFetcher/Implementations/GIDProfileDataFetcher.h"
-
+#import "GoogleSignIn/Sources/GIDProfileDataFetcher/Implementations/Fakes/GIDFakeProfileDataFetcher.h"
 
 #if TARGET_OS_IOS && !TARGET_OS_MACCATALYST
 #import "GoogleSignIn/Sources/GIDEMMErrorHandler.h"
 #endif // TARGET_OS_IOS && !TARGET_OS_MACCATALYST
 
 #import "GoogleSignIn/Tests/Unit/GIDFakeMainBundle.h"
+#import "GoogleSignIn/Tests/Unit/GIDProfileData+Testing.h"
 #import "GoogleSignIn/Tests/Unit/OIDAuthorizationResponse+Testing.h"
 #import "GoogleSignIn/Tests/Unit/OIDTokenResponse+Testing.h"
 
@@ -202,6 +202,9 @@ static NSString *const kNewScope = @"newScope";
   // Fake for |GIDHTTPFetcher|.
   GIDFakeHTTPFetcher *_httpFetcher;
   
+  // Fake for |GIDProfileDataFetcher|.
+  GIDFakeProfileDataFetcher *_profileDataFetcher;
+  
 #if TARGET_OS_IOS || TARGET_OS_MACCATALYST
   // Mock |UIViewController|.
   id _presentingViewController;
@@ -313,10 +316,11 @@ static NSString *const kNewScope = @"newScope";
   
   _httpFetcher = [[GIDFakeHTTPFetcher alloc] init];
   
-  id<GIDProfileDataFetcher> profileDataFetcher = [[GIDProfileDataFetcher alloc] init];
+  _profileDataFetcher = [[GIDFakeProfileDataFetcher alloc] init];
+  
   _signIn = [[GIDSignIn alloc] initWithKeychainHandler:_keychainHandler
                                            httpFetcher:_httpFetcher
-                                    profileDataFetcher:profileDataFetcher];
+                                    profileDataFetcher:_profileDataFetcher];
   _hint = nil;
 
   __weak GIDSignInTest *weakSelf = self;
@@ -1208,6 +1212,15 @@ static NSString *const kNewScope = @"newScope";
                refreshToken:kRefreshToken
                codeVerifier:nil
        additionalParameters:tokenResponse.request.additionalParameters];
+  
+  // Set the response for GIDProfileDataFetcher.
+    GIDProfileDataFetcherTestBlock testBlock = ^(GIDProfileDataFetcherFakeResponseProvider
+                                                 responseProvider) {
+      GIDProfileData *profileData = [GIDProfileData testInstance];
+      responseProvider(profileData, nil);
+    };
+    
+    [_profileDataFetcher setTestBlock:testBlock];
 
   if (restoredSignIn) {
     // maybeFetchToken
@@ -1334,9 +1347,6 @@ static NSString *const kNewScope = @"newScope";
     return;
   }
 
-  // DecodeIdTokenCallback
-  [[[_authState expect] andReturn:tokenResponse] lastTokenResponse];
-
   // SaveAuthCallback
   __block OIDAuthState *authState;
   __block OIDTokenResponse *updatedTokenResponse;
@@ -1390,10 +1400,7 @@ static NSString *const kNewScope = @"newScope";
     XCTAssertNotNil(authState);
   }
   // Check fat ID token decoding
-  XCTAssertEqualObjects(profileData.name, kFatName);
-  XCTAssertEqualObjects(profileData.givenName, kFatGivenName);
-  XCTAssertEqualObjects(profileData.familyName, kFatFamilyName);
-  XCTAssertTrue(profileData.hasImage);
+  XCTAssertEqualObjects(profileData, [GIDProfileData testInstance]);
 
   // If attempt to authenticate again, will reuse existing auth object.
   _completionCalled = NO;