1 | /*
|
2 | Copyright 2017 New Vector Ltd
|
3 |
|
4 | Licensed under the Apache License, Version 2.0 (the "License");
|
5 | you may not use this file except in compliance with the License.
|
6 | You may obtain a copy of the License at
|
7 |
|
8 | http://www.apache.org/licenses/LICENSE-2.0
|
9 |
|
10 | Unless required by applicable law or agreed to in writing, software
|
11 | distributed under the License is distributed on an "AS IS" BASIS,
|
12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
13 | See the License for the specific language governing permissions and
|
14 | limitations under the License.
|
15 | */
|
16 |
|
17 | import sdk from '../..';
|
18 | const MatrixEvent = sdk.MatrixEvent;
|
19 |
|
20 | import testUtils from '../test-utils';
|
21 |
|
22 | import expect from 'expect';
|
23 | import Promise from 'bluebird';
|
24 | import logger from '../../src/logger';
|
25 |
|
26 | describe("MatrixEvent", () => {
|
27 | beforeEach(function() {
|
28 | testUtils.beforeEach(this); // eslint-disable-line babel/no-invalid-this
|
29 | });
|
30 |
|
31 | describe(".attemptDecryption", () => {
|
32 | let encryptedEvent;
|
33 |
|
34 | beforeEach(() => {
|
35 | encryptedEvent = new MatrixEvent({
|
36 | id: 'test_encrypted_event',
|
37 | type: 'm.room.encrypted',
|
38 | content: {
|
39 | ciphertext: 'secrets',
|
40 | },
|
41 | });
|
42 | });
|
43 |
|
44 | it('should retry decryption if a retry is queued', () => {
|
45 | let callCount = 0;
|
46 |
|
47 | let prom2;
|
48 |
|
49 | const crypto = {
|
50 | decryptEvent: function() {
|
51 | ++callCount;
|
52 | logger.log(`decrypt: ${callCount}`);
|
53 | if (callCount == 1) {
|
54 | // schedule a second decryption attempt while
|
55 | // the first one is still running.
|
56 | prom2 = encryptedEvent.attemptDecryption(crypto);
|
57 |
|
58 | const error = new Error("nope");
|
59 | error.name = 'DecryptionError';
|
60 | return Promise.reject(error);
|
61 | } else {
|
62 | expect(prom2.isFulfilled()).toBe(
|
63 | false, 'second attemptDecryption resolved too soon');
|
64 |
|
65 | return Promise.resolve({
|
66 | clearEvent: {
|
67 | type: 'm.room.message',
|
68 | },
|
69 | });
|
70 | }
|
71 | },
|
72 | };
|
73 |
|
74 | return encryptedEvent.attemptDecryption(crypto).then(() => {
|
75 | expect(callCount).toEqual(2);
|
76 | expect(encryptedEvent.getType()).toEqual('m.room.message');
|
77 |
|
78 | // make sure the second attemptDecryption resolves
|
79 | return prom2;
|
80 | });
|
81 | });
|
82 | });
|
83 | });
|