FinalizeMFAEnrollmentRequestTests.swift 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. /** @class FinalizeMFAEnrollmentRequestTests
  18. @brief Tests for @c FinalizeMFAEnrollmentRequest
  19. */
  20. @available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
  21. class FinalizeMFAEnrollmentRequestTests: RPCBaseTests {
  22. let kAPIKey = "APIKey"
  23. /**
  24. @fn testTOTPFinalizeMFAEnrollmentRequest
  25. @brief Tests the Finalize MFA Enrollment using TOTP request.
  26. */
  27. func testTOTPStartMFAEnrollmentRequest() async throws {
  28. try await assertTOTPStartMFAEnrollmentRequest(displayName: "sparky")
  29. }
  30. func testTOTPStartMFAEnrollmentRequest_WhenDisplayNameIsNil() async throws {
  31. try await assertTOTPStartMFAEnrollmentRequest(displayName: nil)
  32. }
  33. func assertTOTPStartMFAEnrollmentRequest(displayName: String?) async throws {
  34. let kIDToken = "idToken"
  35. let kDisplayName = "displayName"
  36. let kSessionInfo = "sessionInfo"
  37. let kVerificationCode = "code"
  38. let kTOTPVerificationInfo = "totpVerificationInfo"
  39. let kPhoneVerificationInfo = "phoneVerificationInfo"
  40. let requestConfiguration = AuthRequestConfiguration(apiKey: kAPIKey, appID: "appID")
  41. let requestInfo = AuthProtoFinalizeMFATOTPEnrollmentRequestInfo(sessionInfo: kSessionInfo,
  42. verificationCode: kVerificationCode)
  43. let request = FinalizeMFAEnrollmentRequest(idToken: kIDToken,
  44. displayName: displayName,
  45. totpVerificationInfo: requestInfo,
  46. requestConfiguration: requestConfiguration)
  47. let expectedURL =
  48. "https://identitytoolkit.googleapis.com/v2/accounts/mfaEnrollment:finalize?key=" +
  49. "\(kAPIKey)"
  50. try await checkRequest(
  51. request: request,
  52. expected: expectedURL,
  53. key: kIDToken,
  54. value: kIDToken
  55. )
  56. let requestDictionary = try XCTUnwrap(rpcIssuer.decodedRequest as? [String: AnyHashable])
  57. XCTAssertEqual(requestDictionary[kDisplayName], displayName)
  58. let totpInfo = try XCTUnwrap(requestDictionary[kTOTPVerificationInfo] as? [String: String])
  59. XCTAssertEqual(totpInfo["verificationCode"], kVerificationCode)
  60. XCTAssertNil(requestDictionary[kPhoneVerificationInfo])
  61. }
  62. }