Skip to content

Commit 47fd1b5

Browse files
committed
Adding Unit test for apple signIn flow in FIROAuthProviderTests and FIRAuthTest
1 parent 62e4fb6 commit 47fd1b5

File tree

4 files changed

+152
-4
lines changed

4 files changed

+152
-4
lines changed

FirebaseAuth/Sources/AuthProvider/OAuth/FIROAuthCredential.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ - (void)prepareVerifyAssertionRequest:(FIRVerifyAssertionRequest *)request {
113113
request.sessionID = _sessionID;
114114
request.providerOAuthTokenSecret = _secret;
115115
request.pendingToken = _pendingToken;
116+
request.displayName = _displayName;
116117
}
117118

118119
#pragma mark - NSSecureCoding

FirebaseAuth/Sources/Backend/RPC/FIRVerifyAssertionRequest.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,11 @@ NS_ASSUME_NONNULL_BEGIN
9696
*/
9797
@property(nonatomic, assign) BOOL autoCreate;
9898

99+
/** @property displayName
100+
@brief A displayName linked to its provider
101+
*/
102+
@property(nonatomic, copy, nullable) NSString *displayName;
103+
99104
/** @fn initWithEndpoint:requestConfiguration:
100105
@brief Please use initWithProviderID:requestConfifuration instead.
101106
*/

FirebaseAuth/Tests/Unit/FIRAuthTests.m

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,36 @@
160160
*/
161161
static NSString *const kGoogleIDToken = @"GOOGLE_ID_TOKEN";
162162

163+
/** @var FIRAppleAuthProviderID
164+
@brief The provider ID for Apple Sign-In.
165+
*/
166+
NSString *const FIRAppleAuthProviderID = @"apple.com";
167+
168+
/** @var kAppleUD
169+
@brief The fake user ID under Apple Sign-In.
170+
*/
171+
static NSString *const kAppleID = @"APPLE_ID";
172+
173+
/** @var kAppleEmail
174+
@brief The fake user email under Apple Sign-In.
175+
*/
176+
static NSString *const kAppleEmail = @"user@icloud.com";
177+
178+
/** @var kAppleDisplayName
179+
@brief The fake user display name under Apple Sign-In.
180+
*/
181+
static NSString *const kAppleDisplayName = @"Apple Doe";
182+
183+
/** @var kAppleAccessToken
184+
@brief The fake access token from Apple Sign-In.
185+
*/
186+
static NSString *const kAppleAccessToken = @"Apple_ACCESS_TOKEN";
187+
188+
/** @var kAppleIDToken
189+
@brief The fake ID token from Apple Sign-In.
190+
*/
191+
static NSString *const kAppleIDToken = @"APPLE_ID_TOKEN";
192+
163193
/** @var kCustomToken
164194
@brief The fake custom token to sign in.
165195
*/
@@ -321,6 +351,23 @@ + (NSDictionary *)googleProfile {
321351
return kGoogleProfile;
322352
}
323353

354+
/** @fn appleProfile
355+
@brief The fake user profile under additional user data in @c FIRVerifyAssertionResponse.
356+
*/
357+
+ (NSDictionary *)appleProfile {
358+
static NSDictionary *kAppleProfile = nil;
359+
static dispatch_once_t onceToken;
360+
dispatch_once(&onceToken, ^{
361+
kAppleProfile = @{
362+
@"iss" : @"https://accounts.apple.com\\",
363+
@"email" : kAppleEmail,
364+
@"given_name" : @"User",
365+
@"family_name" : @"Doe"
366+
};
367+
});
368+
return kAppleProfile;
369+
}
370+
324371
- (void)setUp {
325372
[super setUp];
326373

@@ -1374,6 +1421,54 @@ - (void)testSignInWithGoogleCredentialFailure {
13741421
OCMVerifyAll(_mockBackend);
13751422
}
13761423

1424+
/** @fn testSignInWithAppleCredentialSuccess
1425+
@brief Tests the flow of a successful @c signInWithCredential:completion: call
1426+
with an Apple Sign-In credential.
1427+
*/
1428+
- (void)testSignInWithAppleCredentialSuccess {
1429+
OCMExpect([_mockBackend verifyAssertion:[OCMArg any] callback:[OCMArg any]])
1430+
.andCallBlock2(^(FIRVerifyAssertionRequest *_Nullable request,
1431+
FIRVerifyAssertionResponseCallback callback) {
1432+
XCTAssertEqualObjects(request.APIKey, kAPIKey);
1433+
XCTAssertEqualObjects(request.providerID, FIRAppleAuthProviderID);
1434+
XCTAssertEqualObjects(request.providerIDToken, kAppleIDToken);
1435+
XCTAssertEqualObjects(request.providerAccessToken, kAppleAccessToken);
1436+
XCTAssertTrue(request.returnSecureToken);
1437+
dispatch_async(FIRAuthGlobalWorkQueue(), ^() {
1438+
id mockVerifyAssertionResponse = OCMClassMock([FIRVerifyAssertionResponse class]);
1439+
OCMStub([mockVerifyAssertionResponse federatedID]).andReturn(kAppleID);
1440+
OCMStub([mockVerifyAssertionResponse providerID]).andReturn(FIRAppleAuthProviderID);
1441+
OCMStub([mockVerifyAssertionResponse localID]).andReturn(kLocalID);
1442+
OCMStub([mockVerifyAssertionResponse displayName]).andReturn(kAppleDisplayName);
1443+
OCMStub([mockVerifyAssertionResponse profile]).andReturn([[self class] appleProfile]);
1444+
OCMStub([mockVerifyAssertionResponse username]).andReturn(kDisplayName);
1445+
[self stubTokensWithMockResponse:mockVerifyAssertionResponse];
1446+
callback(mockVerifyAssertionResponse, nil);
1447+
});
1448+
});
1449+
[self expectGetAccountInfoApple];
1450+
XCTestExpectation *expectation = [self expectationWithDescription:@"callback"];
1451+
[[FIRAuth auth] signOut:NULL];
1452+
FIRAuthCredential *appleCredential =
1453+
[FIROAuthProvider credentialWithProviderID:FIRAppleAuthProviderID IDToken:kAppleIDToken rawNonce:nil accessToken:kAppleAccessToken displayName:kAppleDisplayName];
1454+
[[FIRAuth auth]
1455+
signInWithCredential:appleCredential
1456+
completion:^(FIRAuthDataResult *_Nullable authResult, NSError *_Nullable error) {
1457+
XCTAssertTrue([NSThread isMainThread]);
1458+
[self assertUserApple:authResult.user];
1459+
XCTAssertEqualObjects(authResult.additionalUserInfo.profile,
1460+
[[self class] appleProfile]);
1461+
XCTAssertEqualObjects(authResult.additionalUserInfo.username, kDisplayName);
1462+
XCTAssertEqualObjects(authResult.additionalUserInfo.providerID,
1463+
FIRAppleAuthProviderID);
1464+
XCTAssertNil(error);
1465+
[expectation fulfill];
1466+
}];
1467+
[self waitForExpectationsWithTimeout:kExpectationTimeout handler:nil];
1468+
[self assertUserApple:[FIRAuth auth].currentUser];
1469+
OCMVerifyAll(_mockBackend);
1470+
}
1471+
13771472
/** @fn testSignInAnonymouslySuccess
13781473
@brief Tests the flow of a successful @c signInAnonymouslyWithCompletion: call.
13791474
*/
@@ -2584,6 +2679,53 @@ - (void)assertUserGoogle:(FIRUser *)user {
25842679
XCTAssertEqualObjects(googleUserInfo.email, kGoogleEmail);
25852680
}
25862681

2682+
/** @fn expectGetAccountInfoApple
2683+
@brief Expects a GetAccountInfo request on the mock backend and calls back with fake account
2684+
data for a Apple Sign-In user.
2685+
*/
2686+
- (void)expectGetAccountInfoApple {
2687+
OCMExpect([_mockBackend getAccountInfo:[OCMArg any] callback:[OCMArg any]])
2688+
.andCallBlock2(^(FIRGetAccountInfoRequest *_Nullable request,
2689+
FIRGetAccountInfoResponseCallback callback) {
2690+
XCTAssertEqualObjects(request.APIKey, kAPIKey);
2691+
XCTAssertEqualObjects(request.accessToken, kAccessToken);
2692+
dispatch_async(FIRAuthGlobalWorkQueue(), ^() {
2693+
id mockAppleUserInfo = OCMClassMock([FIRGetAccountInfoResponseProviderUserInfo class]);
2694+
OCMStub([mockAppleUserInfo providerID]).andReturn(FIRAppleAuthProviderID);
2695+
OCMStub([mockAppleUserInfo displayName]).andReturn(kAppleDisplayName);
2696+
OCMStub([mockAppleUserInfo federatedID]).andReturn(kAppleID);
2697+
OCMStub([mockAppleUserInfo email]).andReturn(kAppleEmail);
2698+
id mockGetAccountInfoResponseUser = OCMClassMock([FIRGetAccountInfoResponseUser class]);
2699+
OCMStub([mockGetAccountInfoResponseUser localID]).andReturn(kLocalID);
2700+
OCMStub([mockGetAccountInfoResponseUser displayName]).andReturn(kDisplayName);
2701+
OCMStub([mockGetAccountInfoResponseUser providerUserInfo])
2702+
.andReturn((@[ mockAppleUserInfo ]));
2703+
id mockGetAccountInfoResponse = OCMClassMock([FIRGetAccountInfoResponse class]);
2704+
OCMStub([mockGetAccountInfoResponse users]).andReturn(@[
2705+
mockGetAccountInfoResponseUser
2706+
]);
2707+
callback(mockGetAccountInfoResponse, nil);
2708+
});
2709+
});
2710+
}
2711+
2712+
/** @fn assertUserApple
2713+
@brief Asserts the given FIRUser matching the fake data returned by
2714+
@c expectGetAccountInfoApple.
2715+
@param user The user object to be verified.
2716+
*/
2717+
- (void)assertUserApple:(FIRUser *)user {
2718+
XCTAssertNotNil(user);
2719+
XCTAssertEqualObjects(user.uid, kLocalID);
2720+
XCTAssertEqualObjects(user.displayName, kDisplayName);
2721+
XCTAssertEqual(user.providerData.count, 1u);
2722+
id<FIRUserInfo> appleUserInfo = user.providerData[0];
2723+
XCTAssertEqualObjects(appleUserInfo.providerID, FIRAppleAuthProviderID);
2724+
XCTAssertEqualObjects(appleUserInfo.uid, kAppleID);
2725+
XCTAssertEqualObjects(appleUserInfo.displayName, kAppleDisplayName);
2726+
XCTAssertEqualObjects(appleUserInfo.email, kAppleEmail);
2727+
}
2728+
25872729
/** @fn expectGetAccountInfoAnonymous
25882730
@brief Expects a GetAccountInfo request on the mock backend and calls back with fake anonymous
25892731
account data.

FirebaseAuth/Tests/Unit/FIROAuthProviderTests.m

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -243,18 +243,18 @@ - (void)testObtainingOAuthCredentialNoIDToken {
243243
XCTAssertNil(OAuthCredential.IDToken);
244244
}
245245

246-
/** @fn testObtainingOAuthCredentialNoIDToken
247-
@brief Tests the correct creation of an OAuthCredential without an IDToken.
246+
/** @fn testObtainingOAuthCredentialWithDisplayName
247+
@brief Tests the correct creation of an OAuthCredential with a displayName.
248248
*/
249249
- (void)testObtainingOAuthCredentialWithDisplayName {
250250
FIRAuthCredential *credential = [FIROAuthProvider credentialWithProviderID:kFakeProviderID
251-
accessToken:kFakeAccessToken];
251+
IDToken:kFakeIDToken rawNonce:nil accessToken:kFakeAccessToken displayName:kFakeDisplayName];
252252
XCTAssertTrue([credential isKindOfClass:[FIROAuthCredential class]]);
253253
FIROAuthCredential *OAuthCredential = (FIROAuthCredential *)credential;
254254
XCTAssertEqualObjects(OAuthCredential.accessToken, kFakeAccessToken);
255255
XCTAssertEqualObjects(OAuthCredential.provider, kFakeProviderID);
256+
XCTAssertEqualObjects(OAuthCredential.IDToken, kFakeIDToken);
256257
XCTAssertEqualObjects(OAuthCredential.displayName,kFakeDisplayName);
257-
XCTAssertNil(OAuthCredential.IDToken);
258258
}
259259

260260
/** @fn testObtainingOAuthCredentialWithIDToken

0 commit comments

Comments
 (0)