FIRPhoneAuthProviderTests.m 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  1. /*
  2. * Copyright 2017 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import <XCTest/XCTest.h>
  17. #import "FirebaseCommunity/FIRAuth.h"
  18. #import "FIRPhoneAuthProvider.h"
  19. #import "Phone/FIRPhoneAuthCredential_Internal.h"
  20. #import "Phone/NSString+FIRAuth.h"
  21. #import "FIRAuthAPNSToken.h"
  22. #import "FIRAuthAPNSTokenManager.h"
  23. #import "FIRAuthAppCredential.h"
  24. #import "FIRAuthAppCredentialManager.h"
  25. #import "FIRAuthNotificationManager.h"
  26. #import "FIRAuth_Internal.h"
  27. #import "FIRAuthCredential_Internal.h"
  28. #import "FIRAuthErrorUtils.h"
  29. #import "FIRAuthGlobalWorkQueue.h"
  30. #import "FIRAuthBackend.h"
  31. #import "FIRSendVerificationCodeRequest.h"
  32. #import "FIRSendVerificationCodeResponse.h"
  33. #import "FIRVerifyClientRequest.h"
  34. #import "FIRVerifyClientResponse.h"
  35. #import "FIRApp+FIRAuthUnitTests.h"
  36. #import "OCMStubRecorder+FIRAuthUnitTests.h"
  37. #import <OCMock/OCMock.h>
  38. NS_ASSUME_NONNULL_BEGIN
  39. /** @var kTestPhoneNumber
  40. @brief A testing phone number.
  41. */
  42. static NSString *const kTestPhoneNumber = @"55555555";
  43. /** @var kTestInvalidPhoneNumber
  44. @brief An invalid testing phone number.
  45. */
  46. static NSString *const kTestInvalidPhoneNumber = @"555+!*55555";
  47. /** @var kTestVerificationID
  48. @brief A testing verfication ID.
  49. */
  50. static NSString *const kTestVerificationID = @"verificationID";
  51. /** @var kTestReceipt
  52. @brief A fake receipt for testing.
  53. */
  54. static NSString *const kTestReceipt = @"receipt";
  55. /** @var kTestSecret
  56. @brief A fake secret for testing.
  57. */
  58. static NSString *const kTestSecret = @"secret";
  59. /** @var kTestOldReceipt
  60. @brief A fake old receipt for testing.
  61. */
  62. static NSString *const kTestOldReceipt = @"old_receipt";
  63. /** @var kTestOldSecret
  64. @brief A fake old secret for testing.
  65. */
  66. static NSString *const kTestOldSecret = @"old_secret";
  67. /** @var kTestVerificationCode
  68. @brief A fake verfication code.
  69. */
  70. static NSString *const kTestVerificationCode = @"verificationCode";
  71. /** @var kTestTimeout
  72. @brief A fake timeout value for waiting for push notification.
  73. */
  74. static const NSTimeInterval kTestTimeout = 5;
  75. /** @var kExpectationTimeout
  76. @brief The maximum time waiting for expectations to fulfill.
  77. */
  78. static const NSTimeInterval kExpectationTimeout = 1;
  79. /** @class FIRPhoneAuthProviderTests
  80. @brief Tests for @c FIRPhoneAuthProvider
  81. */
  82. @interface FIRPhoneAuthProviderTests : XCTestCase
  83. @end
  84. @implementation FIRPhoneAuthProviderTests {
  85. /** @var _mockBackend
  86. @brief The mock @c FIRAuthBackendImplementation .
  87. */
  88. id _mockBackend;
  89. /** @var _provider
  90. @brief The @c FIRPhoneAuthProvider instance under test.
  91. */
  92. FIRPhoneAuthProvider *_provider;
  93. /** @var _mockAuth
  94. @brief The mock @c FIRAuth instance associated with @c _provider .
  95. */
  96. id _mockAuth;
  97. /** @var _mockAPNSTokenManager
  98. @brief The mock @c FIRAuthAPNSTokenManager instance associated with @c _mockAuth .
  99. */
  100. id _mockAPNSTokenManager;
  101. /** @var _mockAppCredentialManager
  102. @brief The mock @c FIRAuthAppCredentialManager instance associated with @c _mockAuth .
  103. */
  104. id _mockAppCredentialManager;
  105. /** @var _mockNotificationManager
  106. @brief The mock @c FIRAuthNotificationManager instance associated with @c _mockAuth .
  107. */
  108. id _mockNotificationManager;
  109. }
  110. - (void)setUp {
  111. [super setUp];
  112. _mockBackend = OCMProtocolMock(@protocol(FIRAuthBackendImplementation));
  113. [FIRAuthBackend setBackendImplementation:_mockBackend];
  114. _mockAuth = OCMClassMock([FIRAuth class]);
  115. _mockAPNSTokenManager = OCMClassMock([FIRAuthAPNSTokenManager class]);
  116. OCMStub([_mockAuth tokenManager]).andReturn(_mockAPNSTokenManager);
  117. _mockAppCredentialManager = OCMClassMock([FIRAuthAppCredentialManager class]);
  118. OCMStub([_mockAuth appCredentialManager]).andReturn(_mockAppCredentialManager);
  119. _mockNotificationManager = OCMClassMock([FIRAuthNotificationManager class]);
  120. OCMStub([_mockAuth notificationManager]).andReturn(_mockNotificationManager);
  121. _provider = [FIRPhoneAuthProvider providerWithAuth:_mockAuth];
  122. }
  123. - (void)tearDown {
  124. [FIRAuthBackend setDefaultBackendImplementationWithRPCIssuer:nil];
  125. [super tearDown];
  126. }
  127. /** @fn testCredentialWithVerificationID
  128. @brief Tests the @c credentialWithToken method to make sure that it returns a valid
  129. FIRAuthCredential instance.
  130. */
  131. - (void)testCredentialWithVerificationID {
  132. FIRPhoneAuthCredential *credential =
  133. [_provider credentialWithVerificationID:kTestVerificationID
  134. verificationCode:kTestVerificationCode];
  135. XCTAssertEqualObjects(credential.verificationID, kTestVerificationID);
  136. XCTAssertEqualObjects(credential.verificationCode, kTestVerificationCode);
  137. XCTAssertNil(credential.temporaryProof);
  138. XCTAssertNil(credential.phoneNumber);
  139. }
  140. /** @fn testVerifyEmptyPhoneNumber
  141. @brief Tests a failed invocation @c verifyPhoneNumber:completion: because an empty phone
  142. number was provided.
  143. */
  144. - (void)testVerifyEmptyPhoneNumber {
  145. // Empty phone number is checked on the client side so no backend RPC is mocked.
  146. XCTestExpectation *expectation = [self expectationWithDescription:@"callback"];
  147. [_provider verifyPhoneNumber:@""
  148. completion:^(NSString *_Nullable verificationID, NSError *_Nullable error) {
  149. XCTAssertNotNil(error);
  150. XCTAssertEqual(error.code, FIRAuthErrorCodeMissingPhoneNumber);
  151. [expectation fulfill];
  152. }];
  153. [self waitForExpectationsWithTimeout:kExpectationTimeout handler:nil];
  154. }
  155. /** @fn testVerifyInvalidPhoneNumber
  156. @brief Tests a failed invocation @c verifyPhoneNumber:completion: because an invalid phone
  157. number was provided.
  158. */
  159. - (void)testVerifyInvalidPhoneNumber {
  160. OCMExpect([_mockNotificationManager checkNotificationForwardingWithCallback:OCMOCK_ANY])
  161. .andCallBlock1(^(FIRAuthNotificationForwardingCallback callback) { callback(YES); });
  162. OCMStub([_mockAppCredentialManager credential])
  163. .andReturn([[FIRAuthAppCredential alloc] initWithReceipt:kTestReceipt secret:kTestSecret]);
  164. OCMExpect([_mockBackend sendVerificationCode:[OCMArg any] callback:[OCMArg any]])
  165. .andCallBlock2(^(FIRSendVerificationCodeRequest *request,
  166. FIRSendVerificationCodeResponseCallback callback) {
  167. XCTAssertEqualObjects(request.phoneNumber, kTestPhoneNumber);
  168. XCTAssertEqualObjects(request.appCredential.receipt, kTestReceipt);
  169. XCTAssertEqualObjects(request.appCredential.secret, kTestSecret);
  170. dispatch_async(FIRAuthGlobalWorkQueue(), ^() {
  171. callback(nil, [FIRAuthErrorUtils invalidPhoneNumberErrorWithMessage:nil]);
  172. });
  173. });
  174. XCTestExpectation *expectation = [self expectationWithDescription:@"callback"];
  175. [_provider verifyPhoneNumber:kTestPhoneNumber
  176. completion:^(NSString *_Nullable verificationID, NSError *_Nullable error) {
  177. XCTAssertTrue([NSThread isMainThread]);
  178. XCTAssertNil(verificationID);
  179. XCTAssertEqual(error.code, FIRAuthErrorCodeInvalidPhoneNumber);
  180. [expectation fulfill];
  181. }];
  182. [self waitForExpectationsWithTimeout:kExpectationTimeout handler:nil];
  183. OCMVerifyAll(_mockBackend);
  184. OCMVerifyAll(_mockNotificationManager);
  185. OCMVerifyAll(_mockAppCredentialManager);
  186. }
  187. /** @fn testVerifyPhoneNumber
  188. @brief Tests a successful invocation of @c verifyPhoneNumber:completion:.
  189. */
  190. - (void)testVerifyPhoneNumber {
  191. OCMExpect([_mockNotificationManager checkNotificationForwardingWithCallback:OCMOCK_ANY])
  192. .andCallBlock1(^(FIRAuthNotificationForwardingCallback callback) { callback(YES); });
  193. OCMStub([_mockAppCredentialManager credential])
  194. .andReturn([[FIRAuthAppCredential alloc] initWithReceipt:kTestReceipt secret:kTestSecret]);
  195. OCMExpect([_mockBackend sendVerificationCode:[OCMArg any] callback:[OCMArg any]])
  196. .andCallBlock2(^(FIRSendVerificationCodeRequest *request,
  197. FIRSendVerificationCodeResponseCallback callback) {
  198. XCTAssertEqualObjects(request.phoneNumber, kTestPhoneNumber);
  199. XCTAssertEqualObjects(request.appCredential.receipt, kTestReceipt);
  200. XCTAssertEqualObjects(request.appCredential.secret, kTestSecret);
  201. dispatch_async(FIRAuthGlobalWorkQueue(), ^() {
  202. id mockSendVerificationCodeResponse = OCMClassMock([FIRSendVerificationCodeResponse class]);
  203. OCMStub([mockSendVerificationCodeResponse verificationID]).andReturn(kTestVerificationID);
  204. callback(mockSendVerificationCodeResponse, nil);
  205. });
  206. });
  207. XCTestExpectation *expectation = [self expectationWithDescription:@"callback"];
  208. [_provider verifyPhoneNumber:kTestPhoneNumber
  209. completion:^(NSString *_Nullable verificationID, NSError *_Nullable error) {
  210. XCTAssertTrue([NSThread isMainThread]);
  211. XCTAssertNil(error);
  212. XCTAssertEqualObjects(verificationID, kTestVerificationID);
  213. XCTAssertEqualObjects(verificationID.fir_authPhoneNumber, kTestPhoneNumber);
  214. [expectation fulfill];
  215. }];
  216. [self waitForExpectationsWithTimeout:kExpectationTimeout handler:nil];
  217. OCMVerifyAll(_mockBackend);
  218. OCMVerifyAll(_mockNotificationManager);
  219. OCMVerifyAll(_mockAppCredentialManager);
  220. }
  221. /** @fn testNotForwardingNotification
  222. @brief Tests returning an error for the app failing to forward notification.
  223. */
  224. - (void)testNotForwardingNotification {
  225. OCMExpect([_mockNotificationManager checkNotificationForwardingWithCallback:OCMOCK_ANY])
  226. .andCallBlock1(^(FIRAuthNotificationForwardingCallback callback) { callback(NO); });
  227. XCTestExpectation *expectation = [self expectationWithDescription:@"callback"];
  228. [_provider verifyPhoneNumber:kTestPhoneNumber
  229. completion:^(NSString *_Nullable verificationID, NSError *_Nullable error) {
  230. XCTAssertTrue([NSThread isMainThread]);
  231. XCTAssertNil(verificationID);
  232. XCTAssertEqual(error.code, FIRAuthErrorCodeNotificationNotForwarded);
  233. [expectation fulfill];
  234. }];
  235. [self waitForExpectationsWithTimeout:kExpectationTimeout handler:nil];
  236. OCMVerifyAll(_mockNotificationManager);
  237. }
  238. /** @fn testMissingAPNSToken
  239. @brief Tests returning an error for the app failing to provide an APNS device token.
  240. */
  241. - (void)testMissingAPNSToken {
  242. OCMExpect([_mockNotificationManager checkNotificationForwardingWithCallback:OCMOCK_ANY])
  243. .andCallBlock1(^(FIRAuthNotificationForwardingCallback callback) { callback(YES); });
  244. OCMExpect([_mockAppCredentialManager credential]).andReturn(nil);
  245. OCMExpect([_mockAPNSTokenManager getTokenWithCallback:OCMOCK_ANY])
  246. .andCallBlock1(^(FIRAuthAPNSTokenCallback callback) { callback(nil); });
  247. // Expect verify client request to the backend wth empty token.
  248. OCMExpect([_mockBackend verifyClient:[OCMArg any] callback:[OCMArg any]])
  249. .andCallBlock2(^(FIRVerifyClientRequest *request,
  250. FIRVerifyClientResponseCallback callback) {
  251. XCTAssertNil(request.appToken);
  252. dispatch_async(FIRAuthGlobalWorkQueue(), ^() {
  253. // The backend is supposed to return an error.
  254. callback(nil, [NSError errorWithDomain:FIRAuthErrorDomain
  255. code:FIRAuthErrorCodeMissingAppToken
  256. userInfo:nil]);
  257. });
  258. });
  259. XCTestExpectation *expectation = [self expectationWithDescription:@"callback"];
  260. [_provider verifyPhoneNumber:kTestPhoneNumber
  261. completion:^(NSString *_Nullable verificationID, NSError *_Nullable error) {
  262. XCTAssertTrue([NSThread isMainThread]);
  263. XCTAssertNil(verificationID);
  264. XCTAssertEqualObjects(error.domain, FIRAuthErrorDomain);
  265. XCTAssertEqual(error.code, FIRAuthErrorCodeMissingAppToken);
  266. [expectation fulfill];
  267. }];
  268. [self waitForExpectationsWithTimeout:kExpectationTimeout handler:nil];
  269. OCMVerifyAll(_mockNotificationManager);
  270. OCMVerifyAll(_mockAppCredentialManager);
  271. OCMVerifyAll(_mockAPNSTokenManager);
  272. }
  273. /** @fn testVerifyClient
  274. @brief Tests verifying client before sending verification code.
  275. */
  276. - (void)testVerifyClient {
  277. OCMExpect([_mockNotificationManager checkNotificationForwardingWithCallback:OCMOCK_ANY])
  278. .andCallBlock1(^(FIRAuthNotificationForwardingCallback callback) { callback(YES); });
  279. OCMExpect([_mockAppCredentialManager credential]).andReturn(nil);
  280. NSData *data = [@"!@#$%^" dataUsingEncoding:NSUTF8StringEncoding];
  281. FIRAuthAPNSToken *token = [[FIRAuthAPNSToken alloc] initWithData:data
  282. type:FIRAuthAPNSTokenTypeProd];
  283. OCMExpect([_mockAPNSTokenManager getTokenWithCallback:OCMOCK_ANY])
  284. .andCallBlock1(^(FIRAuthAPNSTokenCallback callback) { callback(token); });
  285. // Expect verify client request to the backend.
  286. OCMExpect([_mockBackend verifyClient:[OCMArg any] callback:[OCMArg any]])
  287. .andCallBlock2(^(FIRVerifyClientRequest *request,
  288. FIRVerifyClientResponseCallback callback) {
  289. XCTAssertEqualObjects(request.appToken, @"21402324255E");
  290. XCTAssertFalse(request.isSandbox);
  291. dispatch_async(FIRAuthGlobalWorkQueue(), ^() {
  292. id mockVerifyClientResponse = OCMClassMock([FIRVerifyClientResponse class]);
  293. OCMStub([mockVerifyClientResponse receipt]).andReturn(kTestReceipt);
  294. OCMStub([mockVerifyClientResponse suggestedTimeOutDate])
  295. .andReturn([NSDate dateWithTimeIntervalSinceNow:kTestTimeout]);
  296. callback(mockVerifyClientResponse, nil);
  297. });
  298. });
  299. // Mock receiving of push notification.
  300. OCMExpect([[_mockAppCredentialManager ignoringNonObjectArgs]
  301. didStartVerificationWithReceipt:OCMOCK_ANY timeout:0 callback:OCMOCK_ANY])
  302. .andCallIdDoubleIdBlock(^(NSString *receipt,
  303. NSTimeInterval timeout,
  304. FIRAuthAppCredentialCallback callback) {
  305. XCTAssertEqualObjects(receipt, kTestReceipt);
  306. // Unfortunately 'ignoringNonObjectArgs' means the real value for 'timeout' doesn't get passed
  307. // into the block either, so we can't verify it here.
  308. dispatch_async(FIRAuthGlobalWorkQueue(), ^() {
  309. callback([[FIRAuthAppCredential alloc] initWithReceipt:kTestReceipt secret:kTestSecret]);
  310. });
  311. });
  312. // Expect send verification code request to the backend.
  313. OCMExpect([_mockBackend sendVerificationCode:[OCMArg any] callback:[OCMArg any]])
  314. .andCallBlock2(^(FIRSendVerificationCodeRequest *request,
  315. FIRSendVerificationCodeResponseCallback callback) {
  316. XCTAssertEqualObjects(request.phoneNumber, kTestPhoneNumber);
  317. XCTAssertEqualObjects(request.appCredential.receipt, kTestReceipt);
  318. XCTAssertEqualObjects(request.appCredential.secret, kTestSecret);
  319. dispatch_async(FIRAuthGlobalWorkQueue(), ^() {
  320. id mockSendVerificationCodeResponse = OCMClassMock([FIRSendVerificationCodeResponse class]);
  321. OCMStub([mockSendVerificationCodeResponse verificationID]).andReturn(kTestVerificationID);
  322. callback(mockSendVerificationCodeResponse, nil);
  323. });
  324. });
  325. XCTestExpectation *expectation = [self expectationWithDescription:@"callback"];
  326. [_provider verifyPhoneNumber:kTestPhoneNumber
  327. completion:^(NSString *_Nullable verificationID, NSError *_Nullable error) {
  328. XCTAssertTrue([NSThread isMainThread]);
  329. XCTAssertNil(error);
  330. XCTAssertEqualObjects(verificationID, kTestVerificationID);
  331. XCTAssertEqualObjects(verificationID.fir_authPhoneNumber, kTestPhoneNumber);
  332. [expectation fulfill];
  333. }];
  334. [self waitForExpectationsWithTimeout:kExpectationTimeout handler:nil];
  335. OCMVerifyAll(_mockBackend);
  336. OCMVerifyAll(_mockNotificationManager);
  337. OCMVerifyAll(_mockAppCredentialManager);
  338. OCMVerifyAll(_mockAPNSTokenManager);
  339. }
  340. /** @fn testSendVerificationCodeFailedRetry
  341. @brief Tests failed retry after failing to send verification code.
  342. */
  343. - (void)testSendVerificationCodeFailedRetry {
  344. OCMExpect([_mockNotificationManager checkNotificationForwardingWithCallback:OCMOCK_ANY])
  345. .andCallBlock1(^(FIRAuthNotificationForwardingCallback callback) { callback(YES); });
  346. // Expect twice due to null check consumes one expectation.
  347. OCMExpect([_mockAppCredentialManager credential])
  348. .andReturn([[FIRAuthAppCredential alloc] initWithReceipt:kTestOldReceipt
  349. secret:kTestOldSecret]);
  350. OCMExpect([_mockAppCredentialManager credential])
  351. .andReturn([[FIRAuthAppCredential alloc] initWithReceipt:kTestOldReceipt
  352. secret:kTestOldSecret]);
  353. NSData *data = [@"!@#$%^" dataUsingEncoding:NSUTF8StringEncoding];
  354. FIRAuthAPNSToken *token = [[FIRAuthAPNSToken alloc] initWithData:data
  355. type:FIRAuthAPNSTokenTypeProd];
  356. // Expect first sendVerificationCode request to the backend, with request containing old app
  357. // credential.
  358. OCMExpect([_mockBackend sendVerificationCode:[OCMArg any] callback:[OCMArg any]])
  359. .andCallBlock2(^(FIRSendVerificationCodeRequest *request,
  360. FIRSendVerificationCodeResponseCallback callback) {
  361. XCTAssertEqualObjects(request.phoneNumber, kTestPhoneNumber);
  362. XCTAssertEqualObjects(request.appCredential.receipt, kTestOldReceipt);
  363. XCTAssertEqualObjects(request.appCredential.secret, kTestOldSecret);
  364. dispatch_async(FIRAuthGlobalWorkQueue(), ^() {
  365. callback(nil, [FIRAuthErrorUtils invalidAppCredentialWithMessage:nil]);
  366. });
  367. });
  368. // Expect send verification code request to the backend, with request containing new app
  369. // credential data.
  370. OCMExpect([_mockBackend sendVerificationCode:[OCMArg any] callback:[OCMArg any]])
  371. .andCallBlock2(^(FIRSendVerificationCodeRequest *request,
  372. FIRSendVerificationCodeResponseCallback callback) {
  373. XCTAssertEqualObjects(request.phoneNumber, kTestPhoneNumber);
  374. XCTAssertEqualObjects(request.appCredential.receipt, kTestReceipt);
  375. XCTAssertEqualObjects(request.appCredential.secret, kTestSecret);
  376. dispatch_async(FIRAuthGlobalWorkQueue(), ^() {
  377. callback(nil, [FIRAuthErrorUtils invalidAppCredentialWithMessage:nil]);
  378. });
  379. });
  380. OCMExpect([_mockAPNSTokenManager getTokenWithCallback:OCMOCK_ANY])
  381. .andCallBlock1(^(FIRAuthAPNSTokenCallback callback) { callback(token); });
  382. // Expect verify client request to the backend.
  383. OCMExpect([_mockBackend verifyClient:[OCMArg any] callback:[OCMArg any]])
  384. .andCallBlock2(^(FIRVerifyClientRequest *request,
  385. FIRVerifyClientResponseCallback callback) {
  386. XCTAssertEqualObjects(request.appToken, @"21402324255E");
  387. XCTAssertFalse(request.isSandbox);
  388. dispatch_async(FIRAuthGlobalWorkQueue(), ^() {
  389. id mockVerifyClientResponse = OCMClassMock([FIRVerifyClientResponse class]);
  390. OCMStub([mockVerifyClientResponse receipt]).andReturn(kTestReceipt);
  391. OCMStub([mockVerifyClientResponse suggestedTimeOutDate])
  392. .andReturn([NSDate dateWithTimeIntervalSinceNow:kTestTimeout]);
  393. callback(mockVerifyClientResponse, nil);
  394. });
  395. });
  396. // Mock receiving of push notification.
  397. OCMStub([[_mockAppCredentialManager ignoringNonObjectArgs]
  398. didStartVerificationWithReceipt:OCMOCK_ANY timeout:0 callback:OCMOCK_ANY])
  399. .andCallIdDoubleIdBlock(^(NSString *receipt,
  400. NSTimeInterval timeout,
  401. FIRAuthAppCredentialCallback callback) {
  402. XCTAssertEqualObjects(receipt, kTestReceipt);
  403. // Unfortunately 'ignoringNonObjectArgs' means the real value for 'timeout' doesn't get passed
  404. // into the block either, so we can't verify it here.
  405. dispatch_async(FIRAuthGlobalWorkQueue(), ^() {
  406. callback([[FIRAuthAppCredential alloc] initWithReceipt:kTestReceipt secret:kTestSecret]);
  407. });
  408. });
  409. XCTestExpectation *expectation = [self expectationWithDescription:@"callback"];
  410. [_provider verifyPhoneNumber:kTestPhoneNumber
  411. completion:^(NSString *_Nullable verificationID, NSError *_Nullable error) {
  412. XCTAssertTrue([NSThread isMainThread]);
  413. XCTAssertNil(verificationID);
  414. XCTAssertEqual(error.code, FIRAuthErrorCodeInternalError);
  415. [expectation fulfill];
  416. }];
  417. [self waitForExpectationsWithTimeout:kExpectationTimeout handler:nil];
  418. OCMVerifyAll(_mockBackend);
  419. OCMVerifyAll(_mockNotificationManager);
  420. OCMVerifyAll(_mockAppCredentialManager);
  421. OCMVerifyAll(_mockAPNSTokenManager);
  422. }
  423. /** @fn testSendVerificationCodeSuccessFulRetry
  424. @brief Tests successful retry after failing to send verification code.
  425. */
  426. - (void)testSendVerificationCodeSuccessFulRetry {
  427. OCMExpect([_mockNotificationManager checkNotificationForwardingWithCallback:OCMOCK_ANY])
  428. .andCallBlock1(^(FIRAuthNotificationForwardingCallback callback) { callback(YES); });
  429. // Expect twice due to null check consumes one expectation.
  430. OCMExpect([_mockAppCredentialManager credential])
  431. .andReturn([[FIRAuthAppCredential alloc] initWithReceipt:kTestOldReceipt
  432. secret:kTestOldSecret]);
  433. OCMExpect([_mockAppCredentialManager credential])
  434. .andReturn([[FIRAuthAppCredential alloc] initWithReceipt:kTestOldReceipt
  435. secret:kTestOldSecret]);
  436. NSData *data = [@"!@#$%^" dataUsingEncoding:NSUTF8StringEncoding];
  437. FIRAuthAPNSToken *token = [[FIRAuthAPNSToken alloc] initWithData:data
  438. type:FIRAuthAPNSTokenTypeProd];
  439. // Expect first sendVerificationCode request to the backend, with request containing old app
  440. // credential.
  441. OCMExpect([_mockBackend sendVerificationCode:[OCMArg any] callback:[OCMArg any]])
  442. .andCallBlock2(^(FIRSendVerificationCodeRequest *request,
  443. FIRSendVerificationCodeResponseCallback callback) {
  444. XCTAssertEqualObjects(request.phoneNumber, kTestPhoneNumber);
  445. XCTAssertEqualObjects(request.appCredential.receipt, kTestOldReceipt);
  446. XCTAssertEqualObjects(request.appCredential.secret, kTestOldSecret);
  447. dispatch_async(FIRAuthGlobalWorkQueue(), ^() {
  448. callback(nil, [FIRAuthErrorUtils invalidAppCredentialWithMessage:nil]);
  449. });
  450. });
  451. // Expect send verification code request to the backend, with request containing new app
  452. // credential data.
  453. OCMExpect([_mockBackend sendVerificationCode:[OCMArg any] callback:[OCMArg any]])
  454. .andCallBlock2(^(FIRSendVerificationCodeRequest *request,
  455. FIRSendVerificationCodeResponseCallback callback) {
  456. XCTAssertEqualObjects(request.phoneNumber, kTestPhoneNumber);
  457. XCTAssertEqualObjects(request.appCredential.receipt, kTestReceipt);
  458. XCTAssertEqualObjects(request.appCredential.secret, kTestSecret);
  459. dispatch_async(FIRAuthGlobalWorkQueue(), ^() {
  460. id mockSendVerificationCodeResponse = OCMClassMock([FIRSendVerificationCodeResponse class]);
  461. OCMStub([mockSendVerificationCodeResponse verificationID]).andReturn(kTestVerificationID);
  462. callback(mockSendVerificationCodeResponse, nil);
  463. });
  464. });
  465. OCMExpect([_mockAPNSTokenManager getTokenWithCallback:OCMOCK_ANY])
  466. .andCallBlock1(^(FIRAuthAPNSTokenCallback callback) { callback(token); });
  467. // Expect verify client request to the backend.
  468. OCMExpect([_mockBackend verifyClient:[OCMArg any] callback:[OCMArg any]])
  469. .andCallBlock2(^(FIRVerifyClientRequest *request,
  470. FIRVerifyClientResponseCallback callback) {
  471. XCTAssertEqualObjects(request.appToken, @"21402324255E");
  472. XCTAssertFalse(request.isSandbox);
  473. dispatch_async(FIRAuthGlobalWorkQueue(), ^() {
  474. id mockVerifyClientResponse = OCMClassMock([FIRVerifyClientResponse class]);
  475. OCMStub([mockVerifyClientResponse receipt]).andReturn(kTestReceipt);
  476. OCMStub([mockVerifyClientResponse suggestedTimeOutDate])
  477. .andReturn([NSDate dateWithTimeIntervalSinceNow:kTestTimeout]);
  478. callback(mockVerifyClientResponse, nil);
  479. });
  480. });
  481. // Mock receiving of push notification.
  482. OCMStub([[_mockAppCredentialManager ignoringNonObjectArgs]
  483. didStartVerificationWithReceipt:OCMOCK_ANY timeout:0 callback:OCMOCK_ANY])
  484. .andCallIdDoubleIdBlock(^(NSString *receipt,
  485. NSTimeInterval timeout,
  486. FIRAuthAppCredentialCallback callback) {
  487. XCTAssertEqualObjects(receipt, kTestReceipt);
  488. // Unfortunately 'ignoringNonObjectArgs' means the real value for 'timeout' doesn't get passed
  489. // into the block either, so we can't verify it here.
  490. dispatch_async(FIRAuthGlobalWorkQueue(), ^() {
  491. callback([[FIRAuthAppCredential alloc] initWithReceipt:kTestReceipt secret:kTestSecret]);
  492. });
  493. });
  494. XCTestExpectation *expectation = [self expectationWithDescription:@"callback"];
  495. [_provider verifyPhoneNumber:kTestPhoneNumber
  496. completion:^(NSString *_Nullable verificationID, NSError *_Nullable error) {
  497. XCTAssertNil(error);
  498. XCTAssertEqualObjects(verificationID, kTestVerificationID);
  499. XCTAssertEqualObjects(verificationID.fir_authPhoneNumber, kTestPhoneNumber);
  500. [expectation fulfill];
  501. }];
  502. [self waitForExpectationsWithTimeout:kExpectationTimeout handler:nil];
  503. OCMVerifyAll(_mockBackend);
  504. OCMVerifyAll(_mockNotificationManager);
  505. OCMVerifyAll(_mockAppCredentialManager);
  506. OCMVerifyAll(_mockAPNSTokenManager);
  507. }
  508. @end
  509. NS_ASSUME_NONNULL_END