diff --git a/.gitignore b/.gitignore index c811d395f..43b3c5e9d 100644 --- a/.gitignore +++ b/.gitignore @@ -32,6 +32,9 @@ test/test.js ts/**/*.js ts/protobuf/*.d.ts ts/**/*.js.map +test/ts/**/*.js.map +test/ts/**/*.js + # Swapfiles **/*.swp diff --git a/test/ts/keychange_listener_test.ts b/test/ts/keychange_listener_test.ts new file mode 100644 index 000000000..cb63ca72e --- /dev/null +++ b/test/ts/keychange_listener_test.ts @@ -0,0 +1,86 @@ +import { assert } from 'chai'; + +const { libsignal, Whisper, ConversationController } = window; + +import { mapDispatchToProps } from '../../ts/state/actions.js'; + +describe('KeyChangeListener', () => { + const phoneNumberWithKeyChange = '+13016886524'; // nsa + const address = new libsignal.SignalProtocolAddress( + phoneNumberWithKeyChange, + 1 + ); + const oldKey = libsignal.crypto.getRandomBytes(33); + const newKey = libsignal.crypto.getRandomBytes(33); + let store: any; + + beforeEach(async () => { + store = new window.SignalProtocolStore(); + await store.hydrateCaches(); + Whisper.KeyChangeListener.init(store); + return store.saveIdentity(address.toString(), oldKey); + }); + + afterEach(() => { + return store.removeIdentityKey(phoneNumberWithKeyChange); + }); + + describe('When we have a conversation with this contact', () => { + // this.timeout(2000); + + let convo: any; + before(async () => { + convo = ConversationController.dangerouslyCreateAndAdd({ + id: phoneNumberWithKeyChange, + type: 'private', + }); + await window.Signal.Data.saveConversation(convo.attributes, { + Conversation: Whisper.Conversation, + }); + }); + + after(async () => { + await convo.destroyMessages(); + await window.Signal.Data.saveConversation(convo.id); + }); + + it('generates a key change notice in the private conversation with this contact', done => { + convo.once('newmessage', async () => { + await convo.fetchMessages(); + const message = convo.messageCollection.at(0); + assert.strictEqual(message.get('type'), 'keychange'); + done(); + }); + store.saveIdentity(address.toString(), newKey); + }); + }); + + describe('When we have a group with this contact', () => { + let convo: any; + + before(async () => { + convo = ConversationController.dangerouslyCreateAndAdd({ + id: 'groupId', + type: 'group', + members: [phoneNumberWithKeyChange], + }); + await window.Signal.Data.saveConversation(convo.attributes, { + Conversation: Whisper.Conversation, + }); + }); + after(async () => { + await convo.destroyMessages(); + await window.Signal.Data.saveConversation(convo.id); + }); + + it('generates a key change notice in the group conversation with this contact', done => { + convo.once('newmessage', async () => { + await convo.fetchMessages(); + const message = convo.messageCollection.at(0); + assert.strictEqual(message.get('type'), 'keychange'); + done(); + }); + store.saveIdentity(address.toString(), newKey); + }); + }); +});