Skip to content

Commit 1d6483a

Browse files
Merge pull request #1933 from ably/add-encrypted-history-tests
Test that we can decrypt history messages
2 parents 5b9e975 + 9b6a416 commit 1d6483a

File tree

3 files changed

+83
-0
lines changed

3 files changed

+83
-0
lines changed

test/browser/modular.test.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -552,6 +552,39 @@ function registerAblyModularTests(Helper) {
552552
);
553553
}
554554

555+
async function testIsAbleToDecryptHistoryMessages(helper, clientClassConfig) {
556+
const clientOptions = helper.ablyClientOptions();
557+
558+
const client = new clientClassConfig.clientClass({
559+
...clientOptions,
560+
plugins: {
561+
...clientClassConfig.additionalPlugins,
562+
FetchRequest,
563+
Crypto,
564+
},
565+
});
566+
567+
await (clientClassConfig.isRealtime ? monitorConnectionThenCloseAndFinish : async (helper, op) => await op())(
568+
helper,
569+
async () => {
570+
const channelName = 'encrypted_history',
571+
messageText = 'Test message';
572+
573+
const key = await generateRandomKey();
574+
575+
const channel = client.channels.get(channelName, { cipher: { key: key } });
576+
await channel.publish('event0', messageText);
577+
let items;
578+
await helper.waitFor(async () => {
579+
items = (await channel.history()).items;
580+
return items.length > 0;
581+
}, 10_000);
582+
expect(items[0].data).to.equal(messageText);
583+
},
584+
client,
585+
);
586+
}
587+
555588
for (const clientClassConfig of [
556589
{ clientClass: BaseRest, isRealtime: false },
557590
{
@@ -567,6 +600,22 @@ function registerAblyModularTests(Helper) {
567600
});
568601
});
569602
}
603+
604+
for (const clientClassConfig of [
605+
{ clientClass: BaseRest, isRealtime: false },
606+
{
607+
clientClass: BaseRealtime,
608+
additionalPlugins: { WebSocketTransport, Rest },
609+
isRealtime: true,
610+
},
611+
]) {
612+
describe(clientClassConfig.clientClass.name, () => {
613+
/** @nospec */
614+
it('is able to decrypt history messages', async function () {
615+
await testIsAbleToDecryptHistoryMessages(this.test.helper, clientClassConfig);
616+
});
617+
});
618+
}
570619
});
571620
});
572621

test/common/modules/shared_helper.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,15 @@ define([
483483
dumpPrivateApiUsage() {
484484
privateApiRecorder.dump();
485485
}
486+
487+
async waitFor(condition, remaining) {
488+
const success = await condition();
489+
if (success || remaining <= 0) {
490+
return success;
491+
}
492+
await this.setTimeoutAsync(100);
493+
return this.waitFor(condition, remaining - 100);
494+
}
486495
}
487496

488497
SharedHelper.testOnAllTransports.skip = function (thisInDescribe, name, testFn) {

test/realtime/crypto.test.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -771,6 +771,31 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async
771771
});
772772
});
773773

774+
/**
775+
* @spec RSL5a
776+
*/
777+
it('encrypted history', async function () {
778+
if (!Crypto) {
779+
done(new Error('Encryption not supported'));
780+
return;
781+
}
782+
783+
const helper = this.test.helper,
784+
rest = helper.AblyRest(),
785+
channelName = 'encrypted_history',
786+
messageText = 'Test message';
787+
788+
const key = await Crypto.generateRandomKey();
789+
const channel = rest.channels.get(channelName, { cipher: { key: key } });
790+
await channel.publish('event0', messageText);
791+
let items;
792+
await helper.waitFor(async () => {
793+
items = (await channel.history()).items;
794+
return items.length > 0;
795+
}, 10_000);
796+
expect(items[0].data).to.equal(messageText);
797+
});
798+
774799
/**
775800
* Connect twice to the service, using different cipher keys.
776801
* Publish an encrypted message on that channel using

0 commit comments

Comments
 (0)