Skip to content

Commit

Permalink
fix: Set publicKeyHash to null when not populated
Browse files Browse the repository at this point in the history
  • Loading branch information
sitaram-kalluri committed Dec 16, 2024
1 parent 81434db commit cbfc386
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,14 @@ class MonitorVerbHandler extends AbstractVerbHandler {
"skeEncKeyName": atNotification.atMetadata?.skeEncKeyName,
"skeEncAlgo": atNotification.atMetadata?.skeEncAlgo,
"sharedKeyEnc": atNotification.atMetadata?.sharedKeyEnc,
"pubKeyHash":
jsonEncode(atNotification.atMetadata?.pubKeyHash?.toJson())
};

notification.metadata?.putIfAbsent(
"pubKeyHash",
() => (atNotification.atMetadata?.pubKeyHash != null)
? jsonEncode(atNotification.atMetadata?.pubKeyHash?.toJson())
: null);

await _checkAndSend(notification);
}
}
Expand Down Expand Up @@ -224,9 +228,13 @@ class Notification {
"availableAt": atNotification.atMetadata?.availableAt.toString(),
"expiresAt":
(atNotification.atMetadata?.expiresAt ?? atNotification.expiresAt)
.toString(),
'pubKeyHash': jsonEncode(atNotification.atMetadata?.pubKeyHash?.toJson())
.toString()
};
metadata?.putIfAbsent(
"pubKeyHash",
() => (atNotification.atMetadata?.pubKeyHash != null)
? jsonEncode(atNotification.atMetadata?.pubKeyHash?.toJson())
: null);
}

Map toJson() => {
Expand Down
64 changes: 64 additions & 0 deletions packages/at_secondary_server/test/monitor_verb_test.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'dart:collection';
import 'dart:convert';

import 'package:at_chops/at_chops.dart';
import 'package:at_commons/at_commons.dart';
import 'package:at_persistence_secondary_server/at_persistence_secondary_server.dart';
import 'package:at_secondary/src/connection/inbound/dummy_inbound_connection.dart';
Expand Down Expand Up @@ -105,6 +106,69 @@ void main() {
expect(notificationMap['messageType'], 'MessageType.key');
expect(notificationMap['operation'], 'update');
});

test(
'A test to verify publicKeyHash is written on InboundConnection when populated',
() async {
HashMap<String, String?> verbParams = HashMap<String, String?>();
verbParams[AtConstants.regex] = 'wavi';
inboundConnection.metaData.isAuthenticated = true;
MonitorVerbHandler monitorVerbHandler =
MonitorVerbHandler(secondaryKeyStore);
await monitorVerbHandler.processVerb(
Response(), verbParams, inboundConnection);

var atNotification = (AtNotificationBuilder()
..id = 'abc'
..fromAtSign = '@bob'
..notificationDateTime = DateTime.now()
..toAtSign = alice
..notification = 'phone.wavi'
..type = NotificationType.received
..opType = OperationType.update
..messageType = MessageType.key
..atMetaData = (AtMetaData()
..pubKeyHash =
PublicKeyHash('dummy_hash', HashingAlgoType.sha512.name)))
.build();
await monitorVerbHandler.processAtNotification(atNotification);
inboundConnection.lastWrittenData = inboundConnection.lastWrittenData
?.replaceAll('notification:', '')
.trim();
Map notificationMap = jsonDecode(inboundConnection.lastWrittenData!);
expect(notificationMap['metadata']['pubKeyHash'],
'{"hash":"dummy_hash","hashingAlgo":"sha512"}');
});

test(
'A test to verify publicKeyHash is set to null on InboundConnection when not populated',
() async {
HashMap<String, String?> verbParams = HashMap<String, String?>();
verbParams[AtConstants.regex] = 'wavi';
inboundConnection.metaData.isAuthenticated = true;
MonitorVerbHandler monitorVerbHandler =
MonitorVerbHandler(secondaryKeyStore);
await monitorVerbHandler.processVerb(
Response(), verbParams, inboundConnection);

var atNotification = (AtNotificationBuilder()
..id = 'abc'
..fromAtSign = '@bob'
..notificationDateTime = DateTime.now()
..toAtSign = alice
..notification = 'phone.wavi'
..type = NotificationType.received
..opType = OperationType.update
..messageType = MessageType.key
..atMetaData = (AtMetaData()..encKeyName = 'encKeyName'))
.build();
await monitorVerbHandler.processAtNotification(atNotification);
inboundConnection.lastWrittenData = inboundConnection.lastWrittenData
?.replaceAll('notification:', '')
.trim();
var notificationMap = jsonDecode(inboundConnection.lastWrittenData!);
expect(notificationMap['metadata']['pubKeyHash'], null);
});
tearDown(() async {
await verbTestsTearDown();
AtNotificationCallback.getInstance().callbackMethods.clear();
Expand Down

0 comments on commit cbfc386

Please sign in to comment.