From b3f70fadeca34506e3ae0bd3d3dc3cc14383b408 Mon Sep 17 00:00:00 2001 From: Andrew Chavez Date: Thu, 23 May 2024 13:37:06 -0400 Subject: [PATCH] Set infinite TTL for redirects (#375) * Set infinite TTL for redirects * .skip broken test * nvm ... test works in CI --- server/cache.js | 4 +- server/list.js | 2 +- test/functional/pages.test.js | 273 ++++++++++++++++++---------------- 3 files changed, 149 insertions(+), 130 deletions(-) diff --git a/server/cache.js b/server/cache.js index ede90b21..159fc2aa 100644 --- a/server/cache.js +++ b/server/cache.js @@ -39,7 +39,7 @@ middlewareRouter.use(async (req, res) => { exports.middleware = middlewareRouter -exports.add = async (id, newModified, content) => { +exports.add = async (id, newModified, content, ttl) => { if (!newModified) throw new Error(`Refusing to store ${id} without modified time.`) const data = await cache.get(id) @@ -49,7 +49,7 @@ exports.add = async (id, newModified, content) => { // if there was previous data and it is not older than the new data, don't do anything if (oldContent && modified && !isNewer(modified, newModified)) return // nothing to do if data is current // store new data in the cache - return cache.set(id, {content, modified: newModified, id}) + return cache.set(id, {content, modified: newModified, id}, {ttl: ttl}) } // expose the purgeCache method externally so that list can call while building tree diff --git a/server/list.js b/server/list.js index 44333760..a01bcb0f 100644 --- a/server/list.js +++ b/server/list.js @@ -240,7 +240,7 @@ async function setRedirects(oldDocsInfo, newDocsInfo) { // if no lastPath, file is a new addition to the drive if (currPath && lastPath && currPath !== lastPath) { log.info(`Doc ${id} moved, REDIRECT ${lastPath} → ${currPath}`) - cache.add(lastPath, new Date(), {redirect: currPath}) + cache.add(lastPath, new Date(), {redirect: currPath}, 0) } }) } diff --git a/test/functional/pages.test.js b/test/functional/pages.test.js index 9cbce39b..5ef543d4 100644 --- a/test/functional/pages.test.js +++ b/test/functional/pages.test.js @@ -1,177 +1,196 @@ -'use strict' +"use strict"; -const request = require('supertest') -const {expect} = require('chai') -const sinon = require('sinon') -const {allFilenames} = require('../utils') +const request = require("supertest"); +const { expect } = require("chai"); +const sinon = require("sinon"); +const { allFilenames } = require("../utils"); -const app = require('../../server/index') +const app = require("../../server/index"); const userInfo = { - emails: [{value: 'test.user@test.com'}], - id: '10', - userId: '10', - _json: {domain: 'test.com'} -} + emails: [{ value: "test.user@test.com" }], + id: "10", + userId: "10", + _json: { domain: "test.com" }, +}; -describe('Server responses', () => { - beforeEach(() => sinon.stub(app.request, 'session').value({passport: {user: userInfo}})) - afterEach(() => sinon.restore()) +describe("Server responses", () => { + beforeEach(() => + sinon.stub(app.request, "session").value({ passport: { user: userInfo } }) + ); + afterEach(() => sinon.restore()); - describe('that return HTML', () => { - it('should return 200 and content for homepage', () => { + describe("that return HTML", () => { + it("should return 200 and content for homepage", () => { return request(app) - .get('/') + .get("/") .expect(200) - .then((res) => expect(res.text).to.include('Team Library')) - }) + .then((res) => + expect(res.text).to.include("Team Library") + ); + }); - it('should return 200 OK for healthcheck', () => { + it("should return 200 OK for healthcheck", () => { return request(app) - .get('/healthcheck') + .get("/healthcheck") .expect(200) .then((res) => { - expect(res.text).to.equal('OK') - }) - }) + expect(res.text).to.equal("OK"); + }); + }); - it('should display subfolders for folder', () => { + it("should display subfolders for folder", () => { return request(app) - .get('/test-folder-1') + .get("/test-folder-1") .expect(200) .then((res) => { // check it resolves name correclty - expect(res.text).to.include('Pages in Test Folder 1') + expect(res.text).to.include("Pages in Test Folder 1"); // check it has links to children - expect(res.text).to.include('Article 1 in test folder 1') - expect(res.text).to.include('Article 2 in test folder 1') - }) - }) + expect(res.text).to.include("Article 1 in test folder 1"); + expect(res.text).to.include("Article 2 in test folder 1"); + }); + }); - it('should remove trailing slash and redirect', () => { - return request(app) - // should strip trailing slash - .get('/test-folder-1/') - .expect(302) // Should be cached at this point - .then((res) => { - expect(res.text).to.equal('Found. Redirecting to /test-folder-1') - }) - }) + it("should remove trailing slash and redirect", () => { + return ( + request(app) + // should strip trailing slash + .get("/test-folder-1/") + .expect(302) // Should be cached at this point + .then((res) => { + expect(res.text).to.equal("Found. Redirecting to /test-folder-1"); + }) + ); + }); - it('should render top level folder in categories', () => { + it("should render top level folder in categories", () => { return request(app) - .get('/categories') + .get("/categories") .expect(200) .then((res) => { - expect(res.text).to.include('

\n \n Test Folder 1\n \n

') - }) - }) + expect(res.text).to.include( + '

\n \n Test Folder 1\n \n

' + ); + }); + }); // also tests insertion into datastore - it('folder with home doc should render the doc', () => { + it("folder with home doc should render the doc", () => { return request(app) - .get('/test-folder-9') + .get("/test-folder-9") .expect(200) .then((res) => { - expect(res.text).to.include('

Home article 10 for test folder 9

') - expect(res.text).to.include('By John Smith') - expect(res.text).to.include('Last edited by Foo Bar') - expect(res.text).to.include('Home article 10 for test folder 9') - }) - }) + expect(res.text).to.include( + '

Home article 10 for test folder 9

' + ); + expect(res.text).to.include( + 'By John Smith' + ); + expect(res.text).to.include( + 'Last edited by Foo Bar' + ); + expect(res.text).to.include("Home article 10 for test folder 9"); + }); + }); - it('duplicate doc should render a warning', () => { + it("duplicate doc should render a warning", () => { return request(app) - .get('/test-folder-9/article-3-in-test-folder-9') + .get("/test-folder-9/article-3-in-test-folder-9") .expect(200) .then((res) => { - expect(res.text).to.include('

Article 3 in test folder 9

') - expect(res.text).to.include('By John Smith') - expect(res.text).to.include('Last edited by Foo Bar') - expect(res.text).to.include('Article 3 in test folder 9') + expect(res.text).to.include( + '

Article 3 in test folder 9

' + ); + expect(res.text).to.include( + 'By John Smith' + ); + expect(res.text).to.include( + 'Last edited by Foo Bar' + ); + expect(res.text).to.include("Article 3 in test folder 9"); expect(res.text).to.include( '
\n Warning: Multiple resources in ' + - '' + - 'this folder share the same name: Article 3 in test folder 9. Only one will be ' + - 'accesible through Library.\n
' - ) - }) - }) + '' + + "this folder share the same name: Article 3 in test folder 9. Only one will be " + + "accesible through Library.\n" + ); + }); + }); - it('should render an inline