2013-06-19 14:06:23 -05:00
|
|
|
module("Discourse.Utilities");
|
|
|
|
|
|
|
|
var utils = Discourse.Utilities;
|
|
|
|
|
|
|
|
test("emailValid", function() {
|
|
|
|
ok(utils.emailValid('Bob@example.com'), "allows upper case in the first part of emails");
|
|
|
|
ok(utils.emailValid('bob@EXAMPLE.com'), "allows upper case in the email domain");
|
|
|
|
});
|
|
|
|
|
2013-07-23 17:54:18 -05:00
|
|
|
var validUpload = utils.validateUploadedFiles;
|
2013-06-19 14:06:23 -05:00
|
|
|
|
2013-07-23 17:54:18 -05:00
|
|
|
test("validateUploadedFiles", function() {
|
2014-06-04 12:51:26 -05: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-19 14:06:23 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
test("uploading one file", function() {
|
|
|
|
this.stub(bootbox, "alert");
|
|
|
|
|
2014-06-04 12:51:26 -05:00
|
|
|
not(validUpload([1, 2]));
|
2013-07-08 18:32:16 -05:00
|
|
|
ok(bootbox.alert.calledWith(I18n.t('post.errors.too_many_uploads')));
|
2013-06-19 14:06:23 -05:00
|
|
|
});
|
|
|
|
|
2013-07-21 19:39:17 -05:00
|
|
|
test("new user cannot upload images", function() {
|
2013-07-04 17:43:54 -05:00
|
|
|
Discourse.SiteSettings.newuser_max_images = 0;
|
|
|
|
this.stub(bootbox, "alert");
|
|
|
|
|
2014-06-04 12:51:26 -05:00
|
|
|
not(validUpload([{name: "image.png"}]), 'the upload is not valid');
|
2013-10-24 11:36:46 -05:00
|
|
|
ok(bootbox.alert.calledWith(I18n.t('post.errors.image_upload_not_allowed_for_new_user')), 'the alert is called');
|
2013-07-21 19:39:17 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
test("new user cannot upload attachments", function() {
|
|
|
|
Discourse.SiteSettings.newuser_max_attachments = 0;
|
|
|
|
this.stub(bootbox, "alert");
|
|
|
|
|
2014-06-04 12:51:26 -05:00
|
|
|
not(validUpload([{name: "roman.txt"}]));
|
2013-07-21 19:39:17 -05:00
|
|
|
ok(bootbox.alert.calledWith(I18n.t('post.errors.attachment_upload_not_allowed_for_new_user')));
|
2013-07-04 17:43:54 -05:00
|
|
|
});
|
|
|
|
|
2013-06-30 19:19:03 -05:00
|
|
|
test("ensures an authorized upload", function() {
|
|
|
|
var html = { name: "unauthorized.html" };
|
|
|
|
var extensions = Discourse.SiteSettings.authorized_extensions.replace(/\|/g, ", ");
|
2013-06-19 14:06:23 -05:00
|
|
|
this.stub(bootbox, "alert");
|
|
|
|
|
2014-06-04 12:51:26 -05:00
|
|
|
not(validUpload([html]));
|
2013-07-08 18:32:16 -05:00
|
|
|
ok(bootbox.alert.calledWith(I18n.t('post.errors.upload_not_authorized', { authorized_extensions: extensions })));
|
2013-06-19 14:06:23 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
test("prevents files that are too big from being uploaded", function() {
|
2013-06-30 19:19:03 -05:00
|
|
|
var image = { name: "image.png", size: 10 * 1024 };
|
2013-07-15 18:59:23 -05:00
|
|
|
Discourse.SiteSettings.max_image_size_kb = 5;
|
2013-09-07 03:49:11 -05:00
|
|
|
Discourse.User.currentProp("trust_level", 1);
|
2013-06-19 14:06:23 -05:00
|
|
|
this.stub(bootbox, "alert");
|
|
|
|
|
2014-06-04 12:51:26 -05:00
|
|
|
not(validUpload([image]));
|
2013-07-23 17:54:18 -05:00
|
|
|
ok(bootbox.alert.calledWith(I18n.t('post.errors.image_too_large', { max_size_kb: 5 })));
|
2013-06-19 14:06:23 -05:00
|
|
|
});
|
|
|
|
|
2013-07-03 17:39:23 -05:00
|
|
|
var dummyBlob = function() {
|
2013-07-10 15:59:16 -05:00
|
|
|
var BlobBuilder = window.BlobBuilder || window.WebKitBlobBuilder || window.MozBlobBuilder || window.MSBlobBuilder;
|
|
|
|
if (BlobBuilder) {
|
|
|
|
var bb = new BlobBuilder();
|
2013-07-03 17:39:23 -05:00
|
|
|
bb.append([1]);
|
|
|
|
return bb.getBlob("image/png");
|
|
|
|
} else {
|
|
|
|
return new Blob([1], { "type" : "image\/png" });
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-06-19 14:06:23 -05:00
|
|
|
test("allows valid uploads to go through", function() {
|
2013-09-07 03:49:11 -05:00
|
|
|
Discourse.User.currentProp("trust_level", 1);
|
2013-07-15 18:59:23 -05:00
|
|
|
Discourse.SiteSettings.max_image_size_kb = 15;
|
2013-06-19 14:06:23 -05:00
|
|
|
this.stub(bootbox, "alert");
|
|
|
|
|
2013-07-03 17:39:23 -05:00
|
|
|
// image
|
|
|
|
var image = { name: "image.png", size: 10 * 1024 };
|
2013-06-19 14:06:23 -05:00
|
|
|
ok(validUpload([image]));
|
2013-07-03 17:39:23 -05:00
|
|
|
// pasted image
|
|
|
|
var pastedImage = dummyBlob();
|
|
|
|
ok(validUpload([pastedImage]));
|
|
|
|
|
2014-06-04 12:51:26 -05:00
|
|
|
not(bootbox.alert.calledOnce);
|
2013-06-19 14:06:23 -05:00
|
|
|
});
|
2013-06-30 19:19:03 -05:00
|
|
|
|
2013-07-10 15:59:16 -05:00
|
|
|
var getUploadMarkdown = function(filename) {
|
|
|
|
return utils.getUploadMarkdown({
|
|
|
|
original_filename: filename,
|
2013-07-14 05:28:24 -05:00
|
|
|
filesize: 42,
|
2013-07-10 15:59:16 -05:00
|
|
|
width: 100,
|
|
|
|
height: 200,
|
2013-07-18 18:26:23 -05:00
|
|
|
url: "/uploads/123/abcdef.ext"
|
2013-07-10 15:59:16 -05:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
test("getUploadMarkdown", function() {
|
2013-07-18 18:26:23 -05:00
|
|
|
ok(getUploadMarkdown("lolcat.gif") === '<img src="/uploads/123/abcdef.ext" width="100" height="200">');
|
2013-10-23 02:52:15 -05:00
|
|
|
ok(getUploadMarkdown("important.txt") === '<a class="attachment" href="/uploads/123/abcdef.ext">important.txt</a> (42 Bytes)');
|
2013-07-10 15:59:16 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
test("isAnImage", function() {
|
2013-07-15 18:59:23 -05:00
|
|
|
_.each(["png", "jpg", "jpeg", "bmp", "gif", "tif", "tiff"], function(extension) {
|
2013-07-10 15:59:16 -05:00
|
|
|
var image = "image." + extension;
|
2013-07-15 18:59:23 -05:00
|
|
|
ok(utils.isAnImage(image), image + " is recognized as an image");
|
|
|
|
ok(utils.isAnImage("http://foo.bar/path/to/" + image), image + " is recognized as an image");
|
2013-07-10 15:59:16 -05:00
|
|
|
});
|
2014-06-04 12:51:26 -05:00
|
|
|
not(utils.isAnImage("file.txt"));
|
|
|
|
not(utils.isAnImage("http://foo.bar/path/to/file.txt"));
|
|
|
|
not(utils.isAnImage(""));
|
2013-07-10 15:59:16 -05:00
|
|
|
});
|
2013-07-18 11:03:09 -05:00
|
|
|
|
|
|
|
test("avatarUrl", function() {
|
2014-06-04 12:51:26 -05:00
|
|
|
blank(utils.avatarUrl('', 'tiny'), "no template returns blank");
|
|
|
|
equal(utils.avatarUrl('/fake/template/{size}.png', 'tiny'), "/fake/template/" + 20*window.devicePixelRatio + ".png", "simple avatar url");
|
|
|
|
equal(utils.avatarUrl('/fake/template/{size}.png', 'large'), "/fake/template/" + 45*window.devicePixelRatio + ".png", "different size");
|
2013-07-18 11:03:09 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
test("avatarImg", function() {
|
2014-05-26 23:40:46 -05:00
|
|
|
var oldRatio = window.devicePixelRatio;
|
|
|
|
window.devicePixelRatio = 2;
|
|
|
|
|
2013-08-13 15:08:29 -05:00
|
|
|
var avatarTemplate = "/path/to/avatar/{size}.png";
|
2014-06-04 12:51:26 -05:00
|
|
|
equal(utils.avatarImg({avatarTemplate: avatarTemplate, size: 'tiny'}),
|
2014-05-26 23:40:46 -05:00
|
|
|
"<img width='20' height='20' src='/path/to/avatar/40.png' class='avatar'>",
|
2013-07-18 11:03:09 -05:00
|
|
|
"it returns the avatar html");
|
|
|
|
|
2014-06-04 12:51:26 -05:00
|
|
|
equal(utils.avatarImg({avatarTemplate: avatarTemplate, size: 'tiny', title: 'evilest trout'}),
|
2014-05-26 23:40:46 -05:00
|
|
|
"<img width='20' height='20' src='/path/to/avatar/40.png' class='avatar' title='evilest trout'>",
|
2013-07-18 11:03:09 -05:00
|
|
|
"it adds a title if supplied");
|
|
|
|
|
2014-06-04 12:51:26 -05:00
|
|
|
equal(utils.avatarImg({avatarTemplate: avatarTemplate, size: 'tiny', extraClasses: 'evil fish'}),
|
2014-05-26 23:40:46 -05:00
|
|
|
"<img width='20' height='20' src='/path/to/avatar/40.png' class='avatar evil fish'>",
|
2013-07-18 11:03:09 -05:00
|
|
|
"it adds extra classes if supplied");
|
|
|
|
|
2014-06-04 12:51:26 -05:00
|
|
|
blank(utils.avatarImg({avatarTemplate: "", size: 'tiny'}),
|
2013-08-13 15:08:29 -05:00
|
|
|
"it doesn't render avatars for invalid avatar template");
|
2014-05-26 23:40:46 -05:00
|
|
|
|
|
|
|
window.devicePixelRatio = oldRatio;
|
2013-07-21 19:39:17 -05:00
|
|
|
});
|
2013-08-23 11:48:29 -05:00
|
|
|
|
2014-06-04 12:51:26 -05:00
|
|
|
test("allowsAttachments", function() {
|
|
|
|
Discourse.SiteSettings.authorized_extensions = "jpg|jpeg|gif";
|
|
|
|
not(utils.allowsAttachments(), "no attachments allowed by default");
|
|
|
|
|
|
|
|
Discourse.SiteSettings.authorized_extensions = "jpg|jpeg|gif|*";
|
|
|
|
ok(utils.allowsAttachments(), "attachments are allowed when all extensions are allowed");
|
|
|
|
|
|
|
|
Discourse.SiteSettings.authorized_extensions = "jpg|jpeg|gif|pdf";
|
|
|
|
ok(utils.allowsAttachments(), "attachments are allowed when at least one extension is not an image extension");
|
|
|
|
});
|
|
|
|
|
2013-12-30 11:46:18 -06:00
|
|
|
test("defaultHomepage", function() {
|
|
|
|
Discourse.SiteSettings.top_menu = "latest|top|hot";
|
2014-06-04 12:51:26 -05:00
|
|
|
equal(utils.defaultHomepage(), "latest", "default homepage is the first item in the top_menu site setting");
|
2013-12-30 11:46:18 -06:00
|
|
|
});
|
|
|
|
|
2013-08-23 11:48:29 -05:00
|
|
|
module("Discourse.Utilities.cropAvatar with animated avatars", {
|
|
|
|
setup: function() { Discourse.SiteSettings.allow_animated_avatars = true; }
|
|
|
|
});
|
|
|
|
|
|
|
|
asyncTestDiscourse("cropAvatar", function() {
|
|
|
|
expect(1);
|
|
|
|
|
2014-06-04 12:51:26 -05:00
|
|
|
utils.cropAvatar("/path/to/avatar.gif", "image/gif").then(function(avatarTemplate) {
|
2013-08-23 11:48:29 -05:00
|
|
|
equal(avatarTemplate, "/path/to/avatar.gif", "returns the url to the gif when animated gif are enabled");
|
|
|
|
start();
|
|
|
|
});
|
|
|
|
});
|