-
-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(h5p-server): multiple down- and uploads don't lead to long filena…
…mes (#1866)
- Loading branch information
Showing
6 changed files
with
129 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
packages/h5p-server/test/helpers/FilenameGenerator.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import filenameGenerator from '../../src/helpers/FilenameGenerator'; | ||
|
||
describe('FilenameGenerator', () => { | ||
it('generates correct filenames', async () => { | ||
const newFilename = await filenameGenerator( | ||
'test.jpg', | ||
(str) => str, | ||
() => new Promise((res) => res(false)) | ||
); | ||
expect(/^test-[0-9a-z]{8}\.jpg$/i.test(newFilename)).toEqual(true); | ||
}); | ||
|
||
it('fixes name collisions', async () => { | ||
let isFirst = true; | ||
let firstTry = ''; | ||
const newFilename = await filenameGenerator( | ||
'test.jpg', | ||
(str) => str, | ||
(f) => | ||
new Promise((res) => { | ||
if (isFirst) { | ||
firstTry = f; | ||
isFirst = false; | ||
return res(true); | ||
} | ||
return res(false); | ||
}) | ||
); | ||
expect(/^test-[0-9a-z]{8}\.jpg$/i.test(newFilename)).toEqual(true); | ||
expect(firstTry).not.toEqual(newFilename); | ||
}); | ||
|
||
it('detects postfixes and replaces them', async () => { | ||
const newFilename = await filenameGenerator( | ||
'test-1234ABCD.jpg', | ||
(str) => str, | ||
() => new Promise((res) => res(false)) | ||
); | ||
expect(/^test-[0-9a-z]{8}\.jpg$/i.test(newFilename)).toEqual(true); | ||
}); | ||
|
||
it("doesn't append multiple postfixes", async () => { | ||
let filename = 'test-1234ABCD.jpg'; | ||
for (let x = 0; x < 100; x++) { | ||
// eslint-disable-next-line no-await-in-loop | ||
filename = await filenameGenerator( | ||
filename, | ||
(str) => str, | ||
() => new Promise((res) => res(false)) | ||
); | ||
} | ||
expect(/^test-[0-9a-z]{8}\.jpg$/i.test(filename)).toEqual(true); | ||
}); | ||
}); |