Skip to content

Commit

Permalink
test: handle unencrypted message when crypto is configured
Browse files Browse the repository at this point in the history
  • Loading branch information
mohitpubnub committed Nov 24, 2023
1 parent 0429921 commit 8b0cc96
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 5 deletions.
20 changes: 15 additions & 5 deletions pubnub/test/integration/subscribe/_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,20 @@ class Subscriber {
return subscription?.cancel();
}

Future<void> expectMessage(String channel, String message) {
Future<void> 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) =>
Expand All @@ -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;
}
}
27 changes: 27 additions & 0 deletions pubnub/test/integration/subscribe/subscribe_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
22 changes: 22 additions & 0 deletions pubnub/test/unit/dx/channel_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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', () {
Expand Down Expand Up @@ -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', () {
Expand Down
3 changes: 3 additions & 0 deletions pubnub/test/unit/dx/fixtures/channel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,6 @@ final _historyMessagesFetchResponse = '''[
0,
0
]''';

final _unEncryptedMessageErrorMessage =
'Can not decrypt the message payload. Please check keyset or crypto configuration';

0 comments on commit 8b0cc96

Please sign in to comment.