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 all commits
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
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
43 changes: 30 additions & 13 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:async';

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

Expand All @@ -17,21 +19,36 @@ void main() {
);
});

test('useEmulator() sets the apiHost to the emulator', () {
final app = FirebaseAdminApp.initializeApp(
'dart-firebase-admin',
Credential.fromApplicationDefaultCredentials(),
);
test(
'useEmulator() uses environment variables to set apiHost to the emulator',
() async {
const firebaseAuthEmulatorHost = '127.0.0.1:9000';
const firestoreEmulatorHost = '127.0.0.1:8000';
final testEnv = <String, String>{
'FIREBASE_AUTH_EMULATOR_HOST': firebaseAuthEmulatorHost,
'FIRESTORE_EMULATOR_HOST': firestoreEmulatorHost,
};

app.useEmulator();
await runZoned(
zoneValues: {envSymbol: testEnv},
() async {
final app = FirebaseAdminApp.initializeApp(
'dart-firebase-admin',
Credential.fromApplicationDefaultCredentials(),
);

expect(
app.authApiHost,
Uri.http('127.0.0.1:9099', 'identitytoolkit.googleapis.com/'),
);
expect(
app.firestoreApiHost,
Uri.http('127.0.0.1:8080', '/'),
app.useEmulator();

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