| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212 |
- // Copyright 2025 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 <XCTest/XCTest.h>
- #import "GoogleSignIn/Sources/GIDAuthorization_Private.h"
- #import "GoogleSignIn/Sources/GIDAuthorizationFlow/Implementations/Fake/GIDAuthorizationFlowFake.h"
- #import "GoogleSignIn/Sources/GIDGoogleUser_Private.h"
- #import "GoogleSignIn/Sources/GIDSignIn_Private.h"
- #import "GoogleSignIn/Sources/GIDSignInInternalOptions.h"
- #import "GoogleSignIn/Sources/GIDSignInResult_Private.h"
- #import "GoogleSignIn/Sources/Public/GoogleSignIn/GIDAuthorization.h"
- #import "GoogleSignIn/Sources/Public/GoogleSignIn/GIDSignInResult.h"
- #import "GoogleSignIn/Tests/Unit/GIDAuthorizationTests/GIDKeychainHelperFake.h"
- #import "GoogleSignIn/Tests/Unit/GIDAuthorizationTests/GIDBundleFake.h"
- #import "GoogleSignIn/Tests/Unit/GIDConfiguration+Testing.h"
- #import "GoogleSignIn/Tests/Unit/GIDProfileData+Testing.h"
- #import "GoogleSignIn/Tests/Unit/OIDAuthState+Testing.h"
- #import "GoogleSignIn/Tests/Unit/OIDAuthorizationRequest+Testing.h"
- static NSString *const kKeychainItemName = @"test_keychain_name";
- @import GTMAppAuth;
- @interface GIDAuthorizationTest : XCTestCase
- @end
- @implementation GIDAuthorizationTest
- - (void)testAuthorizationConfigurationAssertValidParameters {
- GIDConfiguration *config =
- [[GIDConfiguration alloc] initWithClientID:OIDAuthorizationRequestTestingClientID
- serverClientID:kServerClientID
- hostedDomain:kHostedDomain
- openIDRealm:kOpenIDRealm];
-
- GIDSignInInternalOptions *opts = [GIDSignInInternalOptions defaultOptionsWithConfiguration:config
- presentingViewController:nil
- loginHint:nil
- addScopesFlow:NO
- bundle:nil
- completion:nil];
- GIDAuthorizationFlowFake *fakeFlow = [[GIDAuthorizationFlowFake alloc] initWithSignInOptions:opts
- authState:nil
- profileData:nil
- googleUser:nil
- externalUserAgentSession:nil
- emmSupport:nil
- error:nil];
- @try {
- GIDAuthorization *authorization = [[GIDAuthorization alloc] initWithKeychainStore:nil
- configuration:config
- authorizationFlowCoordinator:fakeFlow];
- [authorization assertValidParameters];
- }
- @catch (NSException *exception) {
- XCTFail(@"`authorization` should have valid parameters.");
- }
- @finally {}
- }
- - (void)testAuthorizationConfigurationAssertValidPresentingController {
-
- UIViewController *vc = [[UIViewController alloc] init];
- GIDSignInInternalOptions *opts = [GIDSignInInternalOptions defaultOptionsWithConfiguration:nil
- presentingViewController:vc
- loginHint:nil
- addScopesFlow:NO
- bundle:nil
- completion:nil];
- GIDAuthorizationFlowFake *fakeFlow = [[GIDAuthorizationFlowFake alloc] initWithSignInOptions:opts
- authState:nil
- profileData:nil
- googleUser:nil
- externalUserAgentSession:nil
- emmSupport:nil
- error:nil];
- @try {
- GIDAuthorization *authorization = [[GIDAuthorization alloc] initWithKeychainStore:nil
- configuration:nil
- authorizationFlowCoordinator:fakeFlow];
- [authorization assertValidPresentingController];
- }
- @catch (NSException *exception) {
- XCTFail(@"`authorization` should have a valid presenting controller.");
- }
- @finally {}
- }
- - (void)testThatAuthorizeInteractivelySetsCurrentUser {
- XCTestExpectation *authorizeInteractivelyExpectation =
- [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];
-
- 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);
- [authorizeInteractivelyExpectation fulfill];
- };
-
- GIDBundleFake *bFake = [[GIDBundleFake alloc] init];
- GIDSignInInternalOptions *opts = [GIDSignInInternalOptions defaultOptionsWithConfiguration:config
- presentingViewController:vc
- loginHint:nil
- addScopesFlow:NO
- bundle:bFake
- completion:comp];
- GIDAuthorizationFlowFake *fakeFlow =
- [[GIDAuthorizationFlowFake alloc] initWithSignInOptions:opts
- authState:expectedAuthState
- profileData:expectedProfileData
- googleUser:expectedGoogleUser
- externalUserAgentSession:nil
- emmSupport:nil
- error:nil];
-
- GIDAuthorization *auth = [[GIDAuthorization alloc] initWithKeychainStore:keychainStore
- configuration:config
- authorizationFlowCoordinator:fakeFlow];
- [auth signInWithOptions:opts];
- [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);
- }
- @end
|