Skip to content

Commit

Permalink
Merge pull request #5072 from aws-amplify/feat/fetchCurrentDevice-int…
Browse files Browse the repository at this point in the history
…egration-tests

Feat (Auth): Integration Tests for fetchCurrentDevice API
  • Loading branch information
hahnandrew authored Jun 28, 2024
2 parents 8b60ec6 + a7be58d commit 0f43d6c
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 1 deletion.
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 forgotten.',
(_) 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
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,13 @@ class AmplifyAuthCognitoStub extends AuthPluginInterface
);
}

@override
Future<AuthDevice> fetchCurrentDevice() async {
throw UnimplementedError(
'fetchCurrentDevice is not implemented.',
);
}

@override
Future<void> forgetDevice([AuthDevice? device]) async {
throw UnimplementedError(
Expand All @@ -391,7 +398,6 @@ class AmplifyAuthCognitoStub extends AuthPluginInterface
}

class MockCognitoUser {

factory MockCognitoUser({
required String username,
required String password,
Expand Down

0 comments on commit 0f43d6c

Please sign in to comment.