0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-02-03 23:00:14 -05:00
ghost/test/unit/server/services/url/LocalFileCache.test.js
Hannah Wolfe 0581314796
Fixed location of url service unit tests
- The url service was moved from frontend to server some time ago but the tests were forgotten
- This is only being done now because in 5.0 major changes are happening and it'll be annoying if the
  files move on that branch
2022-04-06 13:05:41 +01: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);
});
});
});