fox-send/test/backend/storage-tests.js

142 lines
3.4 KiB
JavaScript
Raw Normal View History

2018-02-06 22:31:18 +00:00
const assert = require('assert');
const proxyquire = require('proxyquire').noCallThru();
const stream = {};
class MockStorage {
length() {
return Promise.resolve(12);
}
getStream() {
return stream;
}
set() {
return Promise.resolve();
}
del() {
return Promise.resolve();
}
ping() {
return Promise.resolve();
}
}
2018-08-08 18:07:09 +00:00
const config = {
default_expire_seconds: 10,
num_of_buckets: 3,
expire_times_seconds: [86400, 604800, 1209600],
s3_buckets: ['foo', 'bar', 'baz'],
env: 'development',
redis_host: 'localhost'
};
2018-02-06 22:31:18 +00:00
const storage = proxyquire('../../server/storage', {
2018-08-08 18:07:09 +00:00
'../config': config,
2018-02-06 22:31:18 +00:00
'../log': () => {},
'./s3': MockStorage
});
describe('Storage', function() {
describe('ttl', function() {
it('returns milliseconds remaining', async function() {
2018-08-08 18:07:09 +00:00
const time = 40;
await storage.set('x', null, { foo: 'bar' }, time);
2018-02-06 22:31:18 +00:00
const ms = await storage.ttl('x');
await storage.del('x');
2018-08-08 18:07:09 +00:00
assert.equal(ms, time * 1000);
2018-02-06 22:31:18 +00:00
});
});
describe('length', function() {
it('returns the file size', async function() {
2018-08-08 18:07:09 +00:00
await storage.set('x', null);
2018-02-06 22:31:18 +00:00
const len = await storage.length('x');
assert.equal(len, 12);
});
});
describe('get', function() {
2018-08-08 18:07:09 +00:00
it('returns a stream', async function() {
await storage.set('x', null);
const s = await storage.get('x');
2018-02-06 22:31:18 +00:00
assert.equal(s, stream);
});
});
describe('set', function() {
2018-08-08 18:07:09 +00:00
it('sets expiration to expire time', async function() {
const seconds = 100;
await storage.set('x', null, { foo: 'bar' }, seconds);
2018-02-06 22:31:18 +00:00
const s = await storage.redis.ttlAsync('x');
await storage.del('x');
2018-08-08 18:07:09 +00:00
assert.equal(Math.ceil(s), seconds);
});
it('puts into right bucket based on expire time', async function() {
for (let i = 0; i < config.num_of_buckets; i++) {
await storage.set(
'x',
null,
{ foo: 'bar' },
config.expire_times_seconds[i]
);
const bucket = await storage.getBucket('x');
assert.equal(bucket, i);
await storage.del('x');
}
2018-02-06 22:31:18 +00:00
});
it('sets metadata', async function() {
const m = { foo: 'bar' };
await storage.set('x', null, m);
const meta = await storage.redis.hgetallAsync('x');
2018-08-08 18:07:09 +00:00
delete meta.bucket;
2018-02-06 22:31:18 +00:00
await storage.del('x');
assert.deepEqual(meta, m);
});
//it('throws when storage fails');
});
describe('setField', function() {
it('works', async function() {
2018-08-08 18:07:09 +00:00
await storage.set('x', null);
2018-02-06 22:31:18 +00:00
storage.setField('x', 'y', 'z');
const z = await storage.redis.hgetAsync('x', 'y');
assert.equal(z, 'z');
await storage.del('x');
});
});
describe('del', function() {
it('works', async function() {
await storage.set('x', null, { foo: 'bar' });
await storage.del('x');
const meta = await storage.metadata('x');
assert.equal(meta, null);
});
});
describe('ping', function() {
it('works', async function() {
await storage.ping();
});
});
describe('metadata', function() {
it('returns all metadata fields', async function() {
const m = {
pwd: true,
dl: 1,
dlimit: 1,
auth: 'foo',
metadata: 'bar',
nonce: 'baz',
owner: 'bmo'
};
await storage.set('x', null, m);
const meta = await storage.metadata('x');
assert.deepEqual(meta, m);
});
});
});