storage-tests.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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: 'mock'
  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 { stream: 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 { filePath: path_x } = await storage.getPrefixedInfo('x');
  66. assert.equal(path_x, '1-x');
  67. await storage.del('x');
  68. await storage.set('y', null, { foo: 'bar' }, 86400);
  69. const { filePath: path_y } = await storage.getPrefixedInfo('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 { filePath: path_z } = await storage.getPrefixedInfo('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. id: 'a1',
  112. pwd: 0,
  113. dl: 1,
  114. dlimit: 1,
  115. fxa: 1,
  116. auth: 'foo',
  117. metadata: 'bar',
  118. nonce: 'baz',
  119. owner: 'bmo'
  120. };
  121. await storage.set('x', null, m);
  122. const meta = await storage.metadata('x');
  123. assert.deepEqual(
  124. { ...meta, storage: 'excluded' },
  125. {
  126. ...m,
  127. dead: false,
  128. flagged: false,
  129. dlToken: 0,
  130. fxa: true,
  131. pwd: false,
  132. storage: 'excluded'
  133. }
  134. );
  135. });
  136. });
  137. });