diff --git a/packages/auth/amplify_auth_cognito/example/integration_test/device_tracking_test.dart b/packages/auth/amplify_auth_cognito/example/integration_test/device_tracking_test.dart index 500582ad0a..c877ffca52 100644 --- a/packages/auth/amplify_auth_cognito/example/integration_test/device_tracking_test.dart +++ b/packages/auth/amplify_auth_cognito/example/integration_test/device_tracking_test.dart @@ -240,6 +240,45 @@ void main() { await expectLater(Amplify.Auth.rememberDevice(), completes); }); + asyncTest('fetchCurrentDevice returns the current device', (_) async { + await expectLater(Amplify.Auth.fetchCurrentDevice(), completes); + final currentTestDevice = await Amplify.Auth.fetchCurrentDevice(); + final currentDeviceKey = await getDeviceKey(); + expect(currentDeviceKey, currentTestDevice.id); + }); + + asyncTest( + 'The device from fetchCurrentDevice isnt equal to another device.', + (_) async { + final previousDeviceKey = await getDeviceKey(); + await signOutUser(); + await deleteDevice(cognitoUsername, previousDeviceKey!); + await signIn(); + final newCurrentTestDevice = await Amplify.Auth.fetchCurrentDevice(); + expect(newCurrentTestDevice.id, isNot(previousDeviceKey)); + }); + + asyncTest( + 'fetchCurrentDevice throws a DeviceNotTrackedException when device is forgotten.', + (_) async { + expect(await getDeviceState(), DeviceState.remembered); + await Amplify.Auth.forgetDevice(); + await expectLater( + Amplify.Auth.fetchCurrentDevice, + throwsA(isA()), + ); + }); + + asyncTest( + 'fetchCurrentDevice throws a SignedOutException when device signs out.', + (_) async { + await signOutUser(); + await expectLater( + Amplify.Auth.fetchCurrentDevice, + throwsA(isA()), + ); + }); + asyncTest('forgetDevice stops tracking', (_) async { expect(await getDeviceState(), DeviceState.remembered); await Amplify.Auth.forgetDevice(); diff --git a/packages/auth/amplify_auth_cognito_dart/example/lib/common.dart b/packages/auth/amplify_auth_cognito_dart/example/lib/common.dart index 42347c4c90..b266f9ee7d 100644 --- a/packages/auth/amplify_auth_cognito_dart/example/lib/common.dart +++ b/packages/auth/amplify_auth_cognito_dart/example/lib/common.dart @@ -104,6 +104,10 @@ Future> fetchUserAttributes() async { return Amplify.Auth.fetchUserAttributes(); } +Future fetchCurrentDevice() async { + return Amplify.Auth.fetchCurrentDevice(); +} + Future> fetchDevices() async { return Amplify.Auth.fetchDevices(); } diff --git a/packages/test/amplify_integration_test/lib/src/stubs/amplify_auth_cognito_stub.dart b/packages/test/amplify_integration_test/lib/src/stubs/amplify_auth_cognito_stub.dart index d363d66ecc..c28eb95e6a 100644 --- a/packages/test/amplify_integration_test/lib/src/stubs/amplify_auth_cognito_stub.dart +++ b/packages/test/amplify_integration_test/lib/src/stubs/amplify_auth_cognito_stub.dart @@ -368,6 +368,13 @@ class AmplifyAuthCognitoStub extends AuthPluginInterface ); } + @override + Future fetchCurrentDevice() async { + throw UnimplementedError( + 'fetchCurrentDevice is not implemented.', + ); + } + @override Future forgetDevice([AuthDevice? device]) async { throw UnimplementedError( @@ -391,7 +398,6 @@ class AmplifyAuthCognitoStub extends AuthPluginInterface } class MockCognitoUser { - factory MockCognitoUser({ required String username, required String password,