From 8b0cc96de081ba7ae2d260fd83491a92c76a017d Mon Sep 17 00:00:00 2001 From: Mohit Tejani Date: Fri, 24 Nov 2023 19:03:59 +0530 Subject: [PATCH] test: handle unencrypted message when crypto is configured --- pubnub/test/integration/subscribe/_utils.dart | 20 ++++++++++---- .../integration/subscribe/subscribe_test.dart | 27 +++++++++++++++++++ pubnub/test/unit/dx/channel_test.dart | 22 +++++++++++++++ pubnub/test/unit/dx/fixtures/channel.dart | 3 +++ 4 files changed, 67 insertions(+), 5 deletions(-) diff --git a/pubnub/test/integration/subscribe/_utils.dart b/pubnub/test/integration/subscribe/_utils.dart index 874c1582..5f65482d 100644 --- a/pubnub/test/integration/subscribe/_utils.dart +++ b/pubnub/test/integration/subscribe/_utils.dart @@ -36,19 +36,20 @@ class Subscriber { return subscription?.cancel(); } - Future expectMessage(String channel, String message) { + Future expectMessage(String channel, String message, [String? error]) { var actual = queue?.next; - return expectLater( - actual, completion(SubscriptionMessageMatcher(channel, message))); + return expectLater(actual, + completion(SubscriptionMessageMatcher(channel, message, error))); } } class SubscriptionMessageMatcher extends Matcher { final String expectedMessage; final String channel; + String? error; - SubscriptionMessageMatcher(this.channel, this.expectedMessage); + SubscriptionMessageMatcher(this.channel, this.expectedMessage, this.error); @override Description describe(Description description) => @@ -64,5 +65,14 @@ class SubscriptionMessageMatcher extends Matcher { @override bool matches(item, Map matchState) => - item.channel == channel && item.payload == expectedMessage; + item.channel == channel && + item.payload == expectedMessage && + errorMatch(item); + + bool errorMatch(envelope) { + if (!(error?.isEmpty ?? true)) { + return error == (envelope as Envelope).error; + } + return true; + } } diff --git a/pubnub/test/integration/subscribe/subscribe_test.dart b/pubnub/test/integration/subscribe/subscribe_test.dart index fe22283b..e103423a 100644 --- a/pubnub/test/integration/subscribe/subscribe_test.dart +++ b/pubnub/test/integration/subscribe/subscribe_test.dart @@ -90,6 +90,33 @@ void main() { await subscriber.expectMessage(channel, message); }); + test('with crypto configuration and plain message', () async { + var channel = 'test-${DateTime.now().millisecondsSinceEpoch}'; + var message = 'hello pubnub!'; + pubnub = PubNub( + defaultKeyset: Keyset( + subscribeKey: SUBSCRIBE_KEY, + publishKey: PUBLISH_KEY, + userId: UserId('dart-test')), + ); + var pubnubWithCrypto = PubNub( + crypto: + CryptoModule.aesCbcCryptoModule(CipherKey.fromUtf8('cipherKey')), + defaultKeyset: Keyset( + subscribeKey: SUBSCRIBE_KEY, + publishKey: PUBLISH_KEY, + userId: UserId('dart-test'), + ), + ); + subscriber = Subscriber.init(pubnubWithCrypto, SUBSCRIBE_KEY); + subscriber.subscribe(channel); + await Future.delayed(Duration(seconds: 2)); + await pubnub.publish(channel, message); + + await subscriber.expectMessage(channel, message, + 'Can not decrypt the message payload. Please check keyset or crypto configuration.'); + }); + tearDown(() async { await subscriber.cleanup(); await pubnub.unsubscribeAll(); diff --git a/pubnub/test/unit/dx/channel_test.dart b/pubnub/test/unit/dx/channel_test.dart index 845c6343..53798f42 100644 --- a/pubnub/test/unit/dx/channel_test.dart +++ b/pubnub/test/unit/dx/channel_test.dart @@ -7,12 +7,18 @@ part './fixtures/channel.dart'; void main() { PubNub? pubnub; + PubNub? pubnubWithCrypto; group('DX [channel]', () { setUp(() { pubnub = PubNub( defaultKeyset: Keyset( subscribeKey: 'test', publishKey: 'test', uuid: UUID('test')), networking: FakeNetworkingModule()); + pubnubWithCrypto = PubNub( + crypto: CryptoModule.aesCbcCryptoModule(CipherKey.fromUtf8('enigma')), + defaultKeyset: Keyset( + subscribeKey: 'test', publishKey: 'test', uuid: UUID('test')), + networking: FakeNetworkingModule()); }); test('#channel should return an instance of Channel', () { @@ -108,6 +114,22 @@ void main() { expect(history.messages.length, equals(1)); }); + + test('#fetch with crypto configured', () async { + channel = pubnubWithCrypto!.channel('test'); + var history = channel.messages(); + when( + method: 'GET', + path: + 'v2/history/sub-key/test/channel/test?count=100&reverse=true&include_token=true&uuid=test&pnsdk=PubNub-Dart%2F${PubNub.version}', + ).then(status: 200, body: _historyMessagesFetchResponse); + + await history.fetch(); + + expect(history.messages.length, equals(1)); + expect(history.messages[0].error, + equals(_unEncryptedMessageErrorMessage)); + }); }); test('#history should return an instance of PaginatedChannelHistory', () { diff --git a/pubnub/test/unit/dx/fixtures/channel.dart b/pubnub/test/unit/dx/fixtures/channel.dart index 0975a311..6f30132f 100644 --- a/pubnub/test/unit/dx/fixtures/channel.dart +++ b/pubnub/test/unit/dx/fixtures/channel.dart @@ -50,3 +50,6 @@ final _historyMessagesFetchResponse = '''[ 0, 0 ]'''; + +final _unEncryptedMessageErrorMessage = + 'Can not decrypt the message payload. Please check keyset or crypto configuration';