roomAddToList (roomName, listName, values, { bypassPermissions }) { return this.state.getRoom(roomName).then(room => { return Promise.join( room.addToList(this.userName, listName, values, bypassPermissions), room.roomState.accessListsUpdatesGet(), (userNames, update) => { if (update) { this.transport.emitToChannel( roomName, 'roomAccessListAdded', roomName, listName, values) } return this.removeRoomUsers(roomName, userNames) }) }).return() }
test('errors if integrity errors', t => { return BB.join( put(CACHE, KEY, CONTENT, { integrity: 'sha1-BaDDigEST' }).catch(err => { t.equal(err.code, 'EINTEGRITY', 'got error from bad integrity') }) ) })
getNotificationsInfo (author, bypassPermissions) { return this.checkRead(author, bypassPermissions) .then(() => Promise.join( this.roomState.userlistUpdatesGet(), this.roomState.accessListsUpdatesGet(), (enableUserlistUpdates, enableAccessListsUpdates) => ({ enableUserlistUpdates, enableAccessListsUpdates }))) }
tearDown() { return P.join(this.system.destroy(), this.listenerSystem.destroy()); }
test('Promise.join', function (t) { t.plan(8) twice(function () { var trans = ins.startTransaction() var p1 = resolved('p1') var p2 = resolved('p2') var p3 = resolved('p3') Promise.join(p1, p2, p3, function (a, b, c) { t.strictEqual(a, 'p1') t.strictEqual(b, 'p2') t.strictEqual(c, 'p3') t.strictEqual(ins.currentTransaction.id, trans.id) }) }) })
it('should update an instance heartbeat', function (done) { this.timeout(4000) this.slow(2000) chatService = startService({ heartbeatRate: 500 }) const start = _.now() setTimeout(() => { Promise.join( chatService.getInstanceHeartbeat(chatService.instanceUID), chatService.getInstanceHeartbeat(), (ts1, ts2) => { expect(ts1).within(start, start + 2000) expect(ts2).within(start, start + 2000) done() }) }, 1000) })
test('read: errors if content size does not match size option', function (t) { const CONTENT = Buffer.from('foobarbaz') const INTEGRITY = ssri.fromData(CONTENT) const fixture = new Tacks(CacheContent({ [INTEGRITY]: CONTENT.slice(3) // invalid contents! })) fixture.create(CACHE) const stream = read.readStream(CACHE, INTEGRITY, { size: CONTENT.length }) stream.on('end', function () { throw new Error('end was called even though stream errored') }) return BB.join( finished(stream).catch({ code: 'EBADSIZE' }, err => err), read(CACHE, INTEGRITY, { size: CONTENT.length }).catch({ code: 'EBADSIZE' }, err => err), (streamErr, bulkErr) => { t.equal(streamErr.code, 'EBADSIZE', 'stream got the right error') t.equal(bulkErr.code, 'EBADSIZE', 'bulk got the right error') } ) })
test('read: errors if content missing', function (t) { const stream = read.stream(CACHE, 'sha512-whatnot') stream.on('data', function (data) { throw new Error('unexpected data: ' + JSON.stringify(data)) }) stream.on('end', function () { throw new Error('end was called even though stream errored') }) return BB.join( finished(stream).catch({ code: 'ENOENT' }, err => err), read(CACHE, 'sha512-whatnot').catch({ code: 'ENOENT' }, err => err), (streamErr, bulkErr) => { t.equal(streamErr.code, 'ENOENT', 'stream got the right error') t.equal(bulkErr.code, 'ENOENT', 'bulk got the right error') } ) })
test('provides a utility that does resource disposal on tmp', t => { return tmp.withTmp(CACHE, dir => { return fs.statAsync(dir).then(stat => { t.ok(stat.isDirectory(), 'path points to an existing directory') }).then(() => dir) }).then(dir => { return BB.join( fs.statAsync(dir).then(() => { throw new Error('expected fail') }).catch({ code: 'ENOENT' }, () => {}), fs.statAsync(path.join(CACHE, 'tmp')), (nope, yes) => { t.notOk(nope, 'tmp subdir removed') t.ok(yes.isDirectory(), 'tmp parent dir left intact') } ) }) })
test('writes a file with last verification time', t => { return verify(CACHE).then(() => { return BB.join( verify.lastRun(CACHE), fs.readFileAsync( path.join(CACHE, '_lastverified'), 'utf8' ).then(data => { return new Date(parseInt(data)) }), (fromLastRun, fromFile) => { t.equal(+fromLastRun, +fromFile, 'last verified was writen') } ) }) })
test('signals error if error writing to cache', t => { return BB.join( put(CACHE, KEY, CONTENT, { size: 2 }).then(() => { throw new Error('expected error') }).catch(err => err), pipe(fromString(CONTENT), put.stream(CACHE, KEY, { size: 2 })).then(() => { throw new Error('expected error') }).catch(err => err), (bulkErr, streamErr) => { t.equal(bulkErr.code, 'EBADSIZE', 'got error from bulk write') t.equal(streamErr.code, 'EBADSIZE', 'got error from stream write') } ) })
test('read: errors if content fails checksum', function (t) { const CONTENT = Buffer.from('foobarbaz') const INTEGRITY = ssri.fromData(CONTENT) const fixture = new Tacks(CacheContent({ [INTEGRITY]: CONTENT.slice(3) // invalid contents! })) fixture.create(CACHE) const stream = read.readStream(CACHE, INTEGRITY) stream.on('end', function () { throw new Error('end was called even though stream errored') }) return BB.join( finished(stream).catch({ code: 'EINTEGRITY' }, err => err), read(CACHE, INTEGRITY).catch({ code: 'EINTEGRITY' }, err => err), (streamErr, bulkErr) => { t.equal(streamErr.code, 'EINTEGRITY', 'stream got the right error') t.equal(bulkErr.code, 'EINTEGRITY', 'bulk got the right error') } ) })
test('cleans up contents of tmp dir', t => { const tmpFile = path.join(CACHE, 'tmp', 'x') const misc = path.join(CACHE, 'y') return mockCache().then(() => { return BB.join( fs.writeFileAsync(tmpFile, ''), fs.writeFileAsync(misc, ''), () => verify(CACHE) ) }).then(() => { return BB.join( fs.statAsync(tmpFile).catch({ code: 'ENOENT' }, e => e), fs.statAsync(misc), (err, stat) => { t.equal(err.code, 'ENOENT', 'tmp file was blown away') t.ok(stat, 'misc file was not touched') } ) }) })
roomRemoveFromList (roomName, listName, values, { bypassPermissions }) { return this.state.getRoom(roomName).then(room => { return Promise.join( room.removeFromList(this.userName, listName, values, bypassPermissions), room.roomState.accessListsUpdatesGet(), (userNames, update) => { if (update) { this.transport.emitToChannel( roomName, 'roomAccessListRemoved', roomName, listName, values) } return this.removeRoomUsers(roomName, userNames) }) }).return() }
roomSetWhitelistMode (roomName, mode, { bypassPermissions }) { return this.state.getRoom(roomName).then(room => { return Promise.join( room.changeMode(this.userName, mode, bypassPermissions), room.roomState.accessListsUpdatesGet(), ([userNames, mode], update) => { if (update) { this.transport.emitToChannel( roomName, 'roomModeChanged', roomName, mode) } return this.removeRoomUsers(roomName, userNames) }) }).return() }