2018-02-06 23:31:18 +01: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 20:07:09 +02:00
|
|
|
const config = {
|
2018-08-09 23:49:52 +02:00
|
|
|
s3_bucket: 'foo',
|
|
|
|
default_expire_seconds: 20,
|
|
|
|
expire_times_seconds: [10, 20, 30],
|
2018-08-08 20:07:09 +02:00
|
|
|
env: 'development',
|
2020-07-27 20:18:52 +02:00
|
|
|
redis_host: 'mock'
|
2018-08-08 20:07:09 +02:00
|
|
|
};
|
|
|
|
|
2018-02-06 23:31:18 +01:00
|
|
|
const storage = proxyquire('../../server/storage', {
|
2018-08-08 20:07:09 +02:00
|
|
|
'../config': config,
|
2018-02-06 23:31:18 +01:00
|
|
|
'../log': () => {},
|
|
|
|
'./s3': MockStorage
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('Storage', function() {
|
|
|
|
describe('ttl', function() {
|
|
|
|
it('returns milliseconds remaining', async function() {
|
2018-08-08 20:07:09 +02:00
|
|
|
const time = 40;
|
|
|
|
await storage.set('x', null, { foo: 'bar' }, time);
|
2018-02-06 23:31:18 +01:00
|
|
|
const ms = await storage.ttl('x');
|
|
|
|
await storage.del('x');
|
2018-08-08 20:07:09 +02:00
|
|
|
assert.equal(ms, time * 1000);
|
2018-02-06 23:31:18 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('length', function() {
|
|
|
|
it('returns the file size', async function() {
|
|
|
|
const len = await storage.length('x');
|
|
|
|
assert.equal(len, 12);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('get', function() {
|
2018-08-08 20:07:09 +02:00
|
|
|
it('returns a stream', async function() {
|
2020-07-27 20:18:52 +02:00
|
|
|
const { stream: s } = await storage.get('x');
|
2018-02-06 23:31:18 +01:00
|
|
|
assert.equal(s, stream);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('set', function() {
|
2018-08-08 20:07:09 +02:00
|
|
|
it('sets expiration to expire time', async function() {
|
|
|
|
const seconds = 100;
|
|
|
|
await storage.set('x', null, { foo: 'bar' }, seconds);
|
2018-02-06 23:31:18 +01:00
|
|
|
const s = await storage.redis.ttlAsync('x');
|
|
|
|
await storage.del('x');
|
2018-08-08 20:07:09 +02:00
|
|
|
assert.equal(Math.ceil(s), seconds);
|
|
|
|
});
|
|
|
|
|
2018-08-09 23:49:52 +02:00
|
|
|
it('adds right prefix based on expire time', async function() {
|
2018-09-25 00:08:39 +02:00
|
|
|
await storage.set('x', null, { foo: 'bar' }, 300);
|
2020-07-25 20:22:57 +02:00
|
|
|
const { filePath: path_x } = await storage.getPrefixedInfo('x');
|
2018-09-25 00:08:39 +02:00
|
|
|
assert.equal(path_x, '1-x');
|
2018-08-09 23:49:52 +02:00
|
|
|
await storage.del('x');
|
|
|
|
|
2018-09-25 00:08:39 +02:00
|
|
|
await storage.set('y', null, { foo: 'bar' }, 86400);
|
2020-07-25 20:22:57 +02:00
|
|
|
const { filePath: path_y } = await storage.getPrefixedInfo('y');
|
2018-09-25 00:08:39 +02:00
|
|
|
assert.equal(path_y, '1-y');
|
2018-08-09 23:49:52 +02:00
|
|
|
await storage.del('y');
|
|
|
|
|
2018-09-25 00:08:39 +02:00
|
|
|
await storage.set('z', null, { foo: 'bar' }, 86400 * 7);
|
2020-07-25 20:22:57 +02:00
|
|
|
const { filePath: path_z } = await storage.getPrefixedInfo('z');
|
2018-09-25 00:08:39 +02:00
|
|
|
assert.equal(path_z, '7-z');
|
2018-08-09 23:49:52 +02:00
|
|
|
await storage.del('z');
|
2018-02-06 23:31:18 +01: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-09 23:49:52 +02:00
|
|
|
delete meta.prefix;
|
2018-02-06 23:31:18 +01:00
|
|
|
await storage.del('x');
|
|
|
|
assert.deepEqual(meta, m);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('setField', function() {
|
|
|
|
it('works', async function() {
|
2018-08-08 20:07:09 +02:00
|
|
|
await storage.set('x', null);
|
2018-02-06 23:31:18 +01: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 = {
|
2020-07-27 20:18:52 +02:00
|
|
|
id: 'a1',
|
|
|
|
pwd: 0,
|
2018-02-06 23:31:18 +01:00
|
|
|
dl: 1,
|
|
|
|
dlimit: 1,
|
2020-07-27 20:18:52 +02:00
|
|
|
fxa: 1,
|
2018-02-06 23:31:18 +01:00
|
|
|
auth: 'foo',
|
|
|
|
metadata: 'bar',
|
|
|
|
nonce: 'baz',
|
|
|
|
owner: 'bmo'
|
|
|
|
};
|
|
|
|
await storage.set('x', null, m);
|
|
|
|
const meta = await storage.metadata('x');
|
2020-07-27 20:18:52 +02:00
|
|
|
assert.deepEqual(
|
|
|
|
{ ...meta, storage: 'excluded' },
|
|
|
|
{
|
|
|
|
...m,
|
|
|
|
dead: false,
|
|
|
|
flagged: false,
|
|
|
|
dlToken: 0,
|
|
|
|
fxa: true,
|
|
|
|
pwd: false,
|
|
|
|
storage: 'excluded'
|
|
|
|
}
|
|
|
|
);
|
2018-02-06 23:31:18 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|