EmailLinkSignInTests.swift 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 EmailLinkSignInTests: RPCBaseTests {
  19. /** @var kTestEmail
  20. @brief The key for the "email" value in the request.
  21. */
  22. let kTestEmail = "TestEmail@email.com"
  23. /** @var kTestOOBCode
  24. @brief The test value for the "oobCode" in the request.
  25. */
  26. let kTestOOBCode = "TestOOBCode"
  27. /** @var kExpectedAPIURL
  28. @brief The expected URL for the test calls.
  29. */
  30. let kExpectedAPIURL =
  31. "https://www.googleapis.com/identitytoolkit/v3/relyingparty/emailLinkSignin?key=APIKey"
  32. /** @var kEmailKey
  33. @brief The key for the "identifier" value in the request.
  34. */
  35. let kEmailKey = "email"
  36. /** @var kEmailLinkKey
  37. @brief The key for the "oobCode" value in the request.
  38. */
  39. let kOOBCodeKey = "oobCode"
  40. /** @var kIDTokenKey
  41. @brief The key for the "IDToken" value in the request.
  42. */
  43. let kIDTokenKey = "idToken"
  44. /** @fn testEmailLinkRequestCreation
  45. @brief Tests the email link sign-in request with mandatory parameters.
  46. */
  47. func testEmailLinkRequest() async throws {
  48. rpcIssuer?.respondBlock = {
  49. XCTAssertEqual(self.rpcIssuer?.requestURL?.absoluteString, self.kExpectedAPIURL)
  50. guard let requestDictionary = self.rpcIssuer?.decodedRequest as? [AnyHashable: String] else {
  51. XCTFail("decodedRequest is not a dictionary")
  52. return
  53. }
  54. XCTAssertEqual(requestDictionary[self.kEmailKey], self.kTestEmail)
  55. XCTAssertEqual(requestDictionary[self.kOOBCodeKey], self.kTestOOBCode)
  56. XCTAssertNil(requestDictionary[self.kIDTokenKey])
  57. try self.rpcIssuer?.respond(withJSON: [:]) // unblock the await
  58. }
  59. let _ = try await AuthBackend.call(with: makeEmailLinkSignInRequest())
  60. }
  61. /** @fn testEmailLinkRequestCreationOptional
  62. @brief Tests the email link sign-in request with mandatory parameters and optional ID token.
  63. */
  64. func testEmailLinkRequestCreationOptional() async throws {
  65. let kTestIDToken = "testIDToken"
  66. let request = makeEmailLinkSignInRequest()
  67. request.idToken = kTestIDToken
  68. rpcIssuer?.respondBlock = {
  69. XCTAssertEqual(self.rpcIssuer?.requestURL?.absoluteString, self.kExpectedAPIURL)
  70. guard let requestDictionary = self.rpcIssuer?.decodedRequest as? [AnyHashable: String] else {
  71. XCTFail("decodedRequest is not a dictionary")
  72. return
  73. }
  74. XCTAssertEqual(requestDictionary[self.kEmailKey], self.kTestEmail)
  75. XCTAssertEqual(requestDictionary[self.kOOBCodeKey], self.kTestOOBCode)
  76. XCTAssertEqual(requestDictionary[self.kIDTokenKey], kTestIDToken)
  77. try self.rpcIssuer?.respond(withJSON: [:]) // unblock the await
  78. }
  79. let _ = try await AuthBackend.call(with: request)
  80. }
  81. func testEmailLinkSignInErrors() async throws {
  82. let kInvalidEmailErrorMessage = "INVALID_EMAIL"
  83. try await checkBackendError(
  84. request: makeEmailLinkSignInRequest(),
  85. message: kInvalidEmailErrorMessage,
  86. errorCode: AuthErrorCode.invalidEmail
  87. )
  88. }
  89. /** @fn testSuccessfulEmailLinkSignInResponse
  90. @brief Tests a successful email link sign-in response.
  91. */
  92. func testSuccessfulEmailLinkSignInResponse() async throws {
  93. let kTestIDTokenResponse = "fakeToken"
  94. let kTestEmailResponse = "fakeEmail@example.com"
  95. let kTestTokenExpirationTimeInterval: Double = 55 * 60
  96. let kTestRefreshToken = "testRefreshToken"
  97. rpcIssuer?.respondBlock = {
  98. try self.rpcIssuer?.respond(withJSON: ["idToken": kTestIDTokenResponse,
  99. "email": kTestEmailResponse,
  100. "isNewUser": true,
  101. "expiresIn": "\(kTestTokenExpirationTimeInterval)",
  102. "refreshToken": kTestRefreshToken])
  103. }
  104. let response = try await AuthBackend.call(with: makeEmailLinkSignInRequest())
  105. XCTAssertEqual(response.idToken, kTestIDTokenResponse)
  106. XCTAssertEqual(response.email, kTestEmailResponse)
  107. XCTAssertEqual(response.refreshToken, kTestRefreshToken)
  108. XCTAssertTrue(response.isNewUser)
  109. XCTAssertEqual(response.idToken, kTestIDTokenResponse)
  110. let expirationTimeInterval = try XCTUnwrap(response.approximateExpirationDate)
  111. .timeIntervalSinceNow
  112. let testTimeInterval = Date(timeIntervalSinceNow: kTestTokenExpirationTimeInterval)
  113. .timeIntervalSinceNow
  114. let timeIntervalDifference = abs(expirationTimeInterval - testTimeInterval)
  115. XCTAssertLessThan(timeIntervalDifference, 0.001)
  116. }
  117. private func makeEmailLinkSignInRequest() -> EmailLinkSignInRequest {
  118. return EmailLinkSignInRequest(email: kTestEmail,
  119. oobCode: kTestOOBCode,
  120. requestConfiguration: makeRequestConfiguration())
  121. }
  122. }