CreateAuthURITests.swift 3.9 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 CreateAuthURITests: RPCBaseTests {
  19. /** @var kContinueURITestKey
  20. @brief The key for the "continueUri" value in the request.
  21. */
  22. let kContinueURITestKey = "continueUri"
  23. /** @var kTestContinueURI
  24. @brief Fake Continue URI key used for testing.
  25. */
  26. let kTestContinueURI = "ContinueUri"
  27. /** @var kExpectedAPIURL
  28. @brief The expected URL for the test calls.
  29. */
  30. let kExpectedAPIURL =
  31. "https://www.googleapis.com/identitytoolkit/v3/relyingparty/createAuthUri?key=APIKey"
  32. func testCreateAuthUriRequest() async throws {
  33. try await checkRequest(
  34. request: makeAuthURIRequest(),
  35. expected: kExpectedAPIURL,
  36. key: kContinueURITestKey,
  37. value: kTestContinueURI
  38. )
  39. }
  40. func testCreateAuthUriErrors() async throws {
  41. let kMissingContinueURIErrorMessage = "MISSING_CONTINUE_URI:"
  42. let kInvalidIdentifierErrorMessage = "INVALID_IDENTIFIER"
  43. let kInvalidEmailErrorMessage = "INVALID_EMAIL"
  44. try await checkBackendError(
  45. request: makeAuthURIRequest(),
  46. message: kMissingContinueURIErrorMessage,
  47. errorCode: AuthErrorCode.missingContinueURI
  48. )
  49. try await checkBackendError(
  50. request: makeAuthURIRequest(),
  51. message: kInvalidIdentifierErrorMessage,
  52. errorCode: AuthErrorCode.invalidEmail
  53. )
  54. try await checkBackendError(
  55. request: makeAuthURIRequest(),
  56. message: kInvalidEmailErrorMessage,
  57. errorCode: AuthErrorCode.invalidEmail
  58. )
  59. }
  60. /** @fn testSuccessfulCreateAuthURI
  61. @brief This test checks for a successful response
  62. */
  63. func testSuccessfulCreateAuthURIResponse() async throws {
  64. let kAuthUriKey = "authUri"
  65. let kTestAuthUri = "AuthURI"
  66. let rpcIssuer = try XCTUnwrap(self.rpcIssuer)
  67. rpcIssuer.respondBlock = {
  68. try self.rpcIssuer.respond(withJSON: [kAuthUriKey: kTestAuthUri])
  69. }
  70. let rpcResponse = try await authBackend.call(with: makeAuthURIRequest())
  71. XCTAssertEqual(rpcResponse.authURI, kTestAuthUri)
  72. }
  73. func testRequestAndResponseEncoding() async throws {
  74. let kTestExpectedKind = "identitytoolkit#CreateAuthUriResponse"
  75. let kTestProviderID1 = "google.com"
  76. let kTestProviderID2 = "facebook.com"
  77. let rpcIssuer = try XCTUnwrap(self.rpcIssuer)
  78. rpcIssuer.respondBlock = {
  79. try self.rpcIssuer
  80. .respond(withJSON: ["kind": kTestExpectedKind,
  81. "allProviders": [kTestProviderID1, kTestProviderID2]])
  82. }
  83. let rpcResponse = try await authBackend.call(with: makeAuthURIRequest())
  84. XCTAssertEqual(rpcIssuer.requestURL?.absoluteString, kExpectedAPIURL)
  85. XCTAssertEqual(rpcIssuer.decodedRequest?["identifier"] as? String, kTestIdentifier)
  86. XCTAssertEqual(rpcIssuer.decodedRequest?["continueUri"] as? String, kTestContinueURI)
  87. XCTAssertEqual(rpcResponse.allProviders?.count, 2)
  88. XCTAssertEqual(rpcResponse.allProviders?.first, kTestProviderID1)
  89. XCTAssertEqual(rpcResponse.allProviders?[1], kTestProviderID2)
  90. }
  91. private func makeAuthURIRequest() -> CreateAuthURIRequest {
  92. return CreateAuthURIRequest(identifier: kTestIdentifier,
  93. continueURI: kTestContinueURI,
  94. requestConfiguration: makeRequestConfiguration())
  95. }
  96. }