0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-01-27 22:49:56 -05:00
ghost/core/test/unit/middleware/serve-favicon_spec.js
Aileen Nowak e19e91044d 🙇 Blog icon utils and publisher.logo for JSON-LD (#8297)
refs #8221, closes #7688, refs #7558

🙇  Improve meta data publisher logo behaviour
This is a follow-up PR for #8285.

Reasons: The code changes of #8285 caused error messages when falling back to the default `favicon.ico`, as the `image-size` tool doesn't support `ico` files.

This PR takes the logic to decide which logo needs to be listed in our schema into a new fn `blog_logo.js`. There we have now three decisions:
1. If we have a publication **logo**, we'll take that one
2. If we have no publication logo, but an **icon** we'll use this one.
3. If we have none of the above things, we fall back to our default `favicon.ico`

Additional, we're hard coding image dimensions for whenever the logo is an `.ico` file and built and extra decision to not call `image-size` when the dimension are already given.

I will create another follow-up PR, which checks the extension type for the file and offers it as a util.

🛠  Blog icon util

refs #7688

Serve functionality around the blog icon in its own util:
- getIconDimensions -> async function that takes the filepath of on ico file and returns its dimensions
- isIcoImageType -> returns true if file has `.ico` extension
- getIconType -> returns icon-type (`x-icon` or `png`)
- getIconUrl -> returns the absolut or relativ URL for the favicon: `[subdirectory or not]favicon.[ico or png]`

📖  Get .ico sizes for meta data & logo improvement

refs #7558
refs #8221

Use the new `blogIconUtil` in meta data to fetch the dimensions of `.ico` files.

Improvements for `publisher.logo`: We're now returning a hard-coded 'faked' image dimensions value to render an `imageObject` and prevent error our schema (Google structured data). As soon as an image (`.ico` or non-`.ico`) is too large, but - in case of non-`.ico` - a square format, be set the image-dimensions to 60px width and height. This reduces the chances of getting constantly error messages from Googles' webmaster tools.

- add getIconPath util
2017-04-11 18:32:06 +02:00

183 lines
6.1 KiB
JavaScript

var should = require('should'), // jshint ignore:line
sinon = require('sinon'),
express = require('express'),
serveFavicon = require('../../../server/middleware/serve-favicon'),
settingsCache = require('../../../server/settings/cache'),
storage = require('../../../server/storage'),
configUtils = require('../../utils/configUtils'),
path = require('path'),
sandbox = sinon.sandbox.create();
describe('Serve Favicon', function () {
var req, res, next, blogApp, localSettingsCache = {}, originalStoragePath;
beforeEach(function () {
req = sinon.spy();
res = sinon.spy();
next = sinon.spy();
blogApp = express();
req.app = blogApp;
sandbox.stub(settingsCache, 'get', function (key) {
return localSettingsCache[key];
});
originalStoragePath = storage.getStorage().storagePath;
});
afterEach(function () {
sandbox.restore();
configUtils.restore();
localSettingsCache = {};
storage.getStorage().storagePath = originalStoragePath;
});
describe('serveFavicon', function () {
it('should return a middleware', function () {
var middleware = serveFavicon();
middleware.should.be.a.Function();
});
it('should skip if the request does NOT match the file', function () {
var middleware = serveFavicon();
req.path = '/robots.txt';
middleware(req, res, next);
next.called.should.be.true();
});
describe('serves', function () {
it('custom uploaded favicon.png', function (done) {
var middleware = serveFavicon();
req.path = '/favicon.png';
storage.getStorage().storagePath = path.join(__dirname, '../../../test/utils/fixtures/images/');
localSettingsCache.icon = 'favicon.png';
res = {
writeHead: function (statusCode) {
statusCode.should.eql(200);
},
end: function (body) {
body.length.should.eql(6792);
done();
}
};
middleware(req, res, next);
});
it('custom uploaded favicon.ico', function (done) {
var middleware = serveFavicon();
req.path = '/favicon.ico';
storage.getStorage().storagePath = path.join(__dirname, '../../../test/utils/fixtures/images/');
localSettingsCache.icon = 'favicon.ico';
res = {
writeHead: function (statusCode) {
statusCode.should.eql(200);
},
end: function (body) {
body.length.should.eql(15086);
done();
}
};
middleware(req, res, next);
});
it('custom uploaded myicon.ico', function (done) {
var middleware = serveFavicon();
req.path = '/favicon.ico';
storage.getStorage().storagePath = path.join(__dirname, '../../../test/utils/fixtures/images/');
localSettingsCache.icon = 'myicon.ico';
res = {
writeHead: function (statusCode) {
statusCode.should.eql(200);
},
end: function (body) {
body.length.should.eql(15086);
done();
}
};
middleware(req, res, next);
});
it('default favicon.ico', function (done) {
var middleware = serveFavicon();
req.path = '/favicon.ico';
localSettingsCache.icon = '';
res = {
writeHead: function (statusCode) {
statusCode.should.eql(200);
},
end: function (body) {
body.length.should.eql(34494);
done();
}
};
middleware(req, res, next);
});
});
describe('redirects', function () {
it('to custom favicon.ico when favicon.png is requested', function (done) {
var middleware = serveFavicon();
req.path = '/favicon.png';
configUtils.set('paths:contentPath', path.join(__dirname, '../../../test/utils/fixtures/'));
localSettingsCache.icon = 'favicon.ico';
res = {
redirect: function (statusCode) {
statusCode.should.eql(302);
done();
}
};
middleware(req, res, next);
});
it('to custom favicon.png when favicon.ico is requested', function (done) {
var middleware = serveFavicon();
req.path = '/favicon.ico';
configUtils.set('paths:contentPath', path.join(__dirname, '../../../test/utils/fixtures/'));
localSettingsCache.icon = 'favicon.png';
res = {
redirect: function (statusCode) {
statusCode.should.eql(302);
done();
}
};
middleware(req, res, next);
});
it('to favicon.ico when favicon.png is requested', function (done) {
var middleware = serveFavicon();
req.path = '/favicon.png';
configUtils.set('paths:publicFilePath', path.join(__dirname, '../../../test/utils/fixtures/'));
localSettingsCache.icon = '';
res = {
redirect: function (statusCode) {
statusCode.should.eql(302);
done();
}
};
middleware(req, res, next);
});
});
});
});