| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128 |
- // Copyright 2021 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 <TargetConditionals.h>
- #if TARGET_OS_IOS || TARGET_OS_MACCATALYST
- #import <UIKit/UIKit.h>
- #elif TARGET_OS_OSX
- #import <AppKit/AppKit.h>
- #endif // TARGET_OS_IOS || TARGET_OS_MACCATALYST
- #import <SafariServices/SafariServices.h>
- #import <XCTest/XCTest.h>
- // Test module imports
- @import GoogleSignIn;
- #import "GoogleSignIn/Sources/GIDAuthorizationFlowProcessor/API/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"
- #import "GoogleSignIn/Sources/GIDSignInPreferences.h"
- #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/API/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"
- #ifdef SWIFT_PACKAGE
- @import AppAuth;
- @import GTMAppAuth;
- @import GTMSessionFetcherCore;
- @import OCMock;
- #else
- #import <AppAuth/OIDAuthState.h>
- #import <AppAuth/OIDAuthorizationRequest.h>
- #import <AppAuth/OIDAuthorizationResponse.h>
- #import <AppAuth/OIDAuthorizationService.h>
- #import <AppAuth/OIDError.h>
- #import <AppAuth/OIDGrantTypes.h>
- #import <AppAuth/OIDTokenRequest.h>
- #import <AppAuth/OIDTokenResponse.h>
- #import <AppAuth/OIDURLQueryComponent.h>
- #if TARGET_OS_IOS || TARGET_OS_MACCATALYST
- #import <AppAuth/OIDAuthorizationService+IOS.h>
- #elif TARGET_OS_OSX
- #import <AppAuth/OIDAuthorizationService+Mac.h>
- #endif // TARGET_OS_IOS || TARGET_OS_MACCATALYST
- #import <GTMAppAuth/GTMAppAuthFetcherAuthorization+Keychain.h>
- #import <GTMAppAuth/GTMAppAuthFetcherAuthorization.h>
- #import <GTMSessionFetcher/GTMSessionFetcher.h>
- #import <OCMock/OCMock.h>
- #endif
- // Create a BLOCK to store the actual address for arg in param.
- #define SAVE_TO_ARG_BLOCK(param) [OCMArg checkWithBlock:^(id arg) {\
- param = arg;\
- return YES;\
- }]
- #define COPY_TO_ARG_BLOCK(param) [OCMArg checkWithBlock:^(id arg) {\
- param = [arg copy];\
- return YES;\
- }]
- static NSString * const kFakeGaiaID = @"123456789";
- static NSString * const kFakeIDToken = @"FakeIDToken";
- static NSString * const kClientId = @"FakeClientID";
- static NSString * const kDotReversedClientId = @"FakeClientID";
- static NSString * const kClientId2 = @"FakeClientID2";
- static NSString * const kServerClientId = @"FakeServerClientID";
- static NSString * const kLanguage = @"FakeLanguage";
- static NSString * const kAuthCode = @"FakeAuthCode";
- static NSString * const kKeychainName = @"auth";
- static NSString * const kUserEmail = @"FakeUserEmail";
- static NSString * const kVerifier = @"FakeVerifier";
- static NSString * const kOpenIDRealm = @"FakeRealm";
- static NSString * const kFakeHostedDomain = @"fakehosteddomain.com";
- static NSString * const kFakeUserName = @"fake username";
- static NSString * const kFakeUserGivenName = @"fake";
- static NSString * const kFakeUserFamilyName = @"username";
- static NSString * const kFakeUserPictureURL = @"fake_user_picture_url";
- static NSString * const kContinueURL = @"com.google.UnitTests:/oauth2callback";
- static NSString * const kContinueURLWithClientID = @"FakeClientID:/oauth2callback";
- static NSString * const kWrongSchemeURL = @"wrong.app:/oauth2callback";
- static NSString * const kWrongPathURL = @"com.google.UnitTests:/wrong_path";
- static NSString * const kEMMRestartAuthURL =
- @"com.google.UnitTests:///emmcallback?action=restart_auth";
- static NSString * const kEMMWrongPathURL =
- @"com.google.UnitTests:///unknowcallback?action=restart_auth";
- static NSString * const kEMMWrongActionURL =
- @"com.google.UnitTests:///emmcallback?action=unrecognized";
- static NSString * const kDevicePolicyAppBundleID = @"com.google.DevicePolicy";
- static NSString * const kAppHasRunBeforeKey = @"GPP_AppHasRunBefore";
- static NSString * const kFingerprintKeychainName = @"fingerprint";
- static NSString * const kVerifierKeychainName = @"verifier";
- static NSString * const kVerifierKey = @"verifier";
- //static NSString * const kOpenIDRealmKey = @"openid.realm";
- static NSString * const kSavedKeychainServiceName = @"saved-keychain";
- static NSString * const kKeychainAccountName = @"GooglePlus";
- static NSString * const kUserNameKey = @"name";
- static NSString * const kUserGivenNameKey = @"givenName";
- static NSString * const kUserFamilyNameKey = @"familyName";
- static NSString * const kUserImageKey = @"picture";
- static NSString * const kAppName = @"UnitTests";
- static NSString * const kUserIDKey = @"userID";
- static NSString * const kHostedDomainKey = @"hostedDomain";
- static NSString * const kIDTokenExpirationKey = @"idTokenExp";
- static NSString * const kCustomKeychainName = @"CUSTOM_KEYCHAIN_NAME";
- static NSString * const kAddActivity = @"http://schemas.google.com/AddActivity";
- static NSString * const kErrorDomain = @"ERROR_DOMAIN";
- static NSInteger const kErrorCode = 212;
- static NSString *const kDriveScope = @"https://www.googleapis.com/auth/drive";
- static NSString *const kTokenURL = @"https://oauth2.googleapis.com/token";
- static NSString *const kFakeURL = @"http://foo.com";
- static NSString *const kNewScope = @"newScope";
- #if TARGET_OS_IOS || TARGET_OS_MACCATALYST
- // This category is used to allow the test to swizzle a private method.
- @interface UIViewController (Testing)
- // This private method provides access to the window. It's declared here to avoid a warning about
- // an unrecognized selector in the test.
- - (UIWindow *)_window;
- @end
- #endif // TARGET_OS_IOS || TARGET_OS_MACCATALYST
- // This class extension exposes GIDSignIn methods to our tests.
- @interface GIDSignIn ()
- // Exposing private method so we can call it to disambiguate between interactive and non-interactive
- // sign-in attempts for the purposes of testing the GIDSignInUIDelegate (which should not be
- // called in the case of a non-interactive sign in).
- - (void)authenticateMaybeInteractively:(BOOL)interactive withParams:(NSDictionary *)params;
- - (BOOL)assertValidPresentingViewContoller;
- @end
- @interface GIDSignInTest : XCTestCase {
- @private
- // Whether or not the OS version is eligible for EMM.
- BOOL _isEligibleForEMM;
- // Mock `OIDAuthState`.
- id _authState;
- // Mock `OIDTokenResponse`.
- id _tokenResponse;
- // Mock `OIDTokenRequest`.
- id _tokenRequest;
- // Mock `GTMAppAuthFetcherAuthorization`.
- id _authorization;
-
- // Fake for `GIDKeychainHandler`.
- GIDFakeKeychainHandler *_keychainHandler;
- // Fake for `GIDHTTPFetcher`.
- GIDFakeHTTPFetcher *_httpFetcher;
-
- // Fake for `GIDAuthorizationFlowProcessor`.
- GIDFakeAuthorizationFlowProcessor *_authorizationFlowProcessor;
- // Fake for `GIDProfileDataFetcher`.
- GIDFakeProfileDataFetcher *_profileDataFetcher;
-
- #if TARGET_OS_IOS || TARGET_OS_MACCATALYST
- // Mock `UIViewController`.
- id _presentingViewController;
- #elif TARGET_OS_OSX
- // Mock `NSWindow`.
- id _presentingWindow;
- #endif // TARGET_OS_IOS || TARGET_OS_MACCATALYST
- // Mock for `GIDGoogleUser`.
- id _user;
- // Mock for `OIDAuthorizationService`.
- id _oidAuthorizationService;
- // Parameter saved from delegate call.
- NSError *_authError;
- // Whether callback block has been called.
- BOOL _completionCalled;
- // Fake [NSBundle mainBundle];
- GIDFakeMainBundle *_fakeMainBundle;
- // The `GIDSignIn` object being tested.
- GIDSignIn *_signIn;
- // The configuration to be used when testing `GIDSignIn`.
- GIDConfiguration *_configuration;
- // The completion to be used when testing `GIDSignIn`.
- GIDSignInCompletion _completion;
- // The saved token request callback.
- OIDTokenCallback _savedTokenCallback;
- }
- @end
- @implementation GIDSignInTest
- #pragma mark - Lifecycle
- - (void)setUp {
- [super setUp];
- #if TARGET_OS_IOS && !TARGET_OS_MACCATALYST
- _isEligibleForEMM = [UIDevice currentDevice].systemVersion.integerValue >= 9;
- #endif // TARGET_OS_IOS && !TARGET_OS_MACCATALYST
- // States
- _completionCalled = NO;
- // Mocks
- #if TARGET_OS_IOS || TARGET_OS_MACCATALYST
- _presentingViewController = OCMStrictClassMock([UIViewController class]);
- #elif TARGET_OS_OSX
- _presentingWindow = OCMStrictClassMock([NSWindow class]);
- #endif // TARGET_OS_IOS || TARGET_OS_MACCATALYST
- _authState = OCMStrictClassMock([OIDAuthState class]);
- OCMStub([_authState alloc]).andReturn(_authState);
- OCMStub([_authState initWithAuthorizationResponse:OCMOCK_ANY]).andReturn(_authState);
- _tokenResponse = OCMStrictClassMock([OIDTokenResponse class]);
- _tokenRequest = OCMStrictClassMock([OIDTokenRequest class]);
- _authorization = OCMStrictClassMock([GTMAppAuthFetcherAuthorization class]);
- OCMStub([_authorization alloc]).andReturn(_authorization);
- OCMStub([_authorization initWithAuthState:OCMOCK_ANY]).andReturn(_authorization);
- _user = OCMStrictClassMock([GIDGoogleUser class]);
- _oidAuthorizationService = OCMStrictClassMock([OIDAuthorizationService class]);
- OCMStub([self->_oidAuthorizationService
- performTokenRequest:OCMOCK_ANY
- callback:COPY_TO_ARG_BLOCK(self->_savedTokenCallback)]);
- // Fakes
- _fakeMainBundle = [[GIDFakeMainBundle alloc] init];
- [_fakeMainBundle startFakingWithClientID:kClientId];
- [_fakeMainBundle fakeAllSchemesSupported];
- // Object under test
- [[NSUserDefaults standardUserDefaults] setBool:YES
- forKey:kAppHasRunBeforeKey];
- _keychainHandler = [[GIDFakeKeychainHandler alloc] init];
-
- _httpFetcher = [[GIDFakeHTTPFetcher alloc] init];
-
- _authorizationFlowProcessor = [[GIDFakeAuthorizationFlowProcessor alloc] init];
-
- _profileDataFetcher = [[GIDFakeProfileDataFetcher alloc] init];
-
- _signIn = [[GIDSignIn alloc] initWithKeychainHandler:_keychainHandler
- httpFetcher:_httpFetcher
- profileDataFetcher:_profileDataFetcher
- authorizationFlowProcessor:_authorizationFlowProcessor];
- __weak GIDSignInTest *weakSelf = self;
- _completion = ^(GIDSignInResult *_Nullable signInResult, NSError * _Nullable error) {
- GIDSignInTest *strongSelf = weakSelf;
- if (!signInResult) {
- XCTAssertNotNil(error, @"should have an error if the signInResult is nil");
- }
- XCTAssertFalse(strongSelf->_completionCalled, @"callback already called");
- strongSelf->_completionCalled = YES;
- strongSelf->_authError = error;
- };
- }
- - (void)tearDown {
- OCMVerifyAll(_authState);
- OCMVerifyAll(_tokenResponse);
- OCMVerifyAll(_tokenRequest);
- OCMVerifyAll(_user);
- // OCMVerifyAll(_oidAuthorizationService);
- #if TARGET_OS_IOS || TARGET_OS_MACCATALYST
- OCMVerifyAll(_presentingViewController);
- #elif TARGET_OS_OSX
- OCMVerifyAll(_presentingWindow);
- #endif // TARGET_OS_IOS || TARGET_OS_MACCATALYST
- [_fakeMainBundle stopFaking];
- [super tearDown];
- }
- #pragma mark - Tests
- - (void)testShareInstance {
- GIDSignIn *signIn1 = GIDSignIn.sharedInstance;
- GIDSignIn *signIn2 = GIDSignIn.sharedInstance;
- XCTAssertTrue(signIn1 == signIn2, @"shared instance must be singleton");
- }
- - (void)testInitPrivate {
- GIDSignIn *signIn = [[GIDSignIn alloc] initPrivate];
- XCTAssertNotNil(signIn.configuration);
- XCTAssertEqual(signIn.configuration.clientID, kClientId);
- XCTAssertNil(signIn.configuration.serverClientID);
- XCTAssertNil(signIn.configuration.hostedDomain);
- XCTAssertNil(signIn.configuration.openIDRealm);
- }
- - (void)testInitPrivate_noConfig {
- [_fakeMainBundle fakeWithClientID:nil
- serverClientID:nil
- hostedDomain:nil
- openIDRealm:nil];
- GIDSignIn *signIn = [[GIDSignIn alloc] initPrivate];
- XCTAssertNil(signIn.configuration);
- }
- - (void)testInitPrivate_fullConfig {
- [_fakeMainBundle fakeWithClientID:kClientId
- serverClientID:kServerClientId
- hostedDomain:kFakeHostedDomain
- openIDRealm:kOpenIDRealm];
- GIDSignIn *signIn = [[GIDSignIn alloc] initPrivate];
- XCTAssertNotNil(signIn.configuration);
- XCTAssertEqual(signIn.configuration.clientID, kClientId);
- XCTAssertEqual(signIn.configuration.serverClientID, kServerClientId);
- XCTAssertEqual(signIn.configuration.hostedDomain, kFakeHostedDomain);
- XCTAssertEqual(signIn.configuration.openIDRealm, kOpenIDRealm);
- }
- - (void)testInitPrivate_invalidConfig {
- [_fakeMainBundle fakeWithClientID:@[ @"bad", @"config", @"values" ]
- serverClientID:nil
- hostedDomain:nil
- openIDRealm:nil];
- GIDSignIn *signIn = [[GIDSignIn alloc] initPrivate];
- XCTAssertNil(signIn.configuration);
- }
- - (void)testRestorePreviousSignInNoRefresh_hasPreviousUser {
- [[[_authorization stub] andReturn:_authState] authState];
- [[_authorization expect] setTokenRefreshDelegate:OCMOCK_ANY];
- OCMStub([_authState lastTokenResponse]).andReturn(_tokenResponse);
- OCMStub([_authState refreshToken]).andReturn(kRefreshToken);
- [[_authState expect] setStateChangeDelegate:OCMOCK_ANY];
- [_keychainHandler saveAuthState:_authState];
- id idTokenDecoded = OCMClassMock([OIDIDToken class]);
- OCMStub([idTokenDecoded alloc]).andReturn(idTokenDecoded);
- OCMStub([idTokenDecoded initWithIDTokenString:OCMOCK_ANY]).andReturn(idTokenDecoded);
- OCMStub([idTokenDecoded subject]).andReturn(kFakeGaiaID);
-
- // Mock generating a GIDConfiguration when initializing GIDGoogleUser.
- OIDAuthorizationResponse *authResponse =
- [OIDAuthorizationResponse testInstanceWithAdditionalParameters:nil
- errorString:nil];
-
- OCMStub([_authState lastAuthorizationResponse]).andReturn(authResponse);
- OCMStub([_tokenResponse idToken]).andReturn(kFakeIDToken);
- OCMStub([_tokenResponse accessToken]).andReturn(kAccessToken);
- OCMStub([_tokenResponse accessTokenExpirationDate]).andReturn(nil);
-
- GIDProfileData *fakeProfileData = [GIDProfileData testInstance];
- GIDProfileDataFetcherTestBlock testBlock = ^(GIDProfileDataFetcherFakeResponseProvider
- responseProvider) {
- responseProvider(fakeProfileData, nil);
- };
-
- _profileDataFetcher.testBlock = testBlock;
- [_signIn restorePreviousSignInNoRefresh];
- XCTAssertEqual(_signIn.currentUser.userID, kFakeGaiaID);
-
- XCTAssertEqualObjects(_signIn.currentUser.profile, fakeProfileData);
- [idTokenDecoded stopMocking];
- }
- - (void)testRestoredPreviousSignInNoRefresh_hasNoPreviousUser {
- XCTAssertNil([_keychainHandler loadAuthState]);
- [_signIn restorePreviousSignInNoRefresh];
- XCTAssertNil(_signIn.currentUser);
- }
- - (void)testHasPreviousSignIn_HasBeenAuthenticated {
- [_keychainHandler saveAuthState:_authState];
- [[[_authState expect] andReturnValue:[NSNumber numberWithBool:YES]] isAuthorized];
- XCTAssertTrue([_signIn hasPreviousSignIn], @"should return |YES|");
- [_authState verify];
- XCTAssertFalse(_completionCalled, @"should not call delegate");
- XCTAssertNil(_authError, @"should have no error");
- }
- - (void)testHasPreviousSignIn_HasNotBeenAuthenticated {
- [_keychainHandler saveAuthState:_authState];
- [[[_authState expect] andReturnValue:[NSNumber numberWithBool:NO]] isAuthorized];
- XCTAssertFalse([_signIn hasPreviousSignIn], @"should return |NO|");
- [_authState verify];
- XCTAssertFalse(_completionCalled, @"should not call delegate");
- }
- - (void)testRestorePreviousSignInWhenSignedOut {
- [_keychainHandler saveAuthState:_authState];
- [[[_authState expect] andReturnValue:[NSNumber numberWithBool:NO]] isAuthorized];
- _completionCalled = NO;
- _authError = nil;
- XCTestExpectation *expectation = [self expectationWithDescription:@"Callback should be called."];
- [_signIn restorePreviousSignInWithCompletion:^(GIDGoogleUser *_Nullable user,
- NSError * _Nullable error) {
- [expectation fulfill];
- XCTAssertNotNil(error, @"error should not have been nil");
- XCTAssertEqual(error.domain,
- kGIDSignInErrorDomain,
- @"error domain should have been the sign-in error domain.");
- XCTAssertEqual(error.code,
- kGIDSignInErrorCodeHasNoAuthInKeychain,
- @"error code should have been the 'NoAuthInKeychain' error code.");
- }];
- [self waitForExpectationsWithTimeout:1 handler:nil];
- [_authState verify];
- }
- - (void)testOAuthLogin {
- [self OAuthLoginWithAddScopesFlow:NO
- authError:nil
- tokenError:nil
- emmPasscodeInfoRequired:NO
- keychainError:NO
- restoredSignIn:NO
- oldAccessToken:NO
- modalCancel:NO];
- }
- - (void)testOAuthLogin_RestoredSignIn {
- [self OAuthLoginWithAddScopesFlow:NO
- authError:nil
- tokenError:nil
- emmPasscodeInfoRequired:NO
- keychainError:NO
- restoredSignIn:YES
- oldAccessToken:NO
- modalCancel:NO];
- }
- - (void)testOAuthLogin_RestoredSignInOldAccessToken {
- [self OAuthLoginWithAddScopesFlow:NO
- authError:nil
- tokenError:nil
- emmPasscodeInfoRequired:NO
- keychainError:NO
- restoredSignIn:YES
- oldAccessToken:YES
- modalCancel:NO];
- }
- - (void)testOAuthLogin_ConsentCanceled {
- [self OAuthLoginWithAddScopesFlow:NO
- authError:@"access_denied"
- tokenError:nil
- emmPasscodeInfoRequired:NO
- keychainError:NO
- restoredSignIn:NO
- oldAccessToken:NO
- modalCancel:NO];
- [self waitForExpectationsWithTimeout:1 handler:nil];
- XCTAssertTrue(_completionCalled, @"should call delegate");
- XCTAssertEqual(_authError.code, kGIDSignInErrorCodeCanceled);
- }
- - (void)testOAuthLogin_ModalCanceled {
- [self OAuthLoginWithAddScopesFlow:NO
- authError:nil
- tokenError:nil
- emmPasscodeInfoRequired:NO
- keychainError:NO
- restoredSignIn:NO
- oldAccessToken:NO
- modalCancel:YES];
- [self waitForExpectationsWithTimeout:1 handler:nil];
- XCTAssertTrue(_completionCalled, @"should call delegate");
- XCTAssertEqual(_authError.code, kGIDSignInErrorCodeCanceled);
- }
- - (void)testOAuthLogin_KeychainError {
- [self OAuthLoginWithAddScopesFlow:NO
- authError:nil
- tokenError:nil
- emmPasscodeInfoRequired:NO
- keychainError:YES
- restoredSignIn:NO
- oldAccessToken:NO
- modalCancel:NO];
- [self waitForExpectationsWithTimeout:1 handler:nil];
- XCTAssertTrue(_completionCalled, @"should call delegate");
- XCTAssertEqualObjects(_authError.domain, kGIDSignInErrorDomain);
- XCTAssertEqual(_authError.code, kGIDSignInErrorCodeKeychain);
- }
- - (void)testSignOut {
- XCTAssert([_keychainHandler saveAuthState:_authState]);
- // Sign in a user so that we can then sign them out.
- [self OAuthLoginWithAddScopesFlow:NO
- authError:nil
- tokenError:nil
- emmPasscodeInfoRequired:NO
- keychainError:NO
- restoredSignIn:YES
- oldAccessToken:NO
- modalCancel:NO];
- XCTAssertNotNil(_signIn.currentUser);
- XCTAssertNotNil([_keychainHandler loadAuthState]);
- [_signIn signOut];
- XCTAssertNil(_signIn.currentUser, @"should not have a current user");
- XCTAssertNil([_keychainHandler loadAuthState]);
- }
- - (void)testNotHandleWrongPath {
- XCTAssertFalse([_signIn handleURL:[NSURL URLWithString:kWrongPathURL]], @"should not handle URL");
- XCTAssertFalse(_completionCalled, @"should not call delegate");
- }
- #pragma mark - Tests - disconnectWithCallback:
- // Verifies disconnect calls callback with no errors if access token is present.
- - (void)testDisconnect_accessTokenIsPresent {
- [_keychainHandler saveAuthState:_authState];
- OCMStub([_authState lastTokenResponse]).andReturn(_tokenResponse);
- OCMStub([_tokenResponse accessToken]).andReturn(kAccessToken);
-
- XCTestExpectation *fetcherExpectation =
- [self expectationWithDescription:@"testBlock is invoked."];
- GIDHTTPFetcherTestBlock testBlock =
- ^(NSURLRequest *request, GIDHTTPFetcherFakeResponseProviderBlock responseProvider) {
- [self verifyRevokeRequest:request withToken:kAccessToken];
- NSData *data = [[NSData alloc] init];
- responseProvider(data, nil);
- [fetcherExpectation fulfill];
- };
- [_httpFetcher setTestBlock:testBlock];
-
- XCTestExpectation *completionExpectation =
- [self expectationWithDescription:@"Callback called with nil error"];
- [_signIn disconnectWithCompletion:^(NSError * _Nullable error) {
- XCTAssertNil(error);
- [completionExpectation fulfill];
- }];
- [self waitForExpectationsWithTimeout:1 handler:nil];
- XCTAssertNil([_keychainHandler loadAuthState]);
- }
- // Verifies disconnect if access token is present.
- - (void)testDisconnectNoCallback_accessTokenIsPresent {
- [_keychainHandler saveAuthState:_authState];
- OCMStub([_authState lastTokenResponse]).andReturn(_tokenResponse);
- OCMStub([_tokenResponse accessToken]).andReturn(kAccessToken);
-
- XCTestExpectation *fetcherExpectation =
- [self expectationWithDescription:@"testBlock is invoked."];
- GIDHTTPFetcherTestBlock testBlock =
- ^(NSURLRequest *request, GIDHTTPFetcherFakeResponseProviderBlock responseProvider) {
- [self verifyRevokeRequest:request withToken:kAccessToken];
- NSData *data = [[NSData alloc] init];
- responseProvider(data, nil);
- [fetcherExpectation fulfill];
- };
- [_httpFetcher setTestBlock:testBlock];
-
- [_signIn disconnectWithCompletion:nil];
- [self waitForExpectationsWithTimeout:1 handler:nil];
- XCTAssertNil([_keychainHandler loadAuthState]);
- }
- // Verifies disconnect calls callback with no errors if refresh token is present.
- - (void)testDisconnect_refreshTokenIsPresent {
- [_keychainHandler saveAuthState:_authState];
- OCMStub([_authState lastTokenResponse]).andReturn(_tokenResponse);
- OCMStub([_tokenResponse accessToken]).andReturn(nil);
- OCMStub([_tokenResponse refreshToken]).andReturn(kRefreshToken);
-
- XCTestExpectation *fetcherExpectation =
- [self expectationWithDescription:@"testBlock is invoked."];
- GIDHTTPFetcherTestBlock testBlock =
- ^(NSURLRequest *request, GIDHTTPFetcherFakeResponseProviderBlock responseProvider) {
- [self verifyRevokeRequest:request withToken:kRefreshToken];
- NSData *data = [[NSData alloc] init];
- responseProvider(data, nil);
- [fetcherExpectation fulfill];
- };
- [_httpFetcher setTestBlock:testBlock];
-
- XCTestExpectation *completionExpectation =
- [self expectationWithDescription:@"Callback called with nil error"];
- [_signIn disconnectWithCompletion:^(NSError * _Nullable error) {
- XCTAssertNil(error);
- [completionExpectation fulfill];
- }];
- [self waitForExpectationsWithTimeout:1 handler:nil];
- XCTAssertNil([_keychainHandler loadAuthState]);
- }
- // Verifies disconnect errors are passed along to the callback.
- - (void)testDisconnect_errors {
- [_keychainHandler saveAuthState:_authState];
- OCMStub([_authState lastTokenResponse]).andReturn(_tokenResponse);
- OCMStub([_tokenResponse accessToken]).andReturn(kAccessToken);
-
- XCTestExpectation *fetcherExpectation =
- [self expectationWithDescription:@"testBlock is invoked."];
- GIDHTTPFetcherTestBlock testBlock =
- ^(NSURLRequest *request, GIDHTTPFetcherFakeResponseProviderBlock responseProvider) {
- [self verifyRevokeRequest:request withToken:kAccessToken];
- NSError *error = [self error];
- responseProvider(nil, error);
- [fetcherExpectation fulfill];
- };
- [_httpFetcher setTestBlock:testBlock];
-
- XCTestExpectation *completionExpectation =
- [self expectationWithDescription:@"Callback called with an error"];
- [_signIn disconnectWithCompletion:^(NSError * _Nullable error) {
- XCTAssertNotNil(error);
- [completionExpectation fulfill];
- }];
- [self waitForExpectationsWithTimeout:1 handler:nil];
- XCTAssertNotNil([_keychainHandler loadAuthState]);
- }
- // Verifies disconnect with errors
- - (void)testDisconnectNoCallback_errors {
- [_keychainHandler saveAuthState:_authState];
- OCMStub([_authState lastTokenResponse]).andReturn(_tokenResponse);
- OCMStub([_tokenResponse accessToken]).andReturn(kAccessToken);
-
- XCTestExpectation *fetcherExpectation =
- [self expectationWithDescription:@"testBlock is invoked."];
- GIDHTTPFetcherTestBlock testBlock =
- ^(NSURLRequest *request, GIDHTTPFetcherFakeResponseProviderBlock responseProvider) {
- [self verifyRevokeRequest:request withToken:kAccessToken];
- NSError *error = [self error];
- responseProvider(nil, error);
- [fetcherExpectation fulfill];
- };
- [_httpFetcher setTestBlock:testBlock];
-
- [_signIn disconnectWithCompletion:nil];
- [self waitForExpectationsWithTimeout:1 handler:nil];
- XCTAssertNotNil([_keychainHandler loadAuthState]);
- }
- // Verifies disconnect calls callback with no errors and clears keychain if no tokens are present.
- - (void)testDisconnect_noTokens {
- [_keychainHandler saveAuthState:_authState];
- OCMStub([_authState lastTokenResponse]).andReturn(_tokenResponse);
- OCMStub([_tokenResponse accessToken]).andReturn(nil);
- OCMStub([_tokenResponse refreshToken]).andReturn(nil);
- GIDHTTPFetcherTestBlock testBlock =
- ^(NSURLRequest *request, GIDHTTPFetcherFakeResponseProviderBlock responseProvider) {
- XCTFail(@"_httpFetcher should not be invoked.");
- };
- [_httpFetcher setTestBlock:testBlock];
-
- XCTestExpectation *expectation =
- [self expectationWithDescription:@"Callback called with nil error"];
- [_signIn disconnectWithCompletion:^(NSError * _Nullable error) {
- XCTAssertNil(error);
- [expectation fulfill];
- }];
- [self waitForExpectationsWithTimeout:1 handler:nil];
- XCTAssertNil([_keychainHandler loadAuthState]);
- }
- // Verifies disconnect clears keychain if no tokens are present.
- - (void)testDisconnectNoCallback_noTokens {
- [_keychainHandler saveAuthState:_authState];
- OCMStub([_authState lastTokenResponse]).andReturn(_tokenResponse);
- OCMStub([_tokenResponse accessToken]).andReturn(nil);
- OCMStub([_tokenResponse refreshToken]).andReturn(nil);
- GIDHTTPFetcherTestBlock testBlock =
- ^(NSURLRequest *request, GIDHTTPFetcherFakeResponseProviderBlock responseProvider) {
- XCTFail(@"_httpFetcher should not be invoked.");
- };
- [_httpFetcher setTestBlock:testBlock];
-
- [_signIn disconnectWithCompletion:nil];
- XCTAssertNil([_keychainHandler loadAuthState]);
- }
- - (void)testPresentingViewControllerException {
- #if TARGET_OS_IOS || TARGET_OS_MACCATALYST
- _presentingViewController = nil;
- #elif TARGET_OS_OSX
- _presentingWindow = nil;
- #endif // TARGET_OS_IOS || TARGET_OS_MACCATALYST
- #if TARGET_OS_IOS || TARGET_OS_MACCATALYST
- XCTAssertThrows([_signIn signInWithPresentingViewController:_presentingViewController
- #elif TARGET_OS_OSX
- XCTAssertThrows([_signIn signInWithPresentingWindow:_presentingWindow
- #endif // TARGET_OS_IOS || TARGET_OS_MACCATALYST
- hint:nil
- completion:_completion]);
- }
- - (void)testClientIDMissingException {
- #pragma GCC diagnostic push
- #pragma GCC diagnostic ignored "-Wnonnull"
- _signIn.configuration = [[GIDConfiguration alloc] initWithClientID:nil];
- #pragma GCC diagnostic pop
- BOOL threw = NO;
- @try {
- #if TARGET_OS_IOS || TARGET_OS_MACCATALYST
- [_signIn signInWithPresentingViewController:_presentingViewController
- #elif TARGET_OS_OSX
- [_signIn signInWithPresentingWindow:_presentingWindow
- #endif // TARGET_OS_IOS || TARGET_OS_MACCATALYST
- completion:nil];
- } @catch (NSException *exception) {
- threw = YES;
- XCTAssertEqualObjects(exception.description,
- @"You must specify |clientID| in |GIDConfiguration|");
- } @finally {
- }
- XCTAssert(threw);
- }
- - (void)testSchemesNotSupportedException {
- [_fakeMainBundle fakeMissingAllSchemes];
- BOOL threw = NO;
- @try {
- #if TARGET_OS_IOS || TARGET_OS_MACCATALYST
- [_signIn signInWithPresentingViewController:_presentingViewController
- #elif TARGET_OS_OSX
- [_signIn signInWithPresentingWindow:_presentingWindow
- #endif // TARGET_OS_IOS || TARGET_OS_MACCATALYST
- hint:nil
- completion:_completion];
- } @catch (NSException *exception) {
- threw = YES;
- XCTAssertEqualObjects(exception.description,
- @"Your app is missing support for the following URL schemes: "
- "fakeclientid");
- } @finally {
- }
- XCTAssert(threw);
- }
- #pragma mark - Restarting Authentication Tests
- // Verifies that URL is not handled if there is no pending sign-in
- - (void)testRequiringPendingSignIn {
- BOOL result = [_signIn handleURL:[NSURL URLWithString:kEMMRestartAuthURL]];
- XCTAssertFalse(result);
- }
- #pragma mark - EMM tests
- #if TARGET_OS_IOS && !TARGET_OS_MACCATALYST
- - (void)testAuthEndpointEMMError {
- if (!_isEligibleForEMM) {
- return;
- }
- id mockEMMErrorHandler = OCMStrictClassMock([GIDEMMErrorHandler class]);
- [[[mockEMMErrorHandler stub] andReturn:mockEMMErrorHandler] sharedInstance];
- __block void (^completion)(void);
- NSDictionary<NSString *, NSString *> *callbackParams = @{ @"error" : @"EMM Specific Error" };
- [[[mockEMMErrorHandler expect] andReturnValue:@YES]
- handleErrorFromResponse:callbackParams completion:SAVE_TO_ARG_BLOCK(completion)];
- [self OAuthLoginWithAddScopesFlow:NO
- authError:callbackParams[@"error"]
- tokenError:nil
- emmPasscodeInfoRequired:NO
- keychainError:NO
- restoredSignIn:NO
- oldAccessToken:NO
- modalCancel:NO];
- [mockEMMErrorHandler verify];
- [mockEMMErrorHandler stopMocking];
- completion();
- [self waitForExpectationsWithTimeout:1 handler:nil];
- XCTAssertTrue(_completionCalled, @"should call delegate");
- XCTAssertNotNil(_authError, @"should have error");
- XCTAssertEqualObjects(_authError.domain, kGIDSignInErrorDomain);
- XCTAssertEqual(_authError.code, kGIDSignInErrorCodeEMM);
- XCTAssertNil(_signIn.currentUser, @"should not have current user");
- }
- - (void)testTokenEndpointEMMError {
- if (!_isEligibleForEMM) {
- return;
- }
- __block void (^completion)(NSError *);
- NSDictionary *errorJSON = @{ @"error" : @"EMM Specific Error" };
- NSError *emmError = [NSError errorWithDomain:@"anydomain"
- code:12345
- userInfo:@{ OIDOAuthErrorFieldError : errorJSON }];
- id emmSupport = OCMStrictClassMock([GIDEMMSupport class]);
- [[emmSupport expect] handleTokenFetchEMMError:emmError
- completion:SAVE_TO_ARG_BLOCK(completion)];
- [self OAuthLoginWithAddScopesFlow:NO
- authError:nil
- tokenError:emmError
- emmPasscodeInfoRequired:NO
- keychainError:NO
- restoredSignIn:NO
- oldAccessToken:NO
- modalCancel:NO];
- NSError *handledError = [NSError errorWithDomain:kGIDSignInErrorDomain
- code:kGIDSignInErrorCodeEMM
- userInfo:emmError.userInfo];
-
- completion(handledError);
- [self waitForExpectationsWithTimeout:1 handler:nil];
- [emmSupport verify];
- XCTAssertTrue(_completionCalled, @"should call delegate");
- XCTAssertNotNil(_authError, @"should have error");
- XCTAssertEqualObjects(_authError.domain, kGIDSignInErrorDomain);
- XCTAssertEqual(_authError.code, kGIDSignInErrorCodeEMM);
- XCTAssertNil(_signIn.currentUser, @"should not have current user");
- }
- #endif // TARGET_OS_IOS && !TARGET_OS_MACCATALYST
- #pragma mark - Helpers
- - (NSError *)error {
- return [NSError errorWithDomain:kErrorDomain code:kErrorCode userInfo:nil];
- }
- - (void)verifyRevokeRequest:(NSURLRequest *)request withToken:(NSString *)token {
- NSURL *url = request.URL;
- XCTAssertEqualObjects([url scheme], @"https", @"scheme must match");
- XCTAssertEqualObjects([url host], @"accounts.google.com", @"host must match");
- XCTAssertEqualObjects([url path], @"/o/oauth2/revoke", @"path must match");
- OIDURLQueryComponent *queryComponent = [[OIDURLQueryComponent alloc] initWithURL:url];
- NSDictionary<NSString *, NSObject<NSCopying> *> *params = queryComponent.dictionaryValue;
- XCTAssertEqualObjects([params valueForKey:kSDKVersionLoggingParameter], GIDVersion(),
- @"SDK version logging parameter should match");
- XCTAssertEqualObjects([params valueForKey:kEnvironmentLoggingParameter], GIDEnvironment(),
- @"Environment logging parameter should match");
- NSData *body = request.HTTPBody;
- NSString* bodyString = [[NSString alloc] initWithData:body encoding:NSUTF8StringEncoding];
- NSArray<NSString *> *strings = [bodyString componentsSeparatedByString:@"="];
- XCTAssertEqualObjects(strings[1], token);
- }
- // The authorization flow with parameters to control which branches to take.
- - (void)OAuthLoginWithAddScopesFlow:(BOOL)addScopesFlow
- authError:(NSString *)authError
- tokenError:(NSError *)tokenError
- emmPasscodeInfoRequired:(BOOL)emmPasscodeInfoRequired
- keychainError:(BOOL)keychainError
- restoredSignIn:(BOOL)restoredSignIn
- oldAccessToken:(BOOL)oldAccessToken
- modalCancel:(BOOL)modalCancel {
- if (restoredSignIn) {
- // clearAndAuthenticateWithOptions
- [_keychainHandler saveAuthState:_authState];
- BOOL isAuthorized = restoredSignIn ? YES : NO;
- [[[_authState expect] andReturnValue:[NSNumber numberWithBool:isAuthorized]] isAuthorized];
- }
- NSDictionary<NSString *, NSString *> *additionalParameters = emmPasscodeInfoRequired ?
- @{ @"emm_passcode_info_required" : @"1" } : nil;
- OIDAuthorizationResponse *authResponse =
- [OIDAuthorizationResponse testInstanceWithAdditionalParameters:additionalParameters
- errorString:authError];
- OIDTokenResponse *tokenResponse =
- [OIDTokenResponse testInstanceWithIDToken:[OIDTokenResponse fatIDToken]
- accessToken:restoredSignIn ? kAccessToken : nil
- expiresIn:oldAccessToken ? @(300) : nil
- refreshToken:kRefreshToken
- tokenRequest:nil];
- OIDTokenRequest *tokenRequest = [[OIDTokenRequest alloc]
- initWithConfiguration:authResponse.request.configuration
- grantType:OIDGrantTypeRefreshToken
- authorizationCode:nil
- redirectURL:nil
- clientID:authResponse.request.clientID
- clientSecret:authResponse.request.clientSecret
- scope:nil
- refreshToken:kRefreshToken
- codeVerifier:nil
- additionalParameters:tokenResponse.request.additionalParameters];
-
- // Set the response for the auth endpoint.
- GIDAuthorizationFlowProcessorTestBlock authorizationFlowTestBlock;
- if (modalCancel) {
- NSError *error = [NSError errorWithDomain:OIDGeneralErrorDomain
- code:OIDErrorCodeUserCanceledAuthorizationFlow
- userInfo:nil];
- authorizationFlowTestBlock =
- ^(GIDAuthorizationFlowProcessorFakeResponseProviderBlock responseProvider) {
- responseProvider(nil, error);
- };
- } else {
- authorizationFlowTestBlock =
- ^(GIDAuthorizationFlowProcessorFakeResponseProviderBlock responseProvider) {
- responseProvider(authResponse, nil);
- };
- }
- _authorizationFlowProcessor.testBlock = authorizationFlowTestBlock;
-
- // Set the response for `GIDProfileDataFetcher`.
- GIDProfileDataFetcherTestBlock profileDataFetcherTestBlock =
- ^(GIDProfileDataFetcherFakeResponseProvider responseProvider) {
- GIDProfileData *profileData = [GIDProfileData testInstance];
- responseProvider(profileData, nil);
- };
-
- _profileDataFetcher.testBlock = profileDataFetcherTestBlock;
- if (restoredSignIn) {
- // Mock `maybeFetchToken:` method in `restorePreviousSignIn:` flow.
- [[[_authState expect] andReturn:tokenResponse] lastTokenResponse];
- [[[_authState expect] andReturn:tokenResponse] lastTokenResponse];
- if (oldAccessToken) {
- #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:tokenResponse] lastTokenResponse];
- [[[_authState expect] andReturn:tokenResponse] lastTokenResponse];
- [[[_authState expect] andReturn:tokenRequest]
- tokenRefreshRequestWithAdditionalParameters:[OCMArg any]];
- }
- } else {
- XCTestExpectation *expectation = [self expectationWithDescription:@"Callback called"];
- GIDSignInCompletion completion = ^(GIDSignInResult *_Nullable signInResult,
- NSError * _Nullable error) {
- [expectation fulfill];
- if (signInResult) {
- XCTAssertEqualObjects(signInResult.serverAuthCode, kServerAuthCode);
- } else {
- XCTAssertNotNil(error, @"Should have an error if the signInResult is nil");
- }
- XCTAssertFalse(self->_completionCalled, @"callback already called");
- self->_completionCalled = YES;
- self->_authError = error;
- };
- if (addScopesFlow) {
- [_signIn addScopes:@[kNewScope]
- #if TARGET_OS_IOS || TARGET_OS_MACCATALYST
- presentingViewController:_presentingViewController
- #elif TARGET_OS_OSX
- presentingWindow:_presentingWindow
- #endif // TARGET_OS_IOS || TARGET_OS_MACCATALYST
- completion:completion];
- } else {
- // Mock `maybeFetchToken:` method in Sign in flow.
- 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 TARGET_OS_IOS || TARGET_OS_MACCATALYST
- [_signIn signInWithPresentingViewController:_presentingViewController
- #elif TARGET_OS_OSX
- [_signIn signInWithPresentingWindow:_presentingWindow
- #endif // TARGET_OS_IOS || TARGET_OS_MACCATALYST
- hint:nil
- completion:completion];
- }
-
- if (authError || modalCancel) {
- return;
- }
-
- [_authState verify];
- }
- if (restoredSignIn && oldAccessToken) {
- XCTestExpectation *expectation = [self expectationWithDescription:@"Callback should be called"];
- [_signIn restorePreviousSignInWithCompletion:^(GIDGoogleUser * _Nullable user,
- NSError * _Nullable error) {
- [expectation fulfill];
- XCTAssertNil(error, @"should have no error");
- }];
- }
- if (!restoredSignIn || (restoredSignIn && oldAccessToken)) {
- XCTAssertNotNil(_savedTokenCallback);
- // OIDTokenCallback
- if (tokenError) {
- [[_authState expect] updateWithTokenResponse:nil error:tokenError];
- } else {
- [[_authState expect] updateWithTokenResponse:[OCMArg any] error:nil];
- }
- }
- if (tokenError) {
- _savedTokenCallback(nil, tokenError);
- return;
- }
- // SaveAuthCallback
- __block OIDAuthState *authState;
- __block OIDTokenResponse *updatedTokenResponse;
- __block OIDAuthorizationResponse *updatedAuthorizationResponse;
- __block GIDProfileData *profileData;
- if (keychainError) {
- _keychainHandler.failToSave = YES;
- } else {
- if (addScopesFlow) {
- [[[_authState expect] andReturn:authResponse] lastAuthorizationResponse];
- [[[_authState expect] andReturn:tokenResponse] lastTokenResponse];
- [[_user expect] updateWithTokenResponse:SAVE_TO_ARG_BLOCK(updatedTokenResponse)
- authorizationResponse:SAVE_TO_ARG_BLOCK(updatedAuthorizationResponse)
- profileData:SAVE_TO_ARG_BLOCK(profileData)];
- } else {
- [[[_user stub] andReturn:_user] alloc];
- (void)[[[_user expect] andReturn:_user] initWithAuthState:SAVE_TO_ARG_BLOCK(authState)
- profileData:SAVE_TO_ARG_BLOCK(profileData)];
- }
- }
-
- // CompletionCallback - mock server auth code parsing
- if (!keychainError) {
- [[[_authState expect] andReturn:tokenResponse] lastTokenResponse];
- }
- if (restoredSignIn && !oldAccessToken) {
- XCTestExpectation *expectation = [self expectationWithDescription:@"Callback should be called"];
- [_signIn restorePreviousSignInWithCompletion:^(GIDGoogleUser * _Nullable user,
- NSError * _Nullable error) {
- [expectation fulfill];
- XCTAssertNil(error, @"should have no error");
- }];
- } else {
- // Simulate token endpoint response.
- _savedTokenCallback(tokenResponse, nil);
- }
- if (keychainError) {
- return;
- }
- [self waitForExpectationsWithTimeout:1 handler:nil];
-
- [_authState verify];
-
- if (addScopesFlow) {
- XCTAssertNotNil(updatedTokenResponse);
- XCTAssertNotNil(updatedAuthorizationResponse);
- } else {
- XCTAssertNotNil(authState);
- }
- // Check fat ID token decoding
- XCTAssertEqualObjects(profileData, [GIDProfileData testInstance]);
- // If attempt to authenticate again, will reuse existing auth object.
- _completionCalled = NO;
- _authError = nil;
- __block GIDGoogleUserCompletion completion;
- [[_user expect] refreshTokensIfNeededWithCompletion:SAVE_TO_ARG_BLOCK(completion)];
- XCTestExpectation *expectation = [self expectationWithDescription:@"Callback should be called"];
- [_signIn restorePreviousSignInWithCompletion:^(GIDGoogleUser * _Nullable user,
- NSError * _Nullable error) {
- [expectation fulfill];
- XCTAssertNil(error, @"should have no error");
- }];
- completion(_user, nil);
- [self waitForExpectationsWithTimeout:1 handler:nil];
- }
- @end
|