Add getDeviceIds to axolotlstore

And add tests for getDeviceIds and removeAllSessions
This commit is contained in:
lilia 2015-04-21 19:13:13 -07:00
parent 121671c99f
commit f413f03a6b
4 changed files with 102 additions and 3 deletions

View file

@ -214,6 +214,19 @@
});
});
},
getDeviceIds: function(number) {
if (number === null || number === undefined)
throw new Error("Tried to put session for undefined/null key");
return new Promise(function(resolve) {
var contact = new Contact({id: number});
contact.fetch().always(function() {
var sessions = contact.get('sessions') || {};
resolve(_.keys(sessions).map(function(n) {
return parseInt(n);
}));
});
});
},
removeAllSessions: function(number) {
if (number === null || number === undefined)
throw new Error("Tried to put session for undefined/null key");

View file

@ -89,5 +89,26 @@ AxolotlStore.prototype = {
return new Promise(function(resolve) {
resolve(this.put('session' + identifier, record));
}.bind(this));
}
},
removeAllSessions: function(identifier) {
return new Promise(function(resolve) {
for (key in this.store) {
if (key.match(RegExp('^session' + identifier.replace('\+','\\\+') + '.+'))) {
delete this.store[key];
}
}
resolve();
}.bind(this));
},
getDeviceIds: function(identifier) {
return new Promise(function(resolve) {
var deviceIds = [];
for (key in this.store) {
if (key.match(RegExp('^session' + identifier.replace('\+','\\\+') + '.+'))) {
deviceIds.push(parseInt(key.split('.')[1]));
}
}
resolve(deviceIds);
}.bind(this));
}
};

View file

@ -95,7 +95,7 @@ describe("AxolotlStore", function() {
});
});
promise.then(function() {
return Promise.all(devices.map(store.getSession)).then(function(records) {
return Promise.all(devices.map(store.getSession.bind(store))).then(function(records) {
for (var i in records) {
assert.strictEqual(records[i], testRecord + devices[i]);
};
@ -115,7 +115,7 @@ describe("AxolotlStore", function() {
});
promise.then(function() {
return store.removeAllSessions(identifier).then(function(record) {
return Promise.all(devices.map(store.getSession)).then(function(records) {
return Promise.all(devices.map(store.getSession.bind(store))).then(function(records) {
for (var i in records) {
assert.isUndefined(records[i]);
};
@ -123,4 +123,26 @@ describe("AxolotlStore", function() {
});
}).then(done,done);
});
it('returns deviceIds for a number', function(done) {
var testRecord = "an opaque string";
var devices = [1, 2, 3].map(function(deviceId) {
return [identifier, deviceId].join('.');
});
var promise = Promise.resolve();
devices.forEach(function(encodedNumber) {
promise = promise.then(function() {
return store.putSession(encodedNumber, testRecord + encodedNumber)
});
});
promise.then(function() {
return store.getDeviceIds(identifier).then(function(deviceIds) {
assert.sameMembers(deviceIds, [1, 2, 3]);
});
}).then(done,done);
});
it('returns empty array for a number with no device ids', function(done) {
return store.getDeviceIds('foo').then(function(deviceIds) {
assert.sameMembers(deviceIds,[]);
}).then(done,done);
});
});

View file

@ -90,4 +90,47 @@ describe("AxolotlStore", function() {
});
}).then(done,done);
});
it('removes all sessions for a number', function(done) {
var testRecord = "an opaque string";
var devices = [1, 2, 3].map(function(deviceId) {
return [identifier, deviceId].join('.');
});
var promise = Promise.resolve();
devices.forEach(function(encodedNumber) {
promise = promise.then(function() {
return store.putSession(encodedNumber, testRecord + encodedNumber)
});
});
promise.then(function() {
return store.removeAllSessions(identifier).then(function(record) {
return Promise.all(devices.map(store.getSession.bind(store))).then(function(records) {
for (var i in records) {
assert.isUndefined(records[i]);
};
});
});
}).then(done,done);
});
it('returns deviceIds for a number', function(done) {
var testRecord = "an opaque string";
var devices = [1, 2, 3].map(function(deviceId) {
return [identifier, deviceId].join('.');
});
var promise = Promise.resolve();
devices.forEach(function(encodedNumber) {
promise = promise.then(function() {
return store.putSession(encodedNumber, testRecord + encodedNumber)
});
});
promise.then(function() {
return store.getDeviceIds(identifier).then(function(deviceIds) {
assert.sameMembers(deviceIds, [1, 2, 3]);
});
}).then(done,done);
});
it('returns empty array for a number with no device ids', function(done) {
return store.getDeviceIds('foo').then(function(deviceIds) {
assert.sameMembers(deviceIds,[]);
}).then(done,done);
});
});