Skip to content

Commit 0f43d6c

Browse files
authored
Merge pull request #5072 from aws-amplify/feat/fetchCurrentDevice-integration-tests
Feat (Auth): Integration Tests for fetchCurrentDevice API
2 parents 8b60ec6 + a7be58d commit 0f43d6c

File tree

3 files changed

+50
-1
lines changed

3 files changed

+50
-1
lines changed

packages/auth/amplify_auth_cognito/example/integration_test/device_tracking_test.dart

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,45 @@ void main() {
240240
await expectLater(Amplify.Auth.rememberDevice(), completes);
241241
});
242242

243+
asyncTest('fetchCurrentDevice returns the current device', (_) async {
244+
await expectLater(Amplify.Auth.fetchCurrentDevice(), completes);
245+
final currentTestDevice = await Amplify.Auth.fetchCurrentDevice();
246+
final currentDeviceKey = await getDeviceKey();
247+
expect(currentDeviceKey, currentTestDevice.id);
248+
});
249+
250+
asyncTest(
251+
'The device from fetchCurrentDevice isnt equal to another device.',
252+
(_) async {
253+
final previousDeviceKey = await getDeviceKey();
254+
await signOutUser();
255+
await deleteDevice(cognitoUsername, previousDeviceKey!);
256+
await signIn();
257+
final newCurrentTestDevice = await Amplify.Auth.fetchCurrentDevice();
258+
expect(newCurrentTestDevice.id, isNot(previousDeviceKey));
259+
});
260+
261+
asyncTest(
262+
'fetchCurrentDevice throws a DeviceNotTrackedException when device is forgotten.',
263+
(_) async {
264+
expect(await getDeviceState(), DeviceState.remembered);
265+
await Amplify.Auth.forgetDevice();
266+
await expectLater(
267+
Amplify.Auth.fetchCurrentDevice,
268+
throwsA(isA<DeviceNotTrackedException>()),
269+
);
270+
});
271+
272+
asyncTest(
273+
'fetchCurrentDevice throws a SignedOutException when device signs out.',
274+
(_) async {
275+
await signOutUser();
276+
await expectLater(
277+
Amplify.Auth.fetchCurrentDevice,
278+
throwsA(isA<SignedOutException>()),
279+
);
280+
});
281+
243282
asyncTest('forgetDevice stops tracking', (_) async {
244283
expect(await getDeviceState(), DeviceState.remembered);
245284
await Amplify.Auth.forgetDevice();

packages/auth/amplify_auth_cognito_dart/example/lib/common.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,10 @@ Future<List<AuthUserAttribute>> fetchUserAttributes() async {
104104
return Amplify.Auth.fetchUserAttributes();
105105
}
106106

107+
Future<AuthDevice> fetchCurrentDevice() async {
108+
return Amplify.Auth.fetchCurrentDevice();
109+
}
110+
107111
Future<List<AuthDevice>> fetchDevices() async {
108112
return Amplify.Auth.fetchDevices();
109113
}

packages/test/amplify_integration_test/lib/src/stubs/amplify_auth_cognito_stub.dart

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,13 @@ class AmplifyAuthCognitoStub extends AuthPluginInterface
368368
);
369369
}
370370

371+
@override
372+
Future<AuthDevice> fetchCurrentDevice() async {
373+
throw UnimplementedError(
374+
'fetchCurrentDevice is not implemented.',
375+
);
376+
}
377+
371378
@override
372379
Future<void> forgetDevice([AuthDevice? device]) async {
373380
throw UnimplementedError(
@@ -391,7 +398,6 @@ class AmplifyAuthCognitoStub extends AuthPluginInterface
391398
}
392399

393400
class MockCognitoUser {
394-
395401
factory MockCognitoUser({
396402
required String username,
397403
required String password,

0 commit comments

Comments
 (0)