thelounge/test/plugins/link.js

767 lines
21 KiB
JavaScript
Raw Normal View History

"use strict";
2017-06-26 09:01:55 +00:00
const path = require("path");
const expect = require("chai").expect;
const util = require("../util");
const Config = require("../../src/config");
const link = require("../../src/plugins/irc-events/link.js");
2015-09-30 22:39:57 +00:00
describe("Link plugin", function () {
// Increase timeout due to unpredictable I/O on CI services
this.timeout(util.isRunningOnCI() ? 25000 : 5000);
this.slow(300);
2019-12-19 11:40:32 +00:00
const loremIpsum = `Lorem ipsum dolor sit amet, consectetur adipiscing elit.\
Vivamus at pretium mauris. Aenean eu orci id erat pulvinar\
commodo cursus ac augue. Ut dui quam, tempus ac felis et,\
efficitur auctor nisl. Sed semper sit amet metus eu fringilla.\
Vivamus vitae ligula quis eros rutrum tristique. Suspendisse\
luctus molestie tortor, in finibus nulla rutrum quis. Proin\
congue ut elit eget congue. Nam pretium blandit nibh nec laoreet.\
Suspendisse vehicula turpis urna, sit amet molestie diam dapibus in.\
Vivamus bibendum vulputate tincidunt. Sed vitae ligula felis.`;
let app;
beforeEach(function (done) {
app = util.createWebserver();
app.get("/real-test-image.png", function (req, res) {
res.sendFile(path.resolve(__dirname, "../../client/img/logo-grey-bg-120x120px.png"));
2017-06-26 09:01:55 +00:00
});
this.connection = app.listen(0, () => {
this.port = this.connection.address().port;
done();
});
2015-09-30 22:39:57 +00:00
this.irc = util.createClient();
this.network = util.createNetwork();
Config.values.prefetchStorage = false;
2015-09-30 22:39:57 +00:00
});
afterEach(function (done) {
this.connection.close(done);
});
it("should be able to fetch basic information about URLs", function (done) {
const url = "http://localhost:" + this.port + "/basic";
const message = this.irc.createMessage({
text: url,
});
2020-07-22 15:28:12 +00:00
link(this.irc, this.network.channels[0], message, message.text);
2015-09-30 22:39:57 +00:00
2019-07-17 09:33:59 +00:00
expect(message.previews).to.deep.equal([
{
body: "",
head: "",
link: url,
thumb: "",
2019-08-09 20:20:08 +00:00
size: -1,
2019-07-17 09:33:59 +00:00
type: "loading",
shown: null,
2019-07-17 09:33:59 +00:00
},
]);
app.get("/basic", function (req, res) {
2019-07-17 09:33:59 +00:00
res.send(
"<title>test title</title><meta name='description' content='simple description'>"
);
2017-06-26 09:01:55 +00:00
});
this.irc.once("msg:preview", function (data) {
expect(data.preview.type).to.equal("link");
expect(data.preview.head).to.equal("test title");
expect(data.preview.body).to.equal("simple description");
expect(data.preview.link).to.equal(url);
expect(message.previews).to.deep.equal([data.preview]);
2017-06-26 09:01:55 +00:00
done();
});
});
it("should be able to display body for text/plain", function (done) {
2019-12-19 11:40:32 +00:00
const url = "http://localhost:" + this.port + "/basic-text";
const message = this.irc.createMessage({
text: url,
});
2020-07-22 15:28:12 +00:00
link(this.irc, this.network.channels[0], message, message.text);
2019-12-19 11:40:32 +00:00
expect(message.previews).to.deep.equal([
{
body: "",
head: "",
link: url,
thumb: "",
size: -1,
type: "loading",
shown: null,
2019-12-19 11:40:32 +00:00
},
]);
app.get("/basic-text", function (req, res) {
2019-12-19 11:40:32 +00:00
res.type("text").send(loremIpsum);
});
this.irc.once("msg:preview", function (data) {
2019-12-19 11:40:32 +00:00
expect(data.preview.type).to.equal("link");
expect(data.preview.head).to.equal("Untitled page");
expect(data.preview.body).to.equal(loremIpsum.substring(0, 300));
expect(data.preview.body).to.have.length(300);
expect(data.preview.link).to.equal(url);
expect(message.previews).to.deep.equal([data.preview]);
done();
});
});
it("should truncate head and body", function (done) {
2019-12-19 11:40:32 +00:00
const url = "http://localhost:" + this.port + "/truncate";
const message = this.irc.createMessage({
text: url,
});
2020-07-22 15:28:12 +00:00
link(this.irc, this.network.channels[0], message, message.text);
2019-12-19 11:40:32 +00:00
app.get("/truncate", function (req, res) {
2019-12-19 11:40:32 +00:00
res.send(
`<title>${loremIpsum}</title><meta name='description' content='${loremIpsum}'>`
);
});
this.irc.once("msg:preview", function (data) {
2019-12-19 11:40:32 +00:00
expect(data.preview.type).to.equal("link");
expect(data.preview.head).to.equal(loremIpsum.substring(0, 100));
expect(data.preview.body).to.equal(loremIpsum.substring(0, 300));
expect(data.preview.link).to.equal(url);
expect(message.previews).to.deep.equal([data.preview]);
done();
});
});
it("should prefer og:title over title", function (done) {
2017-06-26 09:01:55 +00:00
const message = this.irc.createMessage({
text: "http://localhost:" + this.port + "/basic-og",
2017-06-26 09:01:55 +00:00
});
2020-07-22 15:28:12 +00:00
link(this.irc, this.network.channels[0], message, message.text);
2017-06-26 09:01:55 +00:00
app.get("/basic-og", function (req, res) {
2017-06-26 09:01:55 +00:00
res.send("<title>test</title><meta property='og:title' content='opengraph test'>");
});
this.irc.once("msg:preview", function (data) {
expect(data.preview.head).to.equal("opengraph test");
done();
});
});
it("should find only the first matching tag", function (done) {
const message = this.irc.createMessage({
text: "http://localhost:" + this.port + "/duplicate-tags",
});
2020-07-22 15:28:12 +00:00
link(this.irc, this.network.channels[0], message, message.text);
app.get("/duplicate-tags", function (req, res) {
2019-07-17 09:33:59 +00:00
res.send(
"<title>test</title><title>magnifying glass icon</title><meta name='description' content='desc1'><meta name='description' content='desc2'>"
);
});
this.irc.once("msg:preview", function (data) {
expect(data.preview.head).to.equal("test");
expect(data.preview.body).to.equal("desc1");
2017-06-26 09:01:55 +00:00
done();
});
});
it("should prefer og:description over description", function (done) {
2017-06-26 09:01:55 +00:00
const message = this.irc.createMessage({
text: "http://localhost:" + this.port + "/description-og",
2017-06-26 09:01:55 +00:00
});
2020-07-22 15:28:12 +00:00
link(this.irc, this.network.channels[0], message, message.text);
2017-06-26 09:01:55 +00:00
app.get("/description-og", function (req, res) {
2019-07-17 09:33:59 +00:00
res.send(
"<meta name='description' content='simple description'><meta property='og:description' content='opengraph description'>"
);
2017-06-26 09:01:55 +00:00
});
this.irc.once("msg:preview", function (data) {
expect(data.preview.body).to.equal("opengraph description");
2017-06-26 09:01:55 +00:00
done();
});
});
it("should find og:image with full url", function (done) {
const port = this.port;
2017-06-26 09:01:55 +00:00
const message = this.irc.createMessage({
text: "http://localhost:" + this.port + "/thumb",
2017-06-26 09:01:55 +00:00
});
2020-07-22 15:28:12 +00:00
link(this.irc, this.network.channels[0], message, message.text);
2017-06-26 09:01:55 +00:00
app.get("/thumb", function (req, res) {
2019-07-17 09:33:59 +00:00
res.send(
"<title>Google</title><meta property='og:image' content='http://localhost:" +
port +
"/real-test-image.png'>"
2019-07-17 09:33:59 +00:00
);
2017-06-26 09:01:55 +00:00
});
this.irc.once("msg:preview", function (data) {
expect(data.preview.head).to.equal("Google");
expect(data.preview.thumb).to.equal(
"http://localhost:" + port + "/real-test-image.png"
);
2017-06-26 09:01:55 +00:00
done();
});
});
describe("test disableMediaPreview", function () {
beforeEach(function (done) {
Config.values.disableMediaPreview = true;
done();
});
afterEach(function (done) {
Config.values.disableMediaPreview = false;
done();
});
it("should ignore og:image if disableMediaPreview", function (done) {
const port = this.port;
app.get("/nonexistent-test-image.png", function () {
throw "Should not fetch image";
});
app.get("/thumb", function (req, res) {
res.send(
"<title>Google</title><meta property='og:image' content='http://localhost:" +
port +
"/nonexistent-test-image.png'>"
);
});
const message = this.irc.createMessage({
text: "http://localhost:" + port + "/thumb",
});
2020-07-22 15:28:12 +00:00
link(this.irc, this.network.channels[0], message, message.text);
this.irc.once("msg:preview", function (data) {
expect(data.preview.head).to.equal("Google");
expect(data.preview.type).to.equal("link");
expect(data.preview.thumb).to.equal("");
done();
});
});
it("should ignore og:video if disableMediaPreview", function (done) {
const port = this.port;
app.get("/nonexistent-video.mpr", function () {
throw "Should not fetch video";
});
app.get("/thumb", function (req, res) {
res.send(
"<title>Google</title><meta property='og:video:type' content='video/mp4'><meta property='og:video' content='http://localhost:" +
port +
"/nonexistent-video.mp4'>"
);
});
const message = this.irc.createMessage({
text: "http://localhost:" + port + "/thumb",
});
2020-07-22 15:28:12 +00:00
link(this.irc, this.network.channels[0], message, message.text);
this.irc.once("msg:preview", function (data) {
expect(data.preview.head).to.equal("Google");
expect(data.preview.type).to.equal("link");
done();
});
});
});
it("should find image_src", function (done) {
const port = this.port;
2017-06-26 09:01:55 +00:00
const message = this.irc.createMessage({
text: "http://localhost:" + this.port + "/thumb-image-src",
2017-06-26 09:01:55 +00:00
});
2020-07-22 15:28:12 +00:00
link(this.irc, this.network.channels[0], message, message.text);
2017-06-26 09:01:55 +00:00
app.get("/thumb-image-src", function (req, res) {
res.send(
"<link rel='image_src' href='http://localhost:" + port + "/real-test-image.png'>"
);
2017-06-26 09:01:55 +00:00
});
this.irc.once("msg:preview", function (data) {
expect(data.preview.thumb).to.equal(
"http://localhost:" + port + "/real-test-image.png"
);
done();
});
});
it("should correctly resolve relative protocol", function (done) {
const port = this.port;
const message = this.irc.createMessage({
text: "http://localhost:" + this.port + "/thumb-image-src",
});
2020-07-22 15:28:12 +00:00
link(this.irc, this.network.channels[0], message, message.text);
app.get("/thumb-image-src", function (req, res) {
res.send("<link rel='image_src' href='//localhost:" + port + "/real-test-image.png'>");
});
this.irc.once("msg:preview", function (data) {
expect(data.preview.thumb).to.equal(
"http://localhost:" + port + "/real-test-image.png"
);
done();
});
});
it("should resolve url correctly for relative url", function (done) {
const port = this.port;
const message = this.irc.createMessage({
text: "http://localhost:" + this.port + "/relative-thumb",
});
2020-07-22 15:28:12 +00:00
link(this.irc, this.network.channels[0], message, message.text);
app.get("/relative-thumb", function (req, res) {
2019-07-17 09:33:59 +00:00
res.send(
"<title>test relative image</title><meta property='og:image' content='/real-test-image.png'>"
);
});
this.irc.once("msg:preview", function (data) {
expect(data.preview.thumb).to.equal(
"http://localhost:" + port + "/real-test-image.png"
);
expect(data.preview.head).to.equal("test relative image");
expect(data.preview.link).to.equal("http://localhost:" + port + "/relative-thumb");
2017-06-26 09:01:55 +00:00
done();
});
});
it("should send untitled page if there is a thumbnail", function (done) {
const port = this.port;
2017-06-26 09:01:55 +00:00
const message = this.irc.createMessage({
text: "http://localhost:" + this.port + "/thumb-no-title",
2017-06-26 09:01:55 +00:00
});
2020-07-22 15:28:12 +00:00
link(this.irc, this.network.channels[0], message, message.text);
2017-06-26 09:01:55 +00:00
app.get("/thumb-no-title", function (req, res) {
2019-07-17 09:33:59 +00:00
res.send(
"<meta property='og:image' content='http://localhost:" +
port +
"/real-test-image.png'>"
2019-07-17 09:33:59 +00:00
);
2017-06-26 09:01:55 +00:00
});
this.irc.once("msg:preview", function (data) {
expect(data.preview.head).to.equal("Untitled page");
expect(data.preview.thumb).to.equal(
"http://localhost:" + port + "/real-test-image.png"
);
expect(data.preview.link).to.equal("http://localhost:" + port + "/thumb-no-title");
2017-06-26 09:01:55 +00:00
done();
});
});
it("should send untitled page if there is body", function (done) {
2019-12-19 11:40:32 +00:00
const port = this.port;
const message = this.irc.createMessage({
text: "http://localhost:" + this.port + "/body-no-title",
});
2020-07-22 15:28:12 +00:00
link(this.irc, this.network.channels[0], message, message.text);
2019-12-19 11:40:32 +00:00
app.get("/body-no-title", function (req, res) {
2019-12-19 11:40:32 +00:00
res.send("<meta name='description' content='hello world'>");
});
this.irc.once("msg:preview", function (data) {
2019-12-19 11:40:32 +00:00
expect(data.preview.head).to.equal("Untitled page");
expect(data.preview.body).to.equal("hello world");
expect(data.preview.thumb).to.equal("");
expect(data.preview.link).to.equal("http://localhost:" + port + "/body-no-title");
done();
});
});
it("should not send thumbnail if image is 404", function (done) {
const port = this.port;
2017-06-26 09:01:55 +00:00
const message = this.irc.createMessage({
text: "http://localhost:" + this.port + "/thumb-404",
2017-06-26 09:01:55 +00:00
});
2020-07-22 15:28:12 +00:00
link(this.irc, this.network.channels[0], message, message.text);
2017-06-26 09:01:55 +00:00
app.get("/thumb-404", function (req, res) {
2019-07-17 09:33:59 +00:00
res.send(
"<title>404 image</title><meta property='og:image' content='http://localhost:" +
port +
"/this-image-does-not-exist.png'>"
2019-07-17 09:33:59 +00:00
);
2015-09-30 22:39:57 +00:00
});
this.irc.once("msg:preview", function (data) {
expect(data.preview.head).to.equal("404 image");
expect(data.preview.link).to.equal("http://localhost:" + port + "/thumb-404");
expect(data.preview.thumb).to.be.empty;
2017-06-26 09:01:55 +00:00
done();
});
});
it("should send image preview", function (done) {
const port = this.port;
2017-06-26 09:01:55 +00:00
const message = this.irc.createMessage({
text: "http://localhost:" + port + "/real-test-image.png",
2017-06-26 09:01:55 +00:00
});
2020-07-22 15:28:12 +00:00
link(this.irc, this.network.channels[0], message, message.text);
2017-06-26 09:01:55 +00:00
this.irc.once("msg:preview", function (data) {
expect(data.preview.type).to.equal("image");
expect(data.preview.link).to.equal("http://localhost:" + port + "/real-test-image.png");
expect(data.preview.thumb).to.equal(
"http://localhost:" + port + "/real-test-image.png"
);
2019-08-09 20:20:08 +00:00
expect(data.preview.size).to.equal(960);
2015-09-30 22:39:57 +00:00
done();
});
});
it("should load multiple URLs found in messages", function (done) {
const port = this.port;
const message = this.irc.createMessage({
text: "http://localhost:" + port + "/one http://localhost:" + this.port + "/two",
});
2020-07-22 15:28:12 +00:00
link(this.irc, this.network.channels[0], message, message.text);
2019-07-17 09:33:59 +00:00
expect(message.previews).to.eql([
{
body: "",
head: "",
link: "http://localhost:" + port + "/one",
2019-07-17 09:33:59 +00:00
thumb: "",
2019-08-09 20:20:08 +00:00
size: -1,
2019-07-17 09:33:59 +00:00
type: "loading",
shown: null,
2019-07-17 09:33:59 +00:00
},
{
body: "",
head: "",
link: "http://localhost:" + port + "/two",
2019-07-17 09:33:59 +00:00
thumb: "",
2019-08-09 20:20:08 +00:00
size: -1,
2019-07-17 09:33:59 +00:00
type: "loading",
shown: null,
2019-07-17 09:33:59 +00:00
},
]);
app.get("/one", function (req, res) {
res.send("<title>first title</title>");
});
app.get("/two", function (req, res) {
res.send("<title>second title</title>");
});
const previews = [];
this.irc.on("msg:preview", function (data) {
if (data.preview.link === "http://localhost:" + port + "/one") {
expect(data.preview.head).to.equal("first title");
previews[0] = data.preview;
} else if (data.preview.link === "http://localhost:" + port + "/two") {
expect(data.preview.head).to.equal("second title");
previews[1] = data.preview;
}
if (previews[0] && previews[1]) {
expect(message.previews).to.eql(previews);
done();
}
});
});
it("should use client's preferred language as Accept-Language header", function (done) {
const language = "sv,en-GB;q=0.9,en;q=0.8";
this.irc.config.browser.language = language;
app.get("/language-check", function (req, res) {
expect(req.headers["accept-language"]).to.equal(language);
res.send();
done();
});
const message = this.irc.createMessage({
text: "http://localhost:" + this.port + "/language-check",
});
2020-07-22 15:28:12 +00:00
link(this.irc, this.network.channels[0], message, message.text);
});
it("should send accept text/html for initial request", function (done) {
app.get("/accept-header-html", function (req, res) {
2019-07-17 09:33:59 +00:00
expect(req.headers.accept).to.equal(
"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
);
2018-03-23 14:50:52 +00:00
res.send();
done();
});
const message = this.irc.createMessage({
text: "http://localhost:" + this.port + "/accept-header-html",
2018-03-23 14:50:52 +00:00
});
2020-07-22 15:28:12 +00:00
link(this.irc, this.network.channels[0], message, message.text);
2018-03-23 14:50:52 +00:00
});
it("should send accept */* for meta image", function (done) {
const port = this.port;
app.get("/accept-header-thumb", function (req, res) {
2019-07-17 09:33:59 +00:00
res.send(
"<title>404 image</title><meta property='og:image' content='http://localhost:" +
port +
"/accept-header-thumb.png'>"
2019-07-17 09:33:59 +00:00
);
2018-03-23 14:50:52 +00:00
});
app.get("/accept-header-thumb.png", function (req, res) {
2018-03-23 14:50:52 +00:00
expect(req.headers.accept).to.equal("*/*");
res.send();
done();
});
const message = this.irc.createMessage({
text: "http://localhost:" + port + "/accept-header-thumb",
2018-03-23 14:50:52 +00:00
});
2020-07-22 15:28:12 +00:00
link(this.irc, this.network.channels[0], message, message.text);
2018-03-23 14:50:52 +00:00
});
it("should not add slash to url", function (done) {
const port = this.port;
const message = this.irc.createMessage({
text: "http://localhost:" + port + "",
});
2020-07-22 15:28:12 +00:00
link(this.irc, this.network.channels[0], message, message.text);
this.irc.once("msg:preview", function (data) {
expect(data.preview.link).to.equal("http://localhost:" + port + "");
done();
});
});
it("should work on non-ASCII urls", function (done) {
const message = this.irc.createMessage({
text:
"http://localhost:" +
this.port +
"/unicode/ıoı-test " +
"http://localhost:" +
this.port +
"/unicode/русский-текст-test " +
"http://localhost:" +
this.port +
"/unicode/🙈-emoji-test " +
"http://localhost:" +
this.port +
"/unicodeq/?q=ıoı-test " +
"http://localhost:" +
this.port +
"/unicodeq/?q=русский-текст-test " +
"http://localhost:" +
this.port +
"/unicodeq/?q=🙈-emoji-test",
});
2020-07-22 15:28:12 +00:00
link(this.irc, this.network.channels[0], message, message.text);
app.get("/unicode/:q", function (req, res) {
res.send(`<title>${req.params.q}</title>`);
});
app.get("/unicodeq/", function (req, res) {
res.send(`<title>${req.query.q}</title>`);
});
const previews = [];
this.irc.on("msg:preview", function (data) {
previews.push(data.preview.link);
if (data.preview.link.includes("ıoı-test")) {
expect(data.preview.head).to.equal("ıoı-test");
} else if (data.preview.link.includes("русский-текст-test")) {
expect(data.preview.head).to.equal("русский-текст-test");
} else if (data.preview.link.includes("🙈-emoji-test")) {
expect(data.preview.head).to.equal("🙈-emoji-test");
} else {
expect("This should never happen").to.equal(data.preview.link);
}
if (previews.length === 5) {
expect(message.previews.map((preview) => preview.link)).to.have.members(previews);
done();
}
});
});
it("should not fetch links without a schema", function () {
const port = this.port;
const message = this.irc.createMessage({
text: `//localhost:${port} localhost:${port} //localhost:${port}/test localhost:${port}/test`,
});
2020-07-22 15:28:12 +00:00
link(this.irc, this.network.channels[0], message, message.text);
expect(message.previews).to.be.empty;
});
it("should de-duplicate links", function (done) {
const port = this.port;
const message = this.irc.createMessage({
text:
"//localhost:" +
port +
" http://localhost:" +
port +
" http://localhost:" +
port +
"",
});
2020-07-22 15:28:12 +00:00
link(this.irc, this.network.channels[0], message, message.text);
2019-07-17 09:33:59 +00:00
expect(message.previews).to.deep.equal([
{
type: "loading",
head: "",
body: "",
thumb: "",
2019-08-09 20:20:08 +00:00
size: -1,
link: "http://localhost:" + port + "",
shown: null,
2019-07-17 09:33:59 +00:00
},
]);
this.irc.once("msg:preview", function (data) {
expect(data.preview.link).to.equal("http://localhost:" + port + "");
2019-12-23 10:15:23 +00:00
expect(data.preview.type).to.equal("error");
done();
});
});
it("should not try to fetch links with wrong protocol", function () {
const message = this.irc.createMessage({
text: "ssh://example.com ftp://example.com irc://example.com http:////////example.com",
});
expect(message.previews).to.be.empty;
});
it("should not try to fetch links with username or password", function () {
const message = this.irc.createMessage({
2022-02-09 23:27:34 +00:00
text: "http://root:'some%pass'@hostname/database http://a:%p@c http://a:%p@example.com http://test@example.com",
});
expect(message.previews).to.be.empty;
});
it("should fetch same link only once at the same time", function (done) {
const message = this.irc.createMessage({
text: "http://localhost:" + this.port + "/basic-og-once",
});
let requests = 0;
let responses = 0;
this.irc.config.browser.language = "very nice language";
2020-07-22 15:28:12 +00:00
link(this.irc, this.network.channels[0], message, message.text);
link(this.irc, this.network.channels[0], message, message.text);
process.nextTick(() => link(this.irc, this.network.channels[0], message, message.text));
app.get("/basic-og-once", function (req, res) {
requests++;
expect(req.header("accept-language")).to.equal("very nice language");
// delay the request so it doesn't resolve immediately
setTimeout(() => {
res.send("<title>test prefetch</title>");
}, 100);
});
const cb = (data) => {
responses++;
expect(data.preview.head, "test prefetch");
if (responses === 3) {
this.irc.removeListener("msg:preview", cb);
expect(requests).to.equal(1);
done();
}
};
this.irc.on("msg:preview", cb);
});
it("should fetch same link with different languages multiple times", function (done) {
const message = this.irc.createMessage({
text: "http://localhost:" + this.port + "/basic-og-once-lang",
});
const requests = [];
let responses = 0;
this.irc.config.browser.language = "first language";
2020-07-22 15:28:12 +00:00
link(this.irc, this.network.channels[0], message, message.text);
setTimeout(() => {
this.irc.config.browser.language = "second language";
2020-07-22 15:28:12 +00:00
link(this.irc, this.network.channels[0], message, message.text);
}, 100);
app.get("/basic-og-once-lang", function (req, res) {
requests.push(req.header("accept-language"));
// delay the request so it doesn't resolve immediately
setTimeout(() => {
res.send("<title>test prefetch</title>");
}, 100);
});
const cb = (data) => {
responses++;
expect(data.preview.head, "test prefetch");
if (responses === 2) {
this.irc.removeListener("msg:preview", cb);
2019-07-17 09:33:59 +00:00
expect(requests).to.deep.equal(["first language", "second language"]);
done();
}
};
this.irc.on("msg:preview", cb);
});
});