2013-06-19 15:06:23 -04: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 18:54:18 -04:00
|
|
|
var validUpload = utils.validateUploadedFiles;
|
2013-06-19 15:06:23 -04:00
|
|
|
|
2013-07-23 18:54:18 -04:00
|
|
|
test("validateUploadedFiles", function() {
|
2013-06-19 15:06:23 -04:00
|
|
|
ok(!validUpload(null), "no files are invalid");
|
|
|
|
ok(!validUpload(undefined), "undefined files are invalid");
|
|
|
|
ok(!validUpload([]), "empty array of files is invalid");
|
|
|
|
});
|
|
|
|
|
|
|
|
test("uploading one file", function() {
|
|
|
|
this.stub(bootbox, "alert");
|
|
|
|
|
|
|
|
ok(!validUpload([1, 2]));
|
2013-07-08 19:32:16 -04:00
|
|
|
ok(bootbox.alert.calledWith(I18n.t('post.errors.too_many_uploads')));
|
2013-06-19 15:06:23 -04:00
|
|
|
});
|
|
|
|
|
2013-07-21 20:39:17 -04:00
|
|
|
test("new user cannot upload images", function() {
|
2013-07-04 18:43:54 -04:00
|
|
|
Discourse.SiteSettings.newuser_max_images = 0;
|
|
|
|
this.stub(Discourse.User, 'current').withArgs("trust_level").returns(0);
|
|
|
|
this.stub(bootbox, "alert");
|
|
|
|
|
2013-07-21 20:39:17 -04:00
|
|
|
ok(!validUpload([{name: "image.png"}]));
|
|
|
|
ok(bootbox.alert.calledWith(I18n.t('post.errors.image_upload_not_allowed_for_new_user')));
|
|
|
|
});
|
|
|
|
|
|
|
|
test("new user cannot upload attachments", function() {
|
|
|
|
Discourse.SiteSettings.newuser_max_attachments = 0;
|
|
|
|
this.stub(Discourse.User, 'current').withArgs("trust_level").returns(0);
|
|
|
|
this.stub(bootbox, "alert");
|
|
|
|
|
|
|
|
ok(!validUpload([{name: "roman.txt"}]));
|
|
|
|
ok(bootbox.alert.calledWith(I18n.t('post.errors.attachment_upload_not_allowed_for_new_user')));
|
2013-07-04 18:43:54 -04:00
|
|
|
});
|
|
|
|
|
2013-06-30 20:19:03 -04:00
|
|
|
test("ensures an authorized upload", function() {
|
|
|
|
var html = { name: "unauthorized.html" };
|
|
|
|
var extensions = Discourse.SiteSettings.authorized_extensions.replace(/\|/g, ", ");
|
2013-06-19 15:06:23 -04:00
|
|
|
this.stub(bootbox, "alert");
|
|
|
|
|
|
|
|
ok(!validUpload([html]));
|
2013-07-08 19:32:16 -04:00
|
|
|
ok(bootbox.alert.calledWith(I18n.t('post.errors.upload_not_authorized', { authorized_extensions: extensions })));
|
2013-06-19 15:06:23 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
test("prevents files that are too big from being uploaded", function() {
|
2013-06-30 20:19:03 -04:00
|
|
|
var image = { name: "image.png", size: 10 * 1024 };
|
2013-07-15 19:59:23 -04:00
|
|
|
Discourse.SiteSettings.max_image_size_kb = 5;
|
2013-06-19 15:06:23 -04:00
|
|
|
this.stub(bootbox, "alert");
|
|
|
|
|
|
|
|
ok(!validUpload([image]));
|
2013-07-23 18:54:18 -04:00
|
|
|
ok(bootbox.alert.calledWith(I18n.t('post.errors.image_too_large', { max_size_kb: 5 })));
|
2013-06-19 15:06:23 -04:00
|
|
|
});
|
|
|
|
|
2013-07-03 18:39:23 -04:00
|
|
|
var dummyBlob = function() {
|
2013-07-10 16:59:16 -04:00
|
|
|
var BlobBuilder = window.BlobBuilder || window.WebKitBlobBuilder || window.MozBlobBuilder || window.MSBlobBuilder;
|
|
|
|
if (BlobBuilder) {
|
|
|
|
var bb = new BlobBuilder();
|
2013-07-03 18:39:23 -04:00
|
|
|
bb.append([1]);
|
|
|
|
return bb.getBlob("image/png");
|
|
|
|
} else {
|
|
|
|
return new Blob([1], { "type" : "image\/png" });
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-06-19 15:06:23 -04:00
|
|
|
test("allows valid uploads to go through", function() {
|
2013-07-15 19:59:23 -04:00
|
|
|
Discourse.SiteSettings.max_image_size_kb = 15;
|
2013-06-19 15:06:23 -04:00
|
|
|
this.stub(bootbox, "alert");
|
|
|
|
|
2013-07-03 18:39:23 -04:00
|
|
|
// image
|
|
|
|
var image = { name: "image.png", size: 10 * 1024 };
|
2013-06-19 15:06:23 -04:00
|
|
|
ok(validUpload([image]));
|
2013-07-03 18:39:23 -04:00
|
|
|
// pasted image
|
|
|
|
var pastedImage = dummyBlob();
|
|
|
|
ok(validUpload([pastedImage]));
|
|
|
|
|
2013-06-19 15:06:23 -04:00
|
|
|
ok(!bootbox.alert.calledOnce);
|
|
|
|
});
|
2013-06-30 20:19:03 -04:00
|
|
|
|
2013-07-10 16:59:16 -04:00
|
|
|
var getUploadMarkdown = function(filename) {
|
|
|
|
return utils.getUploadMarkdown({
|
|
|
|
original_filename: filename,
|
2013-07-14 06:28:24 -04:00
|
|
|
filesize: 42,
|
2013-07-10 16:59:16 -04:00
|
|
|
width: 100,
|
|
|
|
height: 200,
|
2013-07-18 19:26:23 -04:00
|
|
|
url: "/uploads/123/abcdef.ext"
|
2013-07-10 16:59:16 -04:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
test("getUploadMarkdown", function() {
|
2013-07-18 19:26:23 -04:00
|
|
|
ok(getUploadMarkdown("lolcat.gif") === '<img src="/uploads/123/abcdef.ext" width="100" height="200">');
|
|
|
|
ok(getUploadMarkdown("important.txt") === '<a class="attachment" href="/uploads/123/abcdef.ext">important.txt</a><span class="size">(42 Bytes)</span>');
|
2013-07-10 16:59:16 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
test("isAnImage", function() {
|
2013-07-15 19:59:23 -04:00
|
|
|
_.each(["png", "jpg", "jpeg", "bmp", "gif", "tif", "tiff"], function(extension) {
|
2013-07-10 16:59:16 -04:00
|
|
|
var image = "image." + extension;
|
2013-07-15 19:59:23 -04: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 16:59:16 -04:00
|
|
|
});
|
|
|
|
ok(!utils.isAnImage("file.txt"));
|
|
|
|
ok(!utils.isAnImage("http://foo.bar/path/to/file.txt"));
|
|
|
|
ok(!utils.isAnImage(""));
|
|
|
|
});
|
2013-07-18 12:03:09 -04:00
|
|
|
|
|
|
|
test("avatarUrl", function() {
|
|
|
|
blank(Discourse.Utilities.avatarUrl('', 'tiny'), "no avatar url returns blank");
|
|
|
|
blank(Discourse.Utilities.avatarUrl('this is not a username', 'tiny'), "invalid username returns blank");
|
|
|
|
|
|
|
|
equal(Discourse.Utilities.avatarUrl('eviltrout', 'tiny'), "/users/eviltrout/avatar/20?__ws=", "simple avatar url");
|
|
|
|
equal(Discourse.Utilities.avatarUrl('eviltrout', 'large'), "/users/eviltrout/avatar/45?__ws=", "different size");
|
|
|
|
equal(Discourse.Utilities.avatarUrl('EvilTrout', 'tiny'), "/users/eviltrout/avatar/20?__ws=", "lowercases username");
|
|
|
|
equal(Discourse.Utilities.avatarUrl('eviltrout', 'tiny', 'test{size}'), "test20", "replaces the size in a template");
|
|
|
|
});
|
|
|
|
|
|
|
|
test("avatarUrl with a baseUrl", function() {
|
|
|
|
Discourse.BaseUrl = "http://try.discourse.org";
|
|
|
|
equal(Discourse.Utilities.avatarUrl('eviltrout', 'tiny'), "/users/eviltrout/avatar/20?__ws=http%3A%2F%2Ftry.discourse.org", "simple avatar url");
|
|
|
|
});
|
|
|
|
|
|
|
|
test("avatarImg", function() {
|
|
|
|
equal(Discourse.Utilities.avatarImg({username: 'eviltrout', size: 'tiny'}),
|
|
|
|
"<img width='20' height='20' src='/users/eviltrout/avatar/20?__ws=' class='avatar'>",
|
|
|
|
"it returns the avatar html");
|
|
|
|
|
|
|
|
equal(Discourse.Utilities.avatarImg({username: 'eviltrout', size: 'tiny', title: 'evilest trout'}),
|
|
|
|
"<img width='20' height='20' src='/users/eviltrout/avatar/20?__ws=' class='avatar' title='evilest trout'>",
|
|
|
|
"it adds a title if supplied");
|
|
|
|
|
|
|
|
equal(Discourse.Utilities.avatarImg({username: 'eviltrout', size: 'tiny', extraClasses: 'evil fish'}),
|
|
|
|
"<img width='20' height='20' src='/users/eviltrout/avatar/20?__ws=' class='avatar evil fish'>",
|
|
|
|
"it adds extra classes if supplied");
|
|
|
|
|
|
|
|
blank(Discourse.Utilities.avatarImg({username: 'weird*username', size: 'tiny'}),
|
|
|
|
"it doesn't render avatars for invalid usernames");
|
2013-07-21 20:39:17 -04:00
|
|
|
});
|