From 28a67145f4660f69f0b3ab4cd8ace8d61ed6a6bf Mon Sep 17 00:00:00 2001 From: Dinko Marinac Date: Sun, 13 Oct 2024 15:07:02 +0200 Subject: [PATCH 1/3] Use environment variables for Firebase emulator, if available. --- .../firebase_admin_app_test.sh | 12 ++++++++++++ .../lib/src/app/firebase_admin.dart | 13 +++++++++++-- .../test/firebase_admin_app_test.dart | 19 ++++++++++++++++--- 3 files changed, 39 insertions(+), 5 deletions(-) create mode 100755 packages/dart_firebase_admin/firebase_admin_app_test.sh diff --git a/packages/dart_firebase_admin/firebase_admin_app_test.sh b/packages/dart_firebase_admin/firebase_admin_app_test.sh new file mode 100755 index 0000000..35fa1eb --- /dev/null +++ b/packages/dart_firebase_admin/firebase_admin_app_test.sh @@ -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 \ No newline at end of file diff --git a/packages/dart_firebase_admin/lib/src/app/firebase_admin.dart b/packages/dart_firebase_admin/lib/src/app/firebase_admin.dart index 5913eda..9238723 100644 --- a/packages/dart_firebase_admin/lib/src/app/firebase_admin.dart +++ b/packages/dart_firebase_admin/lib/src/app/firebase_admin.dart @@ -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? ?? Platform.environment; + + 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 diff --git a/packages/dart_firebase_admin/test/firebase_admin_app_test.dart b/packages/dart_firebase_admin/test/firebase_admin_app_test.dart index 132cfc2..768ae08 100644 --- a/packages/dart_firebase_admin/test/firebase_admin_app_test.dart +++ b/packages/dart_firebase_admin/test/firebase_admin_app_test.dart @@ -1,3 +1,5 @@ +import 'dart:io'; + import 'package:dart_firebase_admin/src/app.dart'; import 'package:test/test.dart'; @@ -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(), @@ -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, '/'), ); }); }); From ceae9ebe03ae15755796d93f092995cfc3b95ec8 Mon Sep 17 00:00:00 2001 From: Dinko Marinac Date: Sun, 13 Oct 2024 21:58:23 +0200 Subject: [PATCH 2/3] Use Zone for running emulator test. --- .../firebase_admin_app_test.sh | 12 ----- .../test/firebase_admin_app_test.dart | 48 ++++++++++--------- 2 files changed, 26 insertions(+), 34 deletions(-) delete mode 100755 packages/dart_firebase_admin/firebase_admin_app_test.sh diff --git a/packages/dart_firebase_admin/firebase_admin_app_test.sh b/packages/dart_firebase_admin/firebase_admin_app_test.sh deleted file mode 100755 index 35fa1eb..0000000 --- a/packages/dart_firebase_admin/firebase_admin_app_test.sh +++ /dev/null @@ -1,12 +0,0 @@ -#!/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 \ No newline at end of file diff --git a/packages/dart_firebase_admin/test/firebase_admin_app_test.dart b/packages/dart_firebase_admin/test/firebase_admin_app_test.dart index 768ae08..b4b6cce 100644 --- a/packages/dart_firebase_admin/test/firebase_admin_app_test.dart +++ b/packages/dart_firebase_admin/test/firebase_admin_app_test.dart @@ -1,4 +1,4 @@ -import 'dart:io'; +import 'dart:async'; import 'package:dart_firebase_admin/src/app.dart'; import 'package:test/test.dart'; @@ -21,30 +21,34 @@ void main() { 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']!; + () async { + const firebaseAuthEmulatorHost = '127.0.0.1:9000'; + const firestoreEmulatorHost = '127.0.0.1:8000'; + final testEnv = { + 'FIREBASE_AUTH_EMULATOR_HOST': firebaseAuthEmulatorHost, + 'FIRESTORE_EMULATOR_HOST': firestoreEmulatorHost, + }; - final app = FirebaseAdminApp.initializeApp( - 'dart-firebase-admin', - Credential.fromApplicationDefaultCredentials(), - ); + await runZoned( + () async { + final app = FirebaseAdminApp.initializeApp( + 'dart-firebase-admin', + Credential.fromApplicationDefaultCredentials(), + ); - app.useEmulator(); + app.useEmulator(); - expect( - app.authApiHost, - Uri.http(firebaseAuthEmulatorHost, 'identitytoolkit.googleapis.com/'), - ); - expect( - app.firestoreApiHost, - Uri.http(firestoreEmulatorHost, '/'), + expect( + app.authApiHost, + Uri.http( + firebaseAuthEmulatorHost, 'identitytoolkit.googleapis.com/'), + ); + expect( + app.firestoreApiHost, + Uri.http(firestoreEmulatorHost, '/'), + ); + }, + zoneValues: {envSymbol: testEnv}, ); }); }); From c08c76b1338106ee66a2bf516685eb2c8150eadd Mon Sep 17 00:00:00 2001 From: Dinko Marinac Date: Sun, 13 Oct 2024 22:36:33 +0200 Subject: [PATCH 3/3] Move zoneValues before the callback. --- packages/dart_firebase_admin/test/firebase_admin_app_test.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/dart_firebase_admin/test/firebase_admin_app_test.dart b/packages/dart_firebase_admin/test/firebase_admin_app_test.dart index b4b6cce..6830cc1 100644 --- a/packages/dart_firebase_admin/test/firebase_admin_app_test.dart +++ b/packages/dart_firebase_admin/test/firebase_admin_app_test.dart @@ -30,6 +30,7 @@ void main() { }; await runZoned( + zoneValues: {envSymbol: testEnv}, () async { final app = FirebaseAdminApp.initializeApp( 'dart-firebase-admin', @@ -48,7 +49,6 @@ void main() { Uri.http(firestoreEmulatorHost, '/'), ); }, - zoneValues: {envSymbol: testEnv}, ); }); });