|
160 | 160 | */ |
161 | 161 | static NSString *const kGoogleIDToken = @"GOOGLE_ID_TOKEN"; |
162 | 162 |
|
| 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 | + |
163 | 193 | /** @var kCustomToken |
164 | 194 | @brief The fake custom token to sign in. |
165 | 195 | */ |
@@ -321,6 +351,23 @@ + (NSDictionary *)googleProfile { |
321 | 351 | return kGoogleProfile; |
322 | 352 | } |
323 | 353 |
|
| 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 | + |
324 | 371 | - (void)setUp { |
325 | 372 | [super setUp]; |
326 | 373 |
|
@@ -1374,6 +1421,54 @@ - (void)testSignInWithGoogleCredentialFailure { |
1374 | 1421 | OCMVerifyAll(_mockBackend); |
1375 | 1422 | } |
1376 | 1423 |
|
| 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 | + |
1377 | 1472 | /** @fn testSignInAnonymouslySuccess |
1378 | 1473 | @brief Tests the flow of a successful @c signInAnonymouslyWithCompletion: call. |
1379 | 1474 | */ |
@@ -2584,6 +2679,53 @@ - (void)assertUserGoogle:(FIRUser *)user { |
2584 | 2679 | XCTAssertEqualObjects(googleUserInfo.email, kGoogleEmail); |
2585 | 2680 | } |
2586 | 2681 |
|
| 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 | + |
2587 | 2729 | /** @fn expectGetAccountInfoAnonymous |
2588 | 2730 | @brief Expects a GetAccountInfo request on the mock backend and calls back with fake anonymous |
2589 | 2731 | account data. |
|
0 commit comments