2017-07-06 15:33:09 +00:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
const fs = require("fs");
|
|
|
|
const path = require("path");
|
|
|
|
const crypto = require("crypto");
|
|
|
|
const expect = require("chai").expect;
|
|
|
|
const util = require("../util");
|
|
|
|
const Helper = require("../../src/helper");
|
|
|
|
const link = require("../../src/plugins/irc-events/link.js");
|
|
|
|
|
2020-03-21 20:55:36 +00:00
|
|
|
describe("Image storage", function () {
|
2019-11-27 18:25:29 +00:00
|
|
|
// Increase timeout due to unpredictable I/O on CI services
|
2020-02-09 12:21:45 +00:00
|
|
|
this.timeout(util.isRunningOnCI() ? 25000 : 5000);
|
|
|
|
this.slow(300);
|
2017-11-27 23:47:19 +00:00
|
|
|
|
2018-01-28 07:20:24 +00:00
|
|
|
const testImagePath = path.resolve(__dirname, "../../client/img/logo-grey-bg-120x120px.png");
|
2019-07-17 09:33:59 +00:00
|
|
|
const correctImageHash = crypto
|
|
|
|
.createHash("sha256")
|
|
|
|
.update(fs.readFileSync(testImagePath))
|
|
|
|
.digest("hex");
|
|
|
|
const correctImageURL = `storage/${correctImageHash.substring(
|
|
|
|
0,
|
|
|
|
2
|
|
|
|
)}/${correctImageHash.substring(2, 4)}/${correctImageHash.substring(4)}.png`;
|
2017-07-06 15:33:09 +00:00
|
|
|
|
2018-01-28 07:20:24 +00:00
|
|
|
const testSvgPath = path.resolve(__dirname, "../../client/img/logo-grey-bg.svg");
|
2019-07-17 09:33:59 +00:00
|
|
|
const correctSvgHash = crypto
|
|
|
|
.createHash("sha256")
|
|
|
|
.update(fs.readFileSync(testSvgPath))
|
|
|
|
.digest("hex");
|
|
|
|
const correctSvgURL = `storage/${correctSvgHash.substring(0, 2)}/${correctSvgHash.substring(
|
|
|
|
2,
|
|
|
|
4
|
|
|
|
)}/${correctSvgHash.substring(4)}.svg`;
|
2017-12-30 10:46:51 +00:00
|
|
|
|
2020-03-21 20:55:36 +00:00
|
|
|
before(function (done) {
|
2017-07-06 15:33:09 +00:00
|
|
|
this.app = util.createWebserver();
|
2020-03-21 20:55:36 +00:00
|
|
|
this.app.get("/real-test-image.png", function (req, res) {
|
2017-07-06 15:33:09 +00:00
|
|
|
res.sendFile(testImagePath);
|
|
|
|
});
|
2020-03-21 20:55:36 +00:00
|
|
|
this.app.get("/logo.svg", function (req, res) {
|
2017-12-30 10:46:51 +00:00
|
|
|
res.sendFile(testSvgPath);
|
|
|
|
});
|
2019-10-04 11:19:04 +00:00
|
|
|
this.connection = this.app.listen(0, () => {
|
|
|
|
this.port = this.connection.address().port;
|
|
|
|
done();
|
|
|
|
});
|
2017-07-06 15:33:09 +00:00
|
|
|
});
|
|
|
|
|
2020-03-21 20:55:36 +00:00
|
|
|
after(function (done) {
|
2017-07-06 15:33:09 +00:00
|
|
|
this.connection.close(done);
|
|
|
|
});
|
|
|
|
|
2020-03-21 20:55:36 +00:00
|
|
|
beforeEach(function () {
|
2017-07-06 15:33:09 +00:00
|
|
|
this.irc = util.createClient();
|
|
|
|
this.network = util.createNetwork();
|
|
|
|
|
|
|
|
Helper.config.prefetchStorage = true;
|
|
|
|
});
|
|
|
|
|
2020-03-21 20:55:36 +00:00
|
|
|
afterEach(function () {
|
2019-10-31 09:01:44 +00:00
|
|
|
Helper.config.prefetchStorage = false;
|
|
|
|
});
|
|
|
|
|
2020-03-21 20:55:36 +00:00
|
|
|
it("should store the thumbnail", function (done) {
|
2019-10-04 11:19:04 +00:00
|
|
|
const port = this.port;
|
2017-07-06 15:33:09 +00:00
|
|
|
const message = this.irc.createMessage({
|
2019-10-04 11:19:04 +00:00
|
|
|
text: "http://localhost:" + port + "/thumb",
|
2017-07-06 15:33:09 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
link(this.irc, this.network.channels[0], message);
|
|
|
|
|
2020-03-21 20:55:36 +00:00
|
|
|
this.app.get("/thumb", function (req, res) {
|
2019-07-17 09:33:59 +00:00
|
|
|
res.send(
|
2019-10-04 11:19:04 +00:00
|
|
|
"<title>Google</title><meta property='og:image' content='http://localhost:" +
|
|
|
|
port +
|
|
|
|
"/real-test-image.png'>"
|
2019-07-17 09:33:59 +00:00
|
|
|
);
|
2017-07-06 15:33:09 +00:00
|
|
|
});
|
|
|
|
|
2020-03-21 20:55:36 +00:00
|
|
|
this.irc.once("msg:preview", function (data) {
|
2017-07-06 15:33:09 +00:00
|
|
|
expect(data.preview.head).to.equal("Google");
|
2019-10-04 11:19:04 +00:00
|
|
|
expect(data.preview.link).to.equal("http://localhost:" + port + "/thumb");
|
2017-07-06 15:33:09 +00:00
|
|
|
expect(data.preview.thumb).to.equal(correctImageURL);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-03-21 20:55:36 +00:00
|
|
|
it("should store the image", function (done) {
|
2019-10-04 11:19:04 +00:00
|
|
|
const port = this.port;
|
2017-07-06 15:33:09 +00:00
|
|
|
const message = this.irc.createMessage({
|
2019-10-04 11:19:04 +00:00
|
|
|
text: "http://localhost:" + port + "/real-test-image.png",
|
2017-07-06 15:33:09 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
link(this.irc, this.network.channels[0], message);
|
|
|
|
|
2020-03-21 20:55:36 +00:00
|
|
|
this.irc.once("msg:preview", function (data) {
|
2017-07-06 15:33:09 +00:00
|
|
|
expect(data.preview.type).to.equal("image");
|
2019-10-04 11:19:04 +00:00
|
|
|
expect(data.preview.link).to.equal("http://localhost:" + port + "/real-test-image.png");
|
2017-07-06 15:33:09 +00:00
|
|
|
expect(data.preview.thumb).to.equal(correctImageURL);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2017-12-30 10:46:51 +00:00
|
|
|
|
2020-03-21 20:55:36 +00:00
|
|
|
it("should lookup correct extension type", function (done) {
|
2019-10-04 11:19:04 +00:00
|
|
|
const port = this.port;
|
2017-12-30 10:46:51 +00:00
|
|
|
const message = this.irc.createMessage({
|
2019-10-04 11:19:04 +00:00
|
|
|
text: "http://localhost:" + port + "/svg-preview",
|
2017-12-30 10:46:51 +00:00
|
|
|
});
|
|
|
|
|
2020-03-21 20:55:36 +00:00
|
|
|
this.app.get("/svg-preview", function (req, res) {
|
2019-07-17 09:33:59 +00:00
|
|
|
res.send(
|
2019-10-04 11:19:04 +00:00
|
|
|
"<title>test title</title><meta property='og:image' content='http://localhost:" +
|
|
|
|
port +
|
|
|
|
"/logo.svg'>"
|
2019-07-17 09:33:59 +00:00
|
|
|
);
|
2017-12-30 10:46:51 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
link(this.irc, this.network.channels[0], message);
|
|
|
|
|
2020-03-21 20:55:36 +00:00
|
|
|
this.irc.once("msg:preview", function (data) {
|
2017-12-30 10:46:51 +00:00
|
|
|
expect(data.preview.type).to.equal("link");
|
2019-10-04 11:19:04 +00:00
|
|
|
expect(data.preview.link).to.equal("http://localhost:" + port + "/svg-preview");
|
2017-12-30 10:46:51 +00:00
|
|
|
expect(data.preview.thumb).to.equal(correctSvgURL);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2017-07-06 15:33:09 +00:00
|
|
|
});
|