This repository has been archived on 2023-03-18. You can view files and clone it, but cannot push or open issues or pull requests.
osr-discourse-src/app/assets/javascripts/discourse/tests/unit/lib/uppy-checksum-plugin-test.js
Martin Brennan dce6c6fb50
FIX: Native File object was not passed to uploadHandler (#15146)
The commit 20b2a42f49 broke
upload handlers, because previously we passed through the
native File object to the handler, not the uppy-wrapped
File object.
2021-12-01 09:01:53 +10:00

210 lines
6.7 KiB
JavaScript

import UppyChecksum from "discourse/lib/uppy-checksum-plugin";
import { module, test } from "qunit";
import { createFile } from "discourse/tests/helpers/qunit-helpers";
import sinon from "sinon";
class FakeUppy {
constructor() {
this.preprocessors = [];
this.emitted = [];
this.files = {
"uppy-test/file/vv2/xvejg5w/blah/png-1d-1d-2v-1d-1e-image/jpeg-9043429-1624921727764": {
meta: {},
data: createFile("test1.png", "image/png", "testblobdata1"),
size: 1024,
},
"uppy-test/file/blah1/ads37x2/blah1/png-1d-1d-2v-1d-1e-image/jpeg-99999-1837921727764": {
meta: {},
data: createFile("test2.png", "image/png", "testblobdata2"),
size: 2048,
},
"uppy-test/file/mnb3/jfhrg43x/blah3/png-1d-1d-2v-1d-1e-image/jpeg-111111-1837921727764": {
meta: {},
data: createFile("test2.png", "image/png", "testblobdata2"),
size: 209715200,
},
};
}
addPreProcessor(fn) {
this.preprocessors.push(fn);
}
getFile(id) {
return this.files[id];
}
emit(event, file, data) {
this.emitted.push({ event, file, data });
}
setFileMeta(fileId, meta) {
this.files[fileId].meta = meta;
}
}
module("Unit | Utility | UppyChecksum Plugin", function () {
test("sets the options passed in", function (assert) {
const capabilities = {};
const fakeUppy = new FakeUppy();
const plugin = new UppyChecksum(fakeUppy, {
capabilities,
});
assert.strictEqual(plugin.id, "uppy-checksum");
assert.strictEqual(plugin.capabilities, capabilities);
});
test("it does nothing if not running in a secure context", function (assert) {
const capabilities = {};
const fakeUppy = new FakeUppy();
const plugin = new UppyChecksum(fakeUppy, {
capabilities,
});
plugin.install();
const done = assert.async();
sinon.stub(plugin, "_secureContext").returns(false);
const fileId =
"uppy-test/file/vv2/xvejg5w/blah/png-1d-1d-2v-1d-1e-image/jpeg-9043429-1624921727764";
plugin.uppy.preprocessors[0]([fileId]).then(() => {
assert.strictEqual(
plugin.uppy.emitted.length,
1,
"only the complete event was fired by the checksum plugin because it skipped the file"
);
done();
});
});
test("it does nothing if the crypto object + cipher is not available", function (assert) {
const capabilities = {};
const fakeUppy = new FakeUppy();
const plugin = new UppyChecksum(fakeUppy, {
capabilities,
});
plugin.install();
const done = assert.async();
sinon.stub(plugin, "_hasCryptoCipher").returns(false);
const fileId =
"uppy-test/file/vv2/xvejg5w/blah/png-1d-1d-2v-1d-1e-image/jpeg-9043429-1624921727764";
plugin.uppy.preprocessors[0]([fileId]).then(() => {
assert.strictEqual(
plugin.uppy.emitted.length,
1,
"only the complete event was fired by the checksum plugin because it skipped the file"
);
done();
});
});
test("it does nothing if the browser is IE11", function (assert) {
const capabilities = { isIE11: true };
const fakeUppy = new FakeUppy();
const plugin = new UppyChecksum(fakeUppy, {
capabilities,
});
plugin.install();
const done = assert.async();
const fileId =
"uppy-test/file/vv2/xvejg5w/blah/png-1d-1d-2v-1d-1e-image/jpeg-9043429-1624921727764";
plugin.uppy.preprocessors[0]([fileId]).then(() => {
assert.strictEqual(
plugin.uppy.emitted.length,
1,
"only the complete event was fired by the checksum plugin because it skipped the file"
);
done();
});
});
test("it does nothing if the file is > 100MB", function (assert) {
const capabilities = {};
const fakeUppy = new FakeUppy();
const plugin = new UppyChecksum(fakeUppy, {
capabilities,
});
plugin.install();
const done = assert.async();
const fileId =
"uppy-test/file/mnb3/jfhrg43x/blah3/png-1d-1d-2v-1d-1e-image/jpeg-111111-1837921727764";
plugin.uppy.preprocessors[0]([fileId]).then(() => {
assert.strictEqual(plugin.uppy.emitted[0].event, "preprocess-progress");
assert.strictEqual(plugin.uppy.emitted[1].event, "preprocess-complete");
assert.strictEqual(
plugin.uppy.getFile(fileId).meta.sha1_checksum,
undefined
);
done();
});
});
test("it gets a sha1 hash of each file and adds it to the file meta", function (assert) {
const capabilities = {};
const fakeUppy = new FakeUppy();
const plugin = new UppyChecksum(fakeUppy, {
capabilities,
});
plugin.install();
const done = assert.async();
const fileIds = [
"uppy-test/file/vv2/xvejg5w/blah/png-1d-1d-2v-1d-1e-image/jpeg-9043429-1624921727764",
"uppy-test/file/blah1/ads37x2/blah1/png-1d-1d-2v-1d-1e-image/jpeg-99999-1837921727764",
];
plugin.uppy.preprocessors[0](fileIds).then(() => {
assert.strictEqual(plugin.uppy.emitted[0].event, "preprocess-progress");
assert.strictEqual(plugin.uppy.emitted[1].event, "preprocess-progress");
assert.strictEqual(plugin.uppy.emitted[2].event, "preprocess-complete");
assert.strictEqual(plugin.uppy.emitted[3].event, "preprocess-complete");
// these checksums are the actual SHA1 hashes of the test file names
assert.strictEqual(
plugin.uppy.getFile(fileIds[0]).meta.sha1_checksum,
"2aa31a700d084c78cecbf030b041ad63eb4f6e8a"
);
assert.strictEqual(
plugin.uppy.getFile(fileIds[1]).meta.sha1_checksum,
"dfa8c725a5a6710ce4467f29655ec9d26a8de3d0"
);
done();
});
});
test("it does nothing if the window.crypto.subtle.digest function throws an error / rejects", function (assert) {
const capabilities = {};
const fakeUppy = new FakeUppy();
const plugin = new UppyChecksum(fakeUppy, {
capabilities,
});
plugin.install();
const done = assert.async();
const fileIds = [
"uppy-test/file/vv2/xvejg5w/blah/png-1d-1d-2v-1d-1e-image/jpeg-9043429-1624921727764",
"uppy-test/file/blah1/ads37x2/blah1/png-1d-1d-2v-1d-1e-image/jpeg-99999-1837921727764",
];
sinon
.stub(window.crypto.subtle, "digest")
.rejects({ message: "Algorithm: Unrecognized name" });
plugin.uppy.preprocessors[0](fileIds).then(() => {
assert.strictEqual(plugin.uppy.emitted[0].event, "preprocess-progress");
assert.strictEqual(plugin.uppy.emitted[1].event, "preprocess-progress");
assert.strictEqual(plugin.uppy.emitted[2].event, "preprocess-complete");
assert.strictEqual(plugin.uppy.emitted[3].event, "preprocess-complete");
assert.deepEqual(plugin.uppy.getFile(fileIds[0]).meta, {});
assert.deepEqual(plugin.uppy.getFile(fileIds[1]).meta, {});
done();
});
});
});