Skip to content

Feat (Auth): Integration Tests for fetchCurrentDevice API #5071

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions packages/amplify_core/doc/lib/auth.dart
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,17 @@ Future<void> forgetSpecificDevice(AuthDevice myDevice) async {
}
// #enddocregion forget-specific-device

// #docregion fetch-current-device
Future<void> fetchCurrentUserDevice() async {
try {
final device = await Amplify.Auth.fetchCurrentDevice();
safePrint('Device: $device');
} on AuthException catch (e) {
safePrint('Get current device failed with error: $e');
}
}
// #enddocregion fetch-current-device

// #docregion fetch-devices
Future<void> fetchAllDevices() async {
try {
Expand Down
31 changes: 31 additions & 0 deletions packages/amplify_core/lib/src/category/amplify_auth_category.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1355,6 +1355,37 @@ class AuthCategory extends AmplifyCategory<AuthPluginInterface> {
() => defaultPlugin.rememberDevice(),
);

/// {@template amplify_core.amplify_auth_category.fetch_current_device}
/// Retrieves the current device.
///
/// For more information about device tracking, see the
/// [Amplify docs](https://docs.amplify.aws/flutter/build-a-backend/auth/manage-users/manage-devices/#fetch-current-device).
///
/// ## Examples
///
/// <?code-excerpt "doc/lib/auth.dart" region="imports"?>
/// ```dart
/// import 'package:amplify_auth_cognito/amplify_auth_cognito.dart';
/// import 'package:amplify_flutter/amplify_flutter.dart';
/// ```
///
/// <?code-excerpt "doc/lib/auth.dart" region="fetch-current-device"?>
/// ```dart
/// Future<AuthDevice> getCurrentUserDevice() async {
/// try {
/// final device = await Amplify.Auth.fetchCurrentDevice();
/// safePrint('Device: $device');
/// } on AuthException catch (e) {
/// safePrint('Get current device failed with error: $e');
/// }
/// }
/// ```
/// {@endtemplate}
Future<AuthDevice> fetchCurrentDevice() => identifyCall(
AuthCategoryMethod.fetchCurrentDevice,
() => defaultPlugin.fetchCurrentDevice(),
);

/// {@template amplify_core.amplify_auth_category.forget_device}
/// Forgets the current device.
///
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ enum AuthCategoryMethod with AmplifyCategoryMethod {
setMfaPreference('49'),
getMfaPreference('50'),
setUpTotp('51'),
verifyTotpSetup('52');
verifyTotpSetup('52'),
fetchCurrentDevice('59');

const AuthCategoryMethod(this.method);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,11 @@ abstract class AuthPluginInterface extends AmplifyPluginInterface {
throw UnimplementedError('forgetDevice() has not been implemented.');
}

/// {@macro amplify_core.amplify_auth_category.fetch_current_device}
Future<AuthDevice> fetchCurrentDevice() {
throw UnimplementedError('fetchCurrentDevice() has not been implemented.');
}

/// {@macro amplify_core.amplify_auth_category.fetch_devices}
Future<List<AuthDevice>> fetchDevices() {
throw UnimplementedError('fetchDevices() has not been implemented.');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 forgotton.',
(_) async {
expect(await getDeviceState(), DeviceState.remembered);
await Amplify.Auth.forgetDevice();
await expectLater(
Amplify.Auth.fetchCurrentDevice,
throwsA(isA<DeviceNotTrackedException>()),
);
});

asyncTest(
'fetchCurrentDevice throws a SignedOutException when device signs out.',
(_) async {
await signOutUser();
await expectLater(
Amplify.Auth.fetchCurrentDevice,
throwsA(isA<SignedOutException>()),
);
});

asyncTest('forgetDevice stops tracking', (_) async {
expect(await getDeviceState(), DeviceState.remembered);
await Amplify.Auth.forgetDevice();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,10 @@ Future<List<AuthUserAttribute>> fetchUserAttributes() async {
return Amplify.Auth.fetchUserAttributes();
}

Future<AuthDevice> fetchCurrentDevice() async {
return Amplify.Auth.fetchCurrentDevice();
}

Future<List<AuthDevice>> fetchDevices() async {
return Amplify.Auth.fetchDevices();
}
Expand Down
Loading