Skip to content

Commit

Permalink
Merge pull request #1933 from ably/add-encrypted-history-tests
Browse files Browse the repository at this point in the history
Test that we can decrypt history messages
  • Loading branch information
lawrence-forooghian authored Jan 6, 2025
2 parents 5b9e975 + 9b6a416 commit 1d6483a
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 0 deletions.
49 changes: 49 additions & 0 deletions test/browser/modular.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,39 @@ function registerAblyModularTests(Helper) {
);
}

async function testIsAbleToDecryptHistoryMessages(helper, clientClassConfig) {
const clientOptions = helper.ablyClientOptions();

const client = new clientClassConfig.clientClass({
...clientOptions,
plugins: {
...clientClassConfig.additionalPlugins,
FetchRequest,
Crypto,
},
});

await (clientClassConfig.isRealtime ? monitorConnectionThenCloseAndFinish : async (helper, op) => await op())(
helper,
async () => {
const channelName = 'encrypted_history',
messageText = 'Test message';

const key = await generateRandomKey();

const channel = client.channels.get(channelName, { cipher: { key: key } });
await channel.publish('event0', messageText);
let items;
await helper.waitFor(async () => {
items = (await channel.history()).items;
return items.length > 0;
}, 10_000);
expect(items[0].data).to.equal(messageText);
},
client,
);
}

for (const clientClassConfig of [
{ clientClass: BaseRest, isRealtime: false },
{
Expand All @@ -567,6 +600,22 @@ function registerAblyModularTests(Helper) {
});
});
}

for (const clientClassConfig of [
{ clientClass: BaseRest, isRealtime: false },
{
clientClass: BaseRealtime,
additionalPlugins: { WebSocketTransport, Rest },
isRealtime: true,
},
]) {
describe(clientClassConfig.clientClass.name, () => {
/** @nospec */
it('is able to decrypt history messages', async function () {
await testIsAbleToDecryptHistoryMessages(this.test.helper, clientClassConfig);
});
});
}
});
});

Expand Down
9 changes: 9 additions & 0 deletions test/common/modules/shared_helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,15 @@ define([
dumpPrivateApiUsage() {
privateApiRecorder.dump();
}

async waitFor(condition, remaining) {
const success = await condition();
if (success || remaining <= 0) {
return success;
}
await this.setTimeoutAsync(100);
return this.waitFor(condition, remaining - 100);
}
}

SharedHelper.testOnAllTransports.skip = function (thisInDescribe, name, testFn) {
Expand Down
25 changes: 25 additions & 0 deletions test/realtime/crypto.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -771,6 +771,31 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async
});
});

/**
* @spec RSL5a
*/
it('encrypted history', async function () {
if (!Crypto) {
done(new Error('Encryption not supported'));
return;
}

const helper = this.test.helper,
rest = helper.AblyRest(),
channelName = 'encrypted_history',
messageText = 'Test message';

const key = await Crypto.generateRandomKey();
const channel = rest.channels.get(channelName, { cipher: { key: key } });
await channel.publish('event0', messageText);
let items;
await helper.waitFor(async () => {
items = (await channel.history()).items;
return items.length > 0;
}, 10_000);
expect(items[0].data).to.equal(messageText);
});

/**
* Connect twice to the service, using different cipher keys.
* Publish an encrypted message on that channel using
Expand Down

0 comments on commit 1d6483a

Please sign in to comment.