You've already forked matrix-js-sdk
mirror of
https://github.com/matrix-org/matrix-js-sdk.git
synced 2025-08-07 23:02:56 +03:00
client.js: Fix error handling in downloadKeys
Fix a bug in the error handling in downloadKeys: If the http request failed, then the exception would get silently swallowed and the promise would never resolve. Also: tests!
This commit is contained in:
@@ -378,10 +378,12 @@ MatrixClient.prototype.uploadKeys = function(maxKeys, deferred) {
|
|||||||
* store.
|
* store.
|
||||||
* @param {Array} userIds The users to fetch.
|
* @param {Array} userIds The users to fetch.
|
||||||
* @param {bool} forceDownload Always download the keys even if cached.
|
* @param {bool} forceDownload Always download the keys even if cached.
|
||||||
* @return {object} A promise that will resolve when the keys are downloadded.
|
*
|
||||||
|
* @return {object} A promise that will resolve when the keys are downloaded;
|
||||||
|
* resolves to a map userId->deviceId->device info
|
||||||
*/
|
*/
|
||||||
MatrixClient.prototype.downloadKeys = function(userIds, forceDownload) {
|
MatrixClient.prototype.downloadKeys = function(userIds, forceDownload) {
|
||||||
if (!CRYPTO_ENABLED || this.sessionStore === null) {
|
if (this.sessionStore === null) {
|
||||||
return q.reject(new Error("End-to-end encryption disabled"));
|
return q.reject(new Error("End-to-end encryption disabled"));
|
||||||
}
|
}
|
||||||
var stored = {};
|
var stored = {};
|
||||||
@@ -399,12 +401,15 @@ MatrixClient.prototype.downloadKeys = function(userIds, forceDownload) {
|
|||||||
downloadKeys = true;
|
downloadKeys = true;
|
||||||
notStored[userId] = {};
|
notStored[userId] = {};
|
||||||
}
|
}
|
||||||
var deferred = q.defer();
|
|
||||||
if (downloadKeys) {
|
if (!downloadKeys) {
|
||||||
|
return q(stored);
|
||||||
|
}
|
||||||
|
|
||||||
var path = "/keys/query";
|
var path = "/keys/query";
|
||||||
var content = {device_keys: notStored};
|
var content = {device_keys: notStored};
|
||||||
var self = this;
|
var self = this;
|
||||||
this._http.authedRequestWithPrefix(
|
return this._http.authedRequestWithPrefix(
|
||||||
undefined, "POST", path, undefined, content,
|
undefined, "POST", path, undefined, content,
|
||||||
httpApi.PREFIX_UNSTABLE
|
httpApi.PREFIX_UNSTABLE
|
||||||
).then(function(res) {
|
).then(function(res) {
|
||||||
@@ -417,12 +422,8 @@ MatrixClient.prototype.downloadKeys = function(userIds, forceDownload) {
|
|||||||
stored[userId] = res.device_keys[userId];
|
stored[userId] = res.device_keys[userId];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
deferred.resolve(stored);
|
return stored;
|
||||||
});
|
});
|
||||||
} else {
|
|
||||||
deferred.resolve(stored);
|
|
||||||
}
|
|
||||||
return deferred.promise;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@@ -6,10 +6,11 @@ var Room = publicGlobals.Room;
|
|||||||
var MatrixInMemoryStore = publicGlobals.MatrixInMemoryStore;
|
var MatrixInMemoryStore = publicGlobals.MatrixInMemoryStore;
|
||||||
var Filter = publicGlobals.Filter;
|
var Filter = publicGlobals.Filter;
|
||||||
var utils = require("../test-utils");
|
var utils = require("../test-utils");
|
||||||
|
var MockStorageApi = require("../MockStorageApi");
|
||||||
|
|
||||||
describe("MatrixClient", function() {
|
describe("MatrixClient", function() {
|
||||||
var baseUrl = "http://localhost.or.something";
|
var baseUrl = "http://localhost.or.something";
|
||||||
var client, httpBackend, store;
|
var client, httpBackend, store, sessionStore;
|
||||||
var userId = "@alice:localhost";
|
var userId = "@alice:localhost";
|
||||||
var accessToken = "aseukfgwef";
|
var accessToken = "aseukfgwef";
|
||||||
|
|
||||||
@@ -17,12 +18,17 @@ describe("MatrixClient", function() {
|
|||||||
utils.beforeEach(this);
|
utils.beforeEach(this);
|
||||||
httpBackend = new HttpBackend();
|
httpBackend = new HttpBackend();
|
||||||
store = new MatrixInMemoryStore();
|
store = new MatrixInMemoryStore();
|
||||||
|
|
||||||
|
var mockStorage = new MockStorageApi();
|
||||||
|
sessionStore = new sdk.WebStorageSessionStore(mockStorage);
|
||||||
|
|
||||||
sdk.request(httpBackend.requestFn);
|
sdk.request(httpBackend.requestFn);
|
||||||
client = sdk.createClient({
|
client = sdk.createClient({
|
||||||
baseUrl: baseUrl,
|
baseUrl: baseUrl,
|
||||||
userId: userId,
|
userId: userId,
|
||||||
accessToken: accessToken,
|
accessToken: accessToken,
|
||||||
store: store
|
store: store,
|
||||||
|
sessionStore: sessionStore,
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -173,4 +179,45 @@ describe("MatrixClient", function() {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
describe("downloadKeys", function() {
|
||||||
|
it("should do an HTTP request and then store the keys", function(done) {
|
||||||
|
var borisKeys = {dev1: {a: 1}};
|
||||||
|
var chazKeys = {dev2: {a: 2}};
|
||||||
|
|
||||||
|
httpBackend.when("POST", "/keys/query").check(function(req) {
|
||||||
|
expect(req.data).toEqual({device_keys: {boris: {}, chaz: {}}});
|
||||||
|
}).respond(200, {
|
||||||
|
device_keys: {
|
||||||
|
boris: borisKeys,
|
||||||
|
chaz: chazKeys,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
client.downloadKeys(["boris", "chaz"]).then(function(res) {
|
||||||
|
expect(res).toEqual({
|
||||||
|
boris: borisKeys,
|
||||||
|
chaz: chazKeys
|
||||||
|
});
|
||||||
|
}).catch(utils.failTest).done(done);
|
||||||
|
|
||||||
|
httpBackend.flush();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should return a rejected promise if the request fails", function(done) {
|
||||||
|
httpBackend.when("POST", "/keys/query").respond(400);
|
||||||
|
|
||||||
|
var exceptionThrown;
|
||||||
|
client.downloadKeys(["bottom"]).then(function() {
|
||||||
|
fail("download didn't fail");
|
||||||
|
}, function(err) {
|
||||||
|
exceptionThrown = err;
|
||||||
|
}).then(function() {
|
||||||
|
expect(exceptionThrown).toBeTruthy();
|
||||||
|
}).catch(utils.failTest).done(done);
|
||||||
|
|
||||||
|
httpBackend.flush();
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
Reference in New Issue
Block a user