0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-01-20 22:42:53 -05:00
ghost/test/unit/frontend/services/url/LocalFileCache.test.js
Naz 34b0196c0f Added writeDisabled flag to URL file cache
refs https://github.com/TryGhost/Toolbox/issues/135

- Allows to turn off overwriting urls/resources JSON file caches on testing environment. This is needed to have predictable state when running multiple test suites that stop the Ghost process and try to persiste URL cache.
2021-11-22 21:56:32 +13:00

88 lines
3 KiB
JavaScript

const should = require('should');
const sinon = require('sinon');
const fs = require('fs-extra');
const LocalFileCache = require('../../../../../core/server/services/url/LocalFileCache');
describe('Unit: services/url/LocalFileCache', function () {
afterEach(function () {
sinon.restore();
});
describe('read', function () {
it('reads from file system by type', async function () {
const storagePath = '/tmp/url-cache/';
sinon.stub(fs, 'stat')
.withArgs(`${storagePath}urls.json`)
.resolves(true);
sinon.stub(fs, 'readFile')
.withArgs(`${storagePath}urls.json`)
.resolves(JSON.stringify({urls: 'urls!'}));
const localFileCache = new LocalFileCache({storagePath});
const cachedUrls = await localFileCache.read('urls');
cachedUrls.should.not.be.undefined();
cachedUrls.urls.should.equal('urls!');
});
it('returns null when the cache file does not exit', async function () {
const storagePath = '/tmp/empty-url-cache/';
const localFileCache = new LocalFileCache({storagePath});
const cachedUrls = await localFileCache.read('urls');
should.equal(cachedUrls, null);
});
it('returns null when the cache file is malformatted', async function () {
const storagePath = '/tmp/empty-url-cache/';
sinon.stub(fs, 'stat')
.withArgs(`${storagePath}urls.json`)
.resolves(true);
sinon.stub(fs, 'readFile')
.withArgs(`${storagePath}urls.json`)
.resolves('I am not a valid JSON');
const localFileCache = new LocalFileCache({storagePath});
const cachedUrls = await localFileCache.read('urls');
should.equal(cachedUrls, null);
});
});
describe('write', function () {
it('writes to the file system by type', async function () {
const storagePath = '/tmp/url-cache/';
const writeFileStub = sinon.stub(fs, 'writeFile')
.withArgs(`${storagePath}urls.json`)
.resolves(true);
const localFileCache = new LocalFileCache({storagePath});
const result = await localFileCache.write('urls', {data: 'test'});
result.should.equal(true);
writeFileStub.called.should.equal(true);
});
it('does not write to the file system is writes are disabled', async function () {
const storagePath = '/tmp/url-cache/';
const writeFileStub = sinon.stub(fs, 'writeFile')
.withArgs(`${storagePath}urls.json`)
.resolves(true);
const localFileCache = new LocalFileCache({
storagePath,
writeDisabled: true
});
const result = await localFileCache.write('urls', {data: 'test'});
should.equal(result, null);
writeFileStub.called.should.equal(false);
});
});
});