Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test that we can decrypt history messages #1933

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions test/browser/modular.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,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,
);
}
lawrence-forooghian marked this conversation as resolved.
Show resolved Hide resolved

for (const clientClassConfig of [
{ clientClass: BaseRest, isRealtime: false },
{
Expand All @@ -522,6 +555,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 @@ -773,6 +773,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
Loading