List.test.ts 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. import {
  2. isLightboxClosed,
  3. isPageReady,
  4. isLightboxShown,
  5. expectToastShown,
  6. isElementGone,
  7. isElementThere,
  8. } from '../lib/isReady';
  9. import { ElementHandle } from 'puppeteer';
  10. import * as fs from 'fs';
  11. const BASE_URL = process.env.BASE_URL ?? 'http://localhost:8080/',
  12. DESTINATION_FONT_FILE = '/tmp/BlackAndWhitePicture-Regular.ttf';
  13. describe('WebDAV.js', () => {
  14. describe('List', () => {
  15. it('should be possible to preview items', async () => {
  16. // Wait for page JS to replace page contents
  17. await isPageReady(page, BASE_URL);
  18. await (
  19. [
  20. [
  21. '[data-full-path="/0.jpg"]',
  22. async (lightbox: ElementHandle) => {
  23. await expect(await lightbox.$('img')).toBeTruthy();
  24. },
  25. ],
  26. [
  27. '[data-full-path="/BlackAndWhitePicture-Regular.ttf"]',
  28. async (lightbox: ElementHandle) => {
  29. await expect(await lightbox.$('img')).toBeFalsy();
  30. await expect(await lightbox.$$('style')).toHaveLength(1);
  31. await expect(await lightbox.$$('h1')).toHaveLength(1);
  32. await expect(await lightbox.$$('p')).toHaveLength(4);
  33. },
  34. ],
  35. [
  36. '[data-full-path="/dummy.pdf"]',
  37. async (lightbox: ElementHandle) => {
  38. await expect(await lightbox.$('iframe')).toBeTruthy();
  39. },
  40. ],
  41. [
  42. '[data-full-path="/style.css"]',
  43. async (lightbox: ElementHandle) => {
  44. await expect(await lightbox.$('pre.language-css')).toBeTruthy();
  45. },
  46. ],
  47. [
  48. '[data-full-path="/video.mp4"]',
  49. async (lightbox: ElementHandle) => {
  50. await expect(await lightbox.$('video[autoplay]')).toBeTruthy();
  51. },
  52. ],
  53. ] as [string, (lightbox: ElementHandle) => Promise<void>][]
  54. ).reduce(
  55. async (
  56. previous: Promise<void>,
  57. [selector, expectation]
  58. ): Promise<void> => {
  59. await previous;
  60. await page.click(selector);
  61. await expectation(await isLightboxShown(page));
  62. await isLightboxClosed(page);
  63. await page.waitForTimeout(500);
  64. },
  65. Promise.resolve(null)
  66. );
  67. });
  68. it('should be possible to create a new navigable directory, rename it and delete it', async () => {
  69. await isPageReady(page, BASE_URL);
  70. page.once(
  71. 'dialog',
  72. async (dialog) => await dialog.accept('new-directory')
  73. );
  74. await page.click('.create-directory');
  75. await isElementThere(page, '[data-full-path="/new-directory/"]');
  76. await expectToastShown(
  77. page,
  78. `'new-directory' has been created.`,
  79. 'success'
  80. );
  81. await page.click('[data-full-path="/new-directory/"]');
  82. await page.waitForTimeout(200);
  83. await expect(await page.$$('main ul li')).toHaveLength(1);
  84. await expect(await page.$('[data-full-path="/"]')).toBeTruthy();
  85. await page.click('[data-full-path="/"]');
  86. await page.waitForTimeout(200);
  87. await expect(await page.$$('main ul li')).toHaveLength(24);
  88. await page.click('[data-full-path="/new-directory/"] .rename');
  89. await page.waitForFunction(() =>
  90. document.activeElement.matches(
  91. '[data-full-path="/new-directory/"] input[type="text"]'
  92. )
  93. );
  94. await page.keyboard.down('Control');
  95. await page.keyboard.press('Backspace');
  96. await page.keyboard.up('Control');
  97. await page.type(
  98. '[data-full-path="/new-directory/"] input[type="text"]',
  99. 'folder'
  100. );
  101. await page.keyboard.press('Enter');
  102. await expectToastShown(
  103. page,
  104. `'new-directory' successfully renamed to 'new-folder'.`,
  105. 'success'
  106. );
  107. await isElementThere(page, '[data-full-path="/new-folder/"]');
  108. page.once('dialog', async (dialog) => await dialog.accept());
  109. await page.click('[data-full-path="/new-folder/"] .delete');
  110. await isElementGone(page, '[data-full-path="/new-folder/"]');
  111. await expectToastShown(page, `'new-folder' has been deleted.`, 'success');
  112. });
  113. it('should show expected errors', async () => {
  114. await isPageReady(page, BASE_URL);
  115. await page.click('[data-full-path="/inaccessible-dir/"]');
  116. await expectToastShown(
  117. page,
  118. 'HEAD /inaccessible-dir/ failed: Forbidden (403)',
  119. 'error'
  120. );
  121. await page.click('[data-full-path="/inaccessible-file"]');
  122. await expectToastShown(
  123. page,
  124. 'GET /inaccessible-file failed: Forbidden (403)',
  125. 'error'
  126. );
  127. await page.click('[data-full-path="/inaccessible-image.jpg"]');
  128. await expectToastShown(
  129. page,
  130. 'HEAD /inaccessible-image.jpg failed: Forbidden (403)',
  131. 'error'
  132. );
  133. await page.click('[data-full-path="/inaccessible-text-file.txt"]');
  134. await expectToastShown(
  135. page,
  136. 'GET /inaccessible-text-file.txt failed: Forbidden (403)',
  137. 'error'
  138. );
  139. });
  140. it('should be possible to download a file', async () => {
  141. await isPageReady(page, BASE_URL);
  142. await expect(() => fs.accessSync(DESTINATION_FONT_FILE)).toThrow();
  143. await page
  144. .target()
  145. .createCDPSession()
  146. .then((client) =>
  147. client.send('Page.setDownloadBehavior', {
  148. behavior: 'allow',
  149. downloadPath: '/tmp',
  150. })
  151. );
  152. await page.click(
  153. '[data-full-path="/BlackAndWhitePicture-Regular.ttf"] [download]'
  154. );
  155. // wait for the file to download
  156. await page.waitForTimeout(400);
  157. await expect(() => fs.accessSync(DESTINATION_FONT_FILE)).not.toThrow();
  158. fs.rmSync(DESTINATION_FONT_FILE);
  159. });
  160. it('should be possible to upload a file', async () => {
  161. await isPageReady(page, BASE_URL);
  162. const elementHandle = (await page.$(
  163. 'input[type=file]'
  164. )) as ElementHandle<HTMLInputElement>;
  165. await elementHandle.uploadFile('./package.json');
  166. await expectToastShown(
  167. page,
  168. `'package.json' has been successfully uploaded.`,
  169. 'success'
  170. );
  171. await page.click('[data-full-path="/package.json"]');
  172. await page.once('dialog', async (dialog) => await dialog.accept());
  173. await page.click('[data-full-path="/package.json"] .delete');
  174. await isElementGone(page, '[data-full-path="/package.json"]');
  175. await expectToastShown(
  176. page,
  177. `'package.json' has been deleted.`,
  178. 'success'
  179. );
  180. });
  181. });
  182. beforeAll(async () => {
  183. try {
  184. fs.accessSync(DESTINATION_FONT_FILE);
  185. fs.rmSync(DESTINATION_FONT_FILE);
  186. } catch (e) {
  187. // we don't need to do anything here
  188. }
  189. });
  190. });