CreateAuthURITests.swift 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. rpcIssuer?.respondBlock = {
  67. try self.rpcIssuer?.respond(withJSON: [kAuthUriKey: kTestAuthUri])
  68. }
  69. let rpcResponse = try await AuthBackend.call(with: makeAuthURIRequest())
  70. XCTAssertEqual(rpcResponse.authURI, kTestAuthUri)
  71. }
  72. func testRequestAndResponseEncoding() async throws {
  73. let kTestExpectedKind = "identitytoolkit#CreateAuthUriResponse"
  74. let kTestProviderID1 = "google.com"
  75. let kTestProviderID2 = "facebook.com"
  76. rpcIssuer?.respondBlock = {
  77. try self.rpcIssuer?
  78. .respond(withJSON: ["kind": kTestExpectedKind,
  79. "allProviders": [kTestProviderID1, kTestProviderID2]])
  80. }
  81. let rpcResponse = try await AuthBackend.call(with: makeAuthURIRequest())
  82. XCTAssertEqual(rpcIssuer?.requestURL?.absoluteString, kExpectedAPIURL)
  83. XCTAssertEqual(rpcIssuer?.decodedRequest?["identifier"] as? String, kTestIdentifier)
  84. XCTAssertEqual(rpcIssuer?.decodedRequest?["continueUri"] as? String, kTestContinueURI)
  85. XCTAssertEqual(rpcResponse.allProviders?.count, 2)
  86. XCTAssertEqual(rpcResponse.allProviders?.first, kTestProviderID1)
  87. XCTAssertEqual(rpcResponse.allProviders?[1], kTestProviderID2)
  88. }
  89. private func makeAuthURIRequest() -> CreateAuthURIRequest {
  90. return CreateAuthURIRequest(identifier: kTestIdentifier,
  91. continueURI: kTestContinueURI,
  92. requestConfiguration: makeRequestConfiguration())
  93. }
  94. }