Skip to content

Commit

Permalink
Merge pull request #1365 from atsign-foundation/add_shared_key_to_met…
Browse files Browse the repository at this point in the history
…adata

fix: Add "sharedKeyEnc" to the metadata
  • Loading branch information
gkc authored Jul 30, 2024
2 parents b2e8163 + 7bd5115 commit 3d2ff7e
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 7 deletions.
1 change: 1 addition & 0 deletions packages/at_client/lib/src/response/at_notification.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class AtNotification {
json['metadata'][AtConstants.sharedKeyEncryptedEncryptingKeyName];
metadata.skeEncAlgo =
json['metadata'][AtConstants.sharedKeyEncryptedEncryptingAlgo];
metadata.sharedKeyEnc = json['metadata'][AtConstants.sharedKeyEncrypted];
}

return AtNotification(json['id'], json['key'], json['from'], json['to'],
Expand Down
18 changes: 11 additions & 7 deletions tests/at_end2end_test/lib/src/test_initializers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ class TestSuiteInitializer {
return _singleton;
}

Future<void> testInitializer(
String atSign, String namespace, String authType) async {
Future<void> testInitializer(String atSign, String namespace, String authType,
{bool enableInitialSync = true,
AtClientPreference? atClientPreference}) async {
try {
late AtChops atChops;
AtAuthResponse? atAuthResponse;
Expand All @@ -36,18 +37,21 @@ class TestSuiteInitializer {
atChops = createAtChopsFromDemoKeys(atSign);
}

atClientPreference ??=
TestPreferences.getInstance().getPreference(atSign);
// Create the atClientManager for the atSign
var atClientManager = await AtClientManager.getInstance()
.setCurrentAtSign(atSign, namespace,
TestPreferences.getInstance().getPreference(atSign),
.setCurrentAtSign(atSign, namespace, atClientPreference,
atChops: atChops, enrollmentId: atAuthResponse?.enrollmentId);
// Set Encryption Keys for currentAtSign
await AtEncryptionKeysLoader.getInstance()
.setEncryptionKeys(atClientManager.atClient, atSign);

await E2ESyncService.getInstance().syncData(
atClientManager.atClient.syncService,
syncOptions: SyncOptions()..waitForFullSyncToComplete = true);
if (enableInitialSync) {
await E2ESyncService.getInstance().syncData(
atClientManager.atClient.syncService,
syncOptions: SyncOptions()..waitForFullSyncToComplete = true);
}

// verify if the public key is in the local secondary
var result = await atClientManager.atClient
Expand Down
83 changes: 83 additions & 0 deletions tests/at_end2end_test/test/notify_with_isolate_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import 'dart:io';
import 'dart:isolate';

import 'package:at_client/at_client.dart';
import 'package:at_end2end_test/config/config_util.dart';
import 'package:at_end2end_test/src/test_initializers.dart';
import 'package:at_end2end_test/utils/test_constants.dart';
import 'package:test/test.dart';

String currentAtSign = ConfigUtil.getYaml()['atSign']['firstAtSign'];
String sharedWithAtSign = ConfigUtil.getYaml()['atSign']['secondAtSign'];
String authType = ConfigUtil.getYaml()['authType'];

void main() {
String notifyKey =
'$sharedWithAtSign:phone.${TestConstants.namespace}$currentAtSign';
String value = '+91 9868123123';

test('A test to send and receive notification with isolate', () async {
ReceivePort mainIsolateReceivePort = ReceivePort('MainIsolateReceivePort');

// Spawn an isolate to listen for notifications
Isolate childIsolate =
await Isolate.spawn(initSharedAtSign, mainIsolateReceivePort.sendPort);
// Listen for messages from isolate
mainIsolateReceivePort.listen(expectAsync1((data) {
expect(data.value, value);
expect(data.key, notifyKey);
expect(data.from, currentAtSign);
expect(data.to, sharedWithAtSign);
childIsolate.kill();
}));

// Initialize another atSign to send notifications
await TestSuiteInitializer.getInstance().testInitializer(
currentAtSign, TestConstants.namespace, authType,
enableInitialSync: false,
atClientPreference: getAtClientPreferences(currentAtSign));

NotificationResult notificationResult = await AtClientManager.getInstance()
.atClient
.notificationService
.notify(NotificationParams.forUpdate(AtKey.fromString(notifyKey),
value: value));

expect(notificationResult.notificationStatusEnum,
NotificationStatusEnum.delivered);
});

tearDown(() {
// Remove hive directories
Directory('test/hive/$currentAtSign').deleteSync(recursive: true);
Directory('test/hive/$sharedWithAtSign').deleteSync(recursive: true);
});
}

Future<void> initSharedAtSign(SendPort mainIsolateSendPort) async {
await TestSuiteInitializer.getInstance().testInitializer(
sharedWithAtSign, TestConstants.namespace, authType,
enableInitialSync: false,
atClientPreference: getAtClientPreferences(sharedWithAtSign));

AtClientManager.getInstance()
.atClient
.notificationService
.subscribe(shouldDecrypt: true)
.listen((onData) {
// Ignore stats notifications
if (onData.id == '-1') {
return;
}
mainIsolateSendPort.send(onData);
});
}

AtClientPreference getAtClientPreferences(String atSign) {
var atClientPreference = AtClientPreference();
atClientPreference.hiveStoragePath = 'test/hive/$atSign';
atClientPreference.commitLogPath = 'test/hive/$atSign/commit/';
atClientPreference.isLocalStoreRequired = true;
atClientPreference.rootDomain = ConfigUtil.getYaml()['root_server']['url'];
return atClientPreference;
}

0 comments on commit 3d2ff7e

Please sign in to comment.