Skip to content

Commit a657fe1

Browse files
test: createUser/sign in with email/password
1 parent fe5d04e commit a657fe1

File tree

2 files changed

+111
-3
lines changed

2 files changed

+111
-3
lines changed

samples/swiftui/FirebaseSwiftUIExample/FirebaseSwiftUIExampleTests/FirebaseSwiftUIExampleTests.swift

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,61 @@
44
//
55
// Created by Russell Wheatley on 18/02/2025.
66
//
7-
7+
import FirebaseAuth
8+
import FirebaseAuthSwiftUI
9+
import FirebaseCore
810
@testable import FirebaseSwiftUIExample
911
import Testing
1012

13+
let kEmailAddress = "[email protected]"
14+
let kPassword = "123456"
15+
1116
struct FirebaseSwiftUIExampleTests {
12-
@Test func example() async throws {
13-
// Write your test here and use APIs like `#expect(...)` to check expected conditions.
17+
@MainActor
18+
func prepareFreshAuthService() async throws -> AuthService {
19+
configureFirebaseIfNeeded()
20+
try await clearAuthEmulatorState()
21+
22+
let auth = Auth.auth()
23+
return AuthService(auth: auth)
24+
}
25+
26+
@Test
27+
@MainActor
28+
func testCreateEmailPasswordUser() async throws {
29+
let service = try await prepareFreshAuthService()
30+
31+
#expect(service.authenticationState == .unauthenticated)
32+
#expect(service.authView == .authPicker)
33+
#expect(service.errorMessage.isEmpty)
34+
#expect(service.signedInCredential == nil)
35+
try await service.createUser(withEmail: kEmailAddress, password: kPassword)
36+
37+
#expect(service.authenticationState == .authenticated)
38+
#expect(service.authView == .authPicker)
39+
#expect(service.errorMessage.isEmpty)
40+
// TODO: - reinstate once this PR is merged: https://github.com/firebase/FirebaseUI-iOS/pull/1256
41+
// #expect(service.signedInCredential is AuthCredential)
42+
}
43+
44+
@Test
45+
@MainActor
46+
func testSignInUser() async throws {
47+
let service = try await prepareFreshAuthService()
48+
// TODO: - it clears emulator data but seems like it is still there?
49+
// try await service.createUser(withEmail: kEmailAddress, password: kPassword)
50+
// try await service.signOut()
51+
// #expect(service.authenticationState == .unauthenticated)
52+
// #expect(service.authView == .authPicker)
53+
// #expect(service.errorMessage.isEmpty)
54+
// #expect(service.signedInCredential == nil)
55+
56+
try await service.signIn(withEmail: kEmailAddress, password: kPassword)
57+
58+
#expect(service.authenticationState == .authenticated)
59+
#expect(service.authView == .authPicker)
60+
#expect(service.errorMessage.isEmpty)
61+
// TODO: - reinstate once this PR is merged: https://github.com/firebase/FirebaseUI-iOS/pull/1256
62+
// #expect(service.signedInCredential is AuthCredential)
1463
}
1564
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
//
2+
// TestHarness.swift
3+
// FirebaseSwiftUIExample
4+
//
5+
// Created by Russell Wheatley on 16/05/2025.
6+
//
7+
8+
import FirebaseAuth
9+
import FirebaseCore
10+
11+
/// Call this at the beginning of any integration test to ensure Firebase is configured and pointed
12+
/// to the Auth Emulator.
13+
@MainActor
14+
func configureFirebaseIfNeeded() {
15+
if FirebaseApp.app() == nil {
16+
FirebaseApp.configure()
17+
}
18+
Auth.auth().useEmulator(withHost: "localhost", port: 9099)
19+
}
20+
21+
private var hasCheckedEmulatorAvailability = false
22+
23+
@MainActor
24+
func isEmulatorRunning() async throws {
25+
if hasCheckedEmulatorAvailability { return }
26+
let healthCheckURL = URL(string: "http://localhost:9099/")!
27+
var healthRequest = URLRequest(url: healthCheckURL)
28+
healthRequest.httpMethod = "HEAD"
29+
30+
let session = URLSession(configuration: .ephemeral)
31+
let (_, response) = try await session.data(for: healthRequest)
32+
33+
guard let httpResponse = response as? HTTPURLResponse, httpResponse.statusCode == 200 else {
34+
throw NSError(
35+
domain: "FirebaseAuthSwiftUITests",
36+
code: 1,
37+
userInfo: [
38+
NSLocalizedDescriptionKey: """
39+
🔌 Firebase Auth Emulator is not running on localhost:9099.
40+
Please run: `firebase emulators:start --only auth`
41+
""",
42+
]
43+
)
44+
}
45+
hasCheckedEmulatorAvailability = true
46+
}
47+
48+
@MainActor
49+
func clearAuthEmulatorState(projectID: String = "flutterfire-e2e-tests") async throws {
50+
try await isEmulatorRunning()
51+
let url = URL(string: "http://localhost:9099/emulator/v1/projects/\(projectID)/accounts")!
52+
var request = URLRequest(url: url)
53+
request.httpMethod = "DELETE"
54+
let (data, response) = try await URLSession.shared.data(for: request)
55+
56+
if let httpResponse = response as? HTTPURLResponse {
57+
print("🔥 clearAuthEmulatorState: status = \(httpResponse.statusCode)")
58+
}
59+
}

0 commit comments

Comments
 (0)