Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use environment variables for Firebase emulator, if available. #55

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions packages/dart_firebase_admin/firebase_admin_app_test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/bash

# Set environment variables
export FIREBASE_AUTH_EMULATOR_HOST="127.0.0.1:9000"
export FIRESTORE_EMULATOR_HOST="127.0.0.1:8000"

# Run the Dart test file
dart test test/firebase_admin_app_test.dart

# Clean up environment variables
unset FIREBASE_AUTH_EMULATOR_HOST
unset FIRESTORE_EMULATOR_HOST
13 changes: 11 additions & 2 deletions packages/dart_firebase_admin/lib/src/app/firebase_admin.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,17 @@ class FirebaseAdminApp {
/// Use the Firebase Emulator Suite to run the app locally.
void useEmulator() {
_isUsingEmulator = true;
authApiHost = Uri.http('127.0.0.1:9099', 'identitytoolkit.googleapis.com/');
firestoreApiHost = Uri.http('127.0.0.1:8080', '/');
final env =
Zone.current[envSymbol] as Map<String, String>? ?? Platform.environment;
dinko7 marked this conversation as resolved.
Show resolved Hide resolved

authApiHost = Uri.http(
env['FIREBASE_AUTH_EMULATOR_HOST'] ?? '127.0.0.1:9099',
'identitytoolkit.googleapis.com/',
);
firestoreApiHost = Uri.http(
env['FIRESTORE_EMULATOR_HOST'] ?? '127.0.0.1:8080',
'/',
);
}

@internal
Expand Down
19 changes: 16 additions & 3 deletions packages/dart_firebase_admin/test/firebase_admin_app_test.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:io';

import 'package:dart_firebase_admin/src/app.dart';
import 'package:test/test.dart';

Expand All @@ -17,7 +19,18 @@ void main() {
);
});

test('useEmulator() sets the apiHost to the emulator', () {
test(
'useEmulator() uses environment variables to set apiHost to the emulator',
() {
assert(Platform.environment['FIREBASE_AUTH_EMULATOR_HOST'] != null,
'FIREBASE_AUTH_EMULATOR_HOST is not set');
assert(Platform.environment['FIRESTORE_EMULATOR_HOST'] != null,
'FIRESTORE_EMULATOR_HOST is not set');
final firebaseAuthEmulatorHost =
Platform.environment['FIREBASE_AUTH_EMULATOR_HOST']!;
final firestoreEmulatorHost =
Platform.environment['FIRESTORE_EMULATOR_HOST']!;

final app = FirebaseAdminApp.initializeApp(
'dart-firebase-admin',
Credential.fromApplicationDefaultCredentials(),
Expand All @@ -27,11 +40,11 @@ void main() {

expect(
app.authApiHost,
Uri.http('127.0.0.1:9099', 'identitytoolkit.googleapis.com/'),
Uri.http(firebaseAuthEmulatorHost, 'identitytoolkit.googleapis.com/'),
);
expect(
app.firestoreApiHost,
Uri.http('127.0.0.1:8080', '/'),
Uri.http(firestoreEmulatorHost, '/'),
);
});
});
Expand Down