Cleaned the comments and used test.describe to nest the tests

This commit is contained in:
David Rotermund 2025-05-26 12:07:06 +02:00
parent 795135e3c9
commit 77c248fa1e
2 changed files with 98 additions and 105 deletions

View file

@ -9,7 +9,6 @@ import (
"strings" "strings"
) )
// var fileNameComponentSanitizeRegexp = regexp.MustCompile(`(?i)\.\.|[<>:\"/\\|?*\x{0000}-\x{001F}]|^(con|prn|aux|nul|com\d|lpt\d)$`)
var fileNameComponentSanitizeRegexp = regexp.MustCompile(`(?i)[<>:\"/\\|?*\x{0000}-\x{001F}]|^(con|prn|aux|nul|com\d|lpt\d)$`) var fileNameComponentSanitizeRegexp = regexp.MustCompile(`(?i)[<>:\"/\\|?*\x{0000}-\x{001F}]|^(con|prn|aux|nul|com\d|lpt\d)$`)
// SanitizePath cleans and validates a file path // SanitizePath cleans and validates a file path

View file

@ -8,138 +8,132 @@
// services/repository/files/upload.go // services/repository/files/upload.go
// @watch end // @watch end
// https://codeberg.org/forgejo/forgejo/src/branch/forgejo/tests/e2e/README.md
// lies. Don't use
// make TAGS="sqlite sqlite_unlock_notify" backend
// use
// make TAGS="sqlite sqlite_unlock_notify" build
// otherwise the test fails.
import {expect} from '@playwright/test'; import {expect} from '@playwright/test';
import {test, dynamic_id, save_visual} from './utils_e2e.ts'; import {test, dynamic_id, save_visual} from './utils_e2e.ts';
test.use({user: 'user2'}); test.use({user: 'user2'});
test.describe('Drag and Drop upload', () => {
test('normal and special characters', async ({page}) => {
const response = await page.goto(`/user2/file-uploads/_upload/main/`);
expect(response?.status()).toBe(200); // Status OK
test('drag and drop upload normal and special characters', async ({page}) => { const testID = dynamic_id();
const response = await page.goto(`/user2/file-uploads/_upload/main/`); const dropzone = page.getByRole('button', {name: 'Drop files or click here to upload.'});
expect(response?.status()).toBe(200); // Status OK
const testID = dynamic_id(); // create the virtual files
const dropzone = page.getByRole('button', {name: 'Drop files or click here to upload.'}); const dataTransferA = await page.evaluateHandle(() => {
const dt = new DataTransfer();
// add items in different folders
dt.items.add(new File(['Filecontent (dir1/file1.txt)'], 'dir1/file1.txt', {type: 'text/plain'}));
dt.items.add(new File(["Another file's content(double / nested / file.txt)"], 'double / nested / file.txt', {type: 'text / plain'}));
dt.items.add(new File(['Root file (root_file.txt)'], 'root_file.txt', {type: 'text/plain'}));
dt.items.add(new File(['Umlaut test'], 'special/äüöÄÜÖß.txt', {type: 'text/plain'}));
dt.items.add(new File(['Unicode test'], 'special/Ʉ₦ł₵ØĐɆ.txt', {type: 'text/plain'}));
return dt;
});
// and drop them to the upload area
await dropzone.dispatchEvent('drop', {dataTransfer: dataTransferA});
// create the virtual files await page.getByText('new branch').click();
const dataTransferA = await page.evaluateHandle(() => {
const dt = new DataTransfer(); await page.getByRole('textbox', {name: 'Name the new branch for this'}).fill(testID);
// add items in different folders await page.getByRole('button', {name: 'Propose file change'}).click();
dt.items.add(new File(['Filecontent (dir1/file1.txt)'], 'dir1/file1.txt', {type: 'text/plain'}));
dt.items.add(new File(["Another file's content(double / nested / file.txt)"], 'double / nested / file.txt', {type: 'text / plain'})); // check that nested file structure is preserved
dt.items.add(new File(['Root file (root_file.txt)'], 'root_file.txt', {type: 'text/plain'})); await expect(page.getByRole('link', {name: 'dir1/file1.txt'})).toBeVisible();
dt.items.add(new File(['Umlaut test'], 'special/äüöÄÜÖß.txt', {type: 'text/plain'})); await expect(page.getByRole('link', {name: 'double/nested/file.txt'})).toBeVisible();
dt.items.add(new File(['Unicode test'], 'special/Ʉ₦ł₵ØĐɆ.txt', {type: 'text/plain'})); await expect(page.getByRole('link', {name: 'special/äüöÄÜÖß.txt'})).toBeVisible();
return dt; await expect(page.getByRole('link', {name: 'special/Ʉ₦ł₵ØĐɆ.txt'})).toBeVisible();
// Since this is a file in root, there two links with the same label
// we take the on in #diff-file-tree
await expect(page.locator('#diff-file-boxes').getByRole('link', {name: 'root_file.txt'})).toBeVisible();
}); });
// and drop them to the upload area
await dropzone.dispatchEvent('drop', {dataTransfer: dataTransferA});
await page.getByText('new branch').click(); test('strange paths and spaces', async ({page}) => {
const response = await page.goto(`/user2/file-uploads/_upload/main/`);
expect(response?.status()).toBe(200); // Status OK
await page.getByRole('textbox', {name: 'Name the new branch for this'}).fill(testID); const testID = dynamic_id();
await page.getByRole('button', {name: 'Propose file change'}).click(); const dropzone = page.getByRole('button', {name: 'Drop files or click here to upload.'});
// check that nested file structure is preserved // create the virtual files
await expect(page.getByRole('link', {name: 'dir1/file1.txt'})).toBeVisible(); const dataTransferA = await page.evaluateHandle(() => {
await expect(page.getByRole('link', {name: 'double/nested/file.txt'})).toBeVisible(); const dt = new DataTransfer();
await expect(page.getByRole('link', {name: 'special/äüöÄÜÖß.txt'})).toBeVisible(); // add items in different folders
await expect(page.getByRole('link', {name: 'special/Ʉ₦ł₵ØĐɆ.txt'})).toBeVisible(); dt.items.add(new File(['1'], '..dots.txt', {type: 'text/plain'}));
// Since this is a file in root, there two links with the same label dt.items.add(new File(['2'], '.dots.vanish.txt', {type: 'text/plain'}));
// we take the on in #diff-file-tree dt.items.add(new File(['3'], 'special/S P A C E !.txt', {type: 'text/plain'}));
await expect(page.locator('#diff-file-boxes').getByRole('link', {name: 'root_file.txt'})).toBeVisible(); return dt;
}); });
// and drop them to the upload area
await dropzone.dispatchEvent('drop', {dataTransfer: dataTransferA});
test('drag and drop upload strange paths and spaces', async ({page}) => { await page.getByText('new branch').click();
const response = await page.goto(`/user2/file-uploads/_upload/main/`);
expect(response?.status()).toBe(200); // Status OK
const testID = dynamic_id(); await page.getByRole('textbox', {name: 'Name the new branch for this'}).fill(testID);
const dropzone = page.getByRole('button', {name: 'Drop files or click here to upload.'}); await page.getByRole('button', {name: 'Propose file change'}).click();
// create the virtual files // check that nested file structure is preserved
const dataTransferA = await page.evaluateHandle(() => { // Since this is a file in root, there two links with the same label
const dt = new DataTransfer(); // we take the on in #diff-file-tree
// add items in different folders await expect(page.locator('#diff-file-boxes').getByRole('link', {name: '.dots.vanish.txt'})).toBeVisible();
dt.items.add(new File(['1'], '..dots.txt', {type: 'text/plain'})); await expect(page.getByRole('link', {name: 'special/S P A C E !.txt'})).toBeVisible();
dt.items.add(new File(['2'], '.dots.vanish.txt', {type: 'text/plain'})); // Since this is a file in root, there two links with the same label
dt.items.add(new File(['3'], 'special/S P A C E !.txt', {type: 'text/plain'})); // we take the on in #diff-file-tree
return dt; await expect(page.locator('#diff-file-boxes').getByRole('link', {name: '..dots.txt'})).toBeVisible();
}); });
// and drop them to the upload area
await dropzone.dispatchEvent('drop', {dataTransfer: dataTransferA});
await page.getByText('new branch').click(); test('broken path slash in front', async ({page}) => {
const response = await page.goto(`/user2/file-uploads/_upload/main/`);
expect(response?.status()).toBe(200); // Status OK
await page.getByRole('textbox', {name: 'Name the new branch for this'}).fill(testID); const testID = dynamic_id();
await page.getByRole('button', {name: 'Propose file change'}).click(); const dropzone = page.getByRole('button', {name: 'Drop files or click here to upload.'});
// check that nested file structure is preserved // create the virtual files
// Since this is a file in root, there two links with the same label const dataTransferA = await page.evaluateHandle(() => {
// we take the on in #diff-file-tree const dt = new DataTransfer();
await expect(page.locator('#diff-file-boxes').getByRole('link', {name: '.dots.vanish.txt'})).toBeVisible(); // add items in different folders
await expect(page.getByRole('link', {name: 'special/S P A C E !.txt'})).toBeVisible(); dt.items.add(new File(['1'], '/special/badfirstslash.txt', {type: 'text/plain'}));
// Since this is a file in root, there two links with the same label return dt;
// we take the on in #diff-file-tree });
await expect(page.locator('#diff-file-boxes').getByRole('link', {name: '..dots.txt'})).toBeVisible(); // and drop them to the upload area
}); await dropzone.dispatchEvent('drop', {dataTransfer: dataTransferA});
test('drag and drop upload broken path slash in front', async ({page}) => { await page.getByText('new branch').click();
const response = await page.goto(`/user2/file-uploads/_upload/main/`);
expect(response?.status()).toBe(200); // Status OK
const testID = dynamic_id(); await page.getByRole('textbox', {name: 'Name the new branch for this'}).fill(testID);
const dropzone = page.getByRole('button', {name: 'Drop files or click here to upload.'}); await page.getByRole('button', {name: 'Propose file change'}).click();
// create the virtual files await expect(page.getByText('Failed to upload files to')).toBeVisible();
const dataTransferA = await page.evaluateHandle(() => {
const dt = new DataTransfer(); await save_visual(page);
// add items in different folders
dt.items.add(new File(['1'], '/special/badfirstslash.txt', {type: 'text/plain'}));
return dt;
}); });
// and drop them to the upload area
await dropzone.dispatchEvent('drop', {dataTransfer: dataTransferA});
await page.getByText('new branch').click(); test('broken path with traversal', async ({page}) => {
const response = await page.goto(`/user2/file-uploads/_upload/main/`);
expect(response?.status()).toBe(200); // Status OK
await page.getByRole('textbox', {name: 'Name the new branch for this'}).fill(testID); const testID = dynamic_id();
await page.getByRole('button', {name: 'Propose file change'}).click(); const dropzone = page.getByRole('button', {name: 'Drop files or click here to upload.'});
await expect(page.getByText('Failed to upload files to')).toBeVisible(); // create the virtual files
const dataTransferA = await page.evaluateHandle(() => {
const dt = new DataTransfer();
// add items in different folders
dt.items.add(new File(['1'], '../baddots.txt', {type: 'text/plain'}));
return dt;
});
// and drop them to the upload area
await dropzone.dispatchEvent('drop', {dataTransfer: dataTransferA});
await save_visual(page); await page.getByText('new branch').click();
});
test('drag and drop upload broken path with traversal', async ({page}) => { await page.getByRole('textbox', {name: 'Name the new branch for this'}).fill(testID);
const response = await page.goto(`/user2/file-uploads/_upload/main/`); await page.getByRole('button', {name: 'Propose file change'}).click();
expect(response?.status()).toBe(200); // Status OK
const testID = dynamic_id(); await expect(page.getByText('Failed to upload files to')).toBeVisible();
const dropzone = page.getByRole('button', {name: 'Drop files or click here to upload.'});
// create the virtual files await save_visual(page);
const dataTransferA = await page.evaluateHandle(() => {
const dt = new DataTransfer();
// add items in different folders
dt.items.add(new File(['1'], '../baddots.txt', {type: 'text/plain'}));
return dt;
}); });
// and drop them to the upload area
await dropzone.dispatchEvent('drop', {dataTransfer: dataTransferA});
await page.getByText('new branch').click();
await page.getByRole('textbox', {name: 'Name the new branch for this'}).fill(testID);
await page.getByRole('button', {name: 'Propose file change'}).click();
await expect(page.getByText('Failed to upload files to')).toBeVisible();
await save_visual(page);
}); });