UNPKG

7.41 kBJavaScriptView Raw
1/*
2Copyright 2016 OpenMarket Ltd
3Copyright 2017 Vector Creations Ltd
4Copyright 2018-2019 New Vector Ltd
5
6Licensed under the Apache License, Version 2.0 (the "License");
7you may not use this file except in compliance with the License.
8You may obtain a copy of the License at
9
10 http://www.apache.org/licenses/LICENSE-2.0
11
12Unless required by applicable law or agreed to in writing, software
13distributed under the License is distributed on an "AS IS" BASIS,
14WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15See the License for the specific language governing permissions and
16limitations under the License.
17*/
18
19"use strict";
20
21// load olm before the sdk if possible
22import './olm-loader';
23
24import sdk from '..';
25import testUtils from './test-utils';
26import MockHttpBackend from 'matrix-mock-request';
27import expect from 'expect';
28import Promise from 'bluebird';
29import LocalStorageCryptoStore from '../lib/crypto/store/localStorage-crypto-store';
30import logger from '../src/logger';
31
32/**
33 * Wrapper for a MockStorageApi, MockHttpBackend and MatrixClient
34 *
35 * @constructor
36 * @param {string} userId
37 * @param {string} deviceId
38 * @param {string} accessToken
39 *
40 * @param {WebStorage=} sessionStoreBackend a web storage object to use for the
41 * session store. If undefined, we will create a MockStorageApi.
42 * @param {object} options additional options to pass to the client
43 */
44export 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 // expose this so the tests can get to it
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
76TestClient.prototype.toString = function() {
77 return 'TestClient[' + this.userId + ']';
78};
79
80/**
81 * start the client, and wait for it to initialise.
82 *
83 * @return {Promise}
84 */
85TestClient.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 // we let the client do a very basic initial sync, which it needs before
92 // it will upload one-time keys.
93 this.httpBackend.when("GET", "/sync").respond(200, { next_batch: 1 });
94
95 this.client.startClient({
96 // set this so that we can get hold of failed events
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 * stop the client
110 * @return {Promise} Resolves once the mock http backend has finished all pending flushes
111 */
112TestClient.prototype.stop = function() {
113 this.client.stopClient();
114 return this.httpBackend.stop();
115};
116
117/**
118 * Set up expectations that the client will upload device keys.
119 */
120TestClient.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 // we expect this to happen before any one-time keys are uploaded.
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 * If one-time keys have already been uploaded, return them. Otherwise,
138 * set up an expectation that the keys will be uploaded, and wait for
139 * that to happen.
140 *
141 * @returns {Promise} for the one-time keys
142 */
143TestClient.prototype.awaitOneTimeKeyUpload = function() {
144 if (Object.keys(this.oneTimeKeys).length != 0) {
145 // already got one-time keys
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 // this can take ages
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 * Set up expectations that the client will query device keys.
180 *
181 * We check that the query contains each of the users in `response`.
182 *
183 * @param {Object} response response to the query.
184 */
185TestClient.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 * get the uploaded curve25519 device key
202 *
203 * @return {string} base64 device key
204 */
205TestClient.prototype.getDeviceKey = function() {
206 const keyId = 'curve25519:' + this.deviceId;
207 return this.deviceKeys.keys[keyId];
208};
209
210
211/**
212 * get the uploaded ed25519 device key
213 *
214 * @return {string} base64 device key
215 */
216TestClient.prototype.getSigningKey = function() {
217 const keyId = 'ed25519:' + this.deviceId;
218 return this.deviceKeys.keys[keyId];
219};
220
221/**
222 * flush a single /sync request, and wait for the syncing event
223 *
224 * @returns {Promise} promise which completes once the sync has been flushed
225 */
226TestClient.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};