storage-tests.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. const assert = require('assert');
  2. const proxyquire = require('proxyquire').noCallThru();
  3. const stream = {};
  4. class MockStorage {
  5. length() {
  6. return Promise.resolve(12);
  7. }
  8. getStream() {
  9. return stream;
  10. }
  11. set() {
  12. return Promise.resolve();
  13. }
  14. del() {
  15. return Promise.resolve();
  16. }
  17. ping() {
  18. return Promise.resolve();
  19. }
  20. }
  21. const config = {
  22. s3_bucket: 'foo',
  23. default_expire_seconds: 20,
  24. expire_times_seconds: [10, 20, 30],
  25. env: 'development',
  26. redis_host: 'localhost'
  27. };
  28. const storage = proxyquire('../../server/storage', {
  29. '../config': config,
  30. '../log': () => {},
  31. './s3': MockStorage
  32. });
  33. describe('Storage', function() {
  34. describe('ttl', function() {
  35. it('returns milliseconds remaining', async function() {
  36. const time = 40;
  37. await storage.set('x', null, { foo: 'bar' }, time);
  38. const ms = await storage.ttl('x');
  39. await storage.del('x');
  40. assert.equal(ms, time * 1000);
  41. });
  42. });
  43. describe('length', function() {
  44. it('returns the file size', async function() {
  45. const len = await storage.length('x');
  46. assert.equal(len, 12);
  47. });
  48. });
  49. describe('get', function() {
  50. it('returns a stream', async function() {
  51. const s = await storage.get('x');
  52. assert.equal(s, stream);
  53. });
  54. });
  55. describe('set', function() {
  56. it('sets expiration to expire time', async function() {
  57. const seconds = 100;
  58. await storage.set('x', null, { foo: 'bar' }, seconds);
  59. const s = await storage.redis.ttlAsync('x');
  60. await storage.del('x');
  61. assert.equal(Math.ceil(s), seconds);
  62. });
  63. it('adds right prefix based on expire time', async function() {
  64. await storage.set('x', null, { foo: 'bar' }, 300);
  65. const path_x = await storage.getPrefixedId('x');
  66. assert.equal(path_x, '1-x');
  67. await storage.del('x');
  68. await storage.set('y', null, { foo: 'bar' }, 86400);
  69. const path_y = await storage.getPrefixedId('y');
  70. assert.equal(path_y, '1-y');
  71. await storage.del('y');
  72. await storage.set('z', null, { foo: 'bar' }, 86400 * 7);
  73. const path_z = await storage.getPrefixedId('z');
  74. assert.equal(path_z, '7-z');
  75. await storage.del('z');
  76. });
  77. it('sets metadata', async function() {
  78. const m = { foo: 'bar' };
  79. await storage.set('x', null, m);
  80. const meta = await storage.redis.hgetallAsync('x');
  81. delete meta.prefix;
  82. await storage.del('x');
  83. assert.deepEqual(meta, m);
  84. });
  85. });
  86. describe('setField', function() {
  87. it('works', async function() {
  88. await storage.set('x', null);
  89. storage.setField('x', 'y', 'z');
  90. const z = await storage.redis.hgetAsync('x', 'y');
  91. assert.equal(z, 'z');
  92. await storage.del('x');
  93. });
  94. });
  95. describe('del', function() {
  96. it('works', async function() {
  97. await storage.set('x', null, { foo: 'bar' });
  98. await storage.del('x');
  99. const meta = await storage.metadata('x');
  100. assert.equal(meta, null);
  101. });
  102. });
  103. describe('ping', function() {
  104. it('works', async function() {
  105. await storage.ping();
  106. });
  107. });
  108. describe('metadata', function() {
  109. it('returns all metadata fields', async function() {
  110. const m = {
  111. pwd: true,
  112. dl: 1,
  113. dlimit: 1,
  114. auth: 'foo',
  115. metadata: 'bar',
  116. nonce: 'baz',
  117. owner: 'bmo'
  118. };
  119. await storage.set('x', null, m);
  120. const meta = await storage.metadata('x');
  121. assert.deepEqual(meta, m);
  122. });
  123. });
  124. });