Skip to content

Commit

Permalink
feat(firebase_auth): use named arguments for ActionCodeSettings (fire…
Browse files Browse the repository at this point in the history
…base#3269)

* use named arguments

* update web conversion
  • Loading branch information
Ehesp authored Aug 26, 2020
1 parent 4a05ceb commit 6942dbe
Show file tree
Hide file tree
Showing 5 changed files with 210 additions and 124 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -226,14 +226,10 @@ class _EmailLinkSignInSectionState extends State<_EmailLinkSignInSection> {
url:
'https://react-native-firebase-testing.firebaseapp.com/emailSignin',
handleCodeInApp: true,
iOS: {
'bundleId': 'io.flutter.plugins.firebaseAuthExample',
},
android: {
'packageName': 'io.flutter.plugins.firebaseauthexample',
'androidInstallIfNotAvailable': true,
'androidMinimumVersion': "1",
},
iOSBundleId: 'io.flutter.plugins.firebaseAuthExample',
androidPackageName: 'io.flutter.plugins.firebaseauthexample',
androidInstallApp: true,
androidMinimumVersion: "1",
));

Scaffold.of(context).showSnackBar(SnackBar(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,43 @@ class ActionCodeSettings {
// ignore: public_member_api_docs
@protected
ActionCodeSettings({
this.android,
@Deprecated("Deprecated in favor of using named args instead ([androidPackageName], [androidMinimumVersion], [androidInstallApp])")
// ignore: deprecated_member_use_from_same_package
this.android,
this.androidPackageName,
this.androidMinimumVersion,
this.androidInstallApp,
this.dynamicLinkDomain,
this.handleCodeInApp,
this.iOS,
@required this.url,
}) : assert(url != null) {
if (android != null) {
assert(android['packageName'] != null);
}
if (iOS != null) {
assert(iOS['bundleId'] != null);
}
}
@Deprecated("Deprecated in favor of using named args instead ([iOSBundleId])")
// ignore: deprecated_member_use_from_same_package
this.iOS,
this.iOSBundleId,
@required
this.url,
}) : assert(url != null);

@Deprecated(
"Deprecated in favor of using named args instead ([androidPackageName], [androidMinimumVersion], [androidInstallApp])")
// ignore: public_member_api_docs
Map<String, dynamic> android;

/// The Android package name of the application to open when the URL is pressed.
final String androidPackageName;

/// The minimum app version which must be installed on the device.
///
/// This argument is only set if [androidPackageName] is also set. If the user
/// has the application on the device but it is a lower version number than the
/// one specified they will be taken to the Play Store to upgrade the application.
final String androidMinimumVersion;

/// Whether or not the user should be automatically prompted to install the app
/// via the Play Store if it is not already installed.
final bool androidInstallApp;

/// Sets the Android package name.
final Map<String, dynamic> android;
/// The iOS app to open if it is installed on the device.
final String iOSBundleId;

/// Sets an optional Dynamic Link domain.
final String dynamicLinkDomain;
Expand All @@ -35,30 +56,54 @@ class ActionCodeSettings {
/// app if installed.
final bool handleCodeInApp;

/// Sets the iOS bundle ID.
final Map<String, dynamic> iOS;
@Deprecated("Deprecated in favor of using named args instead ([iOSBundleId])")
// ignore: public_member_api_docs
Map<String, dynamic> iOS;

/// Sets the link continue/state URL
final String url;

/// Returns the current instance as a [Map].
Map<String, dynamic> asMap() {
// ignore: deprecated_member_use_from_same_package
android ??= {};
// ignore: deprecated_member_use_from_same_package
iOS ??= {};

Map<String, dynamic> androidMap;
Map<String, dynamic> iOSMap;

// ignore: deprecated_member_use_from_same_package
if (androidPackageName != null || android['packageName'] != null) {
androidMap = {};
androidMap['packageName'] =
// ignore: deprecated_member_use_from_same_package
androidPackageName ?? android['packageName'].toString();
androidMap['minimumVersion'] =
// ignore: deprecated_member_use_from_same_package
androidMinimumVersion ?? android['minimumVersion']?.toString();
androidMap['installApp'] = androidInstallApp;

// ignore: deprecated_member_use_from_same_package
if (androidMap['installApp'] == null && android['installApp'] is bool) {
// ignore: deprecated_member_use_from_same_package
androidMap['installApp'] = android['installApp'];
}
}

// ignore: deprecated_member_use_from_same_package
if (iOSBundleId != null || iOS['bundleId'] != null) {
iOSMap = {};
// ignore: deprecated_member_use_from_same_package
iOSMap['bundleId'] = iOSBundleId ?? iOS['bundleId'].toString();
}

return <String, dynamic>{
'url': url,
'dynamicLinkDomain': dynamicLinkDomain,
'handleCodeInApp': handleCodeInApp,
'android': android == null
? null
: <String, dynamic>{
'installApp': android['installApp'],
'minimumVersion': android['minimumVersion'],
'packageName': android['packageName'],
},
'iOS': iOS == null
? null
: <String, dynamic>{
'bundleId': iOS['bundleId'],
}
'android': androidMap,
'iOS': iOSMap,
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,18 @@ void main() {
final bool kMockHandleCodeInApp = true;
final String kMockUrl = 'https://test.url';
final String kMockMinimumVersion = '8.0';
final Map<String, dynamic> kMockInstallApp = <String, dynamic>{};

final Map<String, dynamic> kMockAndroid = <String, dynamic>{
'packageName': kMockPackageName,
'installApp': kMockInstallApp,
'minimumVersion': kMockMinimumVersion,
};
final Map<String, dynamic> kMockIOS = <String, dynamic>{
'bundleId': kMockBundleId
};
final bool kMockInstallApp = true;

group('$ActionCodeSettings', () {
ActionCodeSettings actionCodeSettings = ActionCodeSettings(
android: kMockAndroid,
androidPackageName: kMockPackageName,
androidMinimumVersion: kMockMinimumVersion,
androidInstallApp: kMockInstallApp,
dynamicLinkDomain: kMockDynamicLinkDomain,
handleCodeInApp: kMockHandleCodeInApp,
iOS: kMockIOS,
iOSBundleId: kMockBundleId,
url: kMockUrl);

group('Constructor', () {
test('returns an instance of [ActionCodeInfo]', () {
expect(actionCodeSettings, isA<ActionCodeSettings>());
Expand All @@ -39,77 +33,120 @@ void main() {
equals(kMockDynamicLinkDomain));
expect(
actionCodeSettings.handleCodeInApp, equals(kMockHandleCodeInApp));
expect(actionCodeSettings.android, equals(kMockAndroid));
expect(actionCodeSettings.android['packageName'],
equals(kMockPackageName));
expect(actionCodeSettings.iOS, equals(kMockIOS));
expect(actionCodeSettings.iOS['bundleId'], equals(kMockBundleId));
expect(actionCodeSettings.androidPackageName, equals(kMockPackageName));
expect(actionCodeSettings.androidMinimumVersion,
equals(kMockMinimumVersion));
expect(actionCodeSettings.androidInstallApp, equals(kMockInstallApp));
expect(actionCodeSettings.iOSBundleId, equals(kMockBundleId));
});
test('throws [AssertionError] when url is null', () {
expect(() => ActionCodeSettings(url: null), throwsAssertionError);
});

test('throws [AssertionError] when android.packageName is null', () {
expect(
() => ActionCodeSettings(
url: kMockUrl, android: <String, dynamic>{'packageName': null}),
throwsAssertionError);
});

test('throws [AssertionError] when iOS.bundleId is null', () {
expect(
() => ActionCodeSettings(
url: kMockUrl,
android: kMockAndroid,
iOS: <String, dynamic>{'bundleId': null}),
throwsAssertionError);
});
});

group('asMap', () {
test('returns the current instance as a [Map]', () {
final result = actionCodeSettings.asMap();

expect(result, isA<Map<String, dynamic>>());

expect(result['url'], equals(kMockUrl));
expect(result['dynamicLinkDomain'], equals(kMockDynamicLinkDomain));
expect(result['handleCodeInApp'], equals(kMockHandleCodeInApp));
expect(result['android'], equals(kMockAndroid));
expect(result['android']['packageName'], equals(kMockPackageName));
expect(result['android']['installApp'], equals(kMockInstallApp));
expect(
result['android']['minimumVersion'], equals(kMockMinimumVersion));
expect(result['iOS'], equals(kMockIOS));
expect(result['iOS']['bundleId'], equals(kMockBundleId));
});

test('sets android to null', () {
ActionCodeSettings testActionCodeSettings =
ActionCodeSettings(url: kMockUrl, android: null, iOS: kMockIOS);

final result = testActionCodeSettings.asMap();

expect(result, isA<Map<String, dynamic>>());

expect(result['android'], isNull);
group('asMap', () {
test('returns the current instance as a [Map]', () {
final result = actionCodeSettings.asMap();

expect(result, isA<Map<String, dynamic>>());

expect(result['url'], equals(kMockUrl));
expect(result['dynamicLinkDomain'], equals(kMockDynamicLinkDomain));
expect(result['handleCodeInApp'], equals(kMockHandleCodeInApp));

expect(result['android'], isNotNull);
expect(result['iOS'], isNotNull);

expect(result['android']['packageName'], equals(kMockPackageName));
expect(result['android']['installApp'], equals(kMockInstallApp));
expect(
result['android']['minimumVersion'], equals(kMockMinimumVersion));
expect(result['iOS']['bundleId'], equals(kMockBundleId));
});

test('handles deprecated properties', () {
ActionCodeSettings deprecatedSettings = ActionCodeSettings(
// ignore: deprecated_member_use_from_same_package
android: <String, dynamic>{
'packageName': kMockPackageName,
'minimumVersion': kMockMinimumVersion,
'installApp': true,
},
// ignore: deprecated_member_use_from_same_package
iOS: <String, dynamic>{
'bundleId': kMockBundleId,
},
dynamicLinkDomain: kMockDynamicLinkDomain,
handleCodeInApp: kMockHandleCodeInApp,
url: kMockUrl);

final result = deprecatedSettings.asMap();
expect(result, isA<Map<String, dynamic>>());

expect(result['url'], equals(kMockUrl));
expect(result['dynamicLinkDomain'], equals(kMockDynamicLinkDomain));
expect(result['handleCodeInApp'], equals(kMockHandleCodeInApp));

expect(result['android'], isNotNull);
expect(result['iOS'], isNotNull);

expect(result['android']['packageName'], equals(kMockPackageName));
expect(result['android']['installApp'], equals(kMockInstallApp));
expect(
result['android']['minimumVersion'], equals(kMockMinimumVersion));
expect(result['iOS']['bundleId'], equals(kMockBundleId));
});

test('handles mixed deprecated properties', () {
ActionCodeSettings deprecatedSettings = ActionCodeSettings(
// ignore: deprecated_member_use_from_same_package
android: <String, dynamic>{
'packageName': kMockPackageName,
'minimumVersion': kMockMinimumVersion,
'installApp': true,
},
androidPackageName: kMockPackageName + '!',
// ignore: deprecated_member_use_from_same_package
iOS: <String, dynamic>{
'bundleId': kMockBundleId,
},
iOSBundleId: kMockBundleId + '!',
dynamicLinkDomain: kMockDynamicLinkDomain,
handleCodeInApp: kMockHandleCodeInApp,
url: kMockUrl);

final result = deprecatedSettings.asMap();
expect(result, isA<Map<String, dynamic>>());

expect(result['url'], equals(kMockUrl));
expect(result['dynamicLinkDomain'], equals(kMockDynamicLinkDomain));
expect(result['handleCodeInApp'], equals(kMockHandleCodeInApp));

expect(result['android'], isNotNull);
expect(result['iOS'], isNotNull);

expect(
result['android']['packageName'], equals(kMockPackageName + '!'));
expect(result['android']['installApp'], equals(kMockInstallApp));
expect(
result['android']['minimumVersion'], equals(kMockMinimumVersion));
expect(result['iOS']['bundleId'], equals(kMockBundleId + '!'));
});

test('expects android/iOS Maps to be null', () {
ActionCodeSettings testActionCodeSettings =
ActionCodeSettings(url: kMockUrl);

final result = testActionCodeSettings.asMap();
expect(result, isA<Map<String, dynamic>>());
expect(result['android'], isNull);
expect(result['iOS'], isNull);
});
});

test('sets iOS to null', () {
ActionCodeSettings testActionCodeSettings =
ActionCodeSettings(url: kMockUrl, android: kMockAndroid, iOS: null);

final result = testActionCodeSettings.asMap();

expect(result, isA<Map<String, dynamic>>());

expect(result['iOS'], isNull);
test('toString', () {
expect(actionCodeSettings.toString(),
equals('$ActionCodeSettings(${actionCodeSettings.asMap})'));
});
});

test('toString', () {
expect(actionCodeSettings.toString(),
equals('$ActionCodeSettings(${actionCodeSettings.asMap})'));
});
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -677,11 +677,10 @@ void main() {

group('verifyBeforeUpdateEmail()', () {
final ActionCodeSettings actionCodeSettings = ActionCodeSettings(
url: 'test',
dynamicLinkDomain: null,
handleCodeInApp: null,
android: null,
iOS: null);
url: 'test',
dynamicLinkDomain: null,
handleCodeInApp: null,
);
const newEmail = '[email protected]';
test('verifyBeforeUpdateEmail()', () async {
await auth.currentUser
Expand Down
Loading

0 comments on commit 6942dbe

Please sign in to comment.