diff --git a/tests/at_functional_test/test/enrollment_test.dart b/tests/at_functional_test/test/enrollment_test.dart index 806f84a41..722700b5b 100644 --- a/tests/at_functional_test/test/enrollment_test.dart +++ b/tests/at_functional_test/test/enrollment_test.dart @@ -26,7 +26,7 @@ void main() { await setLastReceivedNotificationDateTime(); }); - void _stopSubscriptions() { + void stopSubscriptions() { atClientManager.atClient.notificationService.stopAllSubscriptions(); print('subscriptions stopped'); } @@ -87,9 +87,59 @@ void main() { print('got enrollment notification: $enrollNotification'); expect(enrollNotification.key, '$enrollmentIdFromServer.new.enrollments.__manage'); - _stopSubscriptions(); + stopSubscriptions(); }, count: 1, max: 1)); }); + + test( + 'validate client functionality to fetch pending enrollments on legacy pkam authenticated client', + () async { + AtClient? client = atClientManager.atClient; + // fetch first otp + String? otp = + await TestUtils.executeCommandAndParse(client, 'otp:get', auth: true); + expect(otp, isNotNull); + // create first enrollment request + RemoteSecondary? secondRemoteSecondary = + RemoteSecondary(atSign, getClient2Preferences()); + var publicKey = + at_demos.pkamPublicKeyMap['@bob🛠']; // can be any random public key + var newEnrollRequest = TestUtils.formatCommand( + 'enroll:request:{"appName":"buzz","deviceName":"pixel","namespaces":{"buzz":"rw"},"otp":"$otp","apkamPublicKey":"$publicKey"}'); + var enrollResponse = await TestUtils.executeCommandAndParse( + null, newEnrollRequest, + remoteSecondary: secondRemoteSecondary); + Map enrollResponse1JsonDecoded = + jsonDecode(enrollResponse!); + expect(enrollResponse1JsonDecoded['enrollmentId'], isNotNull); + expect(enrollResponse1JsonDecoded['status'], 'pending'); + + // fetch second otp + otp = await TestUtils.executeCommandAndParse(client, 'otp:get', auth: true); + expect(otp, isNotNull); + // create second enrollment request + newEnrollRequest = TestUtils.formatCommand( + 'enroll:request:{"appName":"wavi","deviceName":"pixel7","namespaces":{"buzz":"rw", "wavi":"r"},"otp":"$otp","apkamPublicKey":"$publicKey"}'); + enrollResponse = await TestUtils.executeCommandAndParse( + null, newEnrollRequest, + remoteSecondary: secondRemoteSecondary); + var enrollResponse2JsonDecoded = jsonDecode(enrollResponse!); + expect(enrollResponse2JsonDecoded['enrollmentId'], isNotNull); + expect(enrollResponse2JsonDecoded['status'], 'pending'); + + // fetch enrollment requests through client + Map enrollmentRequests = + await client.fetchEnrollmentRequests(); + print(enrollmentRequests.entries); + expect(enrollmentRequests.length, 2); + + String firstEnrollmentKey = enrollmentRequests.keys.toList()[0]; + String secondEnrollmentKey = enrollmentRequests.keys.toList()[1]; + + expect((enrollmentRequests[firstEnrollmentKey]['namespace'] as Map)['buzz'], 'rw'); + expect((enrollmentRequests[secondEnrollmentKey]['namespace'] as Map)['buzz'], 'rw'); + expect((enrollmentRequests[secondEnrollmentKey]['namespace'] as Map)['wavi'], 'r'); + }); } Future setLastReceivedNotificationDateTime() async { diff --git a/tests/at_functional_test/test/test_utils.dart b/tests/at_functional_test/test/test_utils.dart index 6984b1128..cf7b31d0a 100644 --- a/tests/at_functional_test/test/test_utils.dart +++ b/tests/at_functional_test/test/test_utils.dart @@ -8,6 +8,7 @@ import 'package:at_client/at_client.dart'; import 'package:at_functional_test/src/at_demo_credentials.dart' as demo_credentials; + class TestUtils { static AtClientPreference getPreference(String atsign) { var preference = AtClientPreference(); @@ -52,4 +53,16 @@ class TestUtils { atClientManager.atClient, currentAtSign); return atClientManager; } + + static String formatCommand(String command){ + if(!command.contains('\n')) return '$command\n'; + return command; + } + + static Future executeCommandAndParse(AtClient? client, command, {bool auth = false, RemoteSecondary? remoteSecondary}) async { + remoteSecondary ??= client?.getRemoteSecondary(); + String? response = await remoteSecondary?.executeCommand(formatCommand(command), auth: auth); + print('Command: $command -> Response: $response'); + return response?.replaceFirst('data:', ''); + } }