2019-05-28 21:00:25 -04:00
|
|
|
import {
|
|
|
|
lookupCachedUploadUrl,
|
|
|
|
resolveAllShortUrls,
|
|
|
|
resetCache
|
|
|
|
} from "pretty-text/upload-short-url";
|
|
|
|
import { ajax } from "discourse/lib/ajax";
|
2019-06-06 07:10:41 -04:00
|
|
|
import { fixture } from "helpers/qunit-helpers";
|
2020-03-02 14:24:31 -05:00
|
|
|
import pretender from "helpers/create-pretender";
|
2019-05-28 21:00:25 -04:00
|
|
|
|
2020-03-03 18:11:08 -05:00
|
|
|
function stubUrls(imageSrcs, attachmentSrcs, otherMediaSrcs) {
|
|
|
|
const response = object => {
|
|
|
|
return [200, { "Content-Type": "application/json" }, object];
|
|
|
|
};
|
|
|
|
if (!imageSrcs) {
|
|
|
|
imageSrcs = [
|
2019-05-28 21:00:25 -04:00
|
|
|
{
|
|
|
|
short_url: "upload://a.jpeg",
|
2020-07-17 14:12:47 -04:00
|
|
|
url: "/images/avatar.png?a",
|
2019-05-28 21:00:25 -04:00
|
|
|
short_path: "/uploads/short-url/a.jpeg"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
short_url: "upload://b.jpeg",
|
2020-07-17 14:12:47 -04:00
|
|
|
url: "/images/avatar.png?b",
|
2019-05-28 21:00:25 -04:00
|
|
|
short_path: "/uploads/short-url/b.jpeg"
|
2020-03-02 23:44:01 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
short_url: "upload://z.jpeg",
|
2020-07-17 14:12:47 -04:00
|
|
|
url: "/images/avatar.png?z",
|
2020-03-02 23:44:01 -05:00
|
|
|
short_path: "/uploads/short-url/z.jpeg"
|
2019-05-28 21:00:25 -04:00
|
|
|
}
|
|
|
|
];
|
2020-03-03 18:11:08 -05:00
|
|
|
}
|
2019-05-28 21:00:25 -04:00
|
|
|
|
2020-03-03 18:11:08 -05:00
|
|
|
if (!attachmentSrcs) {
|
|
|
|
attachmentSrcs = [
|
2019-05-28 21:00:25 -04:00
|
|
|
{
|
|
|
|
short_url: "upload://c.pdf",
|
|
|
|
url: "/uploads/default/original/3X/c/b/3.pdf",
|
|
|
|
short_path: "/uploads/short-url/c.pdf"
|
|
|
|
}
|
|
|
|
];
|
2020-03-03 18:11:08 -05:00
|
|
|
}
|
2019-05-28 21:00:25 -04:00
|
|
|
|
2020-03-03 18:11:08 -05:00
|
|
|
if (!otherMediaSrcs) {
|
|
|
|
otherMediaSrcs = [
|
2020-01-22 18:41:39 -05:00
|
|
|
{
|
|
|
|
short_url: "upload://d.mp4",
|
|
|
|
url: "/uploads/default/original/3X/c/b/4.mp4",
|
|
|
|
short_path: "/uploads/short-url/d.mp4"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
short_url: "upload://e.mp3",
|
|
|
|
url: "/uploads/default/original/3X/c/b/5.mp3",
|
|
|
|
short_path: "/uploads/short-url/e.mp3"
|
|
|
|
}
|
|
|
|
];
|
2020-03-03 18:11:08 -05:00
|
|
|
}
|
|
|
|
// prettier-ignore
|
|
|
|
pretender.post("/uploads/lookup-urls", () => { //eslint-disable-line
|
|
|
|
return response(imageSrcs.concat(attachmentSrcs.concat(otherMediaSrcs)));
|
|
|
|
});
|
2020-01-22 18:41:39 -05:00
|
|
|
|
2020-03-03 18:11:08 -05:00
|
|
|
fixture().html(
|
|
|
|
imageSrcs.map(src => `<img data-orig-src="${src.short_url}"/>`).join("") +
|
|
|
|
attachmentSrcs
|
|
|
|
.map(
|
|
|
|
src =>
|
|
|
|
`<a data-orig-href="${src.short_url}">big enterprise contract.pdf</a>`
|
|
|
|
)
|
|
|
|
.join("") +
|
|
|
|
`<div class="scoped-area"><img data-orig-src="${imageSrcs[2].url}"></div>`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
QUnit.module("lib:pretty-text/upload-short-url", {
|
2019-05-28 21:00:25 -04:00
|
|
|
afterEach() {
|
|
|
|
resetCache();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
QUnit.test("resolveAllShortUrls", async assert => {
|
2020-03-03 18:11:08 -05:00
|
|
|
stubUrls();
|
2019-05-28 21:00:25 -04:00
|
|
|
let lookup;
|
|
|
|
|
|
|
|
lookup = lookupCachedUploadUrl("upload://a.jpeg");
|
|
|
|
assert.deepEqual(lookup, {});
|
|
|
|
|
2020-06-05 12:15:14 -04:00
|
|
|
await resolveAllShortUrls(ajax, { secure_media: false }, fixture()[0]);
|
2019-05-28 21:00:25 -04:00
|
|
|
|
|
|
|
lookup = lookupCachedUploadUrl("upload://a.jpeg");
|
|
|
|
|
|
|
|
assert.deepEqual(lookup, {
|
2020-07-17 14:12:47 -04:00
|
|
|
url: "/images/avatar.png?a",
|
2019-05-28 21:00:25 -04:00
|
|
|
short_path: "/uploads/short-url/a.jpeg"
|
|
|
|
});
|
|
|
|
|
|
|
|
lookup = lookupCachedUploadUrl("upload://b.jpeg");
|
|
|
|
|
|
|
|
assert.deepEqual(lookup, {
|
2020-07-17 14:12:47 -04:00
|
|
|
url: "/images/avatar.png?b",
|
2019-05-28 21:00:25 -04:00
|
|
|
short_path: "/uploads/short-url/b.jpeg"
|
|
|
|
});
|
|
|
|
|
|
|
|
lookup = lookupCachedUploadUrl("upload://c.jpeg");
|
|
|
|
assert.deepEqual(lookup, {});
|
|
|
|
|
|
|
|
lookup = lookupCachedUploadUrl("upload://c.pdf");
|
|
|
|
assert.deepEqual(lookup, {
|
|
|
|
url: "/uploads/default/original/3X/c/b/3.pdf",
|
|
|
|
short_path: "/uploads/short-url/c.pdf"
|
|
|
|
});
|
2020-01-22 18:41:39 -05:00
|
|
|
|
|
|
|
lookup = lookupCachedUploadUrl("upload://d.mp4");
|
|
|
|
assert.deepEqual(lookup, {
|
|
|
|
url: "/uploads/default/original/3X/c/b/4.mp4",
|
|
|
|
short_path: "/uploads/short-url/d.mp4"
|
|
|
|
});
|
|
|
|
|
|
|
|
lookup = lookupCachedUploadUrl("upload://e.mp3");
|
|
|
|
assert.deepEqual(lookup, {
|
|
|
|
url: "/uploads/default/original/3X/c/b/5.mp3",
|
|
|
|
short_path: "/uploads/short-url/e.mp3"
|
|
|
|
});
|
2019-05-28 21:00:25 -04:00
|
|
|
});
|
2020-03-02 23:44:01 -05:00
|
|
|
|
2020-03-03 18:11:08 -05:00
|
|
|
QUnit.test(
|
|
|
|
"resolveAllShortUrls - href + src replaced correctly",
|
|
|
|
async assert => {
|
|
|
|
stubUrls();
|
2020-06-05 12:15:14 -04:00
|
|
|
await resolveAllShortUrls(ajax, { secure_media: false }, fixture()[0]);
|
2020-03-03 18:11:08 -05:00
|
|
|
|
|
|
|
let image1 = fixture()
|
|
|
|
.find("img")
|
|
|
|
.eq(0);
|
|
|
|
let image2 = fixture()
|
|
|
|
.find("img")
|
|
|
|
.eq(1);
|
|
|
|
let link = fixture().find("a");
|
|
|
|
|
2020-07-17 14:12:47 -04:00
|
|
|
assert.equal(image1.attr("src"), "/images/avatar.png?a");
|
|
|
|
assert.equal(image2.attr("src"), "/images/avatar.png?b");
|
2020-03-03 18:11:08 -05:00
|
|
|
assert.equal(link.attr("href"), "/uploads/short-url/c.pdf");
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
QUnit.test(
|
|
|
|
"resolveAllShortUrls - when secure media is enabled use the attachment full URL",
|
|
|
|
async assert => {
|
|
|
|
stubUrls(
|
|
|
|
null,
|
|
|
|
[
|
|
|
|
{
|
|
|
|
short_url: "upload://c.pdf",
|
|
|
|
url: "/secure-media-uploads/default/original/3X/c/b/3.pdf",
|
|
|
|
short_path: "/uploads/short-url/c.pdf"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
null
|
|
|
|
);
|
2020-06-05 12:15:14 -04:00
|
|
|
await resolveAllShortUrls(ajax, { secure_media: true }, fixture()[0]);
|
2020-03-03 18:11:08 -05:00
|
|
|
|
|
|
|
let link = fixture().find("a");
|
|
|
|
assert.equal(
|
|
|
|
link.attr("href"),
|
|
|
|
"/secure-media-uploads/default/original/3X/c/b/3.pdf"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2020-03-02 23:44:01 -05:00
|
|
|
QUnit.test("resolveAllShortUrls - scoped", async assert => {
|
2020-03-03 18:11:08 -05:00
|
|
|
stubUrls();
|
2020-03-02 23:44:01 -05:00
|
|
|
let lookup;
|
2020-06-05 12:15:14 -04:00
|
|
|
|
|
|
|
let scopedElement = fixture()[0].querySelector(".scoped-area");
|
|
|
|
await resolveAllShortUrls(ajax, {}, scopedElement);
|
2020-03-02 23:44:01 -05:00
|
|
|
|
|
|
|
lookup = lookupCachedUploadUrl("upload://z.jpeg");
|
|
|
|
|
|
|
|
assert.deepEqual(lookup, {
|
2020-07-17 14:12:47 -04:00
|
|
|
url: "/images/avatar.png?z",
|
2020-03-02 23:44:01 -05:00
|
|
|
short_path: "/uploads/short-url/z.jpeg"
|
|
|
|
});
|
|
|
|
|
|
|
|
// do this because the pretender caches ALL the urls, not
|
|
|
|
// just the ones being looked up (like the normal behaviour)
|
|
|
|
resetCache();
|
2020-06-05 12:15:14 -04:00
|
|
|
await resolveAllShortUrls(ajax, {}, scopedElement);
|
2020-03-02 23:44:01 -05:00
|
|
|
|
|
|
|
lookup = lookupCachedUploadUrl("upload://a.jpeg");
|
|
|
|
assert.deepEqual(lookup, {});
|
|
|
|
});
|