2016-02-13 02:31:26 +08:00
|
|
|
/* global Int8Array:true */
|
2015-08-12 00:27:07 +08:00
|
|
|
import { blank } from 'helpers/qunit-helpers';
|
2016-06-15 02:31:51 +08:00
|
|
|
import {
|
|
|
|
emailValid,
|
2016-06-16 01:49:57 +08:00
|
|
|
extractDomainFromUrl,
|
2016-06-15 02:31:51 +08:00
|
|
|
isAnImage,
|
|
|
|
avatarUrl,
|
|
|
|
allowsAttachments,
|
|
|
|
getRawSize,
|
|
|
|
avatarImg,
|
|
|
|
defaultHomepage,
|
|
|
|
validateUploadedFiles,
|
|
|
|
getUploadMarkdown,
|
|
|
|
caretRowCol,
|
|
|
|
setCaretPosition
|
|
|
|
} from 'discourse/lib/utilities';
|
|
|
|
|
|
|
|
module("lib:utilities");
|
2013-06-20 03:06:23 +08:00
|
|
|
|
|
|
|
test("emailValid", function() {
|
2016-06-15 02:31:51 +08:00
|
|
|
ok(emailValid('Bob@example.com'), "allows upper case in the first part of emails");
|
|
|
|
ok(emailValid('bob@EXAMPLE.com'), "allows upper case in the email domain");
|
2013-06-20 03:06:23 +08:00
|
|
|
});
|
|
|
|
|
2016-06-16 01:49:57 +08:00
|
|
|
test("extractDomainFromUrl", function() {
|
|
|
|
equal(extractDomainFromUrl('http://meta.discourse.org:443/random'), 'meta.discourse.org', "extract domain name from url");
|
|
|
|
equal(extractDomainFromUrl('meta.discourse.org:443/random'), 'meta.discourse.org', "extract domain regardless of scheme presence");
|
|
|
|
equal(extractDomainFromUrl('http://192.168.0.1:443/random'), '192.168.0.1', "works for IP address");
|
|
|
|
equal(extractDomainFromUrl('http://localhost:443/random'), 'localhost', "works for localhost");
|
|
|
|
});
|
|
|
|
|
2016-06-15 02:31:51 +08:00
|
|
|
var validUpload = validateUploadedFiles;
|
2013-06-20 03:06:23 +08:00
|
|
|
|
2013-07-24 06:54:18 +08:00
|
|
|
test("validateUploadedFiles", function() {
|
2014-06-05 01:51:26 +08:00
|
|
|
not(validUpload(null), "no files are invalid");
|
|
|
|
not(validUpload(undefined), "undefined files are invalid");
|
|
|
|
not(validUpload([]), "empty array of files is invalid");
|
2013-06-20 03:06:23 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
test("uploading one file", function() {
|
2014-07-31 06:56:01 +08:00
|
|
|
sandbox.stub(bootbox, "alert");
|
2013-06-20 03:06:23 +08:00
|
|
|
|
2014-06-05 01:51:26 +08:00
|
|
|
not(validUpload([1, 2]));
|
2013-07-09 07:32:16 +08:00
|
|
|
ok(bootbox.alert.calledWith(I18n.t('post.errors.too_many_uploads')));
|
2013-06-20 03:06:23 +08:00
|
|
|
});
|
|
|
|
|
2013-07-22 08:39:17 +08:00
|
|
|
test("new user cannot upload images", function() {
|
2013-07-05 06:43:54 +08:00
|
|
|
Discourse.SiteSettings.newuser_max_images = 0;
|
2014-08-01 01:24:07 +08:00
|
|
|
Discourse.User.resetCurrent(Discourse.User.create());
|
2014-07-31 06:56:01 +08:00
|
|
|
sandbox.stub(bootbox, "alert");
|
2013-07-05 06:43:54 +08:00
|
|
|
|
2014-06-05 01:51:26 +08:00
|
|
|
not(validUpload([{name: "image.png"}]), 'the upload is not valid');
|
2013-10-25 00:36:46 +08:00
|
|
|
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
|
|
|
});
|
|
|
|
|
|
|
|
test("new user cannot upload attachments", function() {
|
|
|
|
Discourse.SiteSettings.newuser_max_attachments = 0;
|
2014-07-31 06:56:01 +08:00
|
|
|
sandbox.stub(bootbox, "alert");
|
2014-08-01 01:24:07 +08:00
|
|
|
Discourse.User.resetCurrent(Discourse.User.create());
|
2013-07-22 08:39:17 +08:00
|
|
|
|
2014-06-05 01:51:26 +08:00
|
|
|
not(validUpload([{name: "roman.txt"}]));
|
2013-07-22 08:39:17 +08:00
|
|
|
ok(bootbox.alert.calledWith(I18n.t('post.errors.attachment_upload_not_allowed_for_new_user')));
|
2013-07-05 06:43:54 +08:00
|
|
|
});
|
|
|
|
|
2013-07-01 08:19:03 +08:00
|
|
|
test("ensures an authorized upload", function() {
|
|
|
|
var html = { name: "unauthorized.html" };
|
|
|
|
var extensions = Discourse.SiteSettings.authorized_extensions.replace(/\|/g, ", ");
|
2014-07-31 06:56:01 +08:00
|
|
|
sandbox.stub(bootbox, "alert");
|
2013-06-20 03:06:23 +08:00
|
|
|
|
2014-06-05 01:51:26 +08:00
|
|
|
not(validUpload([html]));
|
2013-07-09 07:32:16 +08:00
|
|
|
ok(bootbox.alert.calledWith(I18n.t('post.errors.upload_not_authorized', { authorized_extensions: extensions })));
|
2013-06-20 03:06:23 +08:00
|
|
|
});
|
|
|
|
|
2015-01-24 02:17:33 +08:00
|
|
|
var imageSize = 10 * 1024;
|
|
|
|
|
2013-07-04 06:39:23 +08:00
|
|
|
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();
|
2015-01-24 02:17:33 +08:00
|
|
|
bb.append([new Int8Array(imageSize)]);
|
2013-07-04 06:39:23 +08:00
|
|
|
return bb.getBlob("image/png");
|
|
|
|
} else {
|
2015-01-24 02:17:33 +08:00
|
|
|
return new Blob([new Int8Array(imageSize)], { "type" : "image\/png" });
|
2013-07-04 06:39:23 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-06-20 03:06:23 +08:00
|
|
|
test("allows valid uploads to go through", function() {
|
2014-08-01 01:24:07 +08:00
|
|
|
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");
|
2013-06-20 03:06:23 +08:00
|
|
|
|
2013-07-04 06:39:23 +08:00
|
|
|
// image
|
2015-01-24 02:17:33 +08:00
|
|
|
var image = { name: "image.png", size: imageSize };
|
2013-06-20 03:06:23 +08:00
|
|
|
ok(validUpload([image]));
|
2013-07-04 06:39:23 +08:00
|
|
|
// pasted image
|
|
|
|
var pastedImage = dummyBlob();
|
|
|
|
ok(validUpload([pastedImage]));
|
|
|
|
|
2014-06-05 01:51:26 +08:00
|
|
|
not(bootbox.alert.calledOnce);
|
2013-06-20 03:06:23 +08:00
|
|
|
});
|
2013-07-01 08:19:03 +08:00
|
|
|
|
2016-06-15 02:31:51 +08:00
|
|
|
var testUploadMarkdown = function(filename) {
|
|
|
|
return getUploadMarkdown({
|
2013-07-11 04:59:16 +08:00
|
|
|
original_filename: filename,
|
2013-07-14 18:28:24 +08:00
|
|
|
filesize: 42,
|
2013-07-11 04:59:16 +08:00
|
|
|
width: 100,
|
|
|
|
height: 200,
|
2013-07-19 07:26:23 +08:00
|
|
|
url: "/uploads/123/abcdef.ext"
|
2013-07-11 04:59:16 +08:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
test("getUploadMarkdown", function() {
|
2016-06-15 02:31:51 +08:00
|
|
|
ok(testUploadMarkdown("lolcat.gif") === '<img src="/uploads/123/abcdef.ext" width="100" height="200">');
|
|
|
|
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
|
|
|
});
|
|
|
|
|
|
|
|
test("isAnImage", function() {
|
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;
|
2016-06-15 02:31:51 +08:00
|
|
|
ok(isAnImage(image), image + " is recognized as an image");
|
|
|
|
ok(isAnImage("http://foo.bar/path/to/" + image), image + " is recognized as an image");
|
2013-07-11 04:59:16 +08:00
|
|
|
});
|
2016-06-15 02:31:51 +08:00
|
|
|
not(isAnImage("file.txt"));
|
|
|
|
not(isAnImage("http://foo.bar/path/to/file.txt"));
|
|
|
|
not(isAnImage(""));
|
2013-07-11 04:59:16 +08:00
|
|
|
});
|
2013-07-19 00:03:09 +08:00
|
|
|
|
|
|
|
test("avatarUrl", function() {
|
2016-06-15 02:31:51 +08:00
|
|
|
var rawSize = getRawSize;
|
|
|
|
blank(avatarUrl('', 'tiny'), "no template returns blank");
|
|
|
|
equal(avatarUrl('/fake/template/{size}.png', 'tiny'), "/fake/template/" + rawSize(20) + ".png", "simple avatar url");
|
|
|
|
equal(avatarUrl('/fake/template/{size}.png', 'large'), "/fake/template/" + rawSize(45) + ".png", "different size");
|
2013-07-19 00:03:09 +08:00
|
|
|
});
|
|
|
|
|
2015-03-20 20:53:56 +08:00
|
|
|
var setDevicePixelRatio = function(value) {
|
2015-08-12 00:27:07 +08:00
|
|
|
if (Object.defineProperty && !window.hasOwnProperty('devicePixelRatio')) {
|
|
|
|
Object.defineProperty(window, "devicePixelRatio", { value: 2 });
|
2015-03-20 20:53:56 +08:00
|
|
|
} else {
|
|
|
|
window.devicePixelRatio = value;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-07-19 00:03:09 +08:00
|
|
|
test("avatarImg", function() {
|
2014-05-27 12:40:46 +08:00
|
|
|
var oldRatio = window.devicePixelRatio;
|
2015-03-20 20:53:56 +08:00
|
|
|
setDevicePixelRatio(2);
|
2014-05-27 12:40:46 +08:00
|
|
|
|
2013-08-14 04:08:29 +08:00
|
|
|
var avatarTemplate = "/path/to/avatar/{size}.png";
|
2016-06-15 02:31:51 +08:00
|
|
|
equal(avatarImg({avatarTemplate: avatarTemplate, size: 'tiny'}),
|
2015-12-15 16:56:45 +08:00
|
|
|
"<img alt='' width='20' height='20' src='/path/to/avatar/40.png' class='avatar'>",
|
2013-07-19 00:03:09 +08:00
|
|
|
"it returns the avatar html");
|
|
|
|
|
2016-06-15 02:31:51 +08:00
|
|
|
equal(avatarImg({avatarTemplate: avatarTemplate, size: 'tiny', title: 'evilest trout'}),
|
2015-12-15 16:56:45 +08:00
|
|
|
"<img alt='' width='20' height='20' src='/path/to/avatar/40.png' class='avatar' title='evilest trout'>",
|
2013-07-19 00:03:09 +08:00
|
|
|
"it adds a title if supplied");
|
|
|
|
|
2016-06-15 02:31:51 +08:00
|
|
|
equal(avatarImg({avatarTemplate: avatarTemplate, size: 'tiny', extraClasses: 'evil fish'}),
|
2015-12-15 16:56:45 +08:00
|
|
|
"<img alt='' width='20' height='20' src='/path/to/avatar/40.png' class='avatar evil fish'>",
|
2013-07-19 00:03:09 +08:00
|
|
|
"it adds extra classes if supplied");
|
|
|
|
|
2016-06-15 02:31:51 +08:00
|
|
|
blank(avatarImg({avatarTemplate: "", size: 'tiny'}),
|
2013-08-14 04:08:29 +08:00
|
|
|
"it doesn't render avatars for invalid avatar template");
|
2014-05-27 12:40:46 +08:00
|
|
|
|
2015-03-20 20:53:56 +08:00
|
|
|
setDevicePixelRatio(oldRatio);
|
2013-07-22 08:39:17 +08:00
|
|
|
});
|
2013-08-24 00:48:29 +08:00
|
|
|
|
2014-06-05 01:51:26 +08:00
|
|
|
test("allowsAttachments", function() {
|
2016-10-20 00:42:24 +08:00
|
|
|
Discourse.SiteSettings.authorized_extensions = ".jpg, .jpeg, .gif";
|
2016-06-15 02:31:51 +08:00
|
|
|
not(allowsAttachments(), "no attachments allowed by default");
|
2014-06-05 01:51:26 +08:00
|
|
|
|
2016-10-20 00:42:24 +08:00
|
|
|
Discourse.SiteSettings.authorized_extensions = ".jpg, .jpeg, .gif, *";
|
2016-06-15 02:31:51 +08:00
|
|
|
ok(allowsAttachments(), "attachments are allowed when all extensions are allowed");
|
2014-06-05 01:51:26 +08:00
|
|
|
|
2016-10-20 00:42:24 +08:00
|
|
|
Discourse.SiteSettings.authorized_extensions = ".jpg, .jpeg, .gif, .pdf";
|
2016-06-15 02:31:51 +08:00
|
|
|
ok(allowsAttachments(), "attachments are allowed when at least one extension is not an image extension");
|
2014-06-05 01:51:26 +08:00
|
|
|
});
|
|
|
|
|
2013-12-31 01:46:18 +08:00
|
|
|
test("defaultHomepage", function() {
|
|
|
|
Discourse.SiteSettings.top_menu = "latest|top|hot";
|
2016-06-15 02:31:51 +08:00
|
|
|
equal(defaultHomepage(), "latest", "default homepage is the first item in the top_menu site setting");
|
2013-12-31 01:46:18 +08:00
|
|
|
});
|
2015-12-28 14:28:16 +08:00
|
|
|
|
|
|
|
test("caretRowCol", () => {
|
|
|
|
var textarea = document.createElement('textarea');
|
|
|
|
const content = document.createTextNode("01234\n56789\n012345");
|
|
|
|
textarea.appendChild(content);
|
|
|
|
document.body.appendChild(textarea);
|
|
|
|
|
|
|
|
const assertResult = (setCaretPos, expectedRowNum, expectedColNum) => {
|
2016-06-15 02:31:51 +08:00
|
|
|
setCaretPosition(textarea, setCaretPos);
|
2015-12-28 14:28:16 +08:00
|
|
|
|
2016-06-15 02:31:51 +08:00
|
|
|
const result = caretRowCol(textarea);
|
2015-12-28 14:28:16 +08:00
|
|
|
equal(result.rowNum, expectedRowNum, "returns the right row of the caret");
|
|
|
|
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);
|
|
|
|
});
|