FinalizePasskeySignInRequestTests.swift 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. #if os(iOS) || os(tvOS) || os(macOS)
  15. @testable import FirebaseAuth
  16. import FirebaseCore
  17. import XCTest
  18. @available(iOS 15.0, macOS 12.0, tvOS 16.0, *)
  19. class FinalizePasskeySignInRequestTests: XCTestCase {
  20. private var request: FinalizePasskeySignInRequest!
  21. private var fakeConfig: AuthRequestConfiguration!
  22. // Fake values
  23. private let kCredentialID = "FAKE_CREDENTIAL_ID"
  24. private let kClientDataJSON = "FAKE_CLIENT_DATA"
  25. private let kAuthenticatorData = "FAKE_AUTHENTICATOR_DATA"
  26. private let kSignature = "FAKE_SIGNATURE"
  27. private let kUserId = "FAKE_USERID"
  28. override func setUp() {
  29. super.setUp()
  30. fakeConfig = AuthRequestConfiguration(
  31. apiKey: "FAKE_API_KEY",
  32. appID: "FAKE_APP_ID"
  33. )
  34. }
  35. override func tearDown() {
  36. request = nil
  37. fakeConfig = nil
  38. super.tearDown()
  39. }
  40. func testInitWithValidParameters() {
  41. request = FinalizePasskeySignInRequest(
  42. credentialID: kCredentialID,
  43. clientDataJSON: kClientDataJSON,
  44. authenticatorData: kAuthenticatorData,
  45. signature: kSignature,
  46. userId: kUserId,
  47. requestConfiguration: fakeConfig
  48. )
  49. XCTAssertEqual(request.credentialID, kCredentialID)
  50. XCTAssertEqual(request.clientDataJSON, kClientDataJSON)
  51. XCTAssertEqual(request.authenticatorData, kAuthenticatorData)
  52. XCTAssertEqual(request.signature, kSignature)
  53. XCTAssertEqual(request.userId, kUserId)
  54. XCTAssertEqual(request.endpoint, "accounts/passkeySignIn:finalize")
  55. XCTAssertTrue(request.useIdentityPlatform)
  56. }
  57. func testUnencodedHTTPRequestBodyWithoutTenantId() {
  58. request = FinalizePasskeySignInRequest(
  59. credentialID: kCredentialID,
  60. clientDataJSON: kClientDataJSON,
  61. authenticatorData: kAuthenticatorData,
  62. signature: kSignature,
  63. userId: kUserId,
  64. requestConfiguration: fakeConfig
  65. )
  66. guard let postBody = request.unencodedHTTPRequestBody else {
  67. return XCTFail("Body should not be nil")
  68. }
  69. guard let authnAssertionResp =
  70. postBody["authenticatorAuthenticationResponse"] as? [String: AnyHashable] else {
  71. return XCTFail("Missing authenticatorAuthenticationResponse")
  72. }
  73. XCTAssertEqual(authnAssertionResp["id"] as? String, kCredentialID)
  74. guard let response = authnAssertionResp["response"] as? [String: AnyHashable] else {
  75. return XCTFail("Missing nested response dictionary")
  76. }
  77. XCTAssertEqual(response["clientDataJSON"] as? String, kClientDataJSON)
  78. XCTAssertEqual(response["authenticatorData"] as? String, kAuthenticatorData)
  79. XCTAssertEqual(response["signature"] as? String, kSignature)
  80. XCTAssertEqual(response["userHandle"] as? String, kUserId)
  81. XCTAssertNil(postBody["tenantId"]) // no tenant by default
  82. }
  83. func testUnencodedHTTPRequestBodyWithTenantId() {
  84. let options = FirebaseOptions(
  85. googleAppID: "0:0000000000000:ios:0000000000000000",
  86. gcmSenderID: "00000000000000000-00000000000-000000000"
  87. )
  88. options.apiKey = "FAKE_API_KEY"
  89. options.projectID = "myProjectID"
  90. let app = FirebaseApp(instanceWithName: "testApp", options: options)
  91. let auth = Auth(app: app)
  92. auth.tenantID = "TEST_TENANT"
  93. let configWithTenant = AuthRequestConfiguration(
  94. apiKey: "FAKE_API_KEY",
  95. appID: "FAKE_APP_ID",
  96. auth: auth
  97. )
  98. request = FinalizePasskeySignInRequest(
  99. credentialID: kCredentialID,
  100. clientDataJSON: kClientDataJSON,
  101. authenticatorData: kAuthenticatorData,
  102. signature: kSignature,
  103. userId: kUserId,
  104. requestConfiguration: configWithTenant
  105. )
  106. guard let body = request.unencodedHTTPRequestBody else {
  107. return XCTFail("Body should not be nil")
  108. }
  109. XCTAssertEqual(body["tenantId"] as? String, "TEST_TENANT")
  110. // also checking structure remains same with tenant
  111. guard let top = body["authenticatorAuthenticationResponse"] as? [String: AnyHashable] else {
  112. return XCTFail("Missing authenticatorAuthenticationResponse")
  113. }
  114. XCTAssertEqual(top["id"] as? String, kCredentialID)
  115. guard let response = top["response"] as? [String: AnyHashable] else {
  116. return XCTFail("Missing nested response dictionary")
  117. }
  118. XCTAssertEqual(response["clientDataJSON"] as? String, kClientDataJSON)
  119. XCTAssertEqual(response["authenticatorData"] as? String, kAuthenticatorData)
  120. XCTAssertEqual(response["signature"] as? String, kSignature)
  121. XCTAssertEqual(response["userHandle"] as? String, kUserId)
  122. }
  123. }
  124. #endif