Skip to content

Commit 1cbc9fa

Browse files
committed
test: restructure fetchcurrentdevice test
1 parent 685fa37 commit 1cbc9fa

File tree

1 file changed

+43
-64
lines changed

1 file changed

+43
-64
lines changed

packages/auth/amplify_auth_cognito_test/test/plugin/fetch_current_device_test.dart

Lines changed: 43 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import 'package:amplify_auth_cognito_dart/amplify_auth_cognito_dart.dart';
55
import 'package:amplify_auth_cognito_dart/src/credentials/cognito_keys.dart';
66
import 'package:amplify_auth_cognito_dart/src/credentials/device_metadata_repository.dart';
77
import 'package:amplify_auth_cognito_dart/src/sdk/cognito_identity_provider.dart';
8-
import 'package:amplify_auth_cognito_dart/src/state/cognito_state_machine.dart';
98
import 'package:amplify_auth_cognito_test/common/mock_clients.dart';
109
import 'package:amplify_auth_cognito_test/common/mock_config.dart';
1110
import 'package:amplify_auth_cognito_test/common/mock_secure_storage.dart';
@@ -23,19 +22,15 @@ void main() {
2322

2423
late DeviceMetadataRepository repo;
2524
late AmplifyAuthCognitoDart plugin;
26-
late CognitoAuthStateMachine stateMachine;
27-
late MockSecureStorage secureStorage;
2825

2926
Future<String?> getDeviceKey() async {
3027
final secrets = await repo.get(username);
3128
return secrets?.deviceKey;
3229
}
3330

34-
group(
35-
'fetchCurrentDevice expected return value and DeviceNotTrackedException',
36-
() {
31+
group('fetchCurrentDevice', () {
3732
setUp(() async {
38-
secureStorage = MockSecureStorage();
33+
final secureStorage = MockSecureStorage();
3934
seedStorage(
4035
secureStorage,
4136
userPoolKeys: userPoolKeys,
@@ -45,74 +40,58 @@ void main() {
4540
plugin = AmplifyAuthCognitoDart(
4641
secureStorageFactory: (_) => secureStorage,
4742
);
48-
stateMachine = plugin.stateMachine;
4943
await plugin.configure(
5044
config: mockConfig,
5145
authProviderRepo: testAuthRepo,
5246
);
53-
final mockIdp = MockCognitoIdentityProviderClient(
54-
getDevice: () async => mockDeviceResponse,
55-
forgetDevice: () async {},
56-
);
57-
stateMachine.addInstance<CognitoIdentityProviderClient>(mockIdp);
58-
repo = stateMachine.getOrCreate<DeviceMetadataRepository>();
47+
repo = plugin.stateMachine.getOrCreate<DeviceMetadataRepository>();
5948
});
6049

61-
test(
62-
'should get the current device. current device id should be equal to the local device id',
63-
() async {
64-
final currentDeviceKey = await getDeviceKey();
65-
expect(currentDeviceKey, isNotNull);
66-
final currentDevice = await plugin.fetchCurrentDevice();
67-
expect(currentDeviceKey, currentDevice.id);
68-
});
50+
group('Cognito GetDevice returns successfully', () {
51+
setUp(() async {
52+
final mockIdp = MockCognitoIdentityProviderClient(
53+
getDevice: () async => mockDeviceResponse,
54+
forgetDevice: () async {},
55+
);
56+
plugin.stateMachine.addInstance<CognitoIdentityProviderClient>(mockIdp);
57+
});
6958

70-
test(
71-
'should should throw a DeviceNotTrackedException when current device key is null',
72-
() async {
73-
await plugin.forgetDevice();
74-
await expectLater(
75-
plugin.fetchCurrentDevice,
76-
throwsA(isA<DeviceNotTrackedException>()),
77-
);
78-
});
59+
test(
60+
'should get the current device. current device id should be equal to the local device id',
61+
() async {
62+
final currentDeviceKey = await getDeviceKey();
63+
expect(currentDeviceKey, isNotNull);
64+
final currentDevice = await plugin.fetchCurrentDevice();
65+
expect(currentDeviceKey, currentDevice.id);
66+
});
7967

80-
tearDown(() async {
81-
await plugin.close();
68+
test(
69+
'should should throw a DeviceNotTrackedException when current device key is null',
70+
() async {
71+
await plugin.forgetDevice();
72+
await expectLater(
73+
plugin.fetchCurrentDevice,
74+
throwsA(isA<DeviceNotTrackedException>()),
75+
);
76+
});
8277
});
83-
});
8478

85-
group('fetchCurrentDevice AWSHttpException', () {
86-
setUp(() async {
87-
secureStorage = MockSecureStorage();
88-
seedStorage(
89-
secureStorage,
90-
userPoolKeys: userPoolKeys,
91-
identityPoolKeys: identityPoolKeys,
92-
deviceKeys: CognitoDeviceKeys(userPoolConfig, username),
93-
);
94-
plugin = AmplifyAuthCognitoDart(
95-
secureStorageFactory: (_) => secureStorage,
96-
);
97-
stateMachine = plugin.stateMachine;
98-
await plugin.configure(
99-
config: mockConfig,
100-
authProviderRepo: testAuthRepo,
101-
);
102-
final mockIdp = MockCognitoIdentityProviderClient(
103-
getDevice: () async => throw AWSHttpException(
104-
AWSHttpRequest.get(Uri.parse('https://www.amazon.com')),
105-
),
106-
);
107-
stateMachine.addInstance<CognitoIdentityProviderClient>(mockIdp);
108-
repo = stateMachine.getOrCreate<DeviceMetadataRepository>();
109-
});
79+
group('Cognito GetDevice throws AWSHttpException', () {
80+
setUp(() async {
81+
final mockIdp = MockCognitoIdentityProviderClient(
82+
getDevice: () async => throw AWSHttpException(
83+
AWSHttpRequest.get(Uri.parse('https://aws.amazon.com/cognito/')),
84+
),
85+
);
86+
plugin.stateMachine.addInstance<CognitoIdentityProviderClient>(mockIdp);
87+
});
11088

111-
test('should throw a NetworkException', () async {
112-
await expectLater(
113-
plugin.fetchCurrentDevice,
114-
throwsA(isA<NetworkException>()),
115-
);
89+
test('should throw a NetworkException', () async {
90+
await expectLater(
91+
plugin.fetchCurrentDevice,
92+
throwsA(isA<NetworkException>()),
93+
);
94+
});
11695
});
11796

11897
tearDown(() async {

0 commit comments

Comments
 (0)