Skip to content

Commit 422597e

Browse files
committed
feat(firebase_auth, emulator): implement useEmulutor
Works in unit testing and when configured for e2e testing, except for the expected test failures on disabled users, OOB and SMS codes as the test suite needs a port to work against an empty auth emulator vs the pre-configured data in the cloud Prior art doing the auth tests is noted, but needs a port to javascript
1 parent 3e7bf73 commit 422597e

File tree

19 files changed

+166
-0
lines changed

19 files changed

+166
-0
lines changed

packages/firebase_auth/firebase_auth/android/src/main/java/io/flutter/plugins/firebase/auth/Constants.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,6 @@ public class Constants {
7777
public static final String HANDLE_CODE_IN_APP = "handleCodeInApp";
7878
public static final String ACTION_CODE_SETTINGS = "actionCodeSettings";
7979
public static final String AUTO_RETRIEVED_SMS_CODE_FOR_TESTING = "autoRetrievedSmsCodeForTesting";
80+
public static final String HOST = "host";
81+
public static final String PORT = "port";
8082
}

packages/firebase_auth/firebase_auth/android/src/main/java/io/flutter/plugins/firebase/auth/FlutterFirebaseAuthPlugin.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -764,6 +764,18 @@ private Task<Void> signOut(Map<String, Object> arguments) {
764764
});
765765
}
766766

767+
private Task<Void> useEmulator(Map<String, Object> arguments) {
768+
return Tasks.call(
769+
cachedThreadPool,
770+
() -> {
771+
FirebaseAuth firebaseAuth = getAuth(arguments);
772+
String host = (String) arguments.get(Constants.HOST);
773+
int port = (int) arguments.get(Constants.PORT);
774+
firebaseAuth.useEmulator(host, port);
775+
return null;
776+
});
777+
}
778+
767779
private Task<Map<String, Object>> verifyPasswordResetCode(Map<String, Object> arguments) {
768780
return Tasks.call(
769781
cachedThreadPool,
@@ -1196,6 +1208,9 @@ public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
11961208
case "Auth#signOut":
11971209
methodCallTask = signOut(call.arguments());
11981210
break;
1211+
case "Auth#useEmulator":
1212+
methodCallTask = useEmulator(call.arguments());
1213+
break;
11991214
case "Auth#verifyPasswordResetCode":
12001215
methodCallTask = verifyPasswordResetCode(call.arguments());
12011216
break;

packages/firebase_auth/firebase_auth/example/android/app/src/main/AndroidManifest.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
additional functionality it is fine to subclass or reimplement
1010
FlutterApplication and put your custom class here. -->
1111
<application
12+
android:usesCleartextTraffic="true"
1213
android:name="io.flutter.app.FlutterApplication"
1314
android:label="firebaseauthexample"
1415
android:icon="@mipmap/ic_launcher">

packages/firebase_auth/firebase_auth/example/ios/Runner/Info.plist

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@
4141
<string>$(FLUTTER_BUILD_NUMBER)</string>
4242
<key>LSRequiresIPhoneOS</key>
4343
<true/>
44+
<key>NSAppTransportSecurity</key>
45+
<dict>
46+
<key>NSAllowsArbitraryLoads</key>
47+
<true/>
48+
</dict>
4449
<key>UILaunchStoryboardName</key>
4550
<string>LaunchScreen</string>
4651
<key>UIMainStoryboardFile</key>

packages/firebase_auth/firebase_auth/example/lib/main.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5+
//import 'package:firebase_auth/firebase_auth.dart'; // Only needed if you configure the Auth Emulator below
56
import 'package:firebase_core/firebase_core.dart';
67
import 'package:flutter/material.dart';
78
import 'package:flutter_signin_button/button_builder.dart';
@@ -12,6 +13,8 @@ import './signin_page.dart';
1213
Future<void> main() async {
1314
WidgetsFlutterBinding.ensureInitialized();
1415
await Firebase.initializeApp();
16+
// Uncomment this to use the auth emulator for testing
17+
// await FirebaseAuth.instance.useEmulator('http://localhost:9099');
1518
runApp(AuthExampleApp());
1619
}
1720

packages/firebase_auth/firebase_auth/example/pubspec.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ dependency_overrides:
2121
path: ../../../firebase_core/firebase_core
2222
firebase_dynamic_links:
2323
path: ../../../firebase_dynamic_links
24+
firebase_auth_platform_interface:
25+
path: ../../firebase_auth_platform_interface
2426

2527
dev_dependencies:
2628
pedantic: ^1.8.0

packages/firebase_auth/firebase_auth/example/test_driver/firebase_auth_e2e.dart

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
// BSD-style license that can be found in the LICENSE file.
66

77
import 'package:drive/drive.dart' as drive;
8+
//import 'package:firebase_auth/firebase_auth.dart'; // only needed if you use the Auth Emulator
89
import 'package:firebase_core/firebase_core.dart';
910
import 'package:flutter_test/flutter_test.dart';
1011

@@ -17,6 +18,13 @@ bool USE_EMULATOR = false;
1718
void testsMain() {
1819
setUpAll(() async {
1920
await Firebase.initializeApp();
21+
22+
// Configure the Auth test suite to use the Auth Emulator
23+
// This may not be enabled until the test suite is ported to:
24+
// - have ability to create disabled users
25+
// - have ability to fetch OOB and SMS verification codes
26+
// JS implementation to port to dart here: https://github.com/invertase/react-native-firebase/pull/4552/commits/4c688413cb6516ecfdbd4ea325103d0d8d8d84a8#diff-44ccd5fb03b0d9e447820032866f2494c5a400a52873f0f65518d06aedafe302
27+
// await FirebaseAuth.instance.useEmulator('http://localhost:9099');
2028
});
2129

2230
runInstanceTests();

packages/firebase_auth/firebase_auth/example/web/index.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
};
2323
// Initialize Firebase
2424
firebase.initializeApp(firebaseConfig);
25+
26+
// Configure web auth for emulator
27+
//firebase.auth().useEmulator('http://localhost:9099');
2528
</script>
2629
<script src="main.dart.js" type="application/javascript"></script>
2730
</body>

packages/firebase_auth/firebase_auth/ios/Classes/FLTFirebaseAuthPlugin.m

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,8 @@ - (void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)flutter
212212
[self signInWithEmailLink:call.arguments withMethodCallResult:methodCallResult];
213213
} else if ([@"Auth#signOut" isEqualToString:call.method]) {
214214
[self signOut:call.arguments withMethodCallResult:methodCallResult];
215+
} else if ([@"Auth#useEmulator" isEqualToString:call.method]) {
216+
[self useEmulator:call.arguments withMethodCallResult:methodCallResult];
215217
} else if ([@"Auth#verifyPasswordResetCode" isEqualToString:call.method]) {
216218
[self verifyPasswordResetCode:call.arguments withMethodCallResult:methodCallResult];
217219
} else if ([@"Auth#verifyPhoneNumber" isEqualToString:call.method]) {
@@ -550,6 +552,12 @@ - (void)signOut:(id)arguments withMethodCallResult:(FLTFirebaseMethodCallResult
550552
}
551553
}
552554

555+
- (void)useEmulator:(id)arguments withMethodCallResult:(FLTFirebaseMethodCallResult *)result {
556+
FIRAuth *auth = [self getFIRAuthFromArguments:arguments];
557+
[auth useEmulatorWithHost:arguments[@"host"] port:[arguments[@"port"] integerValue]];
558+
result.success(nil);
559+
}
560+
553561
- (void)verifyPasswordResetCode:(id)arguments
554562
withMethodCallResult:(FLTFirebaseMethodCallResult *)result {
555563
FIRAuth *auth = [self getFIRAuthFromArguments:arguments];

packages/firebase_auth/firebase_auth/lib/firebase_auth.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import 'dart:async';
99
import 'package:firebase_core_platform_interface/firebase_core_platform_interface.dart';
1010
import 'package:firebase_auth_platform_interface/firebase_auth_platform_interface.dart';
1111
import 'package:firebase_core/firebase_core.dart';
12+
import 'package:flutter/foundation.dart';
1213
import 'package:flutter/material.dart';
1314
import 'package:meta/meta.dart';
1415

0 commit comments

Comments
 (0)