Skip to content

Commit d3d1f7b

Browse files
test: remove duplicate test functions
1 parent 9c2255e commit d3d1f7b

File tree

4 files changed

+79
-270
lines changed

4 files changed

+79
-270
lines changed

samples/swiftui/FirebaseSwiftUIExample/FirebaseSwiftUIExampleUITests/FirebaseSwiftUIExampleUITests.swift

Lines changed: 0 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -27,54 +27,6 @@ final class FirebaseSwiftUIExampleUITests: XCTestCase {
2727
}
2828

2929
override func tearDownWithError() throws {}
30-
31-
/// Creates and configures an XCUIApplication with default test launch arguments
32-
private func createTestApp(mfaEnabled: Bool = false) -> XCUIApplication {
33-
let app = XCUIApplication()
34-
app.launchArguments.append("--test-view-enabled")
35-
if mfaEnabled {
36-
app.launchArguments.append("--mfa-enabled")
37-
}
38-
return app
39-
}
40-
41-
private func dismissAlert(app: XCUIApplication) {
42-
if app.scrollViews.otherElements.buttons["Not Now"].waitForExistence(timeout: 2) {
43-
app.scrollViews.otherElements.buttons["Not Now"].tap()
44-
}
45-
}
46-
47-
/// Helper to create a test user in the emulator via REST API (avoids keychain issues)
48-
private func createTestUser(email: String, password: String) async throws {
49-
// Use Firebase Auth emulator REST API directly to avoid keychain access issues in UI tests
50-
let signUpUrl = "http://127.0.0.1:9099/identitytoolkit.googleapis.com/v1/accounts:signUp?key=fake-api-key"
51-
52-
guard let url = URL(string: signUpUrl) else {
53-
throw NSError(domain: "TestError", code: 1,
54-
userInfo: [NSLocalizedDescriptionKey: "Invalid emulator URL"])
55-
}
56-
57-
var request = URLRequest(url: url)
58-
request.httpMethod = "POST"
59-
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
60-
61-
let body: [String: Any] = [
62-
"email": email,
63-
"password": password,
64-
"returnSecureToken": true
65-
]
66-
67-
request.httpBody = try JSONSerialization.data(withJSONObject: body)
68-
69-
let (data, response) = try await URLSession.shared.data(for: request)
70-
71-
guard let httpResponse = response as? HTTPURLResponse,
72-
httpResponse.statusCode == 200 else {
73-
let errorBody = String(data: data, encoding: .utf8) ?? "Unknown error"
74-
throw NSError(domain: "TestError", code: 2,
75-
userInfo: [NSLocalizedDescriptionKey: "Failed to create user: \(errorBody)"])
76-
}
77-
}
7830

7931
@MainActor
8032
func testExample() throws {

samples/swiftui/FirebaseSwiftUIExample/FirebaseSwiftUIExampleUITests/MFAEnrolmentUITests.swift

Lines changed: 9 additions & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -26,127 +26,6 @@ final class MFAEnrollmentUITests: XCTestCase {
2626
continueAfterFailure = false
2727
}
2828

29-
// MARK: - Helper Methods
30-
31-
/// Creates and configures an XCUIApplication with default test launch arguments
32-
private func createTestApp(mfaEnabled: Bool = true) -> XCUIApplication {
33-
let app = XCUIApplication()
34-
app.launchArguments.append("--test-view-enabled")
35-
if mfaEnabled {
36-
app.launchArguments.append("--mfa-enabled")
37-
}
38-
return app
39-
}
40-
41-
// MARK: - Helper Methods for Test Setup
42-
43-
/// Helper to create a test user in the emulator via REST API (avoids keychain issues)
44-
private func createTestUser(email: String, password: String = "123456", verifyEmail: Bool = false) async throws {
45-
// Use Firebase Auth emulator REST API directly to avoid keychain access issues in UI tests
46-
let signUpUrl = "http://127.0.0.1:9099/identitytoolkit.googleapis.com/v1/accounts:signUp?key=fake-api-key"
47-
48-
guard let url = URL(string: signUpUrl) else {
49-
throw NSError(domain: "TestError", code: 1,
50-
userInfo: [NSLocalizedDescriptionKey: "Invalid emulator URL"])
51-
}
52-
53-
var request = URLRequest(url: url)
54-
request.httpMethod = "POST"
55-
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
56-
57-
let body: [String: Any] = [
58-
"email": email,
59-
"password": password,
60-
"returnSecureToken": true
61-
]
62-
63-
request.httpBody = try JSONSerialization.data(withJSONObject: body)
64-
65-
let (data, response) = try await URLSession.shared.data(for: request)
66-
67-
guard let httpResponse = response as? HTTPURLResponse,
68-
httpResponse.statusCode == 200 else {
69-
let errorBody = String(data: data, encoding: .utf8) ?? "Unknown error"
70-
throw NSError(domain: "TestError", code: 2,
71-
userInfo: [NSLocalizedDescriptionKey: "Failed to create user: \(errorBody)"])
72-
}
73-
74-
// If email verification is requested, verify the email
75-
if verifyEmail {
76-
// Parse the response to get the idToken
77-
if let json = try? JSONSerialization.jsonObject(with: data) as? [String: Any],
78-
let idToken = json["idToken"] as? String {
79-
try await verifyEmailInEmulator(email: email, idToken: idToken)
80-
}
81-
}
82-
}
83-
84-
/// Verifies an email address in the emulator using the OOB code mechanism
85-
private func verifyEmailInEmulator(email: String, idToken: String) async throws {
86-
let base = "http://127.0.0.1:9099"
87-
88-
// Step 1: Trigger email verification (creates OOB code in emulator)
89-
var sendReq = URLRequest(
90-
url: URL(string: "\(base)/identitytoolkit.googleapis.com/v1/accounts:sendOobCode?key=fake-api-key")!
91-
)
92-
sendReq.httpMethod = "POST"
93-
sendReq.setValue("application/json", forHTTPHeaderField: "Content-Type")
94-
sendReq.httpBody = try JSONSerialization.data(withJSONObject: [
95-
"requestType": "VERIFY_EMAIL",
96-
"idToken": idToken
97-
])
98-
99-
let (_, sendResp) = try await URLSession.shared.data(for: sendReq)
100-
guard (sendResp as? HTTPURLResponse)?.statusCode == 200 else {
101-
throw NSError(domain: "TestError", code: 3,
102-
userInfo: [NSLocalizedDescriptionKey: "Failed to send verification email"])
103-
}
104-
105-
// Step 2: Fetch OOB codes from emulator
106-
let oobURL = URL(string: "\(base)/emulator/v1/projects/flutterfire-e2e-tests/oobCodes")!
107-
let (oobData, oobResp) = try await URLSession.shared.data(from: oobURL)
108-
guard (oobResp as? HTTPURLResponse)?.statusCode == 200 else {
109-
throw NSError(domain: "TestError", code: 4,
110-
userInfo: [NSLocalizedDescriptionKey: "Failed to fetch OOB codes"])
111-
}
112-
113-
struct OobEnvelope: Decodable { let oobCodes: [OobItem] }
114-
struct OobItem: Decodable {
115-
let oobCode: String
116-
let email: String
117-
let requestType: String
118-
let creationTime: String?
119-
}
120-
121-
let envelope = try JSONDecoder().decode(OobEnvelope.self, from: oobData)
122-
123-
// Step 3: Find most recent VERIFY_EMAIL code for this email
124-
let iso = ISO8601DateFormatter()
125-
let codeItem = envelope.oobCodes
126-
.filter {
127-
$0.email.caseInsensitiveCompare(email) == .orderedSame && $0.requestType == "VERIFY_EMAIL"
128-
}
129-
.sorted {
130-
let d0 = $0.creationTime.flatMap { iso.date(from: $0) } ?? .distantPast
131-
let d1 = $1.creationTime.flatMap { iso.date(from: $0) } ?? .distantPast
132-
return d0 > d1
133-
}
134-
.first
135-
136-
guard let oobCode = codeItem?.oobCode else {
137-
throw NSError(domain: "TestError", code: 5,
138-
userInfo: [NSLocalizedDescriptionKey: "No VERIFY_EMAIL OOB code found for \(email)"])
139-
}
140-
141-
// Step 4: Apply the OOB code (simulate clicking verification link)
142-
let verifyURL = URL(string: "\(base)/emulator/action?mode=verifyEmail&oobCode=\(oobCode)&apiKey=fake-api-key")!
143-
let (_, verifyResp) = try await URLSession.shared.data(from: verifyURL)
144-
guard (verifyResp as? HTTPURLResponse)?.statusCode == 200 else {
145-
throw NSError(domain: "TestError", code: 6,
146-
userInfo: [NSLocalizedDescriptionKey: "Failed to apply OOB code"])
147-
}
148-
}
149-
15029
// MARK: - MFA Management Navigation Tests
15130

15231
@MainActor
@@ -156,7 +35,7 @@ final class MFAEnrollmentUITests: XCTestCase {
15635
// Create user in test runner before launching app
15736
try await createTestUser(email: email)
15837

159-
let app = createTestApp()
38+
let app = createTestApp(mfaEnabled: true)
16039
app.launch()
16140

16241
// Sign in first to access MFA management
@@ -188,7 +67,7 @@ final class MFAEnrollmentUITests: XCTestCase {
18867
// Create user in test runner before launching app
18968
try await createTestUser(email: email)
19069

191-
let app = createTestApp()
70+
let app = createTestApp(mfaEnabled: true)
19271
app.launch()
19372

19473
// Sign in and navigate to MFA management
@@ -223,7 +102,7 @@ final class MFAEnrollmentUITests: XCTestCase {
223102
// Create user in test runner before launching app
224103
try await createTestUser(email: email)
225104

226-
let app = createTestApp()
105+
let app = createTestApp(mfaEnabled: true)
227106
app.launch()
228107

229108
// Navigate to MFA enrollment
@@ -252,7 +131,7 @@ final class MFAEnrollmentUITests: XCTestCase {
252131
// Create user in test runner before launching app
253132
try await createTestUser(email: email)
254133

255-
let app = createTestApp()
134+
let app = createTestApp(mfaEnabled: true)
256135
app.launch()
257136

258137
// Navigate to MFA enrollment
@@ -289,7 +168,7 @@ final class MFAEnrollmentUITests: XCTestCase {
289168
let email = createEmail()
290169
try await createTestUser(email: email, verifyEmail: true)
291170

292-
let app = createTestApp()
171+
let app = createTestApp(mfaEnabled: true)
293172
app.launch()
294173

295174
// 2) Sign in to reach SignedInView
@@ -388,7 +267,7 @@ final class MFAEnrollmentUITests: XCTestCase {
388267
// Create user in test runner before launching app (with email verification)
389268
try await createTestUser(email: email, verifyEmail: true)
390269

391-
let app = createTestApp()
270+
let app = createTestApp(mfaEnabled: true)
392271
app.launch()
393272

394273
// Navigate to MFA enrollment and select TOTP
@@ -447,7 +326,7 @@ final class MFAEnrollmentUITests: XCTestCase {
447326
// Create user in test runner before launching app
448327
try await createTestUser(email: email)
449328

450-
let app = createTestApp()
329+
let app = createTestApp(mfaEnabled: true)
451330
app.launch()
452331

453332
// Navigate to MFA enrollment
@@ -476,7 +355,7 @@ final class MFAEnrollmentUITests: XCTestCase {
476355
// Create user in test runner before launching app
477356
try await createTestUser(email: email)
478357

479-
let app = createTestApp()
358+
let app = createTestApp(mfaEnabled: true)
480359
app.launch()
481360

482361
// Navigate to MFA enrollment
@@ -506,7 +385,7 @@ final class MFAEnrollmentUITests: XCTestCase {
506385
// Create user in test runner before launching app
507386
try await createTestUser(email: email)
508387

509-
let app = createTestApp()
388+
let app = createTestApp(mfaEnabled: true)
510389
app.launch()
511390

512391
// Sign in and navigate to MFA management

samples/swiftui/FirebaseSwiftUIExample/FirebaseSwiftUIExampleUITests/MFAResolutionUITests.swift

Lines changed: 1 addition & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -21,36 +21,18 @@
2121

2222
import XCTest
2323

24-
func dismissAlert(app: XCUIApplication) {
25-
if app.scrollViews.otherElements.buttons["Not Now"].waitForExistence(timeout: 2) {
26-
app.scrollViews.otherElements.buttons["Not Now"].tap()
27-
}
28-
}
29-
3024
final class MFAResolutionUITests: XCTestCase {
3125
override func setUpWithError() throws {
3226
continueAfterFailure = false
3327
}
3428

35-
// MARK: - Helper Methods
36-
37-
/// Creates and configures an XCUIApplication with default test launch arguments
38-
private func createTestApp(mfaEnabled: Bool = true) -> XCUIApplication {
39-
let app = XCUIApplication()
40-
app.launchArguments.append("--test-view-enabled")
41-
if mfaEnabled {
42-
app.launchArguments.append("--mfa-enabled")
43-
}
44-
return app
45-
}
46-
4729
// MARK: - MFA Resolution UI Tests
4830

4931
// MARK: - Complete MFA Resolution Flow
5032

5133
@MainActor
5234
func testCompleteMFAResolutionFlowWithAPIEnrollment() async throws {
53-
let app = createTestApp()
35+
let app = createTestApp(mfaEnabled: true)
5436
app.launch()
5537

5638
let email = createEmail()
@@ -133,78 +115,6 @@ final class MFAResolutionUITests: XCTestCase {
133115

134116
// MARK: - Helper Methods
135117

136-
private func createEmail() -> String {
137-
let before = UUID().uuidString.prefix(8)
138-
let after = UUID().uuidString.prefix(6)
139-
return "\(before)@\(after).com"
140-
}
141-
142-
/// Verifies an email address in the emulator using the OOB code mechanism
143-
private func verifyEmailInEmulator(email: String, idToken: String) async throws {
144-
let base = "http://127.0.0.1:9099"
145-
146-
// Step 1: Trigger email verification (creates OOB code in emulator)
147-
var sendReq = URLRequest(
148-
url: URL(string: "\(base)/identitytoolkit.googleapis.com/v1/accounts:sendOobCode?key=fake-api-key")!
149-
)
150-
sendReq.httpMethod = "POST"
151-
sendReq.setValue("application/json", forHTTPHeaderField: "Content-Type")
152-
sendReq.httpBody = try JSONSerialization.data(withJSONObject: [
153-
"requestType": "VERIFY_EMAIL",
154-
"idToken": idToken
155-
])
156-
157-
let (_, sendResp) = try await URLSession.shared.data(for: sendReq)
158-
guard (sendResp as? HTTPURLResponse)?.statusCode == 200 else {
159-
throw NSError(domain: "TestError", code: 3,
160-
userInfo: [NSLocalizedDescriptionKey: "Failed to send verification email"])
161-
}
162-
163-
// Step 2: Fetch OOB codes from emulator
164-
let oobURL = URL(string: "\(base)/emulator/v1/projects/flutterfire-e2e-tests/oobCodes")!
165-
let (oobData, oobResp) = try await URLSession.shared.data(from: oobURL)
166-
guard (oobResp as? HTTPURLResponse)?.statusCode == 200 else {
167-
throw NSError(domain: "TestError", code: 4,
168-
userInfo: [NSLocalizedDescriptionKey: "Failed to fetch OOB codes"])
169-
}
170-
171-
struct OobEnvelope: Decodable { let oobCodes: [OobItem] }
172-
struct OobItem: Decodable {
173-
let oobCode: String
174-
let email: String
175-
let requestType: String
176-
let creationTime: String?
177-
}
178-
179-
let envelope = try JSONDecoder().decode(OobEnvelope.self, from: oobData)
180-
181-
// Step 3: Find most recent VERIFY_EMAIL code for this email
182-
let iso = ISO8601DateFormatter()
183-
let codeItem = envelope.oobCodes
184-
.filter {
185-
$0.email.caseInsensitiveCompare(email) == .orderedSame && $0.requestType == "VERIFY_EMAIL"
186-
}
187-
.sorted {
188-
let d0 = $0.creationTime.flatMap { iso.date(from: $0) } ?? .distantPast
189-
let d1 = $1.creationTime.flatMap { iso.date(from: $0) } ?? .distantPast
190-
return d0 > d1
191-
}
192-
.first
193-
194-
guard let oobCode = codeItem?.oobCode else {
195-
throw NSError(domain: "TestError", code: 5,
196-
userInfo: [NSLocalizedDescriptionKey: "No VERIFY_EMAIL OOB code found for \(email)"])
197-
}
198-
199-
// Step 4: Apply the OOB code (simulate clicking verification link)
200-
let verifyURL = URL(string: "\(base)/emulator/action?mode=verifyEmail&oobCode=\(oobCode)&apiKey=fake-api-key")!
201-
let (_, verifyResp) = try await URLSession.shared.data(from: verifyURL)
202-
guard (verifyResp as? HTTPURLResponse)?.statusCode == 200 else {
203-
throw NSError(domain: "TestError", code: 6,
204-
userInfo: [NSLocalizedDescriptionKey: "Failed to apply OOB code"])
205-
}
206-
}
207-
208118
/// Programmatically enables SMS MFA for a user via the Auth emulator REST API
209119
/// - Parameters:
210120
/// - idToken: The user's Firebase ID token

0 commit comments

Comments
 (0)