1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 | "use strict";
|
20 |
|
21 |
|
22 | import './olm-loader';
|
23 |
|
24 | import sdk from '..';
|
25 | import testUtils from './test-utils';
|
26 | import MockHttpBackend from 'matrix-mock-request';
|
27 | import expect from 'expect';
|
28 | import Promise from 'bluebird';
|
29 | import LocalStorageCryptoStore from '../lib/crypto/store/localStorage-crypto-store';
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 | export default function TestClient(
|
44 | userId, deviceId, accessToken, sessionStoreBackend, options,
|
45 | ) {
|
46 | this.userId = userId;
|
47 | this.deviceId = deviceId;
|
48 |
|
49 | if (sessionStoreBackend === undefined) {
|
50 | sessionStoreBackend = new testUtils.MockStorageApi();
|
51 | }
|
52 | const sessionStore = new sdk.WebStorageSessionStore(sessionStoreBackend);
|
53 |
|
54 | this.httpBackend = new MockHttpBackend();
|
55 |
|
56 | options = Object.assign({
|
57 | baseUrl: "http://" + userId + ".test.server",
|
58 | userId: userId,
|
59 | accessToken: accessToken,
|
60 | deviceId: deviceId,
|
61 | sessionStore: sessionStore,
|
62 | request: this.httpBackend.requestFn,
|
63 | }, options);
|
64 | if (!options.cryptoStore) {
|
65 |
|
66 | this.cryptoStore = new LocalStorageCryptoStore(sessionStoreBackend);
|
67 | options.cryptoStore = this.cryptoStore;
|
68 | }
|
69 | this.client = sdk.createClient(options);
|
70 |
|
71 | this.deviceKeys = null;
|
72 | this.oneTimeKeys = {};
|
73 | }
|
74 |
|
75 | TestClient.prototype.toString = function() {
|
76 | return 'TestClient[' + this.userId + ']';
|
77 | };
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 | TestClient.prototype.start = function() {
|
85 | console.log(this + ': starting');
|
86 | this.httpBackend.when("GET", "/pushrules").respond(200, {});
|
87 | this.httpBackend.when("POST", "/filter").respond(200, { filter_id: "fid" });
|
88 | this.expectDeviceKeyUpload();
|
89 |
|
90 |
|
91 |
|
92 | this.httpBackend.when("GET", "/sync").respond(200, { next_batch: 1 });
|
93 |
|
94 | this.client.startClient({
|
95 |
|
96 | pendingEventOrdering: 'detached',
|
97 | });
|
98 |
|
99 | return Promise.all([
|
100 | this.httpBackend.flushAllExpected(),
|
101 | testUtils.syncPromise(this.client),
|
102 | ]).then(() => {
|
103 | console.log(this + ': started');
|
104 | });
|
105 | };
|
106 |
|
107 |
|
108 |
|
109 |
|
110 |
|
111 | TestClient.prototype.stop = function() {
|
112 | this.client.stopClient();
|
113 | return this.httpBackend.stop();
|
114 | };
|
115 |
|
116 |
|
117 |
|
118 |
|
119 | TestClient.prototype.expectDeviceKeyUpload = function() {
|
120 | const self = this;
|
121 | this.httpBackend.when("POST", "/keys/upload").respond(200, function(path, content) {
|
122 | expect(content.one_time_keys).toBe(undefined);
|
123 | expect(content.device_keys).toBeTruthy();
|
124 |
|
125 | console.log(self + ': received device keys');
|
126 |
|
127 | expect(Object.keys(self.oneTimeKeys).length).toEqual(0);
|
128 |
|
129 | self.deviceKeys = content.device_keys;
|
130 | return {one_time_key_counts: {signed_curve25519: 0}};
|
131 | });
|
132 | };
|
133 |
|
134 |
|
135 |
|
136 |
|
137 |
|
138 |
|
139 |
|
140 |
|
141 |
|
142 | TestClient.prototype.awaitOneTimeKeyUpload = function() {
|
143 | if (Object.keys(this.oneTimeKeys).length != 0) {
|
144 |
|
145 | return Promise.resolve(this.oneTimeKeys);
|
146 | }
|
147 |
|
148 | this.httpBackend.when("POST", "/keys/upload")
|
149 | .respond(200, (path, content) => {
|
150 | expect(content.device_keys).toBe(undefined);
|
151 | expect(content.one_time_keys).toBe(undefined);
|
152 | return {one_time_key_counts: {
|
153 | signed_curve25519: Object.keys(this.oneTimeKeys).length,
|
154 | }};
|
155 | });
|
156 |
|
157 | this.httpBackend.when("POST", "/keys/upload")
|
158 | .respond(200, (path, content) => {
|
159 | expect(content.device_keys).toBe(undefined);
|
160 | expect(content.one_time_keys).toBeTruthy();
|
161 | expect(content.one_time_keys).toNotEqual({});
|
162 | console.log('%s: received %i one-time keys', this,
|
163 | Object.keys(content.one_time_keys).length);
|
164 | this.oneTimeKeys = content.one_time_keys;
|
165 | return {one_time_key_counts: {
|
166 | signed_curve25519: Object.keys(this.oneTimeKeys).length,
|
167 | }};
|
168 | });
|
169 |
|
170 |
|
171 | return this.httpBackend.flush('/keys/upload', 2, 1000).then((flushed) => {
|
172 | expect(flushed).toEqual(2);
|
173 | return this.oneTimeKeys;
|
174 | });
|
175 | };
|
176 |
|
177 |
|
178 |
|
179 |
|
180 |
|
181 |
|
182 |
|
183 |
|
184 | TestClient.prototype.expectKeyQuery = function(response) {
|
185 | this.httpBackend.when('POST', '/keys/query').respond(
|
186 | 200, (path, content) => {
|
187 | Object.keys(response.device_keys).forEach((userId) => {
|
188 | expect(content.device_keys[userId]).toEqual(
|
189 | {},
|
190 | "Expected key query for " + userId + ", got " +
|
191 | Object.keys(content.device_keys),
|
192 | );
|
193 | });
|
194 | return response;
|
195 | });
|
196 | };
|
197 |
|
198 |
|
199 |
|
200 |
|
201 |
|
202 |
|
203 |
|
204 | TestClient.prototype.getDeviceKey = function() {
|
205 | const keyId = 'curve25519:' + this.deviceId;
|
206 | return this.deviceKeys.keys[keyId];
|
207 | };
|
208 |
|
209 |
|
210 |
|
211 |
|
212 |
|
213 |
|
214 |
|
215 | TestClient.prototype.getSigningKey = function() {
|
216 | const keyId = 'ed25519:' + this.deviceId;
|
217 | return this.deviceKeys.keys[keyId];
|
218 | };
|
219 |
|
220 |
|
221 |
|
222 |
|
223 |
|
224 |
|
225 | TestClient.prototype.flushSync = function() {
|
226 | console.log(`${this}: flushSync`);
|
227 | return Promise.all([
|
228 | this.httpBackend.flush('/sync', 1),
|
229 | testUtils.syncPromise(this.client),
|
230 | ]).then(() => {
|
231 | console.log(`${this}: flushSync completed`);
|
232 | });
|
233 | };
|