Skip to content

Commit e95e502

Browse files
authored
Add fake for GIDProfileDataFetcher (#279)
1 parent 884b531 commit e95e502

File tree

3 files changed

+122
-34
lines changed

3 files changed

+122
-34
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright 2023 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#import <Foundation/Foundation.h>
18+
19+
#import "GoogleSignIn/Sources/GIDProfileDataFetcher/API/GIDProfileDataFetcher.h"
20+
21+
@class GIDProfileData;
22+
23+
NS_ASSUME_NONNULL_BEGIN
24+
25+
/// The block type providing the response for user info request.
26+
///
27+
/// @param profileData The `GIDProfileData` object returned on success.
28+
/// @param error The error returned on failure.
29+
typedef void (^GIDProfileDataFetcherFakeResponseProvider)(GIDProfileData *_Nullable profileData,
30+
NSError *_Nullable error);
31+
32+
/// The block type setting up the response value.
33+
///
34+
/// @param responseProvider The block which provides the response.
35+
typedef void (^GIDProfileDataFetcherTestBlock)(GIDProfileDataFetcherFakeResponseProvider
36+
responseProvider);
37+
38+
@interface GIDFakeProfileDataFetcher : NSObject <GIDProfileDataFetcher>
39+
40+
/// The test block to set up the response value.
41+
@property(nonatomic) GIDProfileDataFetcherTestBlock testBlock;
42+
43+
@end
44+
45+
NS_ASSUME_NONNULL_END
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#import "GoogleSignIn/Sources/GIDProfileDataFetcher/Implementations/Fakes/GIDFakeProfileDataFetcher.h"
2+
3+
#import "GoogleSignIn/Sources/Public/GoogleSignIn/GIDProfileData.h"
4+
5+
#ifdef SWIFT_PACKAGE
6+
@import AppAuth;
7+
#else
8+
#import <AppAuth/AppAuth.h>
9+
#endif
10+
11+
NS_ASSUME_NONNULL_BEGIN
12+
13+
@implementation GIDFakeProfileDataFetcher
14+
15+
- (void)fetchProfileDataWithAuthState:(OIDAuthState *)authState
16+
completion:(void (^)(GIDProfileData *_Nullable profileData,
17+
NSError *_Nullable error))completion {
18+
NSAssert(self.testBlock != nil, @"Set the test block before invoking this method.");
19+
self.testBlock(^(GIDProfileData *_Nullable profileData, NSError *_Nullable error) {
20+
completion(profileData, error);
21+
});
22+
}
23+
24+
- (nullable GIDProfileData *)fetchProfileDataWithIDToken:(OIDIDToken *)idToken {
25+
NSAssert(self.testBlock != nil, @"Set the test block before invoking this method.");
26+
__block GIDProfileData *profileDataToReturn;
27+
self.testBlock(^(GIDProfileData *_Nullable profileData, NSError *_Nullable error) {
28+
profileDataToReturn = profileData;
29+
});
30+
return profileDataToReturn;
31+
}
32+
33+
@end
34+
35+
NS_ASSUME_NONNULL_END

GoogleSignIn/Tests/Unit/GIDSignInTest.m

Lines changed: 42 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,16 @@
3434
#import "GoogleSignIn/Sources/GIDKeychainHandler/Implementations/Fakes/GIDFakeKeychainHandler.h"
3535
#import "GoogleSignIn/Sources/GIDHTTPFetcher/Implementations/Fakes/GIDFakeHTTPFetcher.h"
3636
#import "GoogleSignIn/Sources/GIDHTTPFetcher/Implementations/GIDHTTPFetcher.h"
37-
#import "GoogleSignIn/Sources/GIDProfileDataFetcher/Implementations/GIDProfileDataFetcher.h"
37+
#import "GoogleSignIn/Sources/GIDProfileDataFetcher/API/GIDProfileDataFetcher.h"
38+
#import "GoogleSignIn/Sources/GIDProfileDataFetcher/Implementations/Fakes/GIDFakeProfileDataFetcher.h"
3839

3940

4041
#if TARGET_OS_IOS && !TARGET_OS_MACCATALYST
4142
#import "GoogleSignIn/Sources/GIDEMMErrorHandler.h"
4243
#endif // TARGET_OS_IOS && !TARGET_OS_MACCATALYST
4344

4445
#import "GoogleSignIn/Tests/Unit/GIDFakeMainBundle.h"
46+
#import "GoogleSignIn/Tests/Unit/GIDProfileData+Testing.h"
4547
#import "GoogleSignIn/Tests/Unit/OIDAuthorizationResponse+Testing.h"
4648
#import "GoogleSignIn/Tests/Unit/OIDTokenResponse+Testing.h"
4749

@@ -185,36 +187,39 @@ @interface GIDSignInTest : XCTestCase {
185187
// Whether or not the OS version is eligible for EMM.
186188
BOOL _isEligibleForEMM;
187189

188-
// Mock |OIDAuthState|.
190+
// Mock `OIDAuthState`.
189191
id _authState;
190192

191-
// Mock |OIDTokenResponse|.
193+
// Mock `OIDTokenResponse`.
192194
id _tokenResponse;
193195

194-
// Mock |OIDTokenRequest|.
196+
// Mock `OIDTokenRequest`.
195197
id _tokenRequest;
196198

197-
// Mock |GTMAppAuthFetcherAuthorization|.
199+
// Mock `GTMAppAuthFetcherAuthorization`.
198200
id _authorization;
199201

200-
// Fake for |GIDKeychainHandler|.
202+
// Fake for `GIDKeychainHandler`.
201203
GIDFakeKeychainHandler *_keychainHandler;
202204

203-
// Fake for |GIDHTTPFetcher|.
205+
// Fake for `GIDHTTPFetcher`.
204206
GIDFakeHTTPFetcher *_httpFetcher;
205207

208+
// Fake for `GIDProfileDataFetcher`.
209+
GIDFakeProfileDataFetcher *_profileDataFetcher;
210+
206211
#if TARGET_OS_IOS || TARGET_OS_MACCATALYST
207-
// Mock |UIViewController|.
212+
// Mock `UIViewController`.
208213
id _presentingViewController;
209214
#elif TARGET_OS_OSX
210-
// Mock |NSWindow|.
215+
// Mock `NSWindow`.
211216
id _presentingWindow;
212217
#endif // TARGET_OS_IOS || TARGET_OS_MACCATALYST
213218

214-
// Mock for |GIDGoogleUser|.
219+
// Mock for `GIDGoogleUser`.
215220
id _user;
216221

217-
// Mock for |OIDAuthorizationService|
222+
// Mock for `OIDAuthorizationService`.
218223
id _oidAuthorizationService;
219224

220225
// Parameter saved from delegate call.
@@ -226,16 +231,16 @@ @interface GIDSignInTest : XCTestCase {
226231
// Fake [NSBundle mainBundle];
227232
GIDFakeMainBundle *_fakeMainBundle;
228233

229-
// The |GIDSignIn| object being tested.
234+
// The `GIDSignIn` object being tested.
230235
GIDSignIn *_signIn;
231236

232-
// The configuration to be used when testing |GIDSignIn|.
237+
// The configuration to be used when testing `GIDSignIn`.
233238
GIDConfiguration *_configuration;
234239

235-
// The login hint to be used when testing |GIDSignIn|.
240+
// The login hint to be used when testing `GIDSignIn`.
236241
NSString *_hint;
237242

238-
// The completion to be used when testing |GIDSignIn|.
243+
// The completion to be used when testing `GIDSignIn`.
239244
GIDSignInCompletion _completion;
240245

241246
// The saved authorization request.
@@ -314,14 +319,14 @@ - (void)setUp {
314319

315320
_httpFetcher = [[GIDFakeHTTPFetcher alloc] init];
316321

322+
_profileDataFetcher = [[GIDFakeProfileDataFetcher alloc] init];
323+
317324
GIDAuthorizationFlowProcessor * authorizationFlowProcessor =
318325
[[GIDAuthorizationFlowProcessor alloc] init];
319326

320-
id<GIDProfileDataFetcher> profileDataFetcher = [[GIDProfileDataFetcher alloc] init];
321-
322327
_signIn = [[GIDSignIn alloc] initWithKeychainHandler:_keychainHandler
323328
httpFetcher:_httpFetcher
324-
profileDataFetcher:profileDataFetcher
329+
profileDataFetcher:_profileDataFetcher
325330
authorizationFlowProcessor:authorizationFlowProcessor];
326331

327332
_hint = nil;
@@ -423,20 +428,21 @@ - (void)testRestorePreviousSignInNoRefresh_hasPreviousUser {
423428

424429
OCMStub([_authState lastAuthorizationResponse]).andReturn(authResponse);
425430
OCMStub([_tokenResponse idToken]).andReturn(kFakeIDToken);
426-
OCMStub([_tokenResponse request]).andReturn(_tokenRequest);
427-
OCMStub([_tokenRequest additionalParameters]).andReturn(nil);
428431
OCMStub([_tokenResponse accessToken]).andReturn(kAccessToken);
429432
OCMStub([_tokenResponse accessTokenExpirationDate]).andReturn(nil);
430433

434+
GIDProfileData *fakeProfileData = [GIDProfileData testInstance];
435+
GIDProfileDataFetcherTestBlock testBlock = ^(GIDProfileDataFetcherFakeResponseProvider
436+
responseProvider) {
437+
responseProvider(fakeProfileData, nil);
438+
};
439+
440+
_profileDataFetcher.testBlock = testBlock;
431441
[_signIn restorePreviousSignInNoRefresh];
432442

433-
[_authState verify];
434-
[_authorization verify];
435-
[_tokenResponse verify];
436-
[_tokenRequest verify];
437-
[idTokenDecoded verify];
438443
XCTAssertEqual(_signIn.currentUser.userID, kFakeGaiaID);
439-
444+
445+
XCTAssertEqualObjects(_signIn.currentUser.profile, fakeProfileData);
440446
[idTokenDecoded stopMocking];
441447
}
442448

@@ -1065,7 +1071,6 @@ - (void)testAuthEndpointEMMError {
10651071
[[[mockEMMErrorHandler expect] andReturnValue:@YES]
10661072
handleErrorFromResponse:callbackParams completion:SAVE_TO_ARG_BLOCK(completion)];
10671073

1068-
10691074
[self OAuthLoginWithAddScopesFlow:NO
10701075
authError:callbackParams[@"error"]
10711076
tokenError:nil
@@ -1215,6 +1220,15 @@ - (void)OAuthLoginWithAddScopesFlow:(BOOL)addScopesFlow
12151220
refreshToken:kRefreshToken
12161221
codeVerifier:nil
12171222
additionalParameters:tokenResponse.request.additionalParameters];
1223+
1224+
// Set the response for `GIDProfileDataFetcher`.
1225+
GIDProfileDataFetcherTestBlock testBlock = ^(GIDProfileDataFetcherFakeResponseProvider
1226+
responseProvider) {
1227+
GIDProfileData *profileData = [GIDProfileData testInstance];
1228+
responseProvider(profileData, nil);
1229+
};
1230+
1231+
_profileDataFetcher.testBlock = testBlock;
12181232

12191233
if (restoredSignIn) {
12201234
// maybeFetchToken
@@ -1341,9 +1355,6 @@ - (void)OAuthLoginWithAddScopesFlow:(BOOL)addScopesFlow
13411355
return;
13421356
}
13431357

1344-
// DecodeIdTokenCallback
1345-
[[[_authState expect] andReturn:tokenResponse] lastTokenResponse];
1346-
13471358
// SaveAuthCallback
13481359
__block OIDAuthState *authState;
13491360
__block OIDTokenResponse *updatedTokenResponse;
@@ -1397,10 +1408,7 @@ - (void)OAuthLoginWithAddScopesFlow:(BOOL)addScopesFlow
13971408
XCTAssertNotNil(authState);
13981409
}
13991410
// Check fat ID token decoding
1400-
XCTAssertEqualObjects(profileData.name, kFatName);
1401-
XCTAssertEqualObjects(profileData.givenName, kFatGivenName);
1402-
XCTAssertEqualObjects(profileData.familyName, kFatFamilyName);
1403-
XCTAssertTrue(profileData.hasImage);
1411+
XCTAssertEqualObjects(profileData, [GIDProfileData testInstance]);
14041412

14051413
// If attempt to authenticate again, will reuse existing auth object.
14061414
_completionCalled = NO;

0 commit comments

Comments
 (0)