Skip to content

Commit a57f8b1

Browse files
committed
Fix tests
1 parent 8f64965 commit a57f8b1

File tree

4 files changed

+102
-65
lines changed

4 files changed

+102
-65
lines changed

FirebaseGoogleAuthUI/FirebaseGoogleAuthUI.xcodeproj/project.pbxproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -775,12 +775,12 @@
775775
"\"${PODS_CONFIGURATION_BUILD_DIR}/GTMSessionFetcher\"",
776776
"\"${PODS_CONFIGURATION_BUILD_DIR}/GoogleDataTransport\"",
777777
"\"${PODS_CONFIGURATION_BUILD_DIR}/GoogleUtilities\"",
778-
"\"${PODS_ROOT}/GoogleSignIn/Frameworks\"",
779778
"\"${PODS_CONFIGURATION_BUILD_DIR}/AppAuth\"",
780779
"\"${PODS_CONFIGURATION_BUILD_DIR}/PromisesObjC\"",
781780
"\"${PODS_CONFIGURATION_BUILD_DIR}/GTMAppAuth\"",
782781
"\"${PODS_CONFIGURATION_BUILD_DIR}/nanopb\"",
783782
"\"$(PODS_CONFIGURATION_BUILD_DIR)/FirebaseAuthUI\"",
783+
"\"$(PODS_CONFIGURATION_BUILD_DIR)/GoogleSignIn\"",
784784
);
785785
OTHER_LDFLAGS = (
786786
"$(inherited)",
@@ -830,12 +830,12 @@
830830
"\"${PODS_CONFIGURATION_BUILD_DIR}/GTMSessionFetcher\"",
831831
"\"${PODS_CONFIGURATION_BUILD_DIR}/GoogleDataTransport\"",
832832
"\"${PODS_CONFIGURATION_BUILD_DIR}/GoogleUtilities\"",
833-
"\"${PODS_ROOT}/GoogleSignIn/Frameworks\"",
834833
"\"${PODS_CONFIGURATION_BUILD_DIR}/AppAuth\"",
835834
"\"${PODS_CONFIGURATION_BUILD_DIR}/PromisesObjC\"",
836835
"\"${PODS_CONFIGURATION_BUILD_DIR}/GTMAppAuth\"",
837836
"\"${PODS_CONFIGURATION_BUILD_DIR}/nanopb\"",
838837
"\"$(PODS_CONFIGURATION_BUILD_DIR)/FirebaseAuthUI\"",
838+
"\"$(PODS_CONFIGURATION_BUILD_DIR)/GoogleSignIn\"",
839839
);
840840
OTHER_LDFLAGS = (
841841
"$(inherited)",

FirebaseGoogleAuthUI/FirebaseGoogleAuthUITests/FirebaseGoogleAuthUITests.m

Lines changed: 50 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,12 @@
2323
#import "FUIGoogleAuth.h"
2424

2525
@interface FUIGoogleAuth (Testing)
26-
- (GIDSignIn *)configuredGoogleSignIn;
26+
- (NSString *)clientID;
27+
- (GIDSignIn *)googleSignIn;
28+
- (void)handleSignInWithUser:(GIDGoogleUser *)user
29+
error:(NSError *)error
30+
presentingViewController:(UIViewController *)presentingViewController
31+
callback:(FUIAuthProviderSignInCompletionBlock)callback;
2732
@end
2833

2934
@interface FirebaseGoogleAuthUITests : XCTestCase
@@ -48,6 +53,7 @@ - (void)setUp {
4853
FIRAuth *auth = [FIRAuth auth];
4954
self.authUI = [FUIAuth authUIWithAuth:auth];
5055
self.mockProvider = OCMPartialMock([[FUIGoogleAuth alloc] initWithAuthUI:self.authUI]);
56+
OCMStub([_mockProvider clientID]).andReturn(@"clientID");
5157
}
5258

5359
- (void)tearDown {
@@ -82,7 +88,7 @@ - (void)testUseEmulatorCreatesOAuthProvider {
8288
OCMVerify([self.mockOAuthProvider providerWithProviderID:@"google.com"]);
8389
}
8490

85-
- (void)testSuccessfullLogin {
91+
- (void)testSuccessfulLogin {
8692
NSString *testIdToken = @"idToken";
8793
NSString *testAccessToken = @"accessToken";
8894

@@ -99,11 +105,16 @@ - (void)testSuccessfullLogin {
99105
OCMExpect([mockGoogleUser authentication]).andReturn(mockAuthentication);
100106
OCMExpect([mockAuthentication idToken]).andReturn(testIdToken);
101107

102-
OCMExpect([_mockProvider configuredGoogleSignIn]).andReturn(mockSignIn);
108+
OCMExpect([_mockProvider googleSignIn]).andReturn(mockSignIn);
103109

104-
//forward call to signIn delegate
105-
OCMExpect([mockSignIn signIn]).andDo(^(NSInvocation *invocation) {
106-
[mockSignInDelegate signIn:mockSignIn didSignInForUser:mockGoogleUser withError:nil];
110+
// forward call to signIn delegate
111+
OCMExpect([mockSignIn signInWithConfiguration:[OCMArg any]
112+
presentingViewController:[OCMArg any]
113+
hint:[OCMArg any]
114+
callback:[OCMArg any]]).andDo(^(NSInvocation *invocation) {
115+
void (^callback)(GIDGoogleUser *, NSError *) = nil;
116+
[invocation getArgument:&callback atIndex:5];
117+
callback(mockGoogleUser, nil);
107118
});
108119

109120
XCTestExpectation *expectation = [self expectationWithDescription:@"logged in"];
@@ -147,13 +158,15 @@ - (void)testLegacyInitSuccessfulLogin {
147158
NSString *testIdToken = @"idToken";
148159
NSString *testAccessToken = @"accessToken";
149160

150-
_mockProvider = OCMPartialMock([[FUIGoogleAuth alloc] init]);
161+
_mockProvider = OCMPartialMock([[FUIGoogleAuth alloc] init]);
151162

152163
id mockSignInDelegate = _mockProvider;
153164
id mockSignIn = OCMClassMock([GIDSignIn class]);
154165
id mockAuthentication = OCMClassMock([GIDAuthentication class]);
155166
id mockGoogleUser = OCMClassMock([GIDGoogleUser class]);
156167

168+
OCMStub([_mockProvider clientID]).andReturn(@"clientID");
169+
157170
// mock accessToken
158171
OCMExpect([mockGoogleUser authentication]).andReturn(mockAuthentication);
159172
OCMExpect([mockAuthentication accessToken]).andReturn(testAccessToken);
@@ -162,11 +175,16 @@ - (void)testLegacyInitSuccessfulLogin {
162175
OCMExpect([mockGoogleUser authentication]).andReturn(mockAuthentication);
163176
OCMExpect([mockAuthentication idToken]).andReturn(testIdToken);
164177

165-
OCMExpect([_mockProvider configuredGoogleSignIn]).andReturn(mockSignIn);
178+
OCMExpect([_mockProvider googleSignIn]).andReturn(mockSignIn);
166179

167-
//forward call to signIn delegate
168-
OCMExpect([mockSignIn signIn]).andDo(^(NSInvocation *invocation) {
169-
[mockSignInDelegate signIn:mockSignIn didSignInForUser:mockGoogleUser withError:nil];
180+
// forward call to signIn delegate
181+
OCMExpect([mockSignIn signInWithConfiguration:[OCMArg any]
182+
presentingViewController:[OCMArg any]
183+
hint:[OCMArg any]
184+
callback:[OCMArg any]]).andDo(^(NSInvocation *invocation) {
185+
void (^callback)(GIDGoogleUser *, NSError *) = nil;
186+
[invocation getArgument:&callback atIndex:5];
187+
callback(mockGoogleUser, nil);
170188
});
171189

172190
XCTestExpectation *expectation = [self expectationWithDescription:@"logged in"];
@@ -222,11 +240,17 @@ - (void)testErrorLogin {
222240
OCMStub([mockGoogleUser authentication]).andReturn(mockAuthentication);
223241
OCMStub([mockAuthentication idToken]).andReturn(testIdToken);
224242

225-
OCMExpect([_mockProvider configuredGoogleSignIn]).andReturn(mockSignIn);
243+
OCMExpect([_mockProvider googleSignIn]).andReturn(mockSignIn);
226244
NSError *signInError = [NSError errorWithDomain:@"sign in domain" code:kGIDSignInErrorCodeUnknown userInfo:@{}];
227245

228-
OCMExpect([mockSignIn signIn]).andDo(^(NSInvocation *invocation) {
229-
[mockSignInDelegate signIn:mockSignIn didSignInForUser:mockGoogleUser withError:signInError];
246+
// forward call to signIn delegate
247+
OCMExpect([mockSignIn signInWithConfiguration:[OCMArg any]
248+
presentingViewController:[OCMArg any]
249+
hint:[OCMArg any]
250+
callback:[OCMArg any]]).andDo(^(NSInvocation *invocation) {
251+
void (^callback)(GIDGoogleUser *, NSError *) = nil;
252+
[invocation getArgument:&callback atIndex:5];
253+
callback(mockGoogleUser, signInError);
230254
});
231255

232256

@@ -275,11 +299,17 @@ - (void)testCancelLogin {
275299
OCMStub([mockGoogleUser authentication]).andReturn(mockAuthentication);
276300
OCMStub([mockAuthentication idToken]).andReturn(testIdToken);
277301

278-
OCMExpect([_mockProvider configuredGoogleSignIn]).andReturn(mockSignIn);
302+
OCMExpect([_mockProvider googleSignIn]).andReturn(mockSignIn);
279303
NSError *signInError = [NSError errorWithDomain:@"sign in domain" code:kGIDSignInErrorCodeCanceled userInfo:@{}];
280304

281-
OCMExpect([mockSignIn signIn]).andDo(^(NSInvocation *invocation) {
282-
[mockSignInDelegate signIn:mockSignIn didSignInForUser:mockGoogleUser withError:signInError];
305+
// forward call to signIn delegate
306+
OCMExpect([mockSignIn signInWithConfiguration:[OCMArg any]
307+
presentingViewController:[OCMArg any]
308+
hint:[OCMArg any]
309+
callback:[OCMArg any]]).andDo(^(NSInvocation *invocation) {
310+
void (^callback)(GIDGoogleUser *, NSError *) = nil;
311+
[invocation getArgument:&callback atIndex:5];
312+
callback(mockGoogleUser, signInError);
283313
});
284314

285315
XCTestExpectation *expectation = [self expectationWithDescription:@"logged in"];
@@ -313,7 +343,7 @@ - (void)testCancelLogin {
313343

314344
- (void)testSignOut {
315345
id mockSignIn = OCMClassMock([GIDSignIn class]);
316-
OCMExpect([_mockProvider configuredGoogleSignIn]).andReturn(mockSignIn);
346+
OCMExpect([_mockProvider googleSignIn]).andReturn(mockSignIn);
317347
OCMExpect([mockSignIn signOut]);
318348

319349
[_mockProvider signOut];
@@ -324,7 +354,7 @@ - (void)testSignOut {
324354

325355
- (void)testUseEmulatorUsesOAuthProvider {
326356
[self.authUI useEmulatorWithHost:@"host" port:12345];
327-
self.mockProvider = OCMPartialMock([[FUIGoogleAuth alloc] initWithAuthUI:self.authUI]);
357+
self.mockProvider = OCMPartialMock([[FUIGoogleAuth alloc] initWithAuthUI:self.authUI]);
328358

329359
[self.mockProvider signInWithDefaultValue:nil
330360
presentingViewController:nil
@@ -334,7 +364,7 @@ - (void)testUseEmulatorUsesOAuthProvider {
334364
NSDictionary *_Nullable userInfo) {}];
335365

336366
OCMVerify([self.mockOAuthProvider getCredentialWithUIDelegate:nil completion:OCMOCK_ANY]);
337-
OCMVerify(never(), [self.mockProvider configuredGoogleSignIn]);
367+
OCMVerify(never(), [self.mockProvider googleSignIn]);
338368
}
339369

340370

FirebaseGoogleAuthUI/Sources/FUIGoogleAuth.m

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,13 @@ - (instancetype)initWithScopes:(NSArray *)scopes {
9696
}
9797
#pragma clang diagnostic pop
9898

99+
- (GIDSignIn *)googleSignIn {
100+
return [GIDSignIn sharedInstance];
101+
}
102+
103+
- (NSString *)clientID {
104+
return self.authUI.auth.app.options.clientID;
105+
}
99106

100107
#pragma mark - FUIAuthProvider
101108

@@ -107,14 +114,14 @@ - (nullable NSString *)accessToken {
107114
if (self.authUI.isEmulatorEnabled) {
108115
return nil;
109116
}
110-
return [GIDSignIn sharedInstance].currentUser.authentication.accessToken;
117+
return [self googleSignIn].currentUser.authentication.accessToken;
111118
}
112119

113120
- (nullable NSString *)idToken {
114121
if (self.authUI.isEmulatorEnabled) {
115122
return nil;
116123
}
117-
return [GIDSignIn sharedInstance].currentUser.authentication.idToken;
124+
return [self googleSignIn].currentUser.authentication.idToken;
118125
}
119126

120127
- (NSString *)shortName {
@@ -161,8 +168,8 @@ - (void)signInWithDefaultValue:(nullable NSString *)defaultValue
161168
return;
162169
}
163170

164-
GIDSignIn *signIn = [GIDSignIn sharedInstance];
165-
NSString *clientID = self.authUI.auth.app.options.clientID;
171+
GIDSignIn *signIn = [self googleSignIn];
172+
NSString *clientID = [self clientID];
166173

167174
if (!clientID) {
168175
[NSException raise:NSInternalInconsistencyException
@@ -178,7 +185,7 @@ - (void)signInWithDefaultValue:(nullable NSString *)defaultValue
178185
_Nullable FIRAuthResultCallback result,
179186
NSDictionary *_Nullable userInfo) {
180187
if (completion) {
181-
completion(credential, error, result, nil);
188+
completion(credential, error, result, userInfo);
182189
}
183190
};
184191

@@ -225,7 +232,7 @@ - (void)signInWithOAuthProvider:(FIROAuthProvider *)oauthProvider
225232

226233
- (void)requestScopesWithPresentingViewController:(UIViewController *)presentingViewController
227234
completion:(FUIAuthProviderSignInCompletionBlock)completion {
228-
GIDSignIn *signIn = [GIDSignIn sharedInstance];
235+
GIDSignIn *signIn = [self googleSignIn];
229236
[signIn addScopes:self.scopes presentingViewController:presentingViewController
230237
callback:^(GIDGoogleUser *user, NSError *error) {
231238
[self handleSignInWithUser:user
@@ -246,15 +253,15 @@ - (void)signOut {
246253
if (self.authUI.isEmulatorEnabled) {
247254
return;
248255
}
249-
GIDSignIn *signIn = [GIDSignIn sharedInstance];
256+
GIDSignIn *signIn = [self googleSignIn];
250257
[signIn signOut];
251258
}
252259

253260
- (BOOL)handleOpenURL:(NSURL *)URL sourceApplication:(NSString *)sourceApplication {
254261
if (self.authUI.isEmulatorEnabled) {
255262
return NO;
256263
}
257-
GIDSignIn *signIn = [GIDSignIn sharedInstance];
264+
GIDSignIn *signIn = [self googleSignIn];
258265
return [signIn handleURL:URL];
259266
}
260267

0 commit comments

Comments
 (0)