ActionCodeURLTests.swift 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // Copyright 2025 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. @testable import FirebaseAuth
  15. import Foundation
  16. import XCTest
  17. /// Unit tests for ActionCodeURL
  18. class ActionCodeURLTests: XCTestCase {
  19. /// Tests parsing a valid URL with resetPassword mode.
  20. func testParseURL() {
  21. let urlString = "https://www.example.com?apiKey=API_KEY&mode=resetPassword&oobCode=OOB_CODE"
  22. let actionCodeURL = ActionCodeURL(link: urlString)
  23. XCTAssertNotNil(actionCodeURL)
  24. XCTAssertEqual(actionCodeURL?.apiKey, "API_KEY")
  25. XCTAssertEqual(actionCodeURL?.operation, .passwordReset)
  26. XCTAssertEqual(actionCodeURL?.code, "OOB_CODE")
  27. }
  28. /// Tests parsing an invalid URL.
  29. func testParseInvalidURL() {
  30. let urlString = "invalid_url"
  31. let actionCodeURL = ActionCodeURL(link: urlString)
  32. XCTAssertNil(actionCodeURL)
  33. }
  34. /// Tests parsing a URL with missing parameters.
  35. func testParseURLMissingParameters() {
  36. let urlString = "https://www.example.com"
  37. let actionCodeURL = ActionCodeURL(link: urlString)
  38. XCTAssertNil(actionCodeURL)
  39. }
  40. // Tests parsing a URL with an operation and a code.
  41. func testParseURLDifferentMode() {
  42. let urlString = "https://www.example.com?apiKey=API_KEY&mode=verifyEmail&oobCode=OOB_CODE"
  43. let actionCodeURL = ActionCodeURL(link: urlString)
  44. XCTAssertNotNil(actionCodeURL)
  45. XCTAssertEqual(actionCodeURL?.apiKey, "API_KEY")
  46. XCTAssertEqual(actionCodeURL?.operation, .verifyEmail)
  47. XCTAssertEqual(actionCodeURL?.code, "OOB_CODE")
  48. }
  49. /// Tests parsing a URL with all properties.
  50. func testParseURLWithAllProperties() {
  51. let urlString =
  52. "https://www.example.com?apiKey=API_KEY&mode=recoverEmail&oobCode=OOB_CODE&continueUrl=https://www.continue.com&lang=en"
  53. let actionCodeURL = ActionCodeURL(link: urlString)
  54. XCTAssertNotNil(actionCodeURL)
  55. XCTAssertEqual(actionCodeURL?.apiKey, "API_KEY")
  56. XCTAssertEqual(actionCodeURL?.operation, .recoverEmail)
  57. XCTAssertEqual(actionCodeURL?.code, "OOB_CODE")
  58. XCTAssertEqual(actionCodeURL?.continueURL?.absoluteString, "https://www.continue.com")
  59. XCTAssertEqual(actionCodeURL?.languageCode, "en")
  60. }
  61. /// Tests parsing a URL with missing oobCode.
  62. func testParseURLMissingOobCode() {
  63. let urlString = "https://www.example.com?apiKey=API_KEY&mode=resetPassword"
  64. let actionCodeURL = ActionCodeURL(link: urlString)
  65. XCTAssertNil(actionCodeURL?.code)
  66. }
  67. /// Tests parsing a URL with invalid mode.
  68. func testParseURLInvalidMode() {
  69. let urlString = "https://www.example.com?apiKey=API_KEY&mode=invalidMode&oobCode=OOB_CODE"
  70. let actionCodeURL = ActionCodeURL(link: urlString)
  71. XCTAssertEqual(actionCodeURL?.operation, .unknown)
  72. }
  73. /// Tests parsing a URL with language code.
  74. func testActionCodeURL_languageCode() {
  75. let urlString = "https://example.com?lang=fr"
  76. let actionCodeURL = ActionCodeURL(link: urlString)
  77. XCTAssertEqual(actionCodeURL?.languageCode, "fr")
  78. }
  79. }