|
| 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 | + |
| 15 | +// |
| 16 | +// MFAEnrollmentUnitTests.swift |
| 17 | +// FirebaseAuthSwiftUITests |
| 18 | +// |
| 19 | +// Unit tests for MFA enrollment data structures |
| 20 | +// |
| 21 | + |
| 22 | +import FirebaseAuth |
| 23 | +import FirebaseAuthSwiftUI |
| 24 | +import Foundation |
| 25 | +import Testing |
| 26 | + |
| 27 | +// MARK: - TOTPEnrollmentInfo Tests |
| 28 | + |
| 29 | +@Suite("TOTPEnrollmentInfo Tests") |
| 30 | +struct TOTPEnrollmentInfoTests { |
| 31 | + @Test("Initialization with shared secret key") |
| 32 | + func testInitializationWithSharedSecretKey() { |
| 33 | + let validSecrets = [ |
| 34 | + "JBSWY3DPEHPK3PXP", |
| 35 | + "GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQ", |
| 36 | + "MFRGG43FMZQW4ZY=", |
| 37 | + ] |
| 38 | + |
| 39 | + for secret in validSecrets { |
| 40 | + let totpInfo = TOTPEnrollmentInfo(sharedSecretKey: secret) |
| 41 | + #expect(totpInfo.sharedSecretKey == secret) |
| 42 | + #expect(totpInfo.verificationStatus == .pending) |
| 43 | + #expect(totpInfo.qrCodeURL == nil) |
| 44 | + #expect(totpInfo.accountName == nil) |
| 45 | + #expect(totpInfo.issuer == nil) |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + @Test("Initialization with all parameters") |
| 50 | + func testInitializationWithAllParameters() throws { |
| 51 | + let totpInfo = TOTPEnrollmentInfo( |
| 52 | + sharedSecretKey: "JBSWY3DPEHPK3PXP", |
| 53 | + qrCodeURL: URL( |
| 54 | + string : "otpauth://totp/Example:[email protected]?secret=JBSWY3DPEHPK3PXP&issuer=Example" |
| 55 | + ), |
| 56 | + |
| 57 | + issuer: "Example", |
| 58 | + verificationStatus: .verified |
| 59 | + ) |
| 60 | + |
| 61 | + #expect(totpInfo.sharedSecretKey == "JBSWY3DPEHPK3PXP") |
| 62 | + #expect (totpInfo .accountName == "[email protected]") |
| 63 | + #expect(totpInfo.issuer == "Example") |
| 64 | + #expect(totpInfo.verificationStatus == .verified) |
| 65 | + |
| 66 | + let qrURL = try #require(totpInfo.qrCodeURL) |
| 67 | + #expect(qrURL.scheme == "otpauth") |
| 68 | + #expect(qrURL.host == "totp") |
| 69 | + #expect(qrURL.query?.contains("secret=JBSWY3DPEHPK3PXP") == true) |
| 70 | + #expect(qrURL.query?.contains("issuer=Example") == true) |
| 71 | + } |
| 72 | + |
| 73 | + @Test("Verification status transitions") |
| 74 | + func testVerificationStatusTransitions() { |
| 75 | + // Default status is pending |
| 76 | + var totpInfo = TOTPEnrollmentInfo(sharedSecretKey: "JBSWY3DPEHPK3PXP") |
| 77 | + #expect(totpInfo.verificationStatus == .pending) |
| 78 | + |
| 79 | + // Verified status |
| 80 | + totpInfo = TOTPEnrollmentInfo( |
| 81 | + sharedSecretKey: "JBSWY3DPEHPK3PXP", |
| 82 | + verificationStatus: .verified |
| 83 | + ) |
| 84 | + #expect(totpInfo.verificationStatus == .verified) |
| 85 | + |
| 86 | + // Failed status |
| 87 | + totpInfo = TOTPEnrollmentInfo( |
| 88 | + sharedSecretKey: "JBSWY3DPEHPK3PXP", |
| 89 | + verificationStatus: .failed |
| 90 | + ) |
| 91 | + #expect(totpInfo.verificationStatus == .failed) |
| 92 | + } |
| 93 | +} |
0 commit comments