GIDAuthorizationTest.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. // Copyright 2025 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #import <XCTest/XCTest.h>
  15. #import "GoogleSignIn/Sources/GIDAuthorization_Private.h"
  16. #import "GoogleSignIn/Sources/GIDAuthorizationFlow/Implementations/Fake/GIDAuthorizationFlowFake.h"
  17. #import "GoogleSignIn/Sources/GIDGoogleUser_Private.h"
  18. #import "GoogleSignIn/Sources/GIDSignIn_Private.h"
  19. #import "GoogleSignIn/Sources/GIDSignInInternalOptions.h"
  20. #import "GoogleSignIn/Sources/GIDSignInResult_Private.h"
  21. #import "GoogleSignIn/Sources/Public/GoogleSignIn/GIDAuthorization.h"
  22. #import "GoogleSignIn/Sources/Public/GoogleSignIn/GIDSignInResult.h"
  23. #import "GoogleSignIn/Tests/Unit/GIDAuthorizationTests/GIDKeychainHelperFake.h"
  24. #import "GoogleSignIn/Tests/Unit/GIDAuthorizationTests/GIDBundleFake.h"
  25. #import "GoogleSignIn/Tests/Unit/GIDConfiguration+Testing.h"
  26. #import "GoogleSignIn/Tests/Unit/GIDProfileData+Testing.h"
  27. #import "GoogleSignIn/Tests/Unit/OIDAuthState+Testing.h"
  28. #import "GoogleSignIn/Tests/Unit/OIDAuthorizationRequest+Testing.h"
  29. static NSString *const kKeychainItemName = @"test_keychain_name";
  30. @import GTMAppAuth;
  31. @interface GIDAuthorizationTest : XCTestCase
  32. @end
  33. @implementation GIDAuthorizationTest
  34. - (void)testAuthorizationConfigurationAssertValidParameters {
  35. GIDConfiguration *config =
  36. [[GIDConfiguration alloc] initWithClientID:OIDAuthorizationRequestTestingClientID
  37. serverClientID:kServerClientID
  38. hostedDomain:kHostedDomain
  39. openIDRealm:kOpenIDRealm];
  40. GIDSignInInternalOptions *opts = [GIDSignInInternalOptions defaultOptionsWithConfiguration:config
  41. presentingViewController:nil
  42. loginHint:nil
  43. addScopesFlow:NO
  44. bundle:nil
  45. completion:nil];
  46. GIDAuthorizationFlowFake *fakeFlow = [[GIDAuthorizationFlowFake alloc] initWithSignInOptions:opts
  47. authState:nil
  48. profileData:nil
  49. googleUser:nil
  50. externalUserAgentSession:nil
  51. emmSupport:nil
  52. error:nil];
  53. @try {
  54. GIDAuthorization *authorization = [[GIDAuthorization alloc] initWithKeychainStore:nil
  55. configuration:config
  56. authorizationFlowCoordinator:fakeFlow];
  57. [authorization assertValidParameters];
  58. }
  59. @catch (NSException *exception) {
  60. XCTFail(@"`authorization` should have valid parameters.");
  61. }
  62. @finally {}
  63. }
  64. - (void)testAuthorizationConfigurationAssertValidPresentingController {
  65. UIViewController *vc = [[UIViewController alloc] init];
  66. GIDSignInInternalOptions *opts = [GIDSignInInternalOptions defaultOptionsWithConfiguration:nil
  67. presentingViewController:vc
  68. loginHint:nil
  69. addScopesFlow:NO
  70. bundle:nil
  71. completion:nil];
  72. GIDAuthorizationFlowFake *fakeFlow = [[GIDAuthorizationFlowFake alloc] initWithSignInOptions:opts
  73. authState:nil
  74. profileData:nil
  75. googleUser:nil
  76. externalUserAgentSession:nil
  77. emmSupport:nil
  78. error:nil];
  79. @try {
  80. GIDAuthorization *authorization = [[GIDAuthorization alloc] initWithKeychainStore:nil
  81. configuration:nil
  82. authorizationFlowCoordinator:fakeFlow];
  83. [authorization assertValidPresentingController];
  84. }
  85. @catch (NSException *exception) {
  86. XCTFail(@"`authorization` should have a valid presenting controller.");
  87. }
  88. @finally {}
  89. }
  90. - (void)testThatAuthorizeInteractivelySetsCurrentUser {
  91. XCTestExpectation *authorizeInteractivelyExpectation =
  92. [self expectationWithDescription:@"Current user expectation"];
  93. GIDKeychainHelperFake *keychainFake =
  94. [[GIDKeychainHelperFake alloc] initWithKeychainAttributes:[NSSet setWithArray:@[]]];
  95. GTMKeychainStore *keychainStore = [[GTMKeychainStore alloc] initWithItemName:kKeychainItemName
  96. keychainHelper:keychainFake];
  97. GIDConfiguration *config =
  98. [[GIDConfiguration alloc] initWithClientID:OIDAuthorizationRequestTestingClientID
  99. serverClientID:kServerClientID
  100. hostedDomain:kHostedDomain
  101. openIDRealm:kOpenIDRealm];
  102. UIViewController *vc = [[UIViewController alloc] init];
  103. OIDAuthState *expectedAuthState = [OIDAuthState testInstance];
  104. GIDProfileData *expectedProfileData = [GIDProfileData testInstanceWithImageURL:@"test.com"];
  105. GIDGoogleUser *expectedGoogleUser = [[GIDGoogleUser alloc] initWithAuthState:expectedAuthState
  106. profileData:expectedProfileData];
  107. GIDSignInCompletion comp = ^(GIDSignInResult *_Nullable result, NSError *_Nullable error) {
  108. XCTAssertNotNil(result, @"The sign in result should be non-nil.");
  109. XCTAssertNil(error, @"There should be no error from authorizing.");
  110. XCTAssertEqualObjects(expectedGoogleUser, result.user);
  111. XCTAssertEqualObjects(expectedAuthState, result.user.authState);
  112. XCTAssertEqualObjects(expectedProfileData, result.user.profile);
  113. [authorizeInteractivelyExpectation fulfill];
  114. };
  115. GIDBundleFake *bFake = [[GIDBundleFake alloc] init];
  116. GIDSignInInternalOptions *opts = [GIDSignInInternalOptions defaultOptionsWithConfiguration:config
  117. presentingViewController:vc
  118. loginHint:nil
  119. addScopesFlow:NO
  120. bundle:bFake
  121. completion:comp];
  122. GIDAuthorizationFlowFake *fakeFlow =
  123. [[GIDAuthorizationFlowFake alloc] initWithSignInOptions:opts
  124. authState:expectedAuthState
  125. profileData:expectedProfileData
  126. googleUser:expectedGoogleUser
  127. externalUserAgentSession:nil
  128. emmSupport:nil
  129. error:nil];
  130. GIDAuthorization *auth = [[GIDAuthorization alloc] initWithKeychainStore:keychainStore
  131. configuration:config
  132. authorizationFlowCoordinator:fakeFlow];
  133. [auth signInWithOptions:opts];
  134. [self waitForExpectations:@[authorizeInteractivelyExpectation] timeout:5];
  135. XCTAssertNotNil(auth.currentUser);
  136. XCTAssertEqualObjects(expectedGoogleUser, auth.currentUser);
  137. }
  138. - (void)testThatAuthorizeSilentlySetsCurrentUser {
  139. XCTestExpectation *authorizeExpectation =
  140. [self expectationWithDescription:@"Current user expectation"];
  141. GIDKeychainHelperFake *keychainFake =
  142. [[GIDKeychainHelperFake alloc] initWithKeychainAttributes:[NSSet setWithArray:@[]]];
  143. GTMKeychainStore *keychainStore = [[GTMKeychainStore alloc] initWithItemName:kKeychainItemName
  144. keychainHelper:keychainFake];
  145. GIDConfiguration *config =
  146. [[GIDConfiguration alloc] initWithClientID:OIDAuthorizationRequestTestingClientID
  147. serverClientID:kServerClientID
  148. hostedDomain:kHostedDomain
  149. openIDRealm:kOpenIDRealm];
  150. UIViewController *vc = [[UIViewController alloc] init];
  151. OIDAuthState *expectedAuthState = [OIDAuthState testInstance];
  152. GIDProfileData *expectedProfileData = [GIDProfileData testInstanceWithImageURL:@"test.com"];
  153. GIDGoogleUser *expectedGoogleUser = [[GIDGoogleUser alloc] initWithAuthState:expectedAuthState
  154. profileData:expectedProfileData];
  155. GTMAuthSession *authSession = [[GTMAuthSession alloc] initWithAuthState:expectedAuthState];
  156. NSError *error;
  157. [keychainStore saveAuthSession:authSession error:&error];
  158. XCTAssertNil(error);
  159. GIDSignInCompletion comp = ^(GIDSignInResult *_Nullable result, NSError *_Nullable error) {
  160. XCTAssertNotNil(result, @"The sign in result should be non-nil.");
  161. XCTAssertNil(error, @"There should be no error from authorizing.");
  162. XCTAssertEqualObjects(expectedGoogleUser, result.user);
  163. XCTAssertEqualObjects(expectedAuthState, result.user.authState);
  164. XCTAssertEqualObjects(expectedProfileData, result.user.profile);
  165. [authorizeExpectation fulfill];
  166. };
  167. GIDBundleFake *bFake = [[GIDBundleFake alloc] init];
  168. GIDSignInInternalOptions *options = [GIDSignInInternalOptions silentOptionsWithCompletion:comp];
  169. // Pass `nil` for `googleUser` below to simulate loading a saved `authSession` from keychain and
  170. // refreshing that to
  171. GIDAuthorizationFlowFake *fakeFlow =
  172. [[GIDAuthorizationFlowFake alloc] initWithSignInOptions:options
  173. authState:expectedAuthState
  174. profileData:expectedProfileData
  175. googleUser:nil
  176. externalUserAgentSession:nil
  177. emmSupport:nil
  178. error:nil];
  179. GIDAuthorization *auth = [[GIDAuthorization alloc] initWithKeychainStore:keychainStore
  180. configuration:config
  181. authorizationFlowCoordinator:fakeFlow];
  182. [auth signInWithOptions:options];
  183. // FIXME: Fails for now because the current user check on `GIDAuthorization` goes thru
  184. // `GIDAuthorizationFlowFoke, which has an injected `currentUser` to fake the request
  185. [self waitForExpectations:@[authorizeExpectation] timeout:5];
  186. XCTAssertNotNil(auth.currentUser);
  187. XCTAssertEqualObjects(expectedGoogleUser, auth.currentUser);
  188. }
  189. @end