workflow-tests.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. import assert from 'assert';
  2. import FileSender from '../../../app/fileSender';
  3. import FileReceiver from '../../../app/fileReceiver';
  4. const headless = /Headless/.test(navigator.userAgent);
  5. const noSave = !headless; // only run the saveFile code if headless
  6. // FileSender uses a File in real life but a Blob works for testing
  7. const blob = new Blob(['hello world!'], { type: 'text/plain' });
  8. blob.name = 'test.txt';
  9. describe('Upload / Download flow', function() {
  10. it('can only download once by default', async function() {
  11. const fs = new FileSender(blob);
  12. const file = await fs.upload();
  13. const fr = new FileReceiver({
  14. secretKey: file.toJSON().secretKey,
  15. id: file.id,
  16. nonce: file.keychain.nonce,
  17. requiresPassword: false
  18. });
  19. await fr.getMetadata();
  20. await fr.download(noSave);
  21. try {
  22. await fr.download(noSave);
  23. assert.fail('downloaded again');
  24. } catch (e) {
  25. assert.equal(e.message, '404');
  26. }
  27. });
  28. it('downloads with the correct password', async function() {
  29. const fs = new FileSender(blob);
  30. const file = await fs.upload();
  31. await file.setPassword('magic');
  32. const fr = new FileReceiver({
  33. secretKey: file.toJSON().secretKey,
  34. id: file.id,
  35. url: file.url,
  36. nonce: file.keychain.nonce,
  37. requiresPassword: true,
  38. password: 'magic'
  39. });
  40. await fr.getMetadata();
  41. await fr.download(noSave);
  42. assert.equal(fr.state, 'complete');
  43. });
  44. it('blocks invalid passwords from downloading', async function() {
  45. const fs = new FileSender(blob);
  46. const file = await fs.upload();
  47. await file.setPassword('magic');
  48. const fr = new FileReceiver({
  49. secretKey: file.toJSON().secretKey,
  50. id: file.id,
  51. url: file.url,
  52. nonce: file.keychain.nonce,
  53. requiresPassword: true,
  54. password: 'password'
  55. });
  56. try {
  57. await fr.getMetadata();
  58. assert.fail('got metadata with bad password');
  59. } catch (e) {
  60. assert.equal(e.message, '401');
  61. }
  62. try {
  63. // We can't decrypt without IV from metadata
  64. // but let's try to download anyway
  65. await fr.download();
  66. assert.fail('downloaded file with bad password');
  67. } catch (e) {
  68. assert.equal(e.message, '401');
  69. }
  70. });
  71. it('retries a bad nonce', async function() {
  72. const fs = new FileSender(blob);
  73. const file = await fs.upload();
  74. const fr = new FileReceiver({
  75. secretKey: file.toJSON().secretKey,
  76. id: file.id,
  77. nonce: null, // oops
  78. requiresPassword: false
  79. });
  80. await fr.getMetadata();
  81. assert.equal(fr.fileInfo.name, blob.name);
  82. });
  83. it('can cancel the upload', async function() {
  84. const fs = new FileSender(blob);
  85. const up = fs.upload();
  86. fs.cancel(); // before encrypting
  87. try {
  88. await up;
  89. assert.fail('not cancelled');
  90. } catch (e) {
  91. assert.equal(e.message, '0');
  92. }
  93. fs.reset();
  94. fs.once('encrypting', () => fs.cancel());
  95. try {
  96. await fs.upload();
  97. assert.fail('not cancelled');
  98. } catch (e) {
  99. assert.equal(e.message, '0');
  100. }
  101. fs.reset();
  102. fs.once('progress', () => fs.cancel());
  103. try {
  104. await fs.upload();
  105. assert.fail('not cancelled');
  106. } catch (e) {
  107. assert.equal(e.message, '0');
  108. }
  109. });
  110. it('can cancel the download', async function() {
  111. const fs = new FileSender(blob);
  112. const file = await fs.upload();
  113. const fr = new FileReceiver({
  114. secretKey: file.toJSON().secretKey,
  115. id: file.id,
  116. nonce: file.keychain.nonce,
  117. requiresPassword: false
  118. });
  119. await fr.getMetadata();
  120. fr.once('progress', () => fr.cancel());
  121. try {
  122. await fr.download(noSave);
  123. assert.fail('not cancelled');
  124. } catch (e) {
  125. assert.equal(e.message, '0');
  126. }
  127. });
  128. it('can increase download count on download', async function() {
  129. const fs = new FileSender(blob);
  130. const file = await fs.upload();
  131. const fr = new FileReceiver({
  132. secretKey: file.toJSON().secretKey,
  133. id: file.id,
  134. nonce: file.keychain.nonce,
  135. requiresPassword: false
  136. });
  137. await fr.getMetadata();
  138. await fr.download(noSave);
  139. await file.updateDownloadCount();
  140. assert.equal(file.dtotal, 1);
  141. });
  142. it('does not increase download count when download cancelled', async function() {
  143. const fs = new FileSender(blob);
  144. const file = await fs.upload();
  145. const fr = new FileReceiver({
  146. secretKey: file.toJSON().secretKey,
  147. id: file.id,
  148. nonce: file.keychain.nonce,
  149. requiresPassword: false
  150. });
  151. await fr.getMetadata();
  152. fr.once('progress', () => fr.cancel());
  153. try {
  154. await fr.download(noSave);
  155. assert.fail('not cancelled');
  156. } catch (e) {
  157. await file.updateDownloadCount();
  158. assert.equal(file.dtotal, 0);
  159. }
  160. });
  161. it('can allow multiple downloads', async function() {
  162. const fs = new FileSender(blob);
  163. const file = await fs.upload();
  164. const fr = new FileReceiver({
  165. secretKey: file.toJSON().secretKey,
  166. id: file.id,
  167. nonce: file.keychain.nonce,
  168. requiresPassword: false
  169. });
  170. await file.changeLimit(2);
  171. await fr.getMetadata();
  172. await fr.download(noSave);
  173. await file.updateDownloadCount();
  174. assert.equal(file.dtotal, 1);
  175. await fr.download(noSave);
  176. await file.updateDownloadCount();
  177. assert.equal(file.dtotal, 2);
  178. try {
  179. await fr.download(noSave);
  180. assert.fail('downloaded too many times');
  181. } catch (e) {
  182. assert.equal(e.message, '404');
  183. }
  184. });
  185. it('can delete the file before download', async function() {
  186. const fs = new FileSender(blob);
  187. const file = await fs.upload();
  188. const fr = new FileReceiver({
  189. secretKey: file.toJSON().secretKey,
  190. id: file.id,
  191. nonce: file.keychain.nonce,
  192. requiresPassword: false
  193. });
  194. await file.del();
  195. try {
  196. await fr.getMetadata();
  197. assert.fail('file still exists');
  198. } catch (e) {
  199. assert.equal(e.message, '404');
  200. }
  201. });
  202. });