| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739 |
- /*
- * Copyright 2017 Google
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- #import <TargetConditionals.h>
- #if TARGET_OS_IOS
- #import <SafariServices/SafariServices.h>
- #import <XCTest/XCTest.h>
- #import "FirebaseAuth/Sources/Public/FirebaseAuth/FIRAuth.h"
- #import "FirebaseAuth/Sources/Public/FirebaseAuth/FIRAuthSettings.h"
- #import "FirebaseAuth/Sources/Public/FirebaseAuth/FIRAuthUIDelegate.h"
- #import "FirebaseAuth/Sources/Public/FirebaseAuth/FIRPhoneAuthProvider.h"
- #import "FirebaseCore/Sources/Private/FirebaseCoreInternal.h"
- #import "OCMock.h"
- #import "FirebaseAuth/Sources/Auth/FIRAuthGlobalWorkQueue.h"
- #import "FirebaseAuth/Sources/Auth/FIRAuth_Internal.h"
- #import "FirebaseAuth/Sources/AuthProvider/FIRAuthCredential_Internal.h"
- #import "FirebaseAuth/Sources/AuthProvider/Phone/FIRPhoneAuthCredential_Internal.h"
- #import "FirebaseAuth/Sources/Backend/FIRAuthBackend.h"
- #import "FirebaseAuth/Sources/Backend/FIRAuthRequestConfiguration.h"
- #import "FirebaseAuth/Sources/Backend/RPC/FIRGetProjectConfigRequest.h"
- #import "FirebaseAuth/Sources/Backend/RPC/FIRGetProjectConfigResponse.h"
- #import "FirebaseAuth/Sources/Backend/RPC/FIRSendVerificationCodeRequest.h"
- #import "FirebaseAuth/Sources/Backend/RPC/FIRSendVerificationCodeResponse.h"
- #import "FirebaseAuth/Sources/Backend/RPC/FIRVerifyClientRequest.h"
- #import "FirebaseAuth/Sources/Backend/RPC/FIRVerifyClientResponse.h"
- #import "FirebaseAuth/Sources/SystemService/FIRAuthAPNSToken.h"
- #import "FirebaseAuth/Sources/SystemService/FIRAuthAPNSTokenManager.h"
- #import "FirebaseAuth/Sources/SystemService/FIRAuthAppCredential.h"
- #import "FirebaseAuth/Sources/SystemService/FIRAuthAppCredentialManager.h"
- #import "FirebaseAuth/Sources/SystemService/FIRAuthNotificationManager.h"
- #import "FirebaseAuth/Sources/Utilities/FIRAuthErrorUtils.h"
- #import "FirebaseAuth/Sources/Utilities/FIRAuthURLPresenter.h"
- #import "FirebaseAuth/Sources/Utilities/FIRAuthWebUtils.h"
- #import "FirebaseAuth/Tests/Unit/OCMStubRecorder+FIRAuthUnitTests.h"
- NS_ASSUME_NONNULL_BEGIN
- /** @var kTestPhoneNumber
- @brief A testing phone number.
- */
- static NSString *const kTestPhoneNumber = @"55555555";
- /** @var kTestInvalidPhoneNumber
- @brief An invalid testing phone number.
- */
- static NSString *const kTestInvalidPhoneNumber = @"555+!*55555";
- /** @var kTestVerificationID
- @brief A testing verfication ID.
- */
- static NSString *const kTestVerificationID = @"verificationID";
- /** @var kTestReceipt
- @brief A fake receipt for testing.
- */
- static NSString *const kTestReceipt = @"receipt";
- /** @var kTestSecret
- @brief A fake secret for testing.
- */
- static NSString *const kTestSecret = @"secret";
- /** @var kTestOldReceipt
- @brief A fake old receipt for testing.
- */
- static NSString *const kTestOldReceipt = @"old_receipt";
- /** @var kTestOldSecret
- @brief A fake old secret for testing.
- */
- static NSString *const kTestOldSecret = @"old_secret";
- /** @var kTestVerificationCode
- @brief A fake verfication code.
- */
- static NSString *const kTestVerificationCode = @"verificationCode";
- /** @var kFakeClientID
- @brief A fake client ID.
- */
- static NSString *const kFakeClientID = @"123456.apps.googleusercontent.com";
- /** @var kFakeReverseClientID
- @brief The dot-reversed version of the fake client ID.
- */
- static NSString *const kFakeReverseClientID = @"com.googleusercontent.apps.123456";
- /** @var kFakeFirebaseAppID
- @brief A fake Firebase app ID.
- */
- static NSString *const kFakeFirebaseAppID = @"1:123456789:ios:123abc456def";
- /** @var kFakeEncodedFirebaseAppID
- @brief A fake encoded Firebase app ID to be used as a custom URL scheme.
- */
- static NSString *const kFakeEncodedFirebaseAppID = @"app-1-123456789-ios-123abc456def";
- /** @var kFakeBundleID
- @brief A fake bundle ID.
- */
- static NSString *const kFakeBundleID = @"com.firebaseapp.example";
- /** @var kFakeAPIKey
- @brief A fake API key.
- */
- static NSString *const kFakeAPIKey = @"asdfghjkl";
- /** @var kFakeAuthorizedDomain
- @brief A fake authorized domain for the app.
- */
- static NSString *const kFakeAuthorizedDomain = @"test.firebaseapp.com";
- /** @var kFakeReCAPTCHAToken
- @brief A fake reCAPTCHA token.
- */
- static NSString *const kFakeReCAPTCHAToken = @"fakeReCAPTCHAToken";
- /** @var kFakeRedirectURLStringWithReCAPTCHAToken
- @brief The format for a fake redirect URL string (minus the scheme) that contains the fake
- reCAPTCHA token above.
- */
- static NSString *const kFakeRedirectURLStringWithReCAPTCHAToken =
- @"://firebaseauth/"
- @"link?deep_link_id=https%3A%2F%2Fexample.firebaseapp.com%2F__%2Fauth%2Fcallback%3FauthType%"
- @"3DverifyApp%26recaptchaToken%3DfakeReCAPTCHAToken";
- /** @var kFakeRedirectURLStringInvalidClientID
- @brief The format for a fake redirect URL string with an invalid client error.
- */
- static NSString *const kFakeRedirectURLStringInvalidClientID =
- @"com.googleusercontent.apps.1"
- "23456://firebaseauth/"
- "link?deep_link_id=https%3A%2F%2Fexample.firebaseapp.com%2F__%2Fauth%2Fcal"
- "lback%3FfirebaseError%3D%257B%2522code%2522%253A%2522auth%252Finvalid-oauth-client-id%2522%"
- "252"
- "C%2522message%2522%253A%2522The%2520OAuth%2520client%2520ID%2520provided%2520is%2520either%"
- "252"
- "0invalid%2520or%2520does%2520not%2520match%2520the%2520specified%2520API%2520key.%2522%257D%"
- "26"
- "authType%3DverifyApp";
- /** @var kFakeRedirectURLStringWebNetworkRequestFailed
- @brief The format for a fake redirect URL string with a web network request failed error.
- */
- static NSString *const kFakeRedirectURLStringWebNetworkRequestFailed =
- @"com.googleusercontent.apps"
- ".123456://firebaseauth/"
- "link?deep_link_id=https%3A%2F%2Fexample.firebaseapp.com%2F__%2Fauth%2Fc"
- "allback%3FfirebaseError%3D%257B%2522code%2522%253A%2522auth%252Fnetwork-request-failed%2522%"
- "25"
- "2C%2522message%2522%253A%2522The%2520network%2520request%2520failed%2520.%2522%257D%"
- "26authType"
- "%3DverifyApp";
- /** @var kFakeRedirectURLStringWebInternalError
- @brief The format for a fake redirect URL string with an internal web error.
- */
- static NSString *const kFakeRedirectURLStringWebInternalError =
- @"com.googleusercontent.apps.1"
- "23456://firebaseauth/"
- "link?deep_link_id=https%3A%2F%2Fexample.firebaseapp.com%2F__%2Fauth%2Fcal"
- "lback%3FfirebaseError%3D%257B%2522code%2522%253A%2522auth%252Finternal-error%2522%252C%"
- "2522mes"
- "sage%2522%253A%2522Internal%2520error%2520.%2522%257D%26authType%3DverifyApp";
- /** @var kFakeRedirectURLStringUnknownError
- @brief The format for a fake redirect URL string with unknown error response.
- */
- static NSString *const kFakeRedirectURLStringUnknownError =
- @"com.googleusercontent.apps.1"
- "23456://firebaseauth/"
- "link?deep_link_id=https%3A%2F%2Fexample.firebaseapp.com%2F__%2Fauth%2Fcal"
- "lback%3FfirebaseError%3D%257B%2522code%2522%253A%2522auth%252Funknown-error-id%2522%252"
- "C%2522message%2522%253A%2522The%2520OAuth%2520client%2520ID%2520provided%2520is%2520either%"
- "252"
- "0invalid%2520or%2520does%2520not%2520match%2520the%2520specified%2520API%2520key.%2522%257D%"
- "26"
- "authType%3DverifyApp";
- /** @var kFakeRedirectURLStringUnstructuredError
- @brief The format for a fake redirect URL string with unstructured error response.
- */
- static NSString *const kFakeRedirectURLStringUnstructuredError =
- @"com.googleusercontent.apps.1"
- "23456://firebaseauth/"
- "link?deep_link_id=https%3A%2F%2Fexample.firebaseapp.com%2F__%2Fauth%2Fcal"
- "lback%3FfirebaseError%3D%257B%2522unstructuredcode%2522%253A%2522auth%252Funknown-error-id%"
- "2522%252"
- "C%2522unstructuredmessage%2522%253A%2522The%2520OAuth%2520client%2520ID%2520provided%2520is%"
- "2520either%252"
- "0invalid%2520or%2520does%2520not%2520match%2520the%2520specified%2520API%2520key.%2522%257D%"
- "26"
- "authType%3DverifyApp";
- /** @var kTestTimeout
- @brief A fake timeout value for waiting for push notification.
- */
- static const NSTimeInterval kTestTimeout = 5;
- /** @var kExpectationTimeout
- @brief The maximum time waiting for expectations to fulfill.
- */
- static const NSTimeInterval kExpectationTimeout = 2;
- /** @class FIRPhoneAuthProviderTests
- @brief Tests for @c FIRPhoneAuthProvider
- */
- @interface FIRPhoneAuthProviderTests : XCTestCase
- @end
- @implementation FIRPhoneAuthProviderTests {
- /** @var _mockBackend
- @brief The mock @c FIRAuthBackendImplementation .
- */
- id _mockBackend;
- /** @var _provider
- @brief The @c FIRPhoneAuthProvider instance under test.
- */
- FIRPhoneAuthProvider *_provider;
- /** @var _mockAuth
- @brief The mock @c FIRAuth instance associated with @c _provider .
- */
- id _mockAuth;
- /** @var _mockApp
- @brief The mock @c FIRApp instance associated with @c _mockAuth .
- */
- id _mockApp;
- /** @var _mockOptions
- @brief The mock @c FIROptions instance associated with @c _mockApp.
- */
- id _mockOptions;
- /** @var _mockAPNSTokenManager
- @brief The mock @c FIRAuthAPNSTokenManager instance associated with @c _mockAuth .
- */
- id _mockAPNSTokenManager;
- /** @var _mockAppCredentialManager
- @brief The mock @c FIRAuthAppCredentialManager instance associated with @c _mockAuth .
- */
- id _mockAppCredentialManager;
- /** @var _mockNotificationManager
- @brief The mock @c FIRAuthNotificationManager instance associated with @c _mockAuth .
- */
- id _mockNotificationManager;
- /** @var _mockURLPresenter
- @brief The mock @c FIRAuthURLPresenter instance associated with @c _mockAuth .
- */
- id _mockURLPresenter;
- }
- - (void)setUp {
- [super setUp];
- _mockBackend = OCMProtocolMock(@protocol(FIRAuthBackendImplementation));
- [FIRAuthBackend setBackendImplementation:_mockBackend];
- _mockAuth = OCMClassMock([FIRAuth class]);
- _mockApp = OCMClassMock([FIRApp class]);
- OCMStub([_mockAuth app]).andReturn(_mockApp);
- _mockOptions = OCMClassMock([FIROptions class]);
- OCMStub([(FIRApp *)_mockApp options]).andReturn(_mockOptions);
- OCMStub([_mockOptions googleAppID]).andReturn(kFakeFirebaseAppID);
- _mockAPNSTokenManager = OCMClassMock([FIRAuthAPNSTokenManager class]);
- OCMStub([_mockAuth tokenManager]).andReturn(_mockAPNSTokenManager);
- _mockAppCredentialManager = OCMClassMock([FIRAuthAppCredentialManager class]);
- OCMStub([_mockAuth appCredentialManager]).andReturn(_mockAppCredentialManager);
- _mockNotificationManager = OCMClassMock([FIRAuthNotificationManager class]);
- OCMStub([_mockAuth notificationManager]).andReturn(_mockNotificationManager);
- _mockURLPresenter = OCMClassMock([FIRAuthURLPresenter class]);
- OCMStub([_mockAuth authURLPresenter]).andReturn(_mockURLPresenter);
- id mockRequestConfiguration = OCMClassMock([FIRAuthRequestConfiguration class]);
- OCMStub([_mockAuth requestConfiguration]).andReturn(mockRequestConfiguration);
- OCMStub([mockRequestConfiguration APIKey]).andReturn(kFakeAPIKey);
- }
- - (void)tearDown {
- [FIRAuthBackend setDefaultBackendImplementationWithRPCIssuer:nil];
- [super tearDown];
- }
- // We're still testing deprecated `verifyPhoneNumber:completion:` extensively.
- #pragma clang diagnostic push
- #pragma clang diagnostic ignored "-Wdeprecated-declarations"
- /** @fn testCredentialWithVerificationID
- @brief Tests the @c credentialWithToken method to make sure that it returns a valid
- FIRAuthCredential instance.
- */
- - (void)testCredentialWithVerificationID {
- OCMStub([_mockOptions clientID]).andReturn(kFakeClientID);
- _provider = [FIRPhoneAuthProvider providerWithAuth:_mockAuth];
- FIRPhoneAuthCredential *credential =
- [_provider credentialWithVerificationID:kTestVerificationID
- verificationCode:kTestVerificationCode];
- XCTAssertEqualObjects(credential.verificationID, kTestVerificationID);
- XCTAssertEqualObjects(credential.verificationCode, kTestVerificationCode);
- XCTAssertNil(credential.temporaryProof);
- XCTAssertNil(credential.phoneNumber);
- }
- /** @fn testVerifyEmptyPhoneNumber
- @brief Tests a failed invocation @c verifyPhoneNumber:completion: because an empty phone
- number was provided.
- */
- - (void)testVerifyEmptyPhoneNumber {
- id mockBundle = OCMClassMock([NSBundle class]);
- OCMStub(ClassMethod([mockBundle mainBundle])).andReturn(mockBundle);
- OCMStub([mockBundle objectForInfoDictionaryKey:@"CFBundleURLTypes"]).andReturn(@[
- @{@"CFBundleURLSchemes" : @[ kFakeReverseClientID ]}
- ]);
- OCMStub([mockBundle bundleIdentifier]).andReturn(kFakeBundleID);
- OCMStub([_mockOptions clientID]).andReturn(kFakeClientID);
- _provider = [FIRPhoneAuthProvider providerWithAuth:_mockAuth];
- // Empty phone number is checked on the client side so no backend RPC is mocked.
- XCTestExpectation *expectation = [self expectationWithDescription:@"callback"];
- [_provider verifyPhoneNumber:@""
- UIDelegate:nil
- completion:^(NSString *_Nullable verificationID, NSError *_Nullable error) {
- XCTAssertNotNil(error);
- XCTAssertEqual(error.code, FIRAuthErrorCodeMissingPhoneNumber);
- [expectation fulfill];
- }];
- [self waitForExpectationsWithTimeout:kExpectationTimeout handler:nil];
- }
- /** @fn testVerifyInvalidPhoneNumber
- @brief Tests a failed invocation @c verifyPhoneNumber:completion: because an invalid phone
- number was provided.
- */
- - (void)testVerifyInvalidPhoneNumber {
- id mockBundle = OCMClassMock([NSBundle class]);
- OCMStub(ClassMethod([mockBundle mainBundle])).andReturn(mockBundle);
- OCMStub([mockBundle objectForInfoDictionaryKey:@"CFBundleURLTypes"]).andReturn(@[
- @{@"CFBundleURLSchemes" : @[ kFakeReverseClientID ]}
- ]);
- OCMStub([mockBundle bundleIdentifier]).andReturn(kFakeBundleID);
- OCMStub([_mockOptions clientID]).andReturn(kFakeClientID);
- _provider = [FIRPhoneAuthProvider providerWithAuth:_mockAuth];
- OCMExpect([_mockNotificationManager checkNotificationForwardingWithCallback:OCMOCK_ANY])
- .andCallBlock1(^(FIRAuthNotificationForwardingCallback callback) {
- callback(YES);
- });
- OCMStub([_mockAppCredentialManager credential])
- .andReturn([[FIRAuthAppCredential alloc] initWithReceipt:kTestReceipt secret:kTestSecret]);
- OCMExpect([_mockBackend sendVerificationCode:[OCMArg any] callback:[OCMArg any]])
- .andCallBlock2(^(FIRSendVerificationCodeRequest *request,
- FIRSendVerificationCodeResponseCallback callback) {
- XCTAssertEqualObjects(request.phoneNumber, kTestPhoneNumber);
- XCTAssertEqualObjects(request.appCredential.receipt, kTestReceipt);
- XCTAssertEqualObjects(request.appCredential.secret, kTestSecret);
- dispatch_async(FIRAuthGlobalWorkQueue(), ^() {
- callback(nil, [FIRAuthErrorUtils invalidPhoneNumberErrorWithMessage:nil]);
- });
- });
- XCTestExpectation *expectation = [self expectationWithDescription:@"callback"];
- [_provider verifyPhoneNumber:kTestPhoneNumber
- UIDelegate:nil
- completion:^(NSString *_Nullable verificationID, NSError *_Nullable error) {
- XCTAssertTrue([NSThread isMainThread]);
- XCTAssertNil(verificationID);
- XCTAssertEqual(error.code, FIRAuthErrorCodeInvalidPhoneNumber);
- [expectation fulfill];
- }];
- [self waitForExpectationsWithTimeout:kExpectationTimeout handler:nil];
- OCMVerifyAll(_mockBackend);
- OCMVerifyAll(_mockNotificationManager);
- OCMVerifyAll(_mockAppCredentialManager);
- }
- /** @fn testVerifyPhoneNumber
- @brief Tests a successful invocation of @c verifyPhoneNumber:completion:.
- */
- - (void)testVerifyPhoneNumber {
- id mockBundle = OCMClassMock([NSBundle class]);
- OCMStub(ClassMethod([mockBundle mainBundle])).andReturn(mockBundle);
- OCMStub([mockBundle objectForInfoDictionaryKey:@"CFBundleURLTypes"]).andReturn(@[
- @{@"CFBundleURLSchemes" : @[ kFakeReverseClientID ]}
- ]);
- OCMStub([mockBundle bundleIdentifier]).andReturn(kFakeBundleID);
- OCMStub([_mockOptions clientID]).andReturn(kFakeClientID);
- _provider = [FIRPhoneAuthProvider providerWithAuth:_mockAuth];
- OCMExpect([_mockNotificationManager checkNotificationForwardingWithCallback:OCMOCK_ANY])
- .andCallBlock1(^(FIRAuthNotificationForwardingCallback callback) {
- callback(YES);
- });
- OCMStub([_mockAppCredentialManager credential])
- .andReturn([[FIRAuthAppCredential alloc] initWithReceipt:kTestReceipt secret:kTestSecret]);
- OCMExpect([_mockBackend sendVerificationCode:[OCMArg any] callback:[OCMArg any]])
- .andCallBlock2(^(FIRSendVerificationCodeRequest *request,
- FIRSendVerificationCodeResponseCallback callback) {
- XCTAssertEqualObjects(request.phoneNumber, kTestPhoneNumber);
- XCTAssertEqualObjects(request.appCredential.receipt, kTestReceipt);
- XCTAssertEqualObjects(request.appCredential.secret, kTestSecret);
- dispatch_async(FIRAuthGlobalWorkQueue(), ^() {
- id mockSendVerificationCodeResponse =
- OCMClassMock([FIRSendVerificationCodeResponse class]);
- OCMStub([mockSendVerificationCodeResponse verificationID]).andReturn(kTestVerificationID);
- callback(mockSendVerificationCodeResponse, nil);
- });
- });
- XCTestExpectation *expectation = [self expectationWithDescription:@"callback"];
- [_provider verifyPhoneNumber:kTestPhoneNumber
- UIDelegate:nil
- completion:^(NSString *_Nullable verificationID, NSError *_Nullable error) {
- XCTAssertTrue([NSThread isMainThread]);
- XCTAssertNil(error);
- XCTAssertEqualObjects(verificationID, kTestVerificationID);
- [expectation fulfill];
- }];
- [self waitForExpectationsWithTimeout:kExpectationTimeout handler:nil];
- OCMVerifyAll(_mockBackend);
- OCMVerifyAll(_mockNotificationManager);
- OCMVerifyAll(_mockAppCredentialManager);
- }
- /** @fn testVerifyPhoneNumberInTestMode
- @brief Tests a successful invocation of @c verifyPhoneNumber:completion: when app verification
- is disabled.
- */
- - (void)testVerifyPhoneNumberInTestMode {
- id mockBundle = OCMClassMock([NSBundle class]);
- OCMStub(ClassMethod([mockBundle mainBundle])).andReturn(mockBundle);
- OCMStub([mockBundle objectForInfoDictionaryKey:@"CFBundleURLTypes"]).andReturn(@[
- @{@"CFBundleURLSchemes" : @[ kFakeReverseClientID ]}
- ]);
- OCMStub([mockBundle bundleIdentifier]).andReturn(kFakeBundleID);
- OCMStub([_mockOptions clientID]).andReturn(kFakeClientID);
- _provider = [FIRPhoneAuthProvider providerWithAuth:_mockAuth];
- // Disable app verification.
- FIRAuthSettings *settings = [[FIRAuthSettings alloc] init];
- settings.appVerificationDisabledForTesting = YES;
- OCMStub([_mockAuth settings]).andReturn(settings);
- OCMExpect([_mockNotificationManager checkNotificationForwardingWithCallback:OCMOCK_ANY])
- .andCallBlock1(^(FIRAuthNotificationForwardingCallback callback) {
- callback(YES);
- });
- OCMExpect([_mockBackend sendVerificationCode:[OCMArg any] callback:[OCMArg any]])
- .andCallBlock2(^(FIRSendVerificationCodeRequest *request,
- FIRSendVerificationCodeResponseCallback callback) {
- XCTAssertEqualObjects(request.phoneNumber, kTestPhoneNumber);
- // Assert that the app credential is nil when in test mode.
- XCTAssertNil(request.appCredential);
- dispatch_async(FIRAuthGlobalWorkQueue(), ^() {
- id mockSendVerificationCodeResponse =
- OCMClassMock([FIRSendVerificationCodeResponse class]);
- OCMStub([mockSendVerificationCodeResponse verificationID]).andReturn(kTestVerificationID);
- callback(mockSendVerificationCodeResponse, nil);
- });
- });
- XCTestExpectation *expectation = [self expectationWithDescription:@"callback"];
- [_provider verifyPhoneNumber:kTestPhoneNumber
- UIDelegate:nil
- completion:^(NSString *_Nullable verificationID, NSError *_Nullable error) {
- XCTAssertTrue([NSThread isMainThread]);
- XCTAssertNil(error);
- XCTAssertEqualObjects(verificationID, kTestVerificationID);
- [expectation fulfill];
- }];
- [self waitForExpectationsWithTimeout:kExpectationTimeout handler:nil];
- OCMVerifyAll(_mockBackend);
- OCMVerifyAll(_mockNotificationManager);
- OCMVerifyAll(_mockAppCredentialManager);
- }
- /** @fn testVerifyPhoneNumberInTestModeFailure
- @brief Tests a failed invocation of @c verifyPhoneNumber:completion: when app verification
- is disabled.
- */
- - (void)testVerifyPhoneNumberInTestModeFailure {
- id mockBundle = OCMClassMock([NSBundle class]);
- OCMStub(ClassMethod([mockBundle mainBundle])).andReturn(mockBundle);
- OCMStub([mockBundle objectForInfoDictionaryKey:@"CFBundleURLTypes"]).andReturn(@[
- @{@"CFBundleURLSchemes" : @[ kFakeReverseClientID ]}
- ]);
- OCMStub([mockBundle bundleIdentifier]).andReturn(kFakeBundleID);
- OCMStub([_mockOptions clientID]).andReturn(kFakeClientID);
- _provider = [FIRPhoneAuthProvider providerWithAuth:_mockAuth];
- // Disable app verification.
- FIRAuthSettings *settings = [[FIRAuthSettings alloc] init];
- settings.appVerificationDisabledForTesting = YES;
- OCMStub([_mockAuth settings]).andReturn(settings);
- OCMExpect([_mockNotificationManager checkNotificationForwardingWithCallback:OCMOCK_ANY])
- .andCallBlock1(^(FIRAuthNotificationForwardingCallback callback) {
- callback(YES);
- });
- OCMExpect([_mockBackend sendVerificationCode:[OCMArg any] callback:[OCMArg any]])
- .andCallBlock2(^(FIRSendVerificationCodeRequest *request,
- FIRSendVerificationCodeResponseCallback callback) {
- XCTAssertEqualObjects(request.phoneNumber, kTestPhoneNumber);
- // Assert that the app credential is nil when in test mode.
- XCTAssertNil(request.appCredential);
- dispatch_async(FIRAuthGlobalWorkQueue(), ^() {
- NSError *underlying = [NSError errorWithDomain:@"Test Error" code:1 userInfo:nil];
- callback(nil, [FIRAuthErrorUtils networkErrorWithUnderlyingError:underlying]);
- });
- });
- XCTestExpectation *expectation = [self expectationWithDescription:@"callback"];
- [_provider verifyPhoneNumber:kTestPhoneNumber
- UIDelegate:nil
- completion:^(NSString *_Nullable verificationID, NSError *_Nullable error) {
- XCTAssertTrue([NSThread isMainThread]);
- XCTAssertNil(verificationID);
- XCTAssertEqual(error.code, FIRAuthErrorCodeNetworkError);
- [expectation fulfill];
- }];
- [self waitForExpectationsWithTimeout:kExpectationTimeout handler:nil];
- OCMVerifyAll(_mockBackend);
- OCMVerifyAll(_mockNotificationManager);
- OCMVerifyAll(_mockAppCredentialManager);
- }
- /** @fn testVerifyPhoneNumberUIDelegateFirebaseAppIdFlow
- @brief Tests a successful invocation of @c verifyPhoneNumber:UIDelegate:completion:.
- */
- - (void)testVerifyPhoneNumberUIDelegateFirebaseAppIdFlow {
- id mockBundle = OCMClassMock([NSBundle class]);
- OCMStub(ClassMethod([mockBundle mainBundle])).andReturn(mockBundle);
- OCMStub([mockBundle objectForInfoDictionaryKey:@"CFBundleURLTypes"]).andReturn(@[
- @{@"CFBundleURLSchemes" : @[ kFakeEncodedFirebaseAppID ]}
- ]);
- OCMStub([mockBundle bundleIdentifier]).andReturn(kFakeBundleID);
- _provider = [FIRPhoneAuthProvider providerWithAuth:_mockAuth];
- // Simulate missing app token error.
- OCMExpect([_mockNotificationManager checkNotificationForwardingWithCallback:OCMOCK_ANY])
- .andCallBlock1(^(FIRAuthNotificationForwardingCallback callback) {
- callback(YES);
- });
- OCMExpect([_mockAppCredentialManager credential]).andReturn(nil);
- OCMExpect([_mockAPNSTokenManager getTokenWithCallback:OCMOCK_ANY])
- .andCallBlock1(^(FIRAuthAPNSTokenCallback callback) {
- NSError *error = [NSError errorWithDomain:FIRAuthErrorDomain
- code:FIRAuthErrorCodeMissingAppToken
- userInfo:nil];
- callback(nil, error);
- });
- OCMExpect([_mockBackend getProjectConfig:[OCMArg any] callback:[OCMArg any]])
- .andCallBlock2(
- ^(FIRGetProjectConfigRequest *request, FIRGetProjectConfigResponseCallback callback) {
- XCTAssertNotNil(request);
- dispatch_async(FIRAuthGlobalWorkQueue(), ^() {
- id mockGetProjectConfigResponse = OCMClassMock([FIRGetProjectConfigResponse class]);
- OCMStub([mockGetProjectConfigResponse authorizedDomains]).andReturn(@[
- kFakeAuthorizedDomain
- ]);
- callback(mockGetProjectConfigResponse, nil);
- });
- });
- id mockUIDelegate = OCMProtocolMock(@protocol(FIRAuthUIDelegate));
- // Expect view controller presentation by UIDelegate.
- OCMExpect([_mockURLPresenter presentURL:OCMOCK_ANY
- UIDelegate:mockUIDelegate
- callbackMatcher:OCMOCK_ANY
- completion:OCMOCK_ANY])
- .andDo(^(NSInvocation *invocation) {
- __unsafe_unretained id unretainedArgument;
- // Indices 0 and 1 indicate the hidden arguments self and _cmd.
- // `presentURL` is at index 2.
- [invocation getArgument:&unretainedArgument atIndex:2];
- NSURL *presentURL = unretainedArgument;
- XCTAssertEqualObjects(presentURL.scheme, @"https");
- XCTAssertEqualObjects(presentURL.host, kFakeAuthorizedDomain);
- XCTAssertEqualObjects(presentURL.path, @"/__/auth/handler");
- NSURLComponents *actualURLComponents = [NSURLComponents componentsWithURL:presentURL
- resolvingAgainstBaseURL:NO];
- NSArray<NSURLQueryItem *> *queryItems = [actualURLComponents queryItems];
- XCTAssertEqualObjects([FIRAuthWebUtils queryItemValue:@"ibi" from:queryItems],
- kFakeBundleID);
- XCTAssertEqualObjects([FIRAuthWebUtils queryItemValue:@"appId" from:queryItems],
- kFakeFirebaseAppID);
- XCTAssertEqualObjects([FIRAuthWebUtils queryItemValue:@"apiKey" from:queryItems],
- kFakeAPIKey);
- XCTAssertEqualObjects([FIRAuthWebUtils queryItemValue:@"authType" from:queryItems],
- @"verifyApp");
- XCTAssertNotNil([FIRAuthWebUtils queryItemValue:@"v" from:queryItems]);
- // `callbackMatcher` is at index 4
- [invocation getArgument:&unretainedArgument atIndex:4];
- FIRAuthURLCallbackMatcher callbackMatcher = unretainedArgument;
- NSMutableString *redirectURL = [NSMutableString
- stringWithString:[kFakeEncodedFirebaseAppID
- stringByAppendingString:kFakeRedirectURLStringWithReCAPTCHAToken]];
- // Verify that the URL is rejected by the callback matcher without the event ID.
- XCTAssertFalse(callbackMatcher([NSURL URLWithString:redirectURL]));
- [redirectURL appendString:@"%26eventId%3D"];
- [redirectURL appendString:[FIRAuthWebUtils queryItemValue:@"eventId" from:queryItems]];
- NSURLComponents *originalComponents = [[NSURLComponents alloc] initWithString:redirectURL];
- // Verify that the URL is accepted by the callback matcher with the matching event ID.
- XCTAssertTrue(callbackMatcher([originalComponents URL]));
- NSURLComponents *components = [originalComponents copy];
- components.query = @"https";
- XCTAssertFalse(callbackMatcher([components URL]));
- components = [originalComponents copy];
- components.host = @"badhost";
- XCTAssertFalse(callbackMatcher([components URL]));
- components = [originalComponents copy];
- components.path = @"badpath";
- XCTAssertFalse(callbackMatcher([components URL]));
- components = [originalComponents copy];
- components.query = @"badquery";
- XCTAssertFalse(callbackMatcher([components URL]));
- // `completion` is at index 5
- [invocation getArgument:&unretainedArgument atIndex:5];
- FIRAuthURLPresentationCompletion completion = unretainedArgument;
- dispatch_async(FIRAuthGlobalWorkQueue(), ^() {
- completion([NSURL URLWithString:[kFakeEncodedFirebaseAppID
- stringByAppendingString:
- kFakeRedirectURLStringWithReCAPTCHAToken]],
- nil);
- });
- });
- OCMExpect([_mockBackend sendVerificationCode:[OCMArg any] callback:[OCMArg any]])
- .andCallBlock2(^(FIRSendVerificationCodeRequest *request,
- FIRSendVerificationCodeResponseCallback callback) {
- XCTAssertEqualObjects(request.phoneNumber, kTestPhoneNumber);
- XCTAssertNil(request.appCredential);
- XCTAssertEqualObjects(request.reCAPTCHAToken, kFakeReCAPTCHAToken);
- dispatch_async(FIRAuthGlobalWorkQueue(), ^() {
- id mockSendVerificationCodeResponse =
- OCMClassMock([FIRSendVerificationCodeResponse class]);
- OCMStub([mockSendVerificationCodeResponse verificationID]).andReturn(kTestVerificationID);
- callback(mockSendVerificationCodeResponse, nil);
- });
- });
- XCTestExpectation *expectation = [self expectationWithDescription:@"callback"];
- [_provider verifyPhoneNumber:kTestPhoneNumber
- UIDelegate:mockUIDelegate
- completion:^(NSString *_Nullable verificationID, NSError *_Nullable error) {
- XCTAssertTrue([NSThread isMainThread]);
- XCTAssertNil(error);
- XCTAssertEqualObjects(verificationID, kTestVerificationID);
- [expectation fulfill];
- }];
- [self waitForExpectationsWithTimeout:kExpectationTimeout handler:nil];
- OCMVerifyAll(_mockBackend);
- OCMVerifyAll(_mockNotificationManager);
- }
- /** @fn testVerifyPhoneNumberUIDelegateFirebaseAppIdWhileClientIdPresentFlow
- @brief Tests a successful invocation of @c verifyPhoneNumber:UIDelegate:completion: when the
- client ID is present in the plist file, but the encoded app ID is the registered custom URL
- scheme.
- */
- - (void)testVerifyPhoneNumberUIDelegateFirebaseAppIdWhileClientIdPresentFlow {
- id mockBundle = OCMClassMock([NSBundle class]);
- OCMStub(ClassMethod([mockBundle mainBundle])).andReturn(mockBundle);
- OCMStub([mockBundle objectForInfoDictionaryKey:@"CFBundleURLTypes"]).andReturn(@[
- @{@"CFBundleURLSchemes" : @[ kFakeEncodedFirebaseAppID ]}
- ]);
- OCMStub([mockBundle bundleIdentifier]).andReturn(kFakeBundleID);
- OCMStub([_mockOptions clientID]).andReturn(kFakeClientID);
- _provider = [FIRPhoneAuthProvider providerWithAuth:_mockAuth];
- // Simulate missing app token error.
- OCMExpect([_mockNotificationManager checkNotificationForwardingWithCallback:OCMOCK_ANY])
- .andCallBlock1(^(FIRAuthNotificationForwardingCallback callback) {
- callback(YES);
- });
- OCMExpect([_mockAppCredentialManager credential]).andReturn(nil);
- OCMExpect([_mockAPNSTokenManager getTokenWithCallback:OCMOCK_ANY])
- .andCallBlock1(^(FIRAuthAPNSTokenCallback callback) {
- NSError *error = [NSError errorWithDomain:FIRAuthErrorDomain
- code:FIRAuthErrorCodeMissingAppToken
- userInfo:nil];
- callback(nil, error);
- });
- OCMExpect([_mockBackend getProjectConfig:[OCMArg any] callback:[OCMArg any]])
- .andCallBlock2(
- ^(FIRGetProjectConfigRequest *request, FIRGetProjectConfigResponseCallback callback) {
- XCTAssertNotNil(request);
- dispatch_async(FIRAuthGlobalWorkQueue(), ^() {
- id mockGetProjectConfigResponse = OCMClassMock([FIRGetProjectConfigResponse class]);
- OCMStub([mockGetProjectConfigResponse authorizedDomains]).andReturn(@[
- kFakeAuthorizedDomain
- ]);
- callback(mockGetProjectConfigResponse, nil);
- });
- });
- id mockUIDelegate = OCMProtocolMock(@protocol(FIRAuthUIDelegate));
- // Expect view controller presentation by UIDelegate.
- OCMExpect([_mockURLPresenter presentURL:OCMOCK_ANY
- UIDelegate:mockUIDelegate
- callbackMatcher:OCMOCK_ANY
- completion:OCMOCK_ANY])
- .andDo(^(NSInvocation *invocation) {
- __unsafe_unretained id unretainedArgument;
- // Indices 0 and 1 indicate the hidden arguments self and _cmd.
- // `presentURL` is at index 2.
- [invocation getArgument:&unretainedArgument atIndex:2];
- NSURL *presentURL = unretainedArgument;
- XCTAssertEqualObjects(presentURL.scheme, @"https");
- XCTAssertEqualObjects(presentURL.host, kFakeAuthorizedDomain);
- XCTAssertEqualObjects(presentURL.path, @"/__/auth/handler");
- NSURLComponents *actualURLComponents = [NSURLComponents componentsWithURL:presentURL
- resolvingAgainstBaseURL:NO];
- NSArray<NSURLQueryItem *> *queryItems = [actualURLComponents queryItems];
- XCTAssertEqualObjects([FIRAuthWebUtils queryItemValue:@"ibi" from:queryItems],
- kFakeBundleID);
- XCTAssertEqualObjects([FIRAuthWebUtils queryItemValue:@"appId" from:queryItems],
- kFakeFirebaseAppID);
- XCTAssertEqualObjects([FIRAuthWebUtils queryItemValue:@"apiKey" from:queryItems],
- kFakeAPIKey);
- XCTAssertEqualObjects([FIRAuthWebUtils queryItemValue:@"authType" from:queryItems],
- @"verifyApp");
- XCTAssertNotNil([FIRAuthWebUtils queryItemValue:@"v" from:queryItems]);
- // `callbackMatcher` is at index 4
- [invocation getArgument:&unretainedArgument atIndex:4];
- FIRAuthURLCallbackMatcher callbackMatcher = unretainedArgument;
- NSMutableString *redirectURL = [NSMutableString
- stringWithString:[kFakeEncodedFirebaseAppID
- stringByAppendingString:kFakeRedirectURLStringWithReCAPTCHAToken]];
- // Verify that the URL is rejected by the callback matcher without the event ID.
- XCTAssertFalse(callbackMatcher([NSURL URLWithString:redirectURL]));
- [redirectURL appendString:@"%26eventId%3D"];
- [redirectURL appendString:[FIRAuthWebUtils queryItemValue:@"eventId" from:queryItems]];
- NSURLComponents *originalComponents = [[NSURLComponents alloc] initWithString:redirectURL];
- // Verify that the URL is accepted by the callback matcher with the matching event ID.
- XCTAssertTrue(callbackMatcher([originalComponents URL]));
- NSURLComponents *components = [originalComponents copy];
- components.query = @"https";
- XCTAssertFalse(callbackMatcher([components URL]));
- components = [originalComponents copy];
- components.host = @"badhost";
- XCTAssertFalse(callbackMatcher([components URL]));
- components = [originalComponents copy];
- components.path = @"badpath";
- XCTAssertFalse(callbackMatcher([components URL]));
- components = [originalComponents copy];
- components.query = @"badquery";
- XCTAssertFalse(callbackMatcher([components URL]));
- // `completion` is at index 5
- [invocation getArgument:&unretainedArgument atIndex:5];
- FIRAuthURLPresentationCompletion completion = unretainedArgument;
- dispatch_async(FIRAuthGlobalWorkQueue(), ^() {
- completion([NSURL URLWithString:[kFakeEncodedFirebaseAppID
- stringByAppendingString:
- kFakeRedirectURLStringWithReCAPTCHAToken]],
- nil);
- });
- });
- OCMExpect([_mockBackend sendVerificationCode:[OCMArg any] callback:[OCMArg any]])
- .andCallBlock2(^(FIRSendVerificationCodeRequest *request,
- FIRSendVerificationCodeResponseCallback callback) {
- XCTAssertEqualObjects(request.phoneNumber, kTestPhoneNumber);
- XCTAssertNil(request.appCredential);
- XCTAssertEqualObjects(request.reCAPTCHAToken, kFakeReCAPTCHAToken);
- dispatch_async(FIRAuthGlobalWorkQueue(), ^() {
- id mockSendVerificationCodeResponse =
- OCMClassMock([FIRSendVerificationCodeResponse class]);
- OCMStub([mockSendVerificationCodeResponse verificationID]).andReturn(kTestVerificationID);
- callback(mockSendVerificationCodeResponse, nil);
- });
- });
- XCTestExpectation *expectation = [self expectationWithDescription:@"callback"];
- [_provider verifyPhoneNumber:kTestPhoneNumber
- UIDelegate:mockUIDelegate
- completion:^(NSString *_Nullable verificationID, NSError *_Nullable error) {
- XCTAssertTrue([NSThread isMainThread]);
- XCTAssertNil(error);
- XCTAssertEqualObjects(verificationID, kTestVerificationID);
- [expectation fulfill];
- }];
- [self waitForExpectationsWithTimeout:kExpectationTimeout handler:nil];
- OCMVerifyAll(_mockBackend);
- OCMVerifyAll(_mockNotificationManager);
- }
- /** @fn testVerifyPhoneNumberUIDelegateClientIdFlow
- @brief Tests a successful invocation of @c verifyPhoneNumber:UIDelegate:completion:.
- */
- - (void)testVerifyPhoneNumberUIDelegateClientIdFlow {
- id mockBundle = OCMClassMock([NSBundle class]);
- OCMStub(ClassMethod([mockBundle mainBundle])).andReturn(mockBundle);
- OCMStub([mockBundle objectForInfoDictionaryKey:@"CFBundleURLTypes"]).andReturn(@[
- @{@"CFBundleURLSchemes" : @[ kFakeReverseClientID ]}
- ]);
- OCMStub([mockBundle bundleIdentifier]).andReturn(kFakeBundleID);
- OCMStub([_mockOptions clientID]).andReturn(kFakeClientID);
- _provider = [FIRPhoneAuthProvider providerWithAuth:_mockAuth];
- // Simulate missing app token error.
- OCMExpect([_mockNotificationManager checkNotificationForwardingWithCallback:OCMOCK_ANY])
- .andCallBlock1(^(FIRAuthNotificationForwardingCallback callback) {
- callback(YES);
- });
- OCMExpect([_mockAppCredentialManager credential]).andReturn(nil);
- OCMExpect([_mockAPNSTokenManager getTokenWithCallback:OCMOCK_ANY])
- .andCallBlock1(^(FIRAuthAPNSTokenCallback callback) {
- NSError *error = [NSError errorWithDomain:FIRAuthErrorDomain
- code:FIRAuthErrorCodeMissingAppToken
- userInfo:nil];
- callback(nil, error);
- });
- OCMExpect([_mockBackend getProjectConfig:[OCMArg any] callback:[OCMArg any]])
- .andCallBlock2(
- ^(FIRGetProjectConfigRequest *request, FIRGetProjectConfigResponseCallback callback) {
- XCTAssertNotNil(request);
- dispatch_async(FIRAuthGlobalWorkQueue(), ^() {
- id mockGetProjectConfigResponse = OCMClassMock([FIRGetProjectConfigResponse class]);
- OCMStub([mockGetProjectConfigResponse authorizedDomains]).andReturn(@[
- kFakeAuthorizedDomain
- ]);
- callback(mockGetProjectConfigResponse, nil);
- });
- });
- id mockUIDelegate = OCMProtocolMock(@protocol(FIRAuthUIDelegate));
- // Expect view controller presentation by UIDelegate.
- OCMExpect([_mockURLPresenter presentURL:OCMOCK_ANY
- UIDelegate:mockUIDelegate
- callbackMatcher:OCMOCK_ANY
- completion:OCMOCK_ANY])
- .andDo(^(NSInvocation *invocation) {
- __unsafe_unretained id unretainedArgument;
- // Indices 0 and 1 indicate the hidden arguments self and _cmd.
- // `presentURL` is at index 2.
- [invocation getArgument:&unretainedArgument atIndex:2];
- NSURL *presentURL = unretainedArgument;
- XCTAssertEqualObjects(presentURL.scheme, @"https");
- XCTAssertEqualObjects(presentURL.host, kFakeAuthorizedDomain);
- XCTAssertEqualObjects(presentURL.path, @"/__/auth/handler");
- NSURLComponents *actualURLComponents = [NSURLComponents componentsWithURL:presentURL
- resolvingAgainstBaseURL:NO];
- NSArray<NSURLQueryItem *> *queryItems = [actualURLComponents queryItems];
- XCTAssertEqualObjects([FIRAuthWebUtils queryItemValue:@"ibi" from:queryItems],
- kFakeBundleID);
- XCTAssertEqualObjects([FIRAuthWebUtils queryItemValue:@"clientId" from:queryItems],
- kFakeClientID);
- XCTAssertEqualObjects([FIRAuthWebUtils queryItemValue:@"apiKey" from:queryItems],
- kFakeAPIKey);
- XCTAssertEqualObjects([FIRAuthWebUtils queryItemValue:@"authType" from:queryItems],
- @"verifyApp");
- XCTAssertNotNil([FIRAuthWebUtils queryItemValue:@"v" from:queryItems]);
- // `callbackMatcher` is at index 4
- [invocation getArgument:&unretainedArgument atIndex:4];
- FIRAuthURLCallbackMatcher callbackMatcher = unretainedArgument;
- NSMutableString *redirectURL = [NSMutableString
- stringWithString:[kFakeReverseClientID
- stringByAppendingString:kFakeRedirectURLStringWithReCAPTCHAToken]];
- // Verify that the URL is rejected by the callback matcher without the event ID.
- XCTAssertFalse(callbackMatcher([NSURL URLWithString:redirectURL]));
- [redirectURL appendString:@"%26eventId%3D"];
- [redirectURL appendString:[FIRAuthWebUtils queryItemValue:@"eventId" from:queryItems]];
- NSURLComponents *originalComponents = [[NSURLComponents alloc] initWithString:redirectURL];
- // Verify that the URL is accepted by the callback matcher with the matching event ID.
- XCTAssertTrue(callbackMatcher([originalComponents URL]));
- NSURLComponents *components = [originalComponents copy];
- components.query = @"https";
- XCTAssertFalse(callbackMatcher([components URL]));
- components = [originalComponents copy];
- components.host = @"badhost";
- XCTAssertFalse(callbackMatcher([components URL]));
- components = [originalComponents copy];
- components.path = @"badpath";
- XCTAssertFalse(callbackMatcher([components URL]));
- components = [originalComponents copy];
- components.query = @"badquery";
- XCTAssertFalse(callbackMatcher([components URL]));
- // `completion` is at index 5
- [invocation getArgument:&unretainedArgument atIndex:5];
- FIRAuthURLPresentationCompletion completion = unretainedArgument;
- dispatch_async(FIRAuthGlobalWorkQueue(), ^() {
- completion([NSURL URLWithString:[kFakeReverseClientID
- stringByAppendingString:
- kFakeRedirectURLStringWithReCAPTCHAToken]],
- nil);
- });
- });
- OCMExpect([_mockBackend sendVerificationCode:[OCMArg any] callback:[OCMArg any]])
- .andCallBlock2(^(FIRSendVerificationCodeRequest *request,
- FIRSendVerificationCodeResponseCallback callback) {
- XCTAssertEqualObjects(request.phoneNumber, kTestPhoneNumber);
- XCTAssertNil(request.appCredential);
- XCTAssertEqualObjects(request.reCAPTCHAToken, kFakeReCAPTCHAToken);
- dispatch_async(FIRAuthGlobalWorkQueue(), ^() {
- id mockSendVerificationCodeResponse =
- OCMClassMock([FIRSendVerificationCodeResponse class]);
- OCMStub([mockSendVerificationCodeResponse verificationID]).andReturn(kTestVerificationID);
- callback(mockSendVerificationCodeResponse, nil);
- });
- });
- XCTestExpectation *expectation = [self expectationWithDescription:@"callback"];
- [_provider verifyPhoneNumber:kTestPhoneNumber
- UIDelegate:mockUIDelegate
- completion:^(NSString *_Nullable verificationID, NSError *_Nullable error) {
- XCTAssertTrue([NSThread isMainThread]);
- XCTAssertNil(error);
- XCTAssertEqualObjects(verificationID, kTestVerificationID);
- [expectation fulfill];
- }];
- [self waitForExpectationsWithTimeout:kExpectationTimeout handler:nil];
- OCMVerifyAll(_mockBackend);
- OCMVerifyAll(_mockNotificationManager);
- }
- /** @fn testVerifyPhoneNumberUIDelegateInvalidClientID
- @brief Tests a invocation of @c verifyPhoneNumber:UIDelegate:completion: which results in an
- invalid client ID error.
- */
- - (void)testVerifyPhoneNumberUIDelegateInvalidClientID {
- id mockBundle = OCMClassMock([NSBundle class]);
- OCMStub(ClassMethod([mockBundle mainBundle])).andReturn(mockBundle);
- OCMStub([mockBundle objectForInfoDictionaryKey:@"CFBundleURLTypes"]).andReturn(@[
- @{@"CFBundleURLSchemes" : @[ kFakeReverseClientID ]}
- ]);
- OCMStub([mockBundle bundleIdentifier]).andReturn(kFakeBundleID);
- OCMStub([_mockOptions clientID]).andReturn(kFakeClientID);
- _provider = [FIRPhoneAuthProvider providerWithAuth:_mockAuth];
- // Simulate missing app token error.
- OCMExpect([_mockNotificationManager checkNotificationForwardingWithCallback:OCMOCK_ANY])
- .andCallBlock1(^(FIRAuthNotificationForwardingCallback callback) {
- callback(YES);
- });
- OCMExpect([_mockAppCredentialManager credential]).andReturn(nil);
- OCMExpect([_mockAPNSTokenManager getTokenWithCallback:OCMOCK_ANY])
- .andCallBlock1(^(FIRAuthAPNSTokenCallback callback) {
- NSError *error = [NSError errorWithDomain:FIRAuthErrorDomain
- code:FIRAuthErrorCodeMissingAppToken
- userInfo:nil];
- callback(nil, error);
- });
- OCMExpect([_mockBackend getProjectConfig:[OCMArg any] callback:[OCMArg any]])
- .andCallBlock2(
- ^(FIRGetProjectConfigRequest *request, FIRGetProjectConfigResponseCallback callback) {
- XCTAssertNotNil(request);
- dispatch_async(FIRAuthGlobalWorkQueue(), ^() {
- id mockGetProjectConfigResponse = OCMClassMock([FIRGetProjectConfigResponse class]);
- OCMStub([mockGetProjectConfigResponse authorizedDomains]).andReturn(@[
- kFakeAuthorizedDomain
- ]);
- callback(mockGetProjectConfigResponse, nil);
- });
- });
- id mockUIDelegate = OCMProtocolMock(@protocol(FIRAuthUIDelegate));
- // Expect view controller presentation by UIDelegate.
- OCMExpect([_mockURLPresenter presentURL:OCMOCK_ANY
- UIDelegate:mockUIDelegate
- callbackMatcher:OCMOCK_ANY
- completion:OCMOCK_ANY])
- .andDo(^(NSInvocation *invocation) {
- __unsafe_unretained id unretainedArgument;
- // Indices 0 and 1 indicate the hidden arguments self and _cmd.
- // `completion` is at index 5
- [invocation getArgument:&unretainedArgument atIndex:5];
- FIRAuthURLPresentationCompletion completion = unretainedArgument;
- dispatch_async(FIRAuthGlobalWorkQueue(), ^() {
- completion([NSURL URLWithString:kFakeRedirectURLStringInvalidClientID], nil);
- });
- });
- XCTestExpectation *expectation = [self expectationWithDescription:@"callback"];
- [_provider verifyPhoneNumber:kTestPhoneNumber
- UIDelegate:mockUIDelegate
- completion:^(NSString *_Nullable verificationID, NSError *_Nullable error) {
- XCTAssertTrue([NSThread isMainThread]);
- XCTAssertEqual(error.code, FIRAuthErrorCodeInvalidClientID);
- XCTAssertNil(verificationID);
- [expectation fulfill];
- }];
- [self waitForExpectationsWithTimeout:kExpectationTimeout handler:nil];
- OCMVerifyAll(_mockBackend);
- OCMVerifyAll(_mockNotificationManager);
- }
- /** @fn testVerifyPhoneNumberUIDelegateWebNetworkRequestFailed
- @brief Tests a invocation of @c verifyPhoneNumber:UIDelegate:completion: which results in a web
- network request failed error.
- */
- - (void)testVerifyPhoneNumberUIDelegateNetworkRequestFailed {
- id mockBundle = OCMClassMock([NSBundle class]);
- OCMStub(ClassMethod([mockBundle mainBundle])).andReturn(mockBundle);
- OCMStub([mockBundle objectForInfoDictionaryKey:@"CFBundleURLTypes"]).andReturn(@[
- @{@"CFBundleURLSchemes" : @[ kFakeReverseClientID ]}
- ]);
- OCMStub([mockBundle bundleIdentifier]).andReturn(kFakeBundleID);
- OCMStub([_mockOptions clientID]).andReturn(kFakeClientID);
- _provider = [FIRPhoneAuthProvider providerWithAuth:_mockAuth];
- // Simulate missing app token error.
- OCMExpect([_mockNotificationManager checkNotificationForwardingWithCallback:OCMOCK_ANY])
- .andCallBlock1(^(FIRAuthNotificationForwardingCallback callback) {
- callback(YES);
- });
- OCMExpect([_mockAppCredentialManager credential]).andReturn(nil);
- OCMExpect([_mockAPNSTokenManager getTokenWithCallback:OCMOCK_ANY])
- .andCallBlock1(^(FIRAuthAPNSTokenCallback callback) {
- NSError *error = [NSError errorWithDomain:FIRAuthErrorDomain
- code:FIRAuthErrorCodeMissingAppToken
- userInfo:nil];
- callback(nil, error);
- });
- OCMExpect([_mockBackend getProjectConfig:[OCMArg any] callback:[OCMArg any]])
- .andCallBlock2(
- ^(FIRGetProjectConfigRequest *request, FIRGetProjectConfigResponseCallback callback) {
- XCTAssertNotNil(request);
- dispatch_async(FIRAuthGlobalWorkQueue(), ^() {
- id mockGetProjectConfigResponse = OCMClassMock([FIRGetProjectConfigResponse class]);
- OCMStub([mockGetProjectConfigResponse authorizedDomains]).andReturn(@[
- kFakeAuthorizedDomain
- ]);
- callback(mockGetProjectConfigResponse, nil);
- });
- });
- id mockUIDelegate = OCMProtocolMock(@protocol(FIRAuthUIDelegate));
- // Expect view controller presentation by UIDelegate.
- OCMExpect([_mockURLPresenter presentURL:OCMOCK_ANY
- UIDelegate:mockUIDelegate
- callbackMatcher:OCMOCK_ANY
- completion:OCMOCK_ANY])
- .andDo(^(NSInvocation *invocation) {
- __unsafe_unretained id unretainedArgument;
- // Indices 0 and 1 indicate the hidden arguments self and _cmd.
- // `completion` is at index 5
- [invocation getArgument:&unretainedArgument atIndex:5];
- FIRAuthURLPresentationCompletion completion = unretainedArgument;
- dispatch_async(FIRAuthGlobalWorkQueue(), ^() {
- completion([NSURL URLWithString:kFakeRedirectURLStringWebNetworkRequestFailed], nil);
- });
- });
- XCTestExpectation *expectation = [self expectationWithDescription:@"callback"];
- [_provider verifyPhoneNumber:kTestPhoneNumber
- UIDelegate:mockUIDelegate
- completion:^(NSString *_Nullable verificationID, NSError *_Nullable error) {
- XCTAssertTrue([NSThread isMainThread]);
- XCTAssertEqual(error.code, FIRAuthErrorCodeWebNetworkRequestFailed);
- XCTAssertNil(verificationID);
- [expectation fulfill];
- }];
- [self waitForExpectationsWithTimeout:kExpectationTimeout handler:nil];
- OCMVerifyAll(_mockBackend);
- OCMVerifyAll(_mockNotificationManager);
- }
- /** @fn testVerifyPhoneNumberUIDelegateWebInternalError
- @brief Tests a invocation of @c verifyPhoneNumber:UIDelegate:completion: which results in a web
- internal error.
- */
- - (void)testVerifyPhoneNumberUIDelegateWebInternalError {
- id mockBundle = OCMClassMock([NSBundle class]);
- OCMStub(ClassMethod([mockBundle mainBundle])).andReturn(mockBundle);
- OCMStub([mockBundle objectForInfoDictionaryKey:@"CFBundleURLTypes"]).andReturn(@[
- @{@"CFBundleURLSchemes" : @[ kFakeReverseClientID ]}
- ]);
- OCMStub([mockBundle bundleIdentifier]).andReturn(kFakeBundleID);
- OCMStub([_mockOptions clientID]).andReturn(kFakeClientID);
- _provider = [FIRPhoneAuthProvider providerWithAuth:_mockAuth];
- // Simulate missing app token error.
- OCMExpect([_mockNotificationManager checkNotificationForwardingWithCallback:OCMOCK_ANY])
- .andCallBlock1(^(FIRAuthNotificationForwardingCallback callback) {
- callback(YES);
- });
- OCMExpect([_mockAppCredentialManager credential]).andReturn(nil);
- OCMExpect([_mockAPNSTokenManager getTokenWithCallback:OCMOCK_ANY])
- .andCallBlock1(^(FIRAuthAPNSTokenCallback callback) {
- NSError *error = [NSError errorWithDomain:FIRAuthErrorDomain
- code:FIRAuthErrorCodeMissingAppToken
- userInfo:nil];
- callback(nil, error);
- });
- OCMExpect([_mockBackend getProjectConfig:[OCMArg any] callback:[OCMArg any]])
- .andCallBlock2(
- ^(FIRGetProjectConfigRequest *request, FIRGetProjectConfigResponseCallback callback) {
- XCTAssertNotNil(request);
- dispatch_async(FIRAuthGlobalWorkQueue(), ^() {
- id mockGetProjectConfigResponse = OCMClassMock([FIRGetProjectConfigResponse class]);
- OCMStub([mockGetProjectConfigResponse authorizedDomains]).andReturn(@[
- kFakeAuthorizedDomain
- ]);
- callback(mockGetProjectConfigResponse, nil);
- });
- });
- id mockUIDelegate = OCMProtocolMock(@protocol(FIRAuthUIDelegate));
- // Expect view controller presentation by UIDelegate.
- OCMExpect([_mockURLPresenter presentURL:OCMOCK_ANY
- UIDelegate:mockUIDelegate
- callbackMatcher:OCMOCK_ANY
- completion:OCMOCK_ANY])
- .andDo(^(NSInvocation *invocation) {
- __unsafe_unretained id unretainedArgument;
- // Indices 0 and 1 indicate the hidden arguments self and _cmd.
- // `completion` is at index 5
- [invocation getArgument:&unretainedArgument atIndex:5];
- FIRAuthURLPresentationCompletion completion = unretainedArgument;
- dispatch_async(FIRAuthGlobalWorkQueue(), ^() {
- completion([NSURL URLWithString:kFakeRedirectURLStringWebInternalError], nil);
- });
- });
- XCTestExpectation *expectation = [self expectationWithDescription:@"callback"];
- [_provider verifyPhoneNumber:kTestPhoneNumber
- UIDelegate:mockUIDelegate
- completion:^(NSString *_Nullable verificationID, NSError *_Nullable error) {
- XCTAssertTrue([NSThread isMainThread]);
- XCTAssertEqual(error.code, FIRAuthErrorCodeWebInternalError);
- XCTAssertNil(verificationID);
- [expectation fulfill];
- }];
- [self waitForExpectationsWithTimeout:kExpectationTimeout handler:nil];
- OCMVerifyAll(_mockBackend);
- OCMVerifyAll(_mockNotificationManager);
- }
- /** @fn testVerifyPhoneNumberUIDelegateUnexpectedError
- @brief Tests a invocation of @c verifyPhoneNumber:UIDelegate:completion: which results in an
- invalid client ID.
- */
- - (void)testVerifyPhoneNumberUIDelegateUnexpectedError {
- id mockBundle = OCMClassMock([NSBundle class]);
- OCMStub(ClassMethod([mockBundle mainBundle])).andReturn(mockBundle);
- OCMStub([mockBundle objectForInfoDictionaryKey:@"CFBundleURLTypes"]).andReturn(@[
- @{@"CFBundleURLSchemes" : @[ kFakeReverseClientID ]}
- ]);
- OCMStub([mockBundle bundleIdentifier]).andReturn(kFakeBundleID);
- OCMStub([_mockOptions clientID]).andReturn(kFakeClientID);
- _provider = [FIRPhoneAuthProvider providerWithAuth:_mockAuth];
- // Simulate missing app token error.
- OCMExpect([_mockNotificationManager checkNotificationForwardingWithCallback:OCMOCK_ANY])
- .andCallBlock1(^(FIRAuthNotificationForwardingCallback callback) {
- callback(YES);
- });
- OCMExpect([_mockAppCredentialManager credential]).andReturn(nil);
- OCMExpect([_mockAPNSTokenManager getTokenWithCallback:OCMOCK_ANY])
- .andCallBlock1(^(FIRAuthAPNSTokenCallback callback) {
- NSError *error = [NSError errorWithDomain:FIRAuthErrorDomain
- code:FIRAuthErrorCodeMissingAppToken
- userInfo:nil];
- callback(nil, error);
- });
- OCMExpect([_mockBackend getProjectConfig:[OCMArg any] callback:[OCMArg any]])
- .andCallBlock2(
- ^(FIRGetProjectConfigRequest *request, FIRGetProjectConfigResponseCallback callback) {
- XCTAssertNotNil(request);
- dispatch_async(FIRAuthGlobalWorkQueue(), ^() {
- id mockGetProjectConfigResponse = OCMClassMock([FIRGetProjectConfigResponse class]);
- OCMStub([mockGetProjectConfigResponse authorizedDomains]).andReturn(@[
- kFakeAuthorizedDomain
- ]);
- callback(mockGetProjectConfigResponse, nil);
- });
- });
- id mockUIDelegate = OCMProtocolMock(@protocol(FIRAuthUIDelegate));
- // Expect view controller presentation by UIDelegate.
- OCMExpect([_mockURLPresenter presentURL:OCMOCK_ANY
- UIDelegate:mockUIDelegate
- callbackMatcher:OCMOCK_ANY
- completion:OCMOCK_ANY])
- .andDo(^(NSInvocation *invocation) {
- __unsafe_unretained id unretainedArgument;
- // Indices 0 and 1 indicate the hidden arguments self and _cmd.
- // `completion` is at index 5
- [invocation getArgument:&unretainedArgument atIndex:5];
- FIRAuthURLPresentationCompletion completion = unretainedArgument;
- dispatch_async(FIRAuthGlobalWorkQueue(), ^() {
- completion([NSURL URLWithString:kFakeRedirectURLStringUnknownError], nil);
- });
- });
- XCTestExpectation *expectation = [self expectationWithDescription:@"callback"];
- [_provider
- verifyPhoneNumber:kTestPhoneNumber
- UIDelegate:mockUIDelegate
- completion:^(NSString *_Nullable verificationID, NSError *_Nullable error) {
- XCTAssertTrue([NSThread isMainThread]);
- XCTAssertEqual(error.code, FIRAuthErrorCodeAppVerificationUserInteractionFailure);
- XCTAssertNil(verificationID);
- [expectation fulfill];
- }];
- [self waitForExpectationsWithTimeout:kExpectationTimeout handler:nil];
- OCMVerifyAll(_mockBackend);
- OCMVerifyAll(_mockNotificationManager);
- }
- /** @fn testVerifyPhoneNumberUIDelegateUnstructuredError
- @brief Tests a invocation of @c verifyPhoneNumber:UIDelegate:completion: which results in an
- error being surfaced with a default NSLocalizedFailureReasonErrorKey due to an unexpected
- structure of the error response.
- */
- - (void)testVerifyPhoneNumberUIDelegateUnstructuredError {
- id mockBundle = OCMClassMock([NSBundle class]);
- OCMStub(ClassMethod([mockBundle mainBundle])).andReturn(mockBundle);
- OCMStub([mockBundle objectForInfoDictionaryKey:@"CFBundleURLTypes"]).andReturn(@[
- @{@"CFBundleURLSchemes" : @[ kFakeReverseClientID ]}
- ]);
- OCMStub([mockBundle bundleIdentifier]).andReturn(kFakeBundleID);
- OCMStub([_mockOptions clientID]).andReturn(kFakeClientID);
- _provider = [FIRPhoneAuthProvider providerWithAuth:_mockAuth];
- // Simulate missing app token error.
- OCMExpect([_mockNotificationManager checkNotificationForwardingWithCallback:OCMOCK_ANY])
- .andCallBlock1(^(FIRAuthNotificationForwardingCallback callback) {
- callback(YES);
- });
- OCMExpect([_mockAppCredentialManager credential]).andReturn(nil);
- OCMExpect([_mockAPNSTokenManager getTokenWithCallback:OCMOCK_ANY])
- .andCallBlock1(^(FIRAuthAPNSTokenCallback callback) {
- NSError *error = [NSError errorWithDomain:FIRAuthErrorDomain
- code:FIRAuthErrorCodeMissingAppToken
- userInfo:nil];
- callback(nil, error);
- });
- OCMExpect([_mockBackend getProjectConfig:[OCMArg any] callback:[OCMArg any]])
- .andCallBlock2(
- ^(FIRGetProjectConfigRequest *request, FIRGetProjectConfigResponseCallback callback) {
- XCTAssertNotNil(request);
- dispatch_async(FIRAuthGlobalWorkQueue(), ^() {
- id mockGetProjectConfigResponse = OCMClassMock([FIRGetProjectConfigResponse class]);
- OCMStub([mockGetProjectConfigResponse authorizedDomains]).andReturn(@[
- kFakeAuthorizedDomain
- ]);
- callback(mockGetProjectConfigResponse, nil);
- });
- });
- id mockUIDelegate = OCMProtocolMock(@protocol(FIRAuthUIDelegate));
- // Expect view controller presentation by UIDelegate.
- OCMExpect([_mockURLPresenter presentURL:OCMOCK_ANY
- UIDelegate:mockUIDelegate
- callbackMatcher:OCMOCK_ANY
- completion:OCMOCK_ANY])
- .andDo(^(NSInvocation *invocation) {
- __unsafe_unretained id unretainedArgument;
- // Indices 0 and 1 indicate the hidden arguments self and _cmd.
- // `completion` is at index 5
- [invocation getArgument:&unretainedArgument atIndex:5];
- FIRAuthURLPresentationCompletion completion = unretainedArgument;
- dispatch_async(FIRAuthGlobalWorkQueue(), ^() {
- completion([NSURL URLWithString:kFakeRedirectURLStringUnstructuredError], nil);
- });
- });
- XCTestExpectation *expectation = [self expectationWithDescription:@"callback"];
- [_provider
- verifyPhoneNumber:kTestPhoneNumber
- UIDelegate:mockUIDelegate
- completion:^(NSString *_Nullable verificationID, NSError *_Nullable error) {
- XCTAssertTrue([NSThread isMainThread]);
- XCTAssertEqual(error.code, FIRAuthErrorCodeAppVerificationUserInteractionFailure);
- XCTAssertNil(verificationID);
- [expectation fulfill];
- }];
- [self waitForExpectationsWithTimeout:kExpectationTimeout handler:nil];
- OCMVerifyAll(_mockBackend);
- OCMVerifyAll(_mockNotificationManager);
- }
- /** @fn testVerifyPhoneNumberUIDelegateRaiseException
- @brief Tests a invocation of @c verifyPhoneNumber:UIDelegate:completion: which results in an
- exception.
- */
- - (void)testVerifyPhoneNumberUIDelegateRaiseException {
- id mockBundle = OCMClassMock([NSBundle class]);
- OCMStub(ClassMethod([mockBundle mainBundle])).andReturn(mockBundle);
- OCMStub([mockBundle objectForInfoDictionaryKey:@"CFBundleURLTypes"]).andReturn(@[
- @{@"CFBundleURLSchemes" : @[ @"badscheme" ]}
- ]);
- OCMStub([_mockOptions clientID]).andReturn(kFakeClientID);
- _provider = [FIRPhoneAuthProvider providerWithAuth:_mockAuth];
- id mockUIDelegate = OCMProtocolMock(@protocol(FIRAuthUIDelegate));
- XCTAssertThrows([_provider
- verifyPhoneNumber:kTestPhoneNumber
- UIDelegate:mockUIDelegate
- completion:^(NSString *_Nullable verificationID, NSError *_Nullable error) {
- XCTFail(@"Shouldn't call completion here.");
- }]);
- }
- /** @fn testNotForwardingNotification
- @brief Tests returning an error for the app failing to forward notification.
- */
- - (void)testNotForwardingNotification {
- id mockBundle = OCMClassMock([NSBundle class]);
- OCMStub(ClassMethod([mockBundle mainBundle])).andReturn(mockBundle);
- OCMStub([mockBundle objectForInfoDictionaryKey:@"CFBundleURLTypes"]).andReturn(@[
- @{@"CFBundleURLSchemes" : @[ kFakeReverseClientID ]}
- ]);
- OCMStub([mockBundle bundleIdentifier]).andReturn(kFakeBundleID);
- OCMStub([_mockOptions clientID]).andReturn(kFakeClientID);
- _provider = [FIRPhoneAuthProvider providerWithAuth:_mockAuth];
- OCMExpect([_mockNotificationManager checkNotificationForwardingWithCallback:OCMOCK_ANY])
- .andCallBlock1(^(FIRAuthNotificationForwardingCallback callback) {
- callback(NO);
- });
- XCTestExpectation *expectation = [self expectationWithDescription:@"callback"];
- [_provider verifyPhoneNumber:kTestPhoneNumber
- UIDelegate:nil
- completion:^(NSString *_Nullable verificationID, NSError *_Nullable error) {
- XCTAssertTrue([NSThread isMainThread]);
- XCTAssertNil(verificationID);
- XCTAssertEqual(error.code, FIRAuthErrorCodeNotificationNotForwarded);
- [expectation fulfill];
- }];
- [self waitForExpectationsWithTimeout:kExpectationTimeout handler:nil];
- OCMVerifyAll(_mockNotificationManager);
- }
- /** @fn testMissingAPNSToken
- @brief Tests returning an error for the app failing to provide an APNS device token.
- */
- - (void)testMissingAPNSToken {
- id mockBundle = OCMClassMock([NSBundle class]);
- OCMStub(ClassMethod([mockBundle mainBundle])).andReturn(mockBundle);
- OCMStub([mockBundle objectForInfoDictionaryKey:@"CFBundleURLTypes"]).andReturn(@[
- @{@"CFBundleURLSchemes" : @[ kFakeReverseClientID ]}
- ]);
- OCMStub([mockBundle bundleIdentifier]).andReturn(kFakeBundleID);
- OCMStub([_mockOptions clientID]).andReturn(kFakeClientID);
- _provider = [FIRPhoneAuthProvider providerWithAuth:_mockAuth];
- // Simulate missing app token error.
- OCMExpect([_mockNotificationManager checkNotificationForwardingWithCallback:OCMOCK_ANY])
- .andCallBlock1(^(FIRAuthNotificationForwardingCallback callback) {
- callback(YES);
- });
- OCMExpect([_mockAppCredentialManager credential]).andReturn(nil);
- OCMExpect([_mockAPNSTokenManager getTokenWithCallback:OCMOCK_ANY])
- .andCallBlock1(^(FIRAuthAPNSTokenCallback callback) {
- NSError *error = [NSError errorWithDomain:FIRAuthErrorDomain
- code:FIRAuthErrorCodeMissingAppToken
- userInfo:nil];
- callback(nil, error);
- });
- OCMExpect([_mockBackend getProjectConfig:[OCMArg any] callback:[OCMArg any]])
- .andCallBlock2(
- ^(FIRGetProjectConfigRequest *request, FIRGetProjectConfigResponseCallback callback) {
- XCTAssertNotNil(request);
- dispatch_async(FIRAuthGlobalWorkQueue(), ^() {
- id mockGetProjectConfigResponse = OCMClassMock([FIRGetProjectConfigResponse class]);
- OCMStub([mockGetProjectConfigResponse authorizedDomains]).andReturn(@[
- kFakeAuthorizedDomain
- ]);
- callback(mockGetProjectConfigResponse, nil);
- });
- });
- id mockUIDelegate = OCMProtocolMock(@protocol(FIRAuthUIDelegate));
- // Expect view controller presentation by UIDelegate.
- OCMExpect([_mockURLPresenter presentURL:OCMOCK_ANY
- UIDelegate:mockUIDelegate
- callbackMatcher:OCMOCK_ANY
- completion:OCMOCK_ANY])
- .andDo(^(NSInvocation *invocation) {
- __unsafe_unretained id unretainedArgument;
- // Indices 0 and 1 indicate the hidden arguments self and _cmd.
- // `completion` is at index 5
- [invocation getArgument:&unretainedArgument atIndex:5];
- FIRAuthURLPresentationCompletion completion = unretainedArgument;
- dispatch_async(FIRAuthGlobalWorkQueue(), ^() {
- completion(nil, [NSError errorWithDomain:FIRAuthErrorDomain
- code:FIRAuthErrorCodeMissingAppToken
- userInfo:nil]);
- });
- });
- XCTestExpectation *expectation = [self expectationWithDescription:@"callback"];
- [_provider verifyPhoneNumber:kTestPhoneNumber
- UIDelegate:mockUIDelegate
- completion:^(NSString *_Nullable verificationID, NSError *_Nullable error) {
- XCTAssertTrue([NSThread isMainThread]);
- XCTAssertEqual(error.code, FIRAuthErrorCodeMissingAppToken);
- XCTAssertNil(verificationID);
- [expectation fulfill];
- }];
- [self waitForExpectationsWithTimeout:kExpectationTimeout handler:nil];
- OCMVerifyAll(_mockBackend);
- OCMVerifyAll(_mockNotificationManager);
- }
- /** @fn testVerifyClient
- @brief Tests verifying client before sending verification code.
- */
- - (void)testVerifyClient {
- id mockBundle = OCMClassMock([NSBundle class]);
- OCMStub(ClassMethod([mockBundle mainBundle])).andReturn(mockBundle);
- OCMStub([mockBundle objectForInfoDictionaryKey:@"CFBundleURLTypes"]).andReturn(@[
- @{@"CFBundleURLSchemes" : @[ kFakeReverseClientID ]}
- ]);
- OCMStub([mockBundle bundleIdentifier]).andReturn(kFakeBundleID);
- OCMStub([_mockOptions clientID]).andReturn(kFakeClientID);
- _provider = [FIRPhoneAuthProvider providerWithAuth:_mockAuth];
- OCMExpect([_mockNotificationManager checkNotificationForwardingWithCallback:OCMOCK_ANY])
- .andCallBlock1(^(FIRAuthNotificationForwardingCallback callback) {
- callback(YES);
- });
- OCMExpect([_mockAppCredentialManager credential]).andReturn(nil);
- NSData *data = [@"!@#$%^" dataUsingEncoding:NSUTF8StringEncoding];
- FIRAuthAPNSToken *token = [[FIRAuthAPNSToken alloc] initWithData:data
- type:FIRAuthAPNSTokenTypeProd];
- OCMExpect([_mockAPNSTokenManager getTokenWithCallback:OCMOCK_ANY])
- .andCallBlock1(^(FIRAuthAPNSTokenCallback callback) {
- callback(token, nil);
- });
- // Expect verify client request to the backend.
- OCMExpect([_mockBackend verifyClient:[OCMArg any] callback:[OCMArg any]])
- .andCallBlock2(^(FIRVerifyClientRequest *request, FIRVerifyClientResponseCallback callback) {
- XCTAssertEqualObjects(request.appToken, @"21402324255E");
- XCTAssertFalse(request.isSandbox);
- dispatch_async(FIRAuthGlobalWorkQueue(), ^() {
- id mockVerifyClientResponse = OCMClassMock([FIRVerifyClientResponse class]);
- OCMStub([mockVerifyClientResponse receipt]).andReturn(kTestReceipt);
- OCMStub([mockVerifyClientResponse suggestedTimeOutDate])
- .andReturn([NSDate dateWithTimeIntervalSinceNow:kTestTimeout]);
- callback(mockVerifyClientResponse, nil);
- });
- });
- // Mock receiving of push notification.
- OCMExpect([_mockAppCredentialManager didStartVerificationWithReceipt:OCMOCK_ANY
- timeout:0
- callback:OCMOCK_ANY])
- .ignoringNonObjectArgs()
- .andCallIdDoubleIdBlock(^(NSString *receipt, NSTimeInterval timeout,
- FIRAuthAppCredentialCallback callback) {
- XCTAssertEqualObjects(receipt, kTestReceipt);
- // Unfortunately 'ignoringNonObjectArgs' means the real value for 'timeout' doesn't get
- // passed into the block either, so we can't verify it here.
- dispatch_async(FIRAuthGlobalWorkQueue(), ^() {
- callback([[FIRAuthAppCredential alloc] initWithReceipt:kTestReceipt secret:kTestSecret]);
- });
- });
- // Expect send verification code request to the backend.
- OCMExpect([_mockBackend sendVerificationCode:[OCMArg any] callback:[OCMArg any]])
- .andCallBlock2(^(FIRSendVerificationCodeRequest *request,
- FIRSendVerificationCodeResponseCallback callback) {
- XCTAssertEqualObjects(request.phoneNumber, kTestPhoneNumber);
- XCTAssertEqualObjects(request.appCredential.receipt, kTestReceipt);
- XCTAssertEqualObjects(request.appCredential.secret, kTestSecret);
- dispatch_async(FIRAuthGlobalWorkQueue(), ^() {
- id mockSendVerificationCodeResponse =
- OCMClassMock([FIRSendVerificationCodeResponse class]);
- OCMStub([mockSendVerificationCodeResponse verificationID]).andReturn(kTestVerificationID);
- callback(mockSendVerificationCodeResponse, nil);
- });
- });
- XCTestExpectation *expectation = [self expectationWithDescription:@"callback"];
- [_provider verifyPhoneNumber:kTestPhoneNumber
- UIDelegate:nil
- completion:^(NSString *_Nullable verificationID, NSError *_Nullable error) {
- XCTAssertTrue([NSThread isMainThread]);
- XCTAssertNil(error);
- XCTAssertEqualObjects(verificationID, kTestVerificationID);
- [expectation fulfill];
- }];
- [self waitForExpectationsWithTimeout:kExpectationTimeout handler:nil];
- OCMVerifyAll(_mockBackend);
- OCMVerifyAll(_mockNotificationManager);
- OCMVerifyAll(_mockAppCredentialManager);
- OCMVerifyAll(_mockAPNSTokenManager);
- }
- /** @fn testSendVerificationCodeFailedRetry
- @brief Tests failed retry after failing to send verification code.
- */
- - (void)testSendVerificationCodeFailedRetry {
- id mockBundle = OCMClassMock([NSBundle class]);
- OCMStub(ClassMethod([mockBundle mainBundle])).andReturn(mockBundle);
- OCMStub([mockBundle objectForInfoDictionaryKey:@"CFBundleURLTypes"]).andReturn(@[
- @{@"CFBundleURLSchemes" : @[ kFakeReverseClientID ]}
- ]);
- OCMStub([mockBundle bundleIdentifier]).andReturn(kFakeBundleID);
- OCMStub([_mockOptions clientID]).andReturn(kFakeClientID);
- _provider = [FIRPhoneAuthProvider providerWithAuth:_mockAuth];
- OCMExpect([_mockNotificationManager checkNotificationForwardingWithCallback:OCMOCK_ANY])
- .andCallBlock1(^(FIRAuthNotificationForwardingCallback callback) {
- callback(YES);
- });
- // Expect twice due to null check consumes one expectation.
- OCMExpect([_mockAppCredentialManager credential])
- .andReturn([[FIRAuthAppCredential alloc] initWithReceipt:kTestOldReceipt
- secret:kTestOldSecret]);
- OCMExpect([_mockAppCredentialManager credential])
- .andReturn([[FIRAuthAppCredential alloc] initWithReceipt:kTestOldReceipt
- secret:kTestOldSecret]);
- NSData *data = [@"!@#$%^" dataUsingEncoding:NSUTF8StringEncoding];
- FIRAuthAPNSToken *token = [[FIRAuthAPNSToken alloc] initWithData:data
- type:FIRAuthAPNSTokenTypeProd];
- // Expect first sendVerificationCode request to the backend, with request containing old app
- // credential.
- OCMExpect([_mockBackend sendVerificationCode:[OCMArg any] callback:[OCMArg any]])
- .andCallBlock2(^(FIRSendVerificationCodeRequest *request,
- FIRSendVerificationCodeResponseCallback callback) {
- XCTAssertEqualObjects(request.phoneNumber, kTestPhoneNumber);
- XCTAssertEqualObjects(request.appCredential.receipt, kTestOldReceipt);
- XCTAssertEqualObjects(request.appCredential.secret, kTestOldSecret);
- dispatch_async(FIRAuthGlobalWorkQueue(), ^() {
- callback(nil, [FIRAuthErrorUtils invalidAppCredentialWithMessage:nil]);
- });
- });
- // Expect send verification code request to the backend, with request containing new app
- // credential data.
- OCMExpect([_mockBackend sendVerificationCode:[OCMArg any] callback:[OCMArg any]])
- .andCallBlock2(^(FIRSendVerificationCodeRequest *request,
- FIRSendVerificationCodeResponseCallback callback) {
- XCTAssertEqualObjects(request.phoneNumber, kTestPhoneNumber);
- XCTAssertEqualObjects(request.appCredential.receipt, kTestReceipt);
- XCTAssertEqualObjects(request.appCredential.secret, kTestSecret);
- dispatch_async(FIRAuthGlobalWorkQueue(), ^() {
- callback(nil, [FIRAuthErrorUtils invalidAppCredentialWithMessage:nil]);
- });
- });
- OCMExpect([_mockAPNSTokenManager getTokenWithCallback:OCMOCK_ANY])
- .andCallBlock1(^(FIRAuthAPNSTokenCallback callback) {
- callback(token, nil);
- });
- // Expect verify client request to the backend.
- OCMExpect([_mockBackend verifyClient:[OCMArg any] callback:[OCMArg any]])
- .andCallBlock2(^(FIRVerifyClientRequest *request, FIRVerifyClientResponseCallback callback) {
- XCTAssertEqualObjects(request.appToken, @"21402324255E");
- XCTAssertFalse(request.isSandbox);
- dispatch_async(FIRAuthGlobalWorkQueue(), ^() {
- id mockVerifyClientResponse = OCMClassMock([FIRVerifyClientResponse class]);
- OCMStub([mockVerifyClientResponse receipt]).andReturn(kTestReceipt);
- OCMStub([mockVerifyClientResponse suggestedTimeOutDate])
- .andReturn([NSDate dateWithTimeIntervalSinceNow:kTestTimeout]);
- callback(mockVerifyClientResponse, nil);
- });
- });
- // Mock receiving of push notification.
- OCMStub([_mockAppCredentialManager didStartVerificationWithReceipt:OCMOCK_ANY
- timeout:0
- callback:OCMOCK_ANY])
- .ignoringNonObjectArgs()
- .andCallIdDoubleIdBlock(^(NSString *receipt, NSTimeInterval timeout,
- FIRAuthAppCredentialCallback callback) {
- XCTAssertEqualObjects(receipt, kTestReceipt);
- // Unfortunately 'ignoringNonObjectArgs' means the real value for 'timeout' doesn't get
- // passed into the block either, so we can't verify it here.
- dispatch_async(FIRAuthGlobalWorkQueue(), ^() {
- callback([[FIRAuthAppCredential alloc] initWithReceipt:kTestReceipt secret:kTestSecret]);
- });
- });
- XCTestExpectation *expectation = [self expectationWithDescription:@"callback"];
- [_provider verifyPhoneNumber:kTestPhoneNumber
- UIDelegate:nil
- completion:^(NSString *_Nullable verificationID, NSError *_Nullable error) {
- XCTAssertTrue([NSThread isMainThread]);
- XCTAssertNil(verificationID);
- XCTAssertEqual(error.code, FIRAuthErrorCodeInternalError);
- [expectation fulfill];
- }];
- [self waitForExpectationsWithTimeout:kExpectationTimeout handler:nil];
- OCMVerifyAll(_mockBackend);
- OCMVerifyAll(_mockNotificationManager);
- OCMVerifyAll(_mockAppCredentialManager);
- OCMVerifyAll(_mockAPNSTokenManager);
- }
- /** @fn testSendVerificationCodeSuccessFulRetry
- @brief Tests successful retry after failing to send verification code.
- */
- - (void)testSendVerificationCodeSuccessFulRetry {
- id mockBundle = OCMClassMock([NSBundle class]);
- OCMStub(ClassMethod([mockBundle mainBundle])).andReturn(mockBundle);
- OCMStub([mockBundle objectForInfoDictionaryKey:@"CFBundleURLTypes"]).andReturn(@[
- @{@"CFBundleURLSchemes" : @[ kFakeReverseClientID ]}
- ]);
- OCMStub([mockBundle bundleIdentifier]).andReturn(kFakeBundleID);
- OCMStub([_mockOptions clientID]).andReturn(kFakeClientID);
- _provider = [FIRPhoneAuthProvider providerWithAuth:_mockAuth];
- OCMExpect([_mockNotificationManager checkNotificationForwardingWithCallback:OCMOCK_ANY])
- .andCallBlock1(^(FIRAuthNotificationForwardingCallback callback) {
- callback(YES);
- });
- // Expect twice due to null check consumes one expectation.
- OCMExpect([_mockAppCredentialManager credential])
- .andReturn([[FIRAuthAppCredential alloc] initWithReceipt:kTestOldReceipt
- secret:kTestOldSecret]);
- OCMExpect([_mockAppCredentialManager credential])
- .andReturn([[FIRAuthAppCredential alloc] initWithReceipt:kTestOldReceipt
- secret:kTestOldSecret]);
- NSData *data = [@"!@#$%^" dataUsingEncoding:NSUTF8StringEncoding];
- FIRAuthAPNSToken *token = [[FIRAuthAPNSToken alloc] initWithData:data
- type:FIRAuthAPNSTokenTypeProd];
- // Expect first sendVerificationCode request to the backend, with request containing old app
- // credential.
- OCMExpect([_mockBackend sendVerificationCode:[OCMArg any] callback:[OCMArg any]])
- .andCallBlock2(^(FIRSendVerificationCodeRequest *request,
- FIRSendVerificationCodeResponseCallback callback) {
- XCTAssertEqualObjects(request.phoneNumber, kTestPhoneNumber);
- XCTAssertEqualObjects(request.appCredential.receipt, kTestOldReceipt);
- XCTAssertEqualObjects(request.appCredential.secret, kTestOldSecret);
- dispatch_async(FIRAuthGlobalWorkQueue(), ^() {
- callback(nil, [FIRAuthErrorUtils invalidAppCredentialWithMessage:nil]);
- });
- });
- // Expect send verification code request to the backend, with request containing new app
- // credential data.
- OCMExpect([_mockBackend sendVerificationCode:[OCMArg any] callback:[OCMArg any]])
- .andCallBlock2(^(FIRSendVerificationCodeRequest *request,
- FIRSendVerificationCodeResponseCallback callback) {
- XCTAssertEqualObjects(request.phoneNumber, kTestPhoneNumber);
- XCTAssertEqualObjects(request.appCredential.receipt, kTestReceipt);
- XCTAssertEqualObjects(request.appCredential.secret, kTestSecret);
- dispatch_async(FIRAuthGlobalWorkQueue(), ^() {
- id mockSendVerificationCodeResponse =
- OCMClassMock([FIRSendVerificationCodeResponse class]);
- OCMStub([mockSendVerificationCodeResponse verificationID]).andReturn(kTestVerificationID);
- callback(mockSendVerificationCodeResponse, nil);
- });
- });
- OCMExpect([_mockAPNSTokenManager getTokenWithCallback:OCMOCK_ANY])
- .andCallBlock1(^(FIRAuthAPNSTokenCallback callback) {
- callback(token, nil);
- });
- // Expect verify client request to the backend.
- OCMExpect([_mockBackend verifyClient:[OCMArg any] callback:[OCMArg any]])
- .andCallBlock2(^(FIRVerifyClientRequest *request, FIRVerifyClientResponseCallback callback) {
- XCTAssertEqualObjects(request.appToken, @"21402324255E");
- XCTAssertFalse(request.isSandbox);
- dispatch_async(FIRAuthGlobalWorkQueue(), ^() {
- id mockVerifyClientResponse = OCMClassMock([FIRVerifyClientResponse class]);
- OCMStub([mockVerifyClientResponse receipt]).andReturn(kTestReceipt);
- OCMStub([mockVerifyClientResponse suggestedTimeOutDate])
- .andReturn([NSDate dateWithTimeIntervalSinceNow:kTestTimeout]);
- callback(mockVerifyClientResponse, nil);
- });
- });
- // Mock receiving of push notification.
- OCMStub([_mockAppCredentialManager didStartVerificationWithReceipt:OCMOCK_ANY
- timeout:0
- callback:OCMOCK_ANY])
- .ignoringNonObjectArgs()
- .andCallIdDoubleIdBlock(^(NSString *receipt, NSTimeInterval timeout,
- FIRAuthAppCredentialCallback callback) {
- XCTAssertEqualObjects(receipt, kTestReceipt);
- // Unfortunately 'ignoringNonObjectArgs' means the real value for 'timeout' doesn't get
- // passed into the block either, so we can't verify it here.
- dispatch_async(FIRAuthGlobalWorkQueue(), ^() {
- callback([[FIRAuthAppCredential alloc] initWithReceipt:kTestReceipt secret:kTestSecret]);
- });
- });
- XCTestExpectation *expectation = [self expectationWithDescription:@"callback"];
- [_provider verifyPhoneNumber:kTestPhoneNumber
- UIDelegate:nil
- completion:^(NSString *_Nullable verificationID, NSError *_Nullable error) {
- XCTAssertNil(error);
- XCTAssertEqualObjects(verificationID, kTestVerificationID);
- [expectation fulfill];
- }];
- [self waitForExpectationsWithTimeout:kExpectationTimeout handler:nil];
- OCMVerifyAll(_mockBackend);
- OCMVerifyAll(_mockNotificationManager);
- OCMVerifyAll(_mockAppCredentialManager);
- OCMVerifyAll(_mockAPNSTokenManager);
- }
- #pragma clang diagnostic pop // ignored "-Wdeprecated-declarations"
- @end
- NS_ASSUME_NONNULL_END
- #endif
|