123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300 |
- import { ConsoleMessage, ElementHandle } from 'puppeteer';
- import {
- isLightboxClosed,
- isPageReady,
- isLightboxShown,
- expectToastShown,
- isElementGone,
- isElementThere,
- } from '../lib/isReady';
- import trailingSlash from '../../src/lib/trailingSlash';
- import * as fs from 'fs';
- const BASE_URL = process.env.BASE_URL ?? 'http://localhost:8080/',
- DESTINATION_FONT_FILE = '/tmp/BlackAndWhitePicture-Regular.ttf';
- if (process.argv.some((flag) => flag.match(/-debug/))) {
- page.on('console', (message: ConsoleMessage): void => {
- if (message.type() === 'error') {
- console.error(message.text());
- const trace = message.stackTrace();
- if (trace.length) {
- console.error(
- [
- `Stack trace:`,
- ...trace.map(
- (message): string =>
- message.url +
- ' (' +
- message.lineNumber +
- ':' +
- message.columnNumber +
- ')'
- ),
- ].join('\n')
- );
- }
- return;
- }
- if (message.type() === 'warning') {
- console.warn(message.text());
- return;
- }
- if (message.type() === 'info') {
- console.info(message.text());
- return;
- }
- console.log(message.text());
- });
- }
- describe('WebDAV.js', () => {
- describe('List', () => {
- it('should be possible to preview items', async () => {
- // Wait for page JS to replace page contents
- await isPageReady(page, BASE_URL);
- await (
- [
- [
- '[data-full-path="/0.jpg"]',
- async (lightbox: ElementHandle) => {
- await expect(await lightbox.$('img')).toBeTruthy();
- },
- ],
- [
- '[data-full-path="/BlackAndWhitePicture-Regular.ttf"]',
- async (lightbox: ElementHandle) => {
- await expect(await lightbox.$('img')).toBeFalsy();
- await expect(await lightbox.$$('style')).toHaveLength(1);
- await expect(await lightbox.$$('h1')).toHaveLength(1);
- await expect(await lightbox.$$('p')).toHaveLength(4);
- },
- ],
- [
- '[data-full-path="/dummy.pdf"]',
- async (lightbox: ElementHandle) => {
- await expect(await lightbox.$('iframe')).toBeTruthy();
- },
- ],
- [
- '[data-full-path="/style.css"]',
- async (lightbox: ElementHandle) => {
- await expect(await lightbox.$('pre.language-css')).toBeTruthy();
- },
- ],
- [
- '[data-full-path="/video.mp4"]',
- async (lightbox: ElementHandle) => {
- await expect(await lightbox.$('video[autoplay]')).toBeTruthy();
- },
- ],
- ] as [string, (lightbox: ElementHandle) => Promise<void>][]
- ).reduce(
- async (
- previous: Promise<void>,
- [selector, expectation]
- ): Promise<void> => {
- await previous;
- await page.click(selector);
- await expectation(await isLightboxShown(page));
- await isLightboxClosed(page);
- await page.waitForTimeout(500);
- },
- Promise.resolve(null)
- );
- });
- it('should be possible to create a new navigable directory, rename it and delete it', async () => {
- await isPageReady(page, BASE_URL);
- page.once(
- 'dialog',
- async (dialog) => await dialog.accept('new-directory')
- );
- await page.click('.create-directory');
- await isElementThere(page, '[data-full-path="/new-directory/"]');
- await expectToastShown(
- page,
- `'new-directory' has been created.`,
- 'success'
- );
- await page.click('[data-full-path="/new-directory/"]');
- await page.waitForTimeout(200);
- await expect(await page.$$('main ul li')).toHaveLength(1);
- await expect(await page.$('[data-full-path="/"]')).toBeTruthy();
- await page.click('[data-full-path="/"]');
- await page.waitForTimeout(200);
- await expect(await page.$$('main ul li')).toHaveLength(24);
- await page.click('[data-full-path="/new-directory/"] .rename');
- await page.waitForFunction(() =>
- document.activeElement.matches(
- '[data-full-path="/new-directory/"] input[type="text"]'
- )
- );
- await page.keyboard.down('Control');
- await page.keyboard.press('Backspace');
- await page.keyboard.up('Control');
- await page.type(
- '[data-full-path="/new-directory/"] input[type="text"]',
- 'folder'
- );
- await page.keyboard.press('Enter');
- await expectToastShown(
- page,
- `'new-directory' successfully renamed to 'new-folder'.`,
- 'success'
- );
- await isElementThere(page, '[data-full-path="/new-folder/"]');
- page.once('dialog', async (dialog) => await dialog.accept());
- await page.click('[data-full-path="/new-folder/"] .delete');
- await isElementGone(page, '[data-full-path="/new-folder/"]');
- await expectToastShown(page, `'new-folder' has been deleted.`, 'success');
- });
- it('should show expected errors', async () => {
- await isPageReady(page, BASE_URL);
- await page.click('[data-full-path="/inaccessible-dir/"]');
- await expectToastShown(
- page,
- `HEAD ${trailingSlash(
- BASE_URL
- )}inaccessible-dir/ failed: Forbidden (403)`,
- 'error'
- );
- await page.click('[data-full-path="/inaccessible-file"]');
- await expectToastShown(
- page,
- `HEAD ${trailingSlash(
- BASE_URL
- )}inaccessible-file failed: Forbidden (403)`,
- 'error'
- );
- await page.click('[data-full-path="/inaccessible-image.jpg"]');
- await expectToastShown(
- page,
- `HEAD ${trailingSlash(
- BASE_URL
- )}inaccessible-image.jpg failed: Forbidden (403)`,
- 'error'
- );
- await page.click('[data-full-path="/inaccessible-text-file.txt"]');
- await expectToastShown(
- page,
- `HEAD ${trailingSlash(
- BASE_URL
- )}inaccessible-text-file.txt failed: Forbidden (403)`,
- 'error'
- );
- });
- it('should be possible to download a file', async () => {
- await isPageReady(page, BASE_URL);
- await expect(() => fs.accessSync(DESTINATION_FONT_FILE)).toThrow();
- await page
- .target()
- .createCDPSession()
- .then((client) =>
- client.send('Page.setDownloadBehavior', {
- behavior: 'allow',
- downloadPath: '/tmp',
- })
- );
- await page.click(
- '[data-full-path="/BlackAndWhitePicture-Regular.ttf"] [download]'
- );
- // wait for the file to download
- await page.waitForTimeout(400);
- await expect(() => fs.accessSync(DESTINATION_FONT_FILE)).not.toThrow();
- fs.rmSync(DESTINATION_FONT_FILE);
- });
- it('should be possible to upload a file', async () => {
- await isPageReady(page, BASE_URL);
- const elementHandle = (await page.$(
- 'input[type=file]'
- )) as ElementHandle<HTMLInputElement>;
- await elementHandle.uploadFile('./package.json');
- await expectToastShown(
- page,
- `'package.json' has been successfully uploaded.`,
- 'success'
- );
- await page.click('[data-full-path="/package.json"]');
- await page.once('dialog', async (dialog) => {
- await dialog.accept();
- });
- await page.click('[data-full-path="/package.json"] .delete');
- await isElementGone(page, '[data-full-path="/package.json"]');
- await expectToastShown(
- page,
- `'package.json' has been deleted.`,
- 'success'
- );
- });
- });
- beforeAll(async () => {
- try {
- fs.accessSync(DESTINATION_FONT_FILE);
- fs.rmSync(DESTINATION_FONT_FILE);
- } catch (e) {
- // we don't need to do anything here
- }
- });
- });
|