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