1
0
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:
Richard van der Hoff
2016-06-02 20:04:29 +01:00
parent f38f983a46
commit b18a4ee16b
2 changed files with 75 additions and 27 deletions

View File

@@ -378,10 +378,12 @@ MatrixClient.prototype.uploadKeys = function(maxKeys, deferred) {
* store.
* @param {Array} userIds The users to fetch.
* @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) {
if (!CRYPTO_ENABLED || this.sessionStore === null) {
if (this.sessionStore === null) {
return q.reject(new Error("End-to-end encryption disabled"));
}
var stored = {};
@@ -399,12 +401,15 @@ MatrixClient.prototype.downloadKeys = function(userIds, forceDownload) {
downloadKeys = true;
notStored[userId] = {};
}
var deferred = q.defer();
if (downloadKeys) {
if (!downloadKeys) {
return q(stored);
}
var path = "/keys/query";
var content = {device_keys: notStored};
var self = this;
this._http.authedRequestWithPrefix(
return this._http.authedRequestWithPrefix(
undefined, "POST", path, undefined, content,
httpApi.PREFIX_UNSTABLE
).then(function(res) {
@@ -417,12 +422,8 @@ MatrixClient.prototype.downloadKeys = function(userIds, forceDownload) {
stored[userId] = res.device_keys[userId];
}
}
deferred.resolve(stored);
return stored;
});
} else {
deferred.resolve(stored);
}
return deferred.promise;
};
/**

View File

@@ -6,10 +6,11 @@ var Room = publicGlobals.Room;
var MatrixInMemoryStore = publicGlobals.MatrixInMemoryStore;
var Filter = publicGlobals.Filter;
var utils = require("../test-utils");
var MockStorageApi = require("../MockStorageApi");
describe("MatrixClient", function() {
var baseUrl = "http://localhost.or.something";
var client, httpBackend, store;
var client, httpBackend, store, sessionStore;
var userId = "@alice:localhost";
var accessToken = "aseukfgwef";
@@ -17,12 +18,17 @@ describe("MatrixClient", function() {
utils.beforeEach(this);
httpBackend = new HttpBackend();
store = new MatrixInMemoryStore();
var mockStorage = new MockStorageApi();
sessionStore = new sdk.WebStorageSessionStore(mockStorage);
sdk.request(httpBackend.requestFn);
client = sdk.createClient({
baseUrl: baseUrl,
userId: userId,
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();
});
});
});