Просмотр исходного кода

Progress on testing -[GIDAuthorizationFlow authorize]

Matt Mathias 1 год назад
Родитель
Сommit
1672ced739

+ 11 - 8
GoogleSignIn/Sources/GIDAuthorization.m

@@ -225,14 +225,17 @@ static NSString *const kTokenURLTemplate = @"https://%@/token";
     return;
   }
 
-  self.authFlow = [[GIDAuthorizationFlow alloc] initWithSignInOptions:self.currentOptions
-                                                            authState:authState
-                                                          profileData:nil
-                                                           googleUser:self.currentUser
-                                             externalUserAgentSession:nil
-                                                           emmSupport:nil
-                                                                error:nil];
-  // TODO: Implement the interactive version with operations as well
+  // If we have an `authFlow`, then it was injected and we are testing; don't overwrite
+  if (!self.authFlow) {
+    self.authFlow = [[GIDAuthorizationFlow alloc] initWithSignInOptions:self.currentOptions
+                                                              authState:authState
+                                                            profileData:nil
+                                                             googleUser:self.currentUser
+                                               externalUserAgentSession:nil
+                                                             emmSupport:nil
+                                                                  error:nil];
+  }
+  
   [self.authFlow authorize];
 }
 

+ 8 - 1
GoogleSignIn/Sources/GIDAuthorizationFlow/Implementations/Fake/GIDAuthorizationFlowFake.m

@@ -17,6 +17,7 @@
 #import "GIDAuthorizationFlowFake.h"
 #import "GoogleSignIn/Sources/Public/GoogleSignIn/GIDConfiguration.h"
 #import "GoogleSignIn/Sources/Public/GoogleSignIn/GIDSignInResult.h"
+#import "GoogleSignIn/Sources/GIDGoogleUser_Private.h"
 #import "GoogleSignIn/Sources/GIDSignInResult_Private.h"
 #import "GoogleSignIn/Sources/GIDSignInInternalOptions.h"
 #import "GoogleSignIn/Sources/GIDConfiguration_Private.h"
@@ -43,7 +44,13 @@
   return self;
 }
 - (void)authorize {
-  // TODO: Implement
+  // Create the `googleUser` from the passed `authState` and `profileData` to simulate creating a
+  // `googleUser` via that sign in silently flow
+  self.googleUser = [[GIDGoogleUser alloc] initWithAuthState:self.authState
+                                                 profileData:self.profileData];
+  GIDSignInResult *result = [[GIDSignInResult alloc] initWithGoogleUser:self.googleUser
+                                                         serverAuthCode:@"abcd"];
+  self.options.completion(result, self.error);
 }
 
 - (void)authorizeInteractively {

+ 60 - 3
GoogleSignIn/Tests/Unit/GIDAuthorizationTests/GIDAuthorizationTest.m

@@ -99,7 +99,7 @@ static NSString *const kKeychainItemName = @"test_keychain_name";
 }
 
 - (void)testThatAuthorizeInteractivelySetsCurrentUser {
-  XCTestExpectation *currentUserExpectation =
+  XCTestExpectation *authorizeInteractivelyExpectation =
     [self expectationWithDescription:@"Current user expectation"];
   GIDKeychainHelperFake *keychainFake =
     [[GIDKeychainHelperFake alloc] initWithKeychainAttributes:[NSSet setWithArray:@[]]];
@@ -124,7 +124,7 @@ static NSString *const kKeychainItemName = @"test_keychain_name";
     XCTAssertEqualObjects(expectedGoogleUser, result.user);
     XCTAssertEqualObjects(expectedAuthState, result.user.authState);
     XCTAssertEqualObjects(expectedProfileData, result.user.profile);
-    [currentUserExpectation fulfill];
+    [authorizeInteractivelyExpectation fulfill];
   };
   
   GIDBundleFake *bFake = [[GIDBundleFake alloc] init];
@@ -147,7 +147,64 @@ static NSString *const kKeychainItemName = @"test_keychain_name";
                                                              configuration:config
                                               authorizationFlowCoordinator:fakeFlow];
   [auth signInWithOptions:opts];
-  [self waitForExpectations:@[currentUserExpectation] timeout:5];
+  [self waitForExpectations:@[authorizeInteractivelyExpectation] timeout:5];
+  XCTAssertNotNil(auth.currentUser);
+  XCTAssertEqualObjects(expectedGoogleUser, auth.currentUser);
+}
+
+- (void)testThatAuthorizeSilentlySetsCurrentUser {
+  XCTestExpectation *authorizeExpectation =
+    [self expectationWithDescription:@"Current user expectation"];
+  GIDKeychainHelperFake *keychainFake =
+    [[GIDKeychainHelperFake alloc] initWithKeychainAttributes:[NSSet setWithArray:@[]]];
+  GTMKeychainStore *keychainStore = [[GTMKeychainStore alloc] initWithItemName:kKeychainItemName
+                                                                keychainHelper:keychainFake];
+  GIDConfiguration *config =
+    [[GIDConfiguration alloc] initWithClientID:OIDAuthorizationRequestTestingClientID
+                                serverClientID:kServerClientID
+                                  hostedDomain:kHostedDomain
+                                   openIDRealm:kOpenIDRealm];
+  UIViewController *vc = [[UIViewController alloc] init];
+  
+  OIDAuthState *expectedAuthState = [OIDAuthState testInstance];
+  GIDProfileData *expectedProfileData = [GIDProfileData testInstanceWithImageURL:@"test.com"];
+  GIDGoogleUser *expectedGoogleUser = [[GIDGoogleUser alloc] initWithAuthState:expectedAuthState
+                                                                   profileData:expectedProfileData];
+  GTMAuthSession *authSession = [[GTMAuthSession alloc] initWithAuthState:expectedAuthState];
+  NSError *error;
+  [keychainStore saveAuthSession:authSession error:&error];
+  XCTAssertNil(error);
+  
+  GIDSignInCompletion comp = ^(GIDSignInResult *_Nullable result, NSError *_Nullable error) {
+    XCTAssertNotNil(result, @"The sign in result should be non-nil.");
+    XCTAssertNil(error, @"There should be no error from authorizing.");
+    XCTAssertEqualObjects(expectedGoogleUser, result.user);
+    XCTAssertEqualObjects(expectedAuthState, result.user.authState);
+    XCTAssertEqualObjects(expectedProfileData, result.user.profile);
+    [authorizeExpectation fulfill];
+  };
+  
+  GIDBundleFake *bFake = [[GIDBundleFake alloc] init];
+  GIDSignInInternalOptions *options = [GIDSignInInternalOptions silentOptionsWithCompletion:comp];
+  
+  // Pass `nil` for `googleUser` below to simulate loading a saved `authSession` from keychain and
+  // refreshing that to
+  GIDAuthorizationFlowFake *fakeFlow =
+    [[GIDAuthorizationFlowFake alloc] initWithSignInOptions:options
+                                                  authState:expectedAuthState
+                                                profileData:expectedProfileData
+                                                 googleUser:nil
+                                   externalUserAgentSession:nil
+                                                 emmSupport:nil
+                                                      error:nil];
+  
+  GIDAuthorization *auth = [[GIDAuthorization alloc] initWithKeychainStore:keychainStore
+                                                             configuration:config
+                                              authorizationFlowCoordinator:fakeFlow];
+  [auth signInWithOptions:options];
+  // FIXME: Fails for now because the current user check on `GIDAuthorization` goes thru
+  // `GIDAuthorizationFlowFoke, which has an injected `currentUser` to fake the request
+  [self waitForExpectations:@[authorizeExpectation] timeout:5];
   XCTAssertNotNil(auth.currentUser);
   XCTAssertEqualObjects(expectedGoogleUser, auth.currentUser);
 }