mirror of
https://github.com/discourse/discourse.git
synced 2025-05-25 00:32:52 +08:00
REFACTOR: Move upload utilities to their own file
This commit is contained in:
@ -2,23 +2,15 @@
|
||||
import {
|
||||
emailValid,
|
||||
extractDomainFromUrl,
|
||||
isAnImage,
|
||||
avatarUrl,
|
||||
authorizedExtensions,
|
||||
allowsImages,
|
||||
allowsAttachments,
|
||||
getRawSize,
|
||||
avatarImg,
|
||||
defaultHomepage,
|
||||
setDefaultHomepage,
|
||||
validateUploadedFiles,
|
||||
getUploadMarkdown,
|
||||
caretRowCol,
|
||||
setCaretPosition,
|
||||
fillMissingDates
|
||||
} from "discourse/lib/utilities";
|
||||
import User from "discourse/models/user";
|
||||
import * as Utilities from "discourse/lib/utilities";
|
||||
|
||||
QUnit.module("lib:utilities");
|
||||
|
||||
@ -56,176 +48,6 @@ QUnit.test("extractDomainFromUrl", assert => {
|
||||
);
|
||||
});
|
||||
|
||||
var validUpload = validateUploadedFiles;
|
||||
|
||||
QUnit.test("validateUploadedFiles", assert => {
|
||||
assert.not(validUpload(null), "no files are invalid");
|
||||
assert.not(validUpload(undefined), "undefined files are invalid");
|
||||
assert.not(validUpload([]), "empty array of files is invalid");
|
||||
});
|
||||
|
||||
QUnit.test("uploading one file", assert => {
|
||||
sandbox.stub(bootbox, "alert");
|
||||
|
||||
assert.not(validUpload([1, 2]));
|
||||
assert.ok(bootbox.alert.calledWith(I18n.t("post.errors.too_many_uploads")));
|
||||
});
|
||||
|
||||
QUnit.test("new user cannot upload images", assert => {
|
||||
Discourse.SiteSettings.newuser_max_images = 0;
|
||||
User.resetCurrent(User.create());
|
||||
sandbox.stub(bootbox, "alert");
|
||||
|
||||
assert.not(validUpload([{ name: "image.png" }]), "the upload is not valid");
|
||||
assert.ok(
|
||||
bootbox.alert.calledWith(
|
||||
I18n.t("post.errors.image_upload_not_allowed_for_new_user")
|
||||
),
|
||||
"the alert is called"
|
||||
);
|
||||
});
|
||||
|
||||
QUnit.test("new user cannot upload attachments", assert => {
|
||||
Discourse.SiteSettings.newuser_max_attachments = 0;
|
||||
sandbox.stub(bootbox, "alert");
|
||||
User.resetCurrent(User.create());
|
||||
|
||||
assert.not(validUpload([{ name: "roman.txt" }]));
|
||||
assert.ok(
|
||||
bootbox.alert.calledWith(
|
||||
I18n.t("post.errors.attachment_upload_not_allowed_for_new_user")
|
||||
)
|
||||
);
|
||||
});
|
||||
|
||||
QUnit.test("ensures an authorized upload", assert => {
|
||||
sandbox.stub(bootbox, "alert");
|
||||
assert.not(validUpload([{ name: "unauthorized.html" }]));
|
||||
assert.ok(
|
||||
bootbox.alert.calledWith(
|
||||
I18n.t("post.errors.upload_not_authorized", {
|
||||
authorized_extensions: authorizedExtensions()
|
||||
})
|
||||
)
|
||||
);
|
||||
});
|
||||
|
||||
QUnit.test("skipping validation works", assert => {
|
||||
const files = [{ name: "backup.tar.gz" }];
|
||||
sandbox.stub(bootbox, "alert");
|
||||
|
||||
assert.not(validUpload(files, { skipValidation: false }));
|
||||
assert.ok(validUpload(files, { skipValidation: true }));
|
||||
});
|
||||
|
||||
QUnit.test("staff can upload anything in PM", assert => {
|
||||
const files = [{ name: "some.docx" }];
|
||||
Discourse.SiteSettings.authorized_extensions = "jpeg";
|
||||
User.resetCurrent(User.create({ moderator: true }));
|
||||
|
||||
sandbox.stub(bootbox, "alert");
|
||||
|
||||
assert.not(validUpload(files));
|
||||
assert.ok(
|
||||
validUpload(files, {
|
||||
isPrivateMessage: true,
|
||||
allowStaffToUploadAnyFileInPm: true
|
||||
})
|
||||
);
|
||||
});
|
||||
|
||||
var imageSize = 10 * 1024;
|
||||
|
||||
var dummyBlob = function() {
|
||||
var BlobBuilder =
|
||||
window.BlobBuilder ||
|
||||
window.WebKitBlobBuilder ||
|
||||
window.MozBlobBuilder ||
|
||||
window.MSBlobBuilder;
|
||||
if (BlobBuilder) {
|
||||
var bb = new BlobBuilder();
|
||||
bb.append([new Int8Array(imageSize)]);
|
||||
return bb.getBlob("image/png");
|
||||
} else {
|
||||
return new Blob([new Int8Array(imageSize)], { type: "image/png" });
|
||||
}
|
||||
};
|
||||
|
||||
QUnit.test("allows valid uploads to go through", assert => {
|
||||
User.resetCurrent(User.create());
|
||||
User.currentProp("trust_level", 1);
|
||||
sandbox.stub(bootbox, "alert");
|
||||
|
||||
// image
|
||||
var image = { name: "image.png", size: imageSize };
|
||||
assert.ok(validUpload([image]));
|
||||
// pasted image
|
||||
var pastedImage = dummyBlob();
|
||||
assert.ok(validUpload([pastedImage]));
|
||||
|
||||
assert.not(bootbox.alert.calledOnce);
|
||||
});
|
||||
|
||||
var testUploadMarkdown = function(filename, opts = {}) {
|
||||
return getUploadMarkdown(
|
||||
Object.assign(
|
||||
{
|
||||
original_filename: filename,
|
||||
filesize: 42,
|
||||
thumbnail_width: 100,
|
||||
thumbnail_height: 200,
|
||||
url: "/uploads/123/abcdef.ext"
|
||||
},
|
||||
opts
|
||||
)
|
||||
);
|
||||
};
|
||||
|
||||
QUnit.test("getUploadMarkdown", assert => {
|
||||
assert.equal(
|
||||
testUploadMarkdown("lolcat.gif"),
|
||||
""
|
||||
);
|
||||
assert.equal(
|
||||
testUploadMarkdown("[foo|bar].png"),
|
||||
""
|
||||
);
|
||||
|
||||
const short_url = "uploads://asdaasd.ext";
|
||||
|
||||
assert.equal(
|
||||
testUploadMarkdown("important.txt", { short_url }),
|
||||
`[important.txt|attachment](${short_url}) (42 Bytes)`
|
||||
);
|
||||
});
|
||||
|
||||
QUnit.test("replaces GUID in image alt text on iOS", assert => {
|
||||
assert.equal(
|
||||
testUploadMarkdown("8F2B469B-6B2C-4213-BC68-57B4876365A0.jpeg"),
|
||||
""
|
||||
);
|
||||
|
||||
sandbox.stub(Utilities, "isAppleDevice").returns(true);
|
||||
assert.equal(
|
||||
testUploadMarkdown("8F2B469B-6B2C-4213-BC68-57B4876365A0.jpeg"),
|
||||
""
|
||||
);
|
||||
});
|
||||
|
||||
QUnit.test("isAnImage", assert => {
|
||||
["png", "jpg", "jpeg", "gif", "ico"].forEach(extension => {
|
||||
var image = "image." + extension;
|
||||
assert.ok(isAnImage(image), image + " is recognized as an image");
|
||||
assert.ok(
|
||||
isAnImage("http://foo.bar/path/to/" + image),
|
||||
image + " is recognized as an image"
|
||||
);
|
||||
});
|
||||
assert.not(isAnImage("file.txt"));
|
||||
assert.not(isAnImage("http://foo.bar/path/to/file.txt"));
|
||||
assert.not(isAnImage(""));
|
||||
});
|
||||
|
||||
QUnit.test("avatarUrl", assert => {
|
||||
var rawSize = getRawSize;
|
||||
assert.blank(avatarUrl("", "tiny"), "no template returns blank");
|
||||
@ -288,46 +110,6 @@ QUnit.test("avatarImg", assert => {
|
||||
setDevicePixelRatio(oldRatio);
|
||||
});
|
||||
|
||||
QUnit.test("allowsImages", assert => {
|
||||
Discourse.SiteSettings.authorized_extensions = "jpg|jpeg|gif";
|
||||
assert.ok(allowsImages(), "works");
|
||||
|
||||
Discourse.SiteSettings.authorized_extensions = ".jpg|.jpeg|.gif";
|
||||
assert.ok(allowsImages(), "works with old extensions syntax");
|
||||
|
||||
Discourse.SiteSettings.authorized_extensions = "txt|pdf|*";
|
||||
assert.ok(
|
||||
allowsImages(),
|
||||
"images are allowed when all extensions are allowed"
|
||||
);
|
||||
|
||||
Discourse.SiteSettings.authorized_extensions = "json|jpg|pdf|txt";
|
||||
assert.ok(
|
||||
allowsImages(),
|
||||
"images are allowed when at least one extension is an image extension"
|
||||
);
|
||||
});
|
||||
|
||||
QUnit.test("allowsAttachments", assert => {
|
||||
Discourse.SiteSettings.authorized_extensions = "jpg|jpeg|gif";
|
||||
assert.not(allowsAttachments(), "no attachments allowed by default");
|
||||
|
||||
Discourse.SiteSettings.authorized_extensions = "jpg|jpeg|gif|*";
|
||||
assert.ok(
|
||||
allowsAttachments(),
|
||||
"attachments are allowed when all extensions are allowed"
|
||||
);
|
||||
|
||||
Discourse.SiteSettings.authorized_extensions = "jpg|jpeg|gif|pdf";
|
||||
assert.ok(
|
||||
allowsAttachments(),
|
||||
"attachments are allowed when at least one extension is not an image extension"
|
||||
);
|
||||
|
||||
Discourse.SiteSettings.authorized_extensions = ".jpg|.jpeg|.gif|.pdf";
|
||||
assert.ok(allowsAttachments(), "works with old extensions syntax");
|
||||
});
|
||||
|
||||
QUnit.test("defaultHomepage", assert => {
|
||||
Discourse.SiteSettings.top_menu = "latest|top|hot";
|
||||
assert.equal(
|
||||
|
Reference in New Issue
Block a user