auth0 / auth0-flutter

Auth0 SDK for Flutter
https://pub.dev/documentation/auth0_flutter/latest/
Apache License 2.0
61 stars 41 forks source link

Auth0 SDK's resetPassword() cannot be tested from unit test files #257

Closed like-engels closed 1 year ago

like-engels commented 1 year ago

Checklist

Description

MissingPluginException(No implementation found for method auth#resetPassword on channel auth0.com/auth0_flutter/auth) package:flutter/src/services/platform_channel.dart 308:7 MethodChannel._invokeMethod ===== asynchronous gap =========================== dart:async new Future.error

Reproduction

  1. Create a unit test file
  2. Put the auth0 sdk and use the auth0's api.resetPassword() function
  3. Run the test

auth0_flutter version

latest

Flutter version

3.10.0

Platform

Android, iOS

Platform version(s)

13,16

stevehobbsdev commented 1 year ago

Thanks for your patience here @like-engels; can you share an example test you're using here?

like-engels commented 1 year ago

@stevehobbsdev sure!

Here's my code and impl.

Implementation class using a representable interface.

/// Auth0 Profile management provider for
/// managing Auth0 profiles
class Auth0ProfileManagementProvider extends ProfileManagementAdapter {

  /// Auth0 Profile management provider for
  /// managing Auth0 profiles
  Auth0ProfileManagementProvider(this.auth0Instance);

  /// Auth0 instance
  final Auth0 auth0Instance;

  @override
  Future<void> changePassword(Map<String, dynamic> data) async {
    try {
      final email = data['email'] as String;
      await auth0Instance.api.resetPassword(email: email, connection: 'Username-Password-Authentication');
    } catch (error, stacktrace) {
      return Future.error(error, stacktrace);
    }
  }

  @override
  Future<void> changeProfileInformation(Map<String, dynamic> profileInfo) async {
    try {
      await auth0Instance.api.userProfile(accessToken: profileInfo['userInfo'] as String);
    } catch (error, stacktrace) {
      return Future.error(error, stacktrace);
    }
  }

  @override
  Future<void> otpLogin(String otp, String mfaToken) async {
    try {
      await auth0Instance.api.loginWithOtp(otp: otp, mfaToken: mfaToken);
    } catch (error, stacktrace) {
      return Future.error(error, stacktrace);
    }
  }

}

The test itself

void main() {
  test('test check', () async {
    WidgetsFlutterBinding.ensureInitialized();
    final provider = Auth0ProfileManagementProvider(
      Auth0('tenancyrul', 
            'apptoken',
      ),
    );

    await provider.changePassword({
      'email': 'someemail@example.com'
    });
  });
}
Widcket commented 1 year ago

Hi @like-engels, apologies for the delay here.

This library should be considered as a black box and mocked when unit-testing, to avoid doing integration tests of third-party libraries in your unit tests.

Widcket commented 1 year ago

Closing, as I believe the above answers your question. Please feel free to ping if you'd prefer to reopen.