Skip to content

Commit 9fce922

Browse files
authored
Merge pull request #5074 from aws-amplify/feat/fetchCurrentDevice-unit-test
chore(auth): Unit tests for fetchCurrentDevice API
2 parents 45202d0 + 10e5a0f commit 9fce922

File tree

1 file changed

+96
-0
lines changed

1 file changed

+96
-0
lines changed
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
import 'package:amplify_auth_cognito_dart/amplify_auth_cognito_dart.dart';
5+
import 'package:amplify_auth_cognito_dart/src/credentials/cognito_keys.dart';
6+
import 'package:amplify_auth_cognito_dart/src/credentials/device_metadata_repository.dart';
7+
import 'package:amplify_auth_cognito_dart/src/sdk/cognito_identity_provider.dart';
8+
import 'package:amplify_auth_cognito_test/common/mock_clients.dart';
9+
import 'package:amplify_auth_cognito_test/common/mock_config.dart';
10+
import 'package:amplify_auth_cognito_test/common/mock_secure_storage.dart';
11+
import 'package:amplify_core/amplify_core.dart';
12+
import 'package:test/test.dart';
13+
14+
void main() {
15+
AmplifyLogger().logLevel = LogLevel.verbose;
16+
17+
final userPoolKeys = CognitoUserPoolKeys(userPoolConfig);
18+
final identityPoolKeys = CognitoIdentityPoolKeys(identityPoolConfig);
19+
final testAuthRepo = AmplifyAuthProviderRepository();
20+
final mockDevice = DeviceType(deviceKey: deviceKey);
21+
final mockDeviceResponse = GetDeviceResponse(device: mockDevice);
22+
23+
late DeviceMetadataRepository repo;
24+
late AmplifyAuthCognitoDart plugin;
25+
26+
group('fetchCurrentDevice', () {
27+
setUp(() async {
28+
final secureStorage = MockSecureStorage();
29+
seedStorage(
30+
secureStorage,
31+
userPoolKeys: userPoolKeys,
32+
identityPoolKeys: identityPoolKeys,
33+
deviceKeys: CognitoDeviceKeys(userPoolConfig, username),
34+
);
35+
plugin = AmplifyAuthCognitoDart(
36+
secureStorageFactory: (_) => secureStorage,
37+
);
38+
await plugin.configure(
39+
config: mockConfig,
40+
authProviderRepo: testAuthRepo,
41+
);
42+
repo = plugin.stateMachine.getOrCreate<DeviceMetadataRepository>();
43+
});
44+
45+
group('should successfully', () {
46+
setUp(() async {
47+
final mockIdp = MockCognitoIdentityProviderClient(
48+
getDevice: () async => mockDeviceResponse,
49+
forgetDevice: () async {},
50+
);
51+
plugin.stateMachine.addInstance<CognitoIdentityProviderClient>(mockIdp);
52+
});
53+
54+
test(
55+
'return the current device where the current device id is equal to the local device id',
56+
() async {
57+
final secrets = await repo.get(username);
58+
final currentDeviceKey = secrets?.deviceKey;
59+
expect(currentDeviceKey, isNotNull);
60+
final currentDevice = await plugin.fetchCurrentDevice();
61+
expect(currentDeviceKey, currentDevice.id);
62+
});
63+
64+
test('throw a DeviceNotTrackedException when current device key is null',
65+
() async {
66+
await plugin.forgetDevice();
67+
await expectLater(
68+
plugin.fetchCurrentDevice,
69+
throwsA(isA<DeviceNotTrackedException>()),
70+
);
71+
});
72+
});
73+
74+
group('should throw', () {
75+
setUp(() async {
76+
final mockIdp = MockCognitoIdentityProviderClient(
77+
getDevice: () async => throw AWSHttpException(
78+
AWSHttpRequest.get(Uri.parse('https://aws.amazon.com/cognito/')),
79+
),
80+
);
81+
plugin.stateMachine.addInstance<CognitoIdentityProviderClient>(mockIdp);
82+
});
83+
84+
test('a NetworkException', () async {
85+
await expectLater(
86+
plugin.fetchCurrentDevice,
87+
throwsA(isA<NetworkException>()),
88+
);
89+
});
90+
});
91+
92+
tearDown(() async {
93+
await plugin.close();
94+
});
95+
});
96+
}

0 commit comments

Comments
 (0)