mirror of
https://github.com/ONLYOFFICE/server.git
synced 2026-02-10 18:05:07 +08:00
[feature] Refactor uploadObject; For bug 73502
This commit is contained in:
@ -119,7 +119,7 @@ function runTestForDir(ctx, isMultitenantMode, specialDir) {
|
||||
});
|
||||
} else {
|
||||
test("uploadObject", async () => {
|
||||
const spy = jest.spyOn(fs, 'createReadStream').mockReturnValue(testFileData3);
|
||||
const spy = jest.spyOn(fs, 'createReadStream').mockReturnValue(Readable.from(testFileData3));
|
||||
let res = await storage.uploadObject(ctx, testFile3, "createReadStream.txt", specialDir);
|
||||
expect(res).toEqual(undefined);
|
||||
let list = await storage.listObjects(ctx, testDir, specialDir);
|
||||
@ -127,6 +127,31 @@ function runTestForDir(ctx, isMultitenantMode, specialDir) {
|
||||
expect(list.sort()).toEqual([testFile1, testFile2, testFile3].sort());
|
||||
spy.mockRestore();
|
||||
});
|
||||
|
||||
test("uploadObject - stream error handling", async () => {
|
||||
const streamErrorMessage = "Test stream error";
|
||||
const mockStream = new Readable({
|
||||
read() {
|
||||
this.emit('error', new Error(streamErrorMessage));
|
||||
}
|
||||
});
|
||||
|
||||
const spy = jest.spyOn(fs, 'createReadStream').mockReturnValue(mockStream);
|
||||
// Verify that the uploadObject function rejects when the stream emits an error
|
||||
await expect(storage.uploadObject(ctx, "test-error-file.txt", "nonexistent.txt", specialDir))
|
||||
.rejects.toThrow(streamErrorMessage);
|
||||
|
||||
spy.mockRestore();
|
||||
});
|
||||
|
||||
test("uploadObject - non-existent file handling", async () => {
|
||||
const nonExistentFile = 'definitely-does-not-exist-' + Date.now() + '.txt';
|
||||
// Verify the file actually doesn't exist
|
||||
expect(fs.existsSync(nonExistentFile)).toBe(false);
|
||||
// Verify that uploadObject properly handles and propagates the error
|
||||
await expect(storage.uploadObject(ctx, "test-error-file.txt", nonExistentFile, specialDir))
|
||||
.rejects.toThrow(/ENOENT/);
|
||||
});
|
||||
}
|
||||
test("copyObject", async () => {
|
||||
let res = await storage.copyObject(ctx, testFile3, testFile4, specialDir, specialDir);
|
||||
|
||||
Reference in New Issue
Block a user