SendVerificationCodeTests.swift 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. // Copyright 2023 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. import Foundation
  15. import XCTest
  16. @testable import FirebaseAuth
  17. @available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
  18. class SendVerificationCodeTests: RPCBaseTests {
  19. private let kTestSecret = "secret"
  20. private let kTestReceipt = "receipt"
  21. private let kTestReCAPTCHAToken = "reCAPTCHAToken"
  22. private let kPhoneNumberKey = "phoneNumber"
  23. private let kReceiptKey = "iosReceipt"
  24. private let kSecretKey = "iosSecret"
  25. private let kExpectedAPIURL =
  26. "https://www.googleapis.com/identitytoolkit/v3/relyingparty/sendVerificationCode?key=APIKey"
  27. /** @fn testSendVerificationCodeRequest
  28. @brief Tests the sendVerificationCode request.
  29. */
  30. func testSendVerificationCodeRequest() throws {
  31. let request = makeSendVerificationCodeRequest()
  32. XCTAssertEqual(request.phoneNumber, kTestPhoneNumber)
  33. XCTAssertEqual(request.appCredential?.receipt, kTestReceipt)
  34. XCTAssertEqual(request.appCredential?.secret, kTestSecret)
  35. XCTAssertEqual(request.reCAPTCHAToken, kTestReCAPTCHAToken)
  36. let issuer = try checkRequest(
  37. request: request,
  38. expected: kExpectedAPIURL,
  39. key: kPhoneNumberKey,
  40. value: kTestPhoneNumber
  41. )
  42. let requestDictionary = try XCTUnwrap(issuer.decodedRequest as? [String: AnyHashable])
  43. XCTAssertEqual(requestDictionary["recaptchaToken"], kTestReCAPTCHAToken)
  44. XCTAssertEqual(requestDictionary[kReceiptKey], kTestReceipt)
  45. XCTAssertEqual(requestDictionary[kSecretKey], kTestSecret)
  46. }
  47. func testSendVerificationCodeRequestErrors() throws {
  48. let kInvalidPhoneNumberErrorMessage = "INVALID_PHONE_NUMBER"
  49. let kQuotaExceededErrorMessage = "QUOTA_EXCEEDED"
  50. let kAppNotVerifiedErrorMessage = "APP_NOT_VERIFIED"
  51. let kCaptchaCheckFailedErrorMessage = "CAPTCHA_CHECK_FAILED"
  52. try checkBackendError(
  53. request: makeSendVerificationCodeRequest(),
  54. message: kInvalidPhoneNumberErrorMessage,
  55. errorCode: AuthErrorCode.invalidPhoneNumber
  56. )
  57. try checkBackendError(
  58. request: makeSendVerificationCodeRequest(),
  59. message: kQuotaExceededErrorMessage,
  60. errorCode: AuthErrorCode.quotaExceeded
  61. )
  62. try checkBackendError(
  63. request: makeSendVerificationCodeRequest(),
  64. message: kAppNotVerifiedErrorMessage,
  65. errorCode: AuthErrorCode.appNotVerified
  66. )
  67. try checkBackendError(
  68. request: makeSendVerificationCodeRequest(),
  69. message: kCaptchaCheckFailedErrorMessage,
  70. errorCode: AuthErrorCode.captchaCheckFailed
  71. )
  72. }
  73. /** @fn testSuccessfulSendVerificationCodeResponse
  74. @brief This test simulates a successful verify CustomToken flow.
  75. */
  76. func testSuccessfulSendVerificationCodeResponse() throws {
  77. let kVerificationIDKey = "sessionInfo"
  78. let kFakeVerificationID = "testVerificationID"
  79. var callbackInvoked = false
  80. var rpcResponse: SendVerificationCodeResponse?
  81. var rpcError: NSError?
  82. AuthBackend.post(with: makeSendVerificationCodeRequest()) { response, error in
  83. callbackInvoked = true
  84. rpcResponse = response
  85. rpcError = error as? NSError
  86. }
  87. _ = try rpcIssuer?.respond(withJSON: [kVerificationIDKey: kFakeVerificationID])
  88. XCTAssert(callbackInvoked)
  89. XCTAssertNil(rpcError)
  90. XCTAssertEqual(rpcResponse?.verificationID, kFakeVerificationID)
  91. }
  92. private func makeSendVerificationCodeRequest() -> SendVerificationCodeRequest {
  93. let credential = AuthAppCredential(receipt: kTestReceipt, secret: kTestSecret)
  94. return SendVerificationCodeRequest(phoneNumber: kTestPhoneNumber,
  95. appCredential: credential,
  96. reCAPTCHAToken: kTestReCAPTCHAToken,
  97. requestConfiguration: makeRequestConfiguration())
  98. }
  99. }