api-tests.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /* global DEFAULTS */
  2. import assert from 'assert';
  3. import Archive from '../../../app/archive';
  4. import * as api from '../../../app/api';
  5. import Keychain from '../../../app/keychain';
  6. const encoder = new TextEncoder();
  7. const plaintext = new Archive([new Blob([encoder.encode('hello world!')])]);
  8. const metadata = {
  9. name: 'test.txt',
  10. type: 'text/plain'
  11. };
  12. describe('API', function() {
  13. describe('websocket upload', function() {
  14. it('returns file info on success', async function() {
  15. const keychain = new Keychain();
  16. const enc = await keychain.encryptStream(plaintext.stream);
  17. const meta = await keychain.encryptMetadata(metadata);
  18. const verifierB64 = await keychain.authKeyB64();
  19. const p = function() {};
  20. const up = api.uploadWs(
  21. enc,
  22. meta,
  23. verifierB64,
  24. DEFAULTS.EXPIRE_SECONDS,
  25. 1,
  26. null,
  27. p
  28. );
  29. const result = await up.result;
  30. assert.ok(result.url);
  31. assert.ok(result.id);
  32. assert.ok(result.ownerToken);
  33. });
  34. it('can be cancelled', async function() {
  35. const keychain = new Keychain();
  36. const enc = await keychain.encryptStream(plaintext.stream);
  37. const meta = await keychain.encryptMetadata(metadata);
  38. const verifierB64 = await keychain.authKeyB64();
  39. const p = function() {};
  40. const up = api.uploadWs(
  41. enc,
  42. meta,
  43. verifierB64,
  44. DEFAULTS.EXPIRE_SECONDS,
  45. null,
  46. p
  47. );
  48. up.cancel();
  49. try {
  50. await up.result;
  51. assert.fail('not cancelled');
  52. } catch (e) {
  53. assert.equal(e.message, '0');
  54. }
  55. });
  56. });
  57. });