|
18 | 18 | #import <OCMock/OCMock.h>
|
19 | 19 | #import <XCTest/XCTest.h>
|
20 | 20 |
|
| 21 | +#import "FirebaseAuth/Sources/AuthProvider/OAuth/FIROAuthCredential_Internal.h" |
21 | 22 | #import "FirebaseAuth/Sources/Public/FirebaseAuth/FIRAuthTokenResult.h"
|
22 | 23 | #import "FirebaseAuth/Sources/Public/FirebaseAuth/FIREmailAuthProvider.h"
|
23 | 24 | #import "FirebaseAuth/Sources/Public/FirebaseAuth/FIRFacebookAuthProvider.h"
|
24 | 25 | #import "FirebaseAuth/Sources/Public/FirebaseAuth/FIRGoogleAuthProvider.h"
|
| 26 | +#import "FirebaseAuth/Sources/Public/FirebaseAuth/FIROAuthCredential.h" |
| 27 | +#import "FirebaseAuth/Sources/Public/FirebaseAuth/FIROAuthProvider.h" |
25 | 28 | #import "FirebaseAuth/Sources/Public/FirebaseAuth/FIRUserInfo.h"
|
26 | 29 | #import "FirebaseAuth/Sources/Public/FirebaseAuth/FIRUserMetadata.h"
|
27 | 30 |
|
|
383 | 386 | */
|
384 | 387 | static NSString *const kEnrolledAt = @"2022-08-01T18:31:15.426458Z";
|
385 | 388 |
|
| 389 | +/** @var kOAuthRequestURI |
| 390 | + @brief Fake OAuthRequest URI for testing. |
| 391 | + */ |
| 392 | +static NSString *const kOAuthRequestURI = @"requestURI"; |
| 393 | + |
| 394 | +/** @var kOAuthSessionID |
| 395 | + @brief Fake session ID for testing. |
| 396 | + */ |
| 397 | +static NSString *const kOAuthSessionID = @"sessionID"; |
| 398 | + |
| 399 | +/** @var kFakeWebSignInUserInteractionFailureReason |
| 400 | + @brief Fake reason for FIRAuthErrorCodeWebSignInUserInteractionFailure error while testing. |
| 401 | + */ |
| 402 | +static NSString *const kFakeWebSignInUserInteractionFailureReason = @"fake_reason"; |
| 403 | + |
386 | 404 | /** @extention FIRSecureTokenService
|
387 | 405 | @brief Extends the FIRSecureTokenService class to expose one private method for testing only.
|
388 | 406 | */
|
@@ -2643,6 +2661,134 @@ - (void)testlinkCredentialProviderAlreadyLinkedError {
|
2643 | 2661 | }
|
2644 | 2662 |
|
2645 | 2663 | #if TARGET_OS_IOS
|
| 2664 | +/** @fn testlinkProviderFailure |
| 2665 | + @brief Tests the flow of a failed @c linkWithProvider:completion: |
| 2666 | + call. |
| 2667 | + */ |
| 2668 | +- (void)testlinkProviderFailure { |
| 2669 | + [self expectVerifyAssertionRequest:FIRFacebookAuthProviderID |
| 2670 | + federatedID:kFacebookID |
| 2671 | + displayName:kFacebookDisplayName |
| 2672 | + profile:[[self class] googleProfile] |
| 2673 | + providerIDToken:kFacebookIDToken |
| 2674 | + providerAccessToken:kFacebookAccessToken]; |
| 2675 | + |
| 2676 | + XCTestExpectation *expectation = [self expectationWithDescription:@"callback"]; |
| 2677 | + [[FIRAuth auth] signOut:NULL]; |
| 2678 | + FIRAuthCredential *facebookCredential = |
| 2679 | + [FIRFacebookAuthProvider credentialWithAccessToken:kFacebookAccessToken]; |
| 2680 | + [[FIRAuth auth] |
| 2681 | + signInWithCredential:facebookCredential |
| 2682 | + completion:^(FIRAuthDataResult *_Nullable authResult, NSError *_Nullable error) { |
| 2683 | + XCTAssertTrue([NSThread isMainThread]); |
| 2684 | + [self assertUserFacebook:authResult.user]; |
| 2685 | + XCTAssertEqualObjects(authResult.additionalUserInfo.profile, |
| 2686 | + [[self class] googleProfile]); |
| 2687 | + XCTAssertEqualObjects(authResult.additionalUserInfo.username, kUserName); |
| 2688 | + XCTAssertEqualObjects(authResult.additionalUserInfo.providerID, |
| 2689 | + FIRFacebookAuthProviderID); |
| 2690 | + XCTAssertNil(error); |
| 2691 | + |
| 2692 | + OCMExpect([self->_mockBackend verifyAssertion:[OCMArg any] callback:[OCMArg any]]) |
| 2693 | + .andDispatchError2( |
| 2694 | + [FIRAuthErrorUtils webSignInUserInteractionFailureWithReason: |
| 2695 | + kFakeWebSignInUserInteractionFailureReason]); |
| 2696 | + id mockProvider = OCMClassMock([FIROAuthProvider class]); |
| 2697 | + OCMExpect([mockProvider getCredentialWithUIDelegate:[OCMArg any] |
| 2698 | + completion:[OCMArg any]]) |
| 2699 | + .andCallBlock2(^(id<FIRAuthUIDelegate> delegate, |
| 2700 | + FIRAuthCredentialCallback callback) { |
| 2701 | + dispatch_async(FIRAuthGlobalWorkQueue(), ^() { |
| 2702 | + FIROAuthCredential *credential = |
| 2703 | + [[FIROAuthCredential alloc] initWithProviderID:FIRGoogleAuthProviderID |
| 2704 | + sessionID:kOAuthSessionID |
| 2705 | + OAuthResponseURLString:kOAuthRequestURI]; |
| 2706 | + callback(credential, nil); |
| 2707 | + }); |
| 2708 | + }); |
| 2709 | + |
| 2710 | + [authResult.user |
| 2711 | + linkWithProvider:mockProvider |
| 2712 | + UIDelegate:nil |
| 2713 | + completion:^(FIRAuthDataResult *_Nullable result, |
| 2714 | + NSError *_Nullable error) { |
| 2715 | + XCTAssertTrue([NSThread isMainThread]); |
| 2716 | + XCTAssertEqual(error.code, |
| 2717 | + FIRAuthErrorCodeWebSignInUserInteractionFailure); |
| 2718 | + XCTAssertEqualObjects( |
| 2719 | + error.userInfo[NSLocalizedFailureReasonErrorKey], |
| 2720 | + kFakeWebSignInUserInteractionFailureReason); |
| 2721 | + [expectation fulfill]; |
| 2722 | + }]; |
| 2723 | + }]; |
| 2724 | + [self waitForExpectationsWithTimeout:kExpectationTimeout handler:nil]; |
| 2725 | + OCMVerifyAll(_mockBackend); |
| 2726 | +} |
| 2727 | + |
| 2728 | +/** @fn testReauthenticateWithProviderFailure |
| 2729 | + @brief Tests the flow of a failed @c reauthenticateWithProvider:completion: call. |
| 2730 | + */ |
| 2731 | +- (void)testReauthenticateWithProviderFailure { |
| 2732 | + [self expectVerifyAssertionRequest:FIRFacebookAuthProviderID |
| 2733 | + federatedID:kFacebookID |
| 2734 | + displayName:kFacebookDisplayName |
| 2735 | + profile:[[self class] googleProfile] |
| 2736 | + providerIDToken:kFacebookIDToken |
| 2737 | + providerAccessToken:kFacebookAccessToken]; |
| 2738 | + |
| 2739 | + XCTestExpectation *expectation = [self expectationWithDescription:@"callback"]; |
| 2740 | + [[FIRAuth auth] signOut:NULL]; |
| 2741 | + FIRAuthCredential *facebookCredential = |
| 2742 | + [FIRFacebookAuthProvider credentialWithAccessToken:kFacebookAccessToken]; |
| 2743 | + [[FIRAuth auth] |
| 2744 | + signInWithCredential:facebookCredential |
| 2745 | + completion:^(FIRAuthDataResult *_Nullable authResult, NSError *_Nullable error) { |
| 2746 | + XCTAssertTrue([NSThread isMainThread]); |
| 2747 | + [self assertUserFacebook:authResult.user]; |
| 2748 | + XCTAssertEqualObjects(authResult.additionalUserInfo.profile, |
| 2749 | + [[self class] googleProfile]); |
| 2750 | + XCTAssertEqualObjects(authResult.additionalUserInfo.username, kUserName); |
| 2751 | + XCTAssertEqualObjects(authResult.additionalUserInfo.providerID, |
| 2752 | + FIRFacebookAuthProviderID); |
| 2753 | + XCTAssertNil(error); |
| 2754 | + |
| 2755 | + OCMExpect([self->_mockBackend verifyAssertion:[OCMArg any] callback:[OCMArg any]]) |
| 2756 | + .andDispatchError2( |
| 2757 | + [FIRAuthErrorUtils webSignInUserInteractionFailureWithReason: |
| 2758 | + kFakeWebSignInUserInteractionFailureReason]); |
| 2759 | + id mockProvider = OCMClassMock([FIROAuthProvider class]); |
| 2760 | + OCMExpect([mockProvider getCredentialWithUIDelegate:[OCMArg any] |
| 2761 | + completion:[OCMArg any]]) |
| 2762 | + .andCallBlock2(^(id<FIRAuthUIDelegate> delegate, |
| 2763 | + FIRAuthCredentialCallback callback) { |
| 2764 | + dispatch_async(FIRAuthGlobalWorkQueue(), ^() { |
| 2765 | + FIROAuthCredential *credential = |
| 2766 | + [[FIROAuthCredential alloc] initWithProviderID:FIRGoogleAuthProviderID |
| 2767 | + sessionID:kOAuthSessionID |
| 2768 | + OAuthResponseURLString:kOAuthRequestURI]; |
| 2769 | + callback(credential, nil); |
| 2770 | + }); |
| 2771 | + }); |
| 2772 | + |
| 2773 | + [authResult.user |
| 2774 | + reauthenticateWithProvider:mockProvider |
| 2775 | + UIDelegate:nil |
| 2776 | + completion:^(FIRAuthDataResult *_Nullable result, |
| 2777 | + NSError *_Nullable error) { |
| 2778 | + XCTAssertTrue([NSThread isMainThread]); |
| 2779 | + XCTAssertEqual( |
| 2780 | + error.code, |
| 2781 | + FIRAuthErrorCodeWebSignInUserInteractionFailure); |
| 2782 | + XCTAssertEqualObjects( |
| 2783 | + error.userInfo[NSLocalizedFailureReasonErrorKey], |
| 2784 | + kFakeWebSignInUserInteractionFailureReason); |
| 2785 | + [expectation fulfill]; |
| 2786 | + }]; |
| 2787 | + }]; |
| 2788 | + [self waitForExpectationsWithTimeout:kExpectationTimeout handler:nil]; |
| 2789 | + OCMVerifyAll(_mockBackend); |
| 2790 | +} |
| 2791 | + |
2646 | 2792 | /** @fn testlinkPhoneAuthCredentialSuccess
|
2647 | 2793 | @brief Tests the flow of a successful @c linkWithCredential:completion:
|
2648 | 2794 | call using a phoneAuthCredential.
|
|
0 commit comments