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");
|
|
|
|
|
|
|
|
describe("Image storage", function() {
|
2017-11-27 23:47:19 +00:00
|
|
|
this.slow(200);
|
|
|
|
|
2018-01-28 07:20:24 +00:00
|
|
|
const testImagePath = path.resolve(__dirname, "../../client/img/logo-grey-bg-120x120px.png");
|
2017-07-06 15:33:09 +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`;
|
|
|
|
|
2018-01-28 07:20:24 +00:00
|
|
|
const testSvgPath = path.resolve(__dirname, "../../client/img/logo-grey-bg.svg");
|
2017-12-30 10:46:51 +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-07-06 15:33:09 +00:00
|
|
|
before(function(done) {
|
|
|
|
this.app = util.createWebserver();
|
|
|
|
this.app.get("/real-test-image.png", function(req, res) {
|
|
|
|
res.sendFile(testImagePath);
|
|
|
|
});
|
2017-12-30 10:46:51 +00:00
|
|
|
this.app.get("/logo.svg", function(req, res) {
|
|
|
|
res.sendFile(testSvgPath);
|
|
|
|
});
|
2017-07-06 15:33:09 +00:00
|
|
|
this.connection = this.app.listen(9003, done);
|
|
|
|
});
|
|
|
|
|
|
|
|
after(function(done) {
|
|
|
|
this.connection.close(done);
|
|
|
|
});
|
|
|
|
|
|
|
|
beforeEach(function() {
|
|
|
|
this.irc = util.createClient();
|
|
|
|
this.network = util.createNetwork();
|
|
|
|
|
|
|
|
Helper.config.prefetchStorage = true;
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should store the thumbnail", function(done) {
|
|
|
|
const message = this.irc.createMessage({
|
2017-11-15 06:35:15 +00:00
|
|
|
text: "http://localhost:9003/thumb",
|
2017-07-06 15:33:09 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
link(this.irc, this.network.channels[0], message);
|
|
|
|
|
|
|
|
this.app.get("/thumb", function(req, res) {
|
|
|
|
res.send("<title>Google</title><meta property='og:image' content='http://localhost:9003/real-test-image.png'>");
|
|
|
|
});
|
|
|
|
|
|
|
|
this.irc.once("msg:preview", function(data) {
|
|
|
|
expect(data.preview.head).to.equal("Google");
|
|
|
|
expect(data.preview.link).to.equal("http://localhost:9003/thumb");
|
|
|
|
expect(data.preview.thumb).to.equal(correctImageURL);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should store the image", function(done) {
|
|
|
|
const message = this.irc.createMessage({
|
2017-11-15 06:35:15 +00:00
|
|
|
text: "http://localhost:9003/real-test-image.png",
|
2017-07-06 15:33:09 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
link(this.irc, this.network.channels[0], message);
|
|
|
|
|
|
|
|
this.irc.once("msg:preview", function(data) {
|
|
|
|
expect(data.preview.type).to.equal("image");
|
|
|
|
expect(data.preview.link).to.equal("http://localhost:9003/real-test-image.png");
|
|
|
|
expect(data.preview.thumb).to.equal(correctImageURL);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2017-12-30 10:46:51 +00:00
|
|
|
|
|
|
|
it("should lookup correct extension type", function(done) {
|
|
|
|
const message = this.irc.createMessage({
|
|
|
|
text: "http://localhost:9003/svg-preview",
|
|
|
|
});
|
|
|
|
|
|
|
|
this.app.get("/svg-preview", function(req, res) {
|
|
|
|
res.send("<title>test title</title><meta property='og:image' content='http://localhost:9003/logo.svg'>");
|
|
|
|
});
|
|
|
|
|
|
|
|
link(this.irc, this.network.channels[0], message);
|
|
|
|
|
|
|
|
this.irc.once("msg:preview", function(data) {
|
|
|
|
expect(data.preview.type).to.equal("link");
|
|
|
|
expect(data.preview.link).to.equal("http://localhost:9003/svg-preview");
|
|
|
|
expect(data.preview.thumb).to.equal(correctSvgURL);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2017-07-06 15:33:09 +00:00
|
|
|
});
|