VerifyClientTests.swift 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 VerifyClientTests: RPCBaseTests {
  19. private let kAPPTokenKey = "appToken"
  20. private let kFakeAppToken = "kAPPTokenKey"
  21. private let kIsSandboxKey = "isSandbox"
  22. private let kExpectedAPIURL =
  23. "https://www.googleapis.com/identitytoolkit/v3/relyingparty/verifyClient?key=APIKey"
  24. /** @fn testVerifyClientRequest
  25. @brief Tests the VerifyClient request.
  26. */
  27. func testVerifyClientRequest() throws {
  28. let request = makeVerifyClientRequest()
  29. let issuer = try checkRequest(
  30. request: request,
  31. expected: kExpectedAPIURL,
  32. key: kAPPTokenKey,
  33. value: kFakeAppToken
  34. )
  35. let requestDictionary = try XCTUnwrap(issuer.decodedRequest as? [String: AnyHashable])
  36. XCTAssertTrue(try XCTUnwrap(requestDictionary[kIsSandboxKey] as? Bool))
  37. }
  38. func testVerifyClientRequestErrors() throws {
  39. let kMissingAppCredentialErrorMessage = "MISSING_APP_CREDENTIAL"
  40. let kInvalidAppCredentialErrorMessage = "INVALID_APP_CREDENTIAL"
  41. try checkBackendError(
  42. request: makeVerifyClientRequest(),
  43. message: kMissingAppCredentialErrorMessage,
  44. errorCode: AuthErrorCode.missingAppCredential
  45. )
  46. try checkBackendError(
  47. request: makeVerifyClientRequest(),
  48. message: kInvalidAppCredentialErrorMessage,
  49. errorCode: AuthErrorCode.invalidAppCredential
  50. )
  51. }
  52. /** @fn testSuccessfulVerifyClientResponse
  53. @brief Tests a successful attempt of the verify password flow.
  54. */
  55. func testSuccessfulVerifyClientResponse() throws {
  56. let kReceiptKey = "receipt"
  57. let kFakeReceipt = "receipt"
  58. let kSuggestedTimeOutKey = "suggestedTimeout"
  59. let kFakeSuggestedTimeout = "1234"
  60. var callbackInvoked = false
  61. var rpcResponse: VerifyClientResponse?
  62. var rpcError: NSError?
  63. AuthBackend.post(with: makeVerifyClientRequest()) { response, error in
  64. callbackInvoked = true
  65. rpcResponse = response
  66. rpcError = error as? NSError
  67. }
  68. try rpcIssuer?.respond(withJSON: [
  69. kReceiptKey: kFakeReceipt,
  70. kSuggestedTimeOutKey: kFakeSuggestedTimeout,
  71. ])
  72. XCTAssert(callbackInvoked)
  73. XCTAssertNil(rpcError)
  74. XCTAssertEqual(rpcResponse?.receipt, kFakeReceipt)
  75. let timeOut = try XCTUnwrap(rpcResponse?.suggestedTimeOutDate?.timeIntervalSinceNow)
  76. XCTAssertEqual(timeOut, 1234, accuracy: 0.1)
  77. }
  78. private func makeVerifyClientRequest() -> VerifyClientRequest {
  79. return VerifyClientRequest(withAppToken: kFakeAppToken,
  80. isSandbox: true,
  81. requestConfiguration: makeRequestConfiguration())
  82. }
  83. }