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