|
| 1 | +// Copyright 2024 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 | + |
| 15 | +import Foundation |
| 16 | +import XCTest |
| 17 | + |
| 18 | +@testable import FirebaseAuth |
| 19 | + |
| 20 | +/** @class StartMFASignInRequestTests |
| 21 | + @brief Tests for @c StartMFASignInRequest |
| 22 | + */ |
| 23 | +@available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *) |
| 24 | +class StartMFASignInRequestTests: RPCBaseTests { |
| 25 | + let kAPIKey = "APIKey" |
| 26 | + let kMfaEnrollmentId = "mfaEnrollmentId" |
| 27 | + let kTOTPEnrollmentInfo = "totpEnrollmentInfo" |
| 28 | + let kPhoneEnrollmentInfo = "enrollmentInfo" |
| 29 | + let kPhoneNumber = "phoneNumber" |
| 30 | + let kReCAPTCHAToken = "recaptchaToken" |
| 31 | + let kCaptchaResponse = "captchaResponse" |
| 32 | + let kRecaptchaVersion = "recaptchaVersion" |
| 33 | + |
| 34 | + /** |
| 35 | + @fn testPhoneStartMFASignInRequest |
| 36 | + @brief Tests the Start MFA Sign In using SMS request. |
| 37 | + */ |
| 38 | + func testPhoneStartMFASignInRequest() async throws { |
| 39 | + let testPendingCredential = "FAKE_PENDING_CREDENTIAL" |
| 40 | + let testEnrollmentID = "FAKE_ENROLLMENT_ID" |
| 41 | + let testPhoneNumber = "1234567890" |
| 42 | + let testRecaptchaToken = "RECAPTCHA_FAKE_TOKEN" |
| 43 | + |
| 44 | + let requestConfiguration = AuthRequestConfiguration(apiKey: kAPIKey, appID: "appID") |
| 45 | + let smsSignInInfo = AuthProtoStartMFAPhoneRequestInfo( |
| 46 | + phoneNumber: testPhoneNumber, |
| 47 | + codeIdentity: CodeIdentity.recaptcha(testRecaptchaToken) |
| 48 | + ) |
| 49 | + |
| 50 | + let request = StartMFASignInRequest( |
| 51 | + MFAPendingCredential: testPendingCredential, |
| 52 | + MFAEnrollmentID: testEnrollmentID, |
| 53 | + signInInfo: smsSignInInfo, |
| 54 | + requestConfiguration: requestConfiguration |
| 55 | + ) |
| 56 | + |
| 57 | + let expectedURL = |
| 58 | + "https://identitytoolkit.googleapis.com/v2/accounts/mfaSignIn:start?key=\(kAPIKey)" |
| 59 | + |
| 60 | + // inject reCAPTCHA response |
| 61 | + let testRecaptchaResponse = "RECAPTCHA_FAKE_RESPONSE" |
| 62 | + let testRecaptchaVersion = "RECAPTCHA_FAKE_ENTERPRISE" |
| 63 | + request.injectRecaptchaFields( |
| 64 | + recaptchaResponse: testRecaptchaResponse, |
| 65 | + recaptchaVersion: testRecaptchaVersion |
| 66 | + ) |
| 67 | + |
| 68 | + do { |
| 69 | + try await checkRequest( |
| 70 | + request: request, |
| 71 | + expected: expectedURL, |
| 72 | + key: kMfaEnrollmentId, |
| 73 | + value: testEnrollmentID |
| 74 | + ) |
| 75 | + } catch { |
| 76 | + // Ignore error from missing users array in fake JSON return. |
| 77 | + return |
| 78 | + } |
| 79 | + |
| 80 | + let requestDictionary = try XCTUnwrap(rpcIssuer.decodedRequest as? [String: AnyHashable]) |
| 81 | + let smsInfo = try XCTUnwrap(requestDictionary["phoneEnrollmentInfo"] as? [String: String]) |
| 82 | + XCTAssertEqual(smsInfo[kPhoneNumber], testPhoneNumber) |
| 83 | + XCTAssertEqual(smsInfo[kReCAPTCHAToken], testRecaptchaToken) |
| 84 | + XCTAssertEqual(smsInfo[kRecaptchaVersion], kRecaptchaVersion) |
| 85 | + XCTAssertEqual(smsInfo[kCaptchaResponse], testRecaptchaResponse) |
| 86 | + |
| 87 | + XCTAssertNil(requestDictionary[kTOTPEnrollmentInfo]) |
| 88 | + } |
| 89 | +} |
0 commit comments