integrate with new ui

This commit is contained in:
Emily 2018-08-08 11:07:09 -07:00
parent 13057804ab
commit bf16e5c8a9
27 changed files with 250 additions and 315 deletions

View file

@ -32,8 +32,8 @@ const S3Storage = proxyquire('../../server/storage/s3', {
});
describe('S3Storage', function() {
it('uses config.s3_bucket', function() {
const s = new S3Storage({ s3_bucket: 'foo' });
it('uses config.s3_buckets', function() {
const s = new S3Storage({ s3_buckets: ['foo', 'bar', 'baz'] }, 0);
assert.equal(s.bucket, 'foo');
});
@ -42,7 +42,7 @@ describe('S3Storage', function() {
s3Stub.headObject = sinon
.stub()
.returns(resolvedPromise({ ContentLength: 123 }));
const s = new S3Storage({ s3_bucket: 'foo' });
const s = new S3Storage({ s3_buckets: ['foo', 'bar', 'baz'] }, 0);
const len = await s.length('x');
assert.equal(len, 123);
sinon.assert.calledWithMatch(s3Stub.headObject, {
@ -54,7 +54,7 @@ describe('S3Storage', function() {
it('throws when id not found', async function() {
const err = new Error();
s3Stub.headObject = sinon.stub().returns(rejectedPromise(err));
const s = new S3Storage({ s3_bucket: 'foo' });
const s = new S3Storage({ s3_buckets: ['foo', 'bar', 'baz'] }, 0);
try {
await s.length('x');
assert.fail();
@ -70,7 +70,7 @@ describe('S3Storage', function() {
s3Stub.getObject = sinon
.stub()
.returns({ createReadStream: () => stream });
const s = new S3Storage({ s3_bucket: 'foo' });
const s = new S3Storage({ s3_buckets: ['foo', 'bar', 'baz'] }, 0);
const result = s.getStream('x');
assert.equal(result, stream);
sinon.assert.calledWithMatch(s3Stub.getObject, {
@ -84,7 +84,7 @@ describe('S3Storage', function() {
it('calls s3.upload', async function() {
const file = { on: sinon.stub() };
s3Stub.upload = sinon.stub().returns(resolvedPromise());
const s = new S3Storage({ s3_bucket: 'foo' });
const s = new S3Storage({ s3_buckets: ['foo', 'bar', 'baz'] }, 0);
await s.set('x', file);
sinon.assert.calledWithMatch(s3Stub.upload, {
Bucket: 'foo',
@ -103,7 +103,7 @@ describe('S3Storage', function() {
promise: () => Promise.reject(err),
abort
});
const s = new S3Storage({ s3_bucket: 'foo' });
const s = new S3Storage({ s3_buckets: ['foo', 'bar', 'baz'] }, 0);
try {
await s.set('x', file);
assert.fail();
@ -119,7 +119,7 @@ describe('S3Storage', function() {
};
const err = new Error();
s3Stub.upload = sinon.stub().returns(rejectedPromise(err));
const s = new S3Storage({ s3_bucket: 'foo' });
const s = new S3Storage({ s3_buckets: ['foo', 'bar', 'baz'] }, 0);
try {
await s.set('x', file);
assert.fail();
@ -132,7 +132,7 @@ describe('S3Storage', function() {
describe('del', function() {
it('calls s3.deleteObject', async function() {
s3Stub.deleteObject = sinon.stub().returns(resolvedPromise(true));
const s = new S3Storage({ s3_bucket: 'foo' });
const s = new S3Storage({ s3_buckets: ['foo', 'bar', 'baz'] }, 0);
const result = await s.del('x');
assert.equal(result, true);
sinon.assert.calledWithMatch(s3Stub.deleteObject, {
@ -145,7 +145,7 @@ describe('S3Storage', function() {
describe('ping', function() {
it('calls s3.headBucket', async function() {
s3Stub.headBucket = sinon.stub().returns(resolvedPromise(true));
const s = new S3Storage({ s3_bucket: 'foo' });
const s = new S3Storage({ s3_buckets: ['foo', 'bar', 'baz'] }, 0);
const result = await s.ping();
assert.equal(result, true);
sinon.assert.calledWithMatch(s3Stub.headBucket, { Bucket: 'foo' });

View file

@ -20,14 +20,17 @@ class MockStorage {
}
}
const expire_seconds = 10;
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'
};
const storage = proxyquire('../../server/storage', {
'../config': {
expire_seconds,
s3_bucket: 'foo',
env: 'development',
redis_host: 'localhost'
},
'../config': config,
'../log': () => {},
'./s3': MockStorage
});
@ -35,39 +38,58 @@ const storage = proxyquire('../../server/storage', {
describe('Storage', function() {
describe('ttl', function() {
it('returns milliseconds remaining', async function() {
await storage.set('x', null, { foo: 'bar' });
const time = 40;
await storage.set('x', null, { foo: 'bar' }, time);
const ms = await storage.ttl('x');
await storage.del('x');
assert.equal(ms, expire_seconds * 1000);
assert.equal(ms, time * 1000);
});
});
describe('length', function() {
it('returns the file size', async function() {
await storage.set('x', null);
const len = await storage.length('x');
assert.equal(len, 12);
});
});
describe('get', function() {
it('returns a stream', function() {
const s = storage.get('x');
it('returns a stream', async function() {
await storage.set('x', null);
const s = await storage.get('x');
assert.equal(s, stream);
});
});
describe('set', function() {
it('sets expiration to config.expire_seconds', async function() {
await storage.set('x', null, { foo: 'bar' });
it('sets expiration to expire time', async function() {
const seconds = 100;
await storage.set('x', null, { foo: 'bar' }, seconds);
const s = await storage.redis.ttlAsync('x');
await storage.del('x');
assert.equal(Math.ceil(s), expire_seconds);
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');
}
});
it('sets metadata', async function() {
const m = { foo: 'bar' };
await storage.set('x', null, m);
const meta = await storage.redis.hgetallAsync('x');
delete meta.bucket;
await storage.del('x');
assert.deepEqual(meta, m);
});
@ -77,6 +99,7 @@ describe('Storage', function() {
describe('setField', function() {
it('works', async function() {
await storage.set('x', null);
storage.setField('x', 'y', 'z');
const z = await storage.redis.hgetAsync('x', 'y');
assert.equal(z, 'z');