Skip to content

Commit

Permalink
Fix GIDSignInTest
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex-4-Git committed Jan 18, 2023
1 parent f7ce004 commit 2d60ea9
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@

NS_ASSUME_NONNULL_BEGIN

/// The block which provides the response for the method `startWithOptions:emmSupport:completion:`.
/// The block type providing the response for the method `startWithOptions:emmSupport:completion:`.
///
/// @param authorizationResponse The OIDAuthorizationResponse object returned if succeeded.
/// @param error The error returned if failed.
typedef void(^GIDAuthorizationFlowProcessorFakeResponseProviderBlock)
(OIDAuthorizationResponse *_Nullable authorizationResponse, NSError *_Nullable error);

/// The block to set up response value for the method
/// The block type setting up response value for the method
/// `startWithOptions:emmSupport:completion:`.
///
/// @param responseProvider The block which provides the response.
Expand Down
74 changes: 29 additions & 45 deletions GoogleSignIn/Tests/Unit/GIDSignInTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -281,14 +281,14 @@ - (void)setUp {
OCMStub([_authorization initWithAuthState:OCMOCK_ANY]).andReturn(_authorization);
_user = OCMStrictClassMock([GIDGoogleUser class]);
_oidAuthorizationService = OCMStrictClassMock([OIDAuthorizationService class]);
OCMStub([_oidAuthorizationService
presentAuthorizationRequest:OCMOCK_ANY
#if TARGET_OS_IOS || TARGET_OS_MACCATALYST
presentingViewController:SAVE_TO_ARG_BLOCK(self->_savedPresentingViewController)
#elif TARGET_OS_OSX
presentingWindow:SAVE_TO_ARG_BLOCK(self->_savedPresentingWindow)
#endif // TARGET_OS_IOS || TARGET_OS_MACCATALYST
callback:COPY_TO_ARG_BLOCK(self->_savedAuthorizationCallback)]);
// OCMStub([_oidAuthorizationService
// presentAuthorizationRequest:OCMOCK_ANY
//#if TARGET_OS_IOS || TARGET_OS_MACCATALYST
// presentingViewController:SAVE_TO_ARG_BLOCK(self->_savedPresentingViewController)
//#elif TARGET_OS_OSX
// presentingWindow:SAVE_TO_ARG_BLOCK(self->_savedPresentingWindow)
//#endif // TARGET_OS_IOS || TARGET_OS_MACCATALYST
// callback:COPY_TO_ARG_BLOCK(self->_savedAuthorizationCallback)]);
OCMStub([self->_oidAuthorizationService
performTokenRequest:SAVE_TO_ARG_BLOCK(self->_savedTokenRequest)
callback:COPY_TO_ARG_BLOCK(self->_savedTokenCallback)]);
Expand Down Expand Up @@ -595,12 +595,6 @@ - (void)testSignOut {
XCTAssertNil([_keychainHandler loadAuthState]);
}

- (void)testNotHandleWrongScheme {
XCTAssertFalse([_signIn handleURL:[NSURL URLWithString:kWrongSchemeURL]],
@"should not handle URL");
XCTAssertFalse(_completionCalled, @"should not call delegate");
}

- (void)testNotHandleWrongPath {
XCTAssertFalse([_signIn handleURL:[NSURL URLWithString:kWrongPathURL]], @"should not handle URL");
XCTAssertFalse(_completionCalled, @"should not call delegate");
Expand Down Expand Up @@ -1038,8 +1032,8 @@ - (void)OAuthLoginWithAddScopesFlow:(BOOL)addScopesFlow
refreshToken:kRefreshToken
codeVerifier:nil
additionalParameters:tokenResponse.request.additionalParameters];

// Simulate auth endpoint response
// Set the response for the auth endpoint.
GIDAuthorizationFlowProcessorTestBlock authorizationFlowTestBlock;
if (modalCancel) {
NSError *error = [NSError errorWithDomain:OIDGeneralErrorDomain
Expand All @@ -1057,29 +1051,17 @@ - (void)OAuthLoginWithAddScopesFlow:(BOOL)addScopesFlow
}
_authorizationFlowProcessor.testBlock = authorizationFlowTestBlock;

// maybeFetchToken
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];
}

// Set the response for `GIDProfileDataFetcher`.
GIDProfileDataFetcherTestBlock testBlock = ^(GIDProfileDataFetcherFakeResponseProvider
responseProvider) {
GIDProfileData *profileData = [GIDProfileData testInstance];
responseProvider(profileData, nil);
GIDProfileDataFetcherTestBlock profileDataFetcherTestBlock =
^(GIDProfileDataFetcherFakeResponseProvider responseProvider) {
GIDProfileData *profileData = [GIDProfileData testInstance];
responseProvider(profileData, nil);
};

_profileDataFetcher.testBlock = testBlock;
_profileDataFetcher.testBlock = profileDataFetcherTestBlock;

if (restoredSignIn) {
// maybeFetchToken
// Mock `maybeFetchToken:` method in `restorePreviousSignIn:` flow.
[[[_authState expect] andReturn:tokenResponse] lastTokenResponse];
[[[_authState expect] andReturn:tokenResponse] lastTokenResponse];
if (oldAccessToken) {
Expand Down Expand Up @@ -1115,6 +1097,18 @@ - (void)OAuthLoginWithAddScopesFlow:(BOOL)addScopesFlow
#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
Expand All @@ -1123,21 +1117,11 @@ - (void)OAuthLoginWithAddScopesFlow:(BOOL)addScopesFlow
hint:_hint
completion:completion];
}

[_authState verify];

XCTAssertNotNil(_savedAuthorizationCallback);

#if TARGET_OS_IOS || TARGET_OS_MACCATALYST
XCTAssertEqual(_savedPresentingViewController, _presentingViewController);
#elif TARGET_OS_OSX
XCTAssertEqual(_savedPresentingWindow, _presentingWindow);
#endif // TARGET_OS_IOS || TARGET_OS_MACCATALYST


if (authError || modalCancel) {
return;
}

[_authState verify];
}

Expand Down

0 comments on commit 2d60ea9

Please sign in to comment.