Skip to content

Commit

Permalink
test: add functional test
Browse files Browse the repository at this point in the history
  • Loading branch information
srieteja committed Jan 11, 2024
1 parent dfa6750 commit bda846a
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 2 deletions.
54 changes: 52 additions & 2 deletions tests/at_functional_test/test/enrollment_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ void main() {
await setLastReceivedNotificationDateTime();
});

void _stopSubscriptions() {
void stopSubscriptions() {
atClientManager.atClient.notificationService.stopAllSubscriptions();
print('subscriptions stopped');
}
Expand Down Expand Up @@ -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<String, dynamic> 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<String, dynamic> 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<String, dynamic>)['buzz'], 'rw');
expect((enrollmentRequests[secondEnrollmentKey]['namespace'] as Map<String, dynamic>)['buzz'], 'rw');
expect((enrollmentRequests[secondEnrollmentKey]['namespace'] as Map<String, dynamic>)['wavi'], 'r');
});
}

Future<void> setLastReceivedNotificationDateTime() async {
Expand Down
13 changes: 13 additions & 0 deletions tests/at_functional_test/test/test_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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<String?> 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:', '');
}
}

0 comments on commit bda846a

Please sign in to comment.