discourse/test/javascripts/lib/utilities-test.js.es6

256 lines
9.7 KiB
Plaintext
Raw Normal View History

/* global Int8Array:true */
import {
emailValid,
2016-06-16 01:49:57 +08:00
extractDomainFromUrl,
isAnImage,
avatarUrl,
authorizedExtensions,
allowsImages,
allowsAttachments,
getRawSize,
avatarImg,
defaultHomepage,
setDefaultHomepage,
validateUploadedFiles,
getUploadMarkdown,
caretRowCol,
setCaretPosition
} from 'discourse/lib/utilities';
import * as Utilities from 'discourse/lib/utilities';
2017-06-15 01:57:58 +08:00
QUnit.module("lib:utilities");
2017-06-15 01:57:58 +08:00
QUnit.test("emailValid", assert => {
assert.ok(emailValid('Bob@example.com'), "allows upper case in the first part of emails");
assert.ok(emailValid('bob@EXAMPLE.com'), "allows upper case in the email domain");
});
2017-06-15 01:57:58 +08:00
QUnit.test("extractDomainFromUrl", assert => {
assert.equal(extractDomainFromUrl('http://meta.discourse.org:443/random'), 'meta.discourse.org', "extract domain name from url");
assert.equal(extractDomainFromUrl('meta.discourse.org:443/random'), 'meta.discourse.org', "extract domain regardless of scheme presence");
assert.equal(extractDomainFromUrl('http://192.168.0.1:443/random'), '192.168.0.1', "works for IP address");
assert.equal(extractDomainFromUrl('http://localhost:443/random'), 'localhost', "works for localhost");
2016-06-16 01:49:57 +08:00
});
var validUpload = validateUploadedFiles;
2017-06-15 01:57:58 +08:00
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");
});
2017-06-15 01:57:58 +08:00
QUnit.test("uploading one file", assert => {
2014-07-31 06:56:01 +08:00
sandbox.stub(bootbox, "alert");
2017-06-15 01:57:58 +08:00
assert.not(validUpload([1, 2]));
assert.ok(bootbox.alert.calledWith(I18n.t('post.errors.too_many_uploads')));
});
2017-06-15 01:57:58 +08:00
QUnit.test("new user cannot upload images", assert => {
Discourse.SiteSettings.newuser_max_images = 0;
Discourse.User.resetCurrent(Discourse.User.create());
2014-07-31 06:56:01 +08:00
sandbox.stub(bootbox, "alert");
2017-06-15 01:57:58 +08:00
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');
2013-07-22 08:39:17 +08:00
});
2017-06-15 01:57:58 +08:00
QUnit.test("new user cannot upload attachments", assert => {
2013-07-22 08:39:17 +08:00
Discourse.SiteSettings.newuser_max_attachments = 0;
2014-07-31 06:56:01 +08:00
sandbox.stub(bootbox, "alert");
Discourse.User.resetCurrent(Discourse.User.create());
2013-07-22 08:39:17 +08:00
2017-06-15 01:57:58 +08:00
assert.not(validUpload([{name: "roman.txt"}]));
assert.ok(bootbox.alert.calledWith(I18n.t('post.errors.attachment_upload_not_allowed_for_new_user')));
});
2017-06-15 01:57:58 +08:00
QUnit.test("ensures an authorized upload", assert => {
2014-07-31 06:56:01 +08:00
sandbox.stub(bootbox, "alert");
2017-06-15 01:57:58 +08:00
assert.not(validUpload([{ name: "unauthorized.html" }]));
assert.ok(bootbox.alert.calledWith(I18n.t('post.errors.upload_not_authorized', { authorized_extensions: authorizedExtensions() })));
});
2017-06-15 01:57:58 +08:00
QUnit.test("staff can upload anything in PM", assert => {
const files = [{ name: "some.docx" }];
Discourse.SiteSettings.authorized_extensions = "jpeg";
Discourse.User.resetCurrent(Discourse.User.create({ moderator: true }));
sandbox.stub(bootbox, "alert");
2017-06-15 01:57:58 +08:00
assert.not(validUpload(files));
assert.ok(validUpload(files, { isPrivateMessage: true, allowStaffToUploadAnyFileInPm: true }));
});
var imageSize = 10 * 1024;
var dummyBlob = function() {
2013-07-11 04:59:16 +08:00
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" });
}
};
2017-06-15 01:57:58 +08:00
QUnit.test("allows valid uploads to go through", assert => {
Discourse.User.resetCurrent(Discourse.User.create());
2013-09-07 16:49:11 +08:00
Discourse.User.currentProp("trust_level", 1);
2014-07-31 06:56:01 +08:00
sandbox.stub(bootbox, "alert");
// image
var image = { name: "image.png", size: imageSize };
2017-06-15 01:57:58 +08:00
assert.ok(validUpload([image]));
// pasted image
var pastedImage = dummyBlob();
2017-06-15 01:57:58 +08:00
assert.ok(validUpload([pastedImage]));
2017-06-15 01:57:58 +08:00
assert.not(bootbox.alert.calledOnce);
});
2013-07-01 08:19:03 +08:00
var testUploadMarkdown = function(filename) {
return getUploadMarkdown({
2013-07-11 04:59:16 +08:00
original_filename: filename,
filesize: 42,
2013-07-11 04:59:16 +08:00
width: 100,
height: 200,
url: "/uploads/123/abcdef.ext"
2013-07-11 04:59:16 +08:00
});
};
2017-06-15 01:57:58 +08:00
QUnit.test("getUploadMarkdown", assert => {
assert.equal(testUploadMarkdown("lolcat.gif"),'![lolcat|100x200](/uploads/123/abcdef.ext)');
2017-06-15 01:57:58 +08:00
assert.ok(testUploadMarkdown("important.txt") === '<a class="attachment" href="/uploads/123/abcdef.ext">important.txt</a> (42 Bytes)\n');
2013-07-11 04:59:16 +08:00
});
QUnit.test("replaces GUID in image alt text on iOS", assert => {
assert.equal(testUploadMarkdown("8F2B469B-6B2C-4213-BC68-57B4876365A0.jpeg"),'![8F2B469B-6B2C-4213-BC68-57B4876365A0|100x200](/uploads/123/abcdef.ext)');
sandbox.stub(Utilities, 'isAppleDevice').returns(true);
assert.equal(testUploadMarkdown("8F2B469B-6B2C-4213-BC68-57B4876365A0.jpeg"),'![image|100x200](/uploads/123/abcdef.ext)');
});
2017-06-15 01:57:58 +08:00
QUnit.test("isAnImage", assert => {
2015-12-31 18:40:23 +08:00
_.each(["png", "jpg", "jpeg", "bmp", "gif", "tif", "tiff", "ico"], function(extension) {
2013-07-11 04:59:16 +08:00
var image = "image." + extension;
2017-06-15 01:57:58 +08:00
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");
2013-07-11 04:59:16 +08:00
});
2017-06-15 01:57:58 +08:00
assert.not(isAnImage("file.txt"));
assert.not(isAnImage("http://foo.bar/path/to/file.txt"));
assert.not(isAnImage(""));
2013-07-11 04:59:16 +08:00
});
2017-06-15 01:57:58 +08:00
QUnit.test("avatarUrl", assert => {
var rawSize = getRawSize;
2017-06-15 01:57:58 +08:00
assert.blank(avatarUrl('', 'tiny'), "no template returns blank");
assert.equal(avatarUrl('/fake/template/{size}.png', 'tiny'), "/fake/template/" + rawSize(20) + ".png", "simple avatar url");
assert.equal(avatarUrl('/fake/template/{size}.png', 'large'), "/fake/template/" + rawSize(45) + ".png", "different size");
});
var setDevicePixelRatio = function(value) {
if (Object.defineProperty && !window.hasOwnProperty('devicePixelRatio')) {
Object.defineProperty(window, "devicePixelRatio", { value: 2 });
} else {
window.devicePixelRatio = value;
}
};
2017-06-15 01:57:58 +08:00
QUnit.test("avatarImg", assert => {
var oldRatio = window.devicePixelRatio;
setDevicePixelRatio(2);
2013-08-14 04:08:29 +08:00
var avatarTemplate = "/path/to/avatar/{size}.png";
2017-06-15 01:57:58 +08:00
assert.equal(avatarImg({avatarTemplate: avatarTemplate, size: 'tiny'}),
"<img alt='' width='20' height='20' src='/path/to/avatar/40.png' class='avatar'>",
"it returns the avatar html");
2017-06-15 01:57:58 +08:00
assert.equal(avatarImg({avatarTemplate: avatarTemplate, size: 'tiny', title: 'evilest trout'}),
"<img alt='' width='20' height='20' src='/path/to/avatar/40.png' class='avatar' title='evilest trout'>",
"it adds a title if supplied");
2017-06-15 01:57:58 +08:00
assert.equal(avatarImg({avatarTemplate: avatarTemplate, size: 'tiny', extraClasses: 'evil fish'}),
"<img alt='' width='20' height='20' src='/path/to/avatar/40.png' class='avatar evil fish'>",
"it adds extra classes if supplied");
2017-06-15 01:57:58 +08:00
assert.blank(avatarImg({avatarTemplate: "", size: 'tiny'}),
2013-08-14 04:08:29 +08:00
"it doesn't render avatars for invalid avatar template");
setDevicePixelRatio(oldRatio);
2013-07-22 08:39:17 +08:00
});
2017-06-15 01:57:58 +08:00
QUnit.test("allowsImages", assert => {
Discourse.SiteSettings.authorized_extensions = "jpg|jpeg|gif";
2017-06-15 01:57:58 +08:00
assert.ok(allowsImages(), "works");
Discourse.SiteSettings.authorized_extensions = ".jpg|.jpeg|.gif";
2017-06-15 01:57:58 +08:00
assert.ok(allowsImages(), "works with old extensions syntax");
Discourse.SiteSettings.authorized_extensions = "txt|pdf|*";
2017-06-15 01:57:58 +08:00
assert.ok(allowsImages(), "images are allowed when all extensions are allowed");
Discourse.SiteSettings.authorized_extensions = "json|jpg|pdf|txt";
2017-06-15 01:57:58 +08:00
assert.ok(allowsImages(), "images are allowed when at least one extension is an image extension");
});
2017-06-15 01:57:58 +08:00
QUnit.test("allowsAttachments", assert => {
Discourse.SiteSettings.authorized_extensions = "jpg|jpeg|gif";
2017-06-15 01:57:58 +08:00
assert.not(allowsAttachments(), "no attachments allowed by default");
Discourse.SiteSettings.authorized_extensions = "jpg|jpeg|gif|*";
2017-06-15 01:57:58 +08:00
assert.ok(allowsAttachments(), "attachments are allowed when all extensions are allowed");
Discourse.SiteSettings.authorized_extensions = "jpg|jpeg|gif|pdf";
2017-06-15 01:57:58 +08:00
assert.ok(allowsAttachments(), "attachments are allowed when at least one extension is not an image extension");
Discourse.SiteSettings.authorized_extensions = ".jpg|.jpeg|.gif|.pdf";
2017-06-15 01:57:58 +08:00
assert.ok(allowsAttachments(), "works with old extensions syntax");
});
2017-06-15 01:57:58 +08:00
QUnit.test("defaultHomepage", assert => {
Discourse.SiteSettings.top_menu = "latest|top|hot";
2017-06-15 01:57:58 +08:00
assert.equal(defaultHomepage(), "latest", "default homepage is the first item in the top_menu site setting");
var meta = document.createElement("meta");
meta.name = "discourse_current_homepage";
meta.content = "hot";
document.body.appendChild(meta);
assert.equal(defaultHomepage(), "hot", "default homepage is pulled from <meta name=discourse_current_homepage>");
document.body.removeChild(meta);
});
QUnit.test("setDefaultHomepage", assert => {
var meta = document.createElement("meta");
meta.name = "discourse_current_homepage";
meta.content = "hot";
document.body.appendChild(meta);
setDefaultHomepage("top");
assert.equal(meta.content, "top", "default homepage set by setDefaultHomepage");
document.body.removeChild(meta);
});
2017-06-15 01:57:58 +08:00
QUnit.test("caretRowCol", assert => {
var textarea = document.createElement('textarea');
const content = document.createTextNode("01234\n56789\n012345");
textarea.appendChild(content);
document.body.appendChild(textarea);
const assertResult = (setCaretPos, expectedRowNum, expectedColNum) => {
setCaretPosition(textarea, setCaretPos);
const result = caretRowCol(textarea);
2017-06-15 01:57:58 +08:00
assert.equal(result.rowNum, expectedRowNum, "returns the right row of the caret");
assert.equal(result.colNum, expectedColNum, "returns the right col of the caret");
};
assertResult(0, 1, 0);
assertResult(5, 1, 5);
assertResult(6, 2, 0);
assertResult(11, 2, 5);
assertResult(14, 3, 2);
document.body.removeChild(textarea);
});