Skip to content

Commit b3f70fa

Browse files
authored
Set infinite TTL for redirects (#375)
* Set infinite TTL for redirects * .skip broken test * nvm ... test works in CI
1 parent 73e244e commit b3f70fa

File tree

3 files changed

+149
-130
lines changed

3 files changed

+149
-130
lines changed

server/cache.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ middlewareRouter.use(async (req, res) => {
3939

4040
exports.middleware = middlewareRouter
4141

42-
exports.add = async (id, newModified, content) => {
42+
exports.add = async (id, newModified, content, ttl) => {
4343
if (!newModified) throw new Error(`Refusing to store ${id} without modified time.`)
4444

4545
const data = await cache.get(id)
@@ -49,7 +49,7 @@ exports.add = async (id, newModified, content) => {
4949
// if there was previous data and it is not older than the new data, don't do anything
5050
if (oldContent && modified && !isNewer(modified, newModified)) return // nothing to do if data is current
5151
// store new data in the cache
52-
return cache.set(id, {content, modified: newModified, id})
52+
return cache.set(id, {content, modified: newModified, id}, {ttl: ttl})
5353
}
5454

5555
// expose the purgeCache method externally so that list can call while building tree

server/list.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ async function setRedirects(oldDocsInfo, newDocsInfo) {
240240
// if no lastPath, file is a new addition to the drive
241241
if (currPath && lastPath && currPath !== lastPath) {
242242
log.info(`Doc ${id} moved, REDIRECT ${lastPath}${currPath}`)
243-
cache.add(lastPath, new Date(), {redirect: currPath})
243+
cache.add(lastPath, new Date(), {redirect: currPath}, 0)
244244
}
245245
})
246246
}

test/functional/pages.test.js

+146-127
Original file line numberDiff line numberDiff line change
@@ -1,177 +1,196 @@
1-
'use strict'
1+
"use strict";
22

3-
const request = require('supertest')
4-
const {expect} = require('chai')
5-
const sinon = require('sinon')
6-
const {allFilenames} = require('../utils')
3+
const request = require("supertest");
4+
const { expect } = require("chai");
5+
const sinon = require("sinon");
6+
const { allFilenames } = require("../utils");
77

8-
const app = require('../../server/index')
8+
const app = require("../../server/index");
99

1010
const userInfo = {
11-
emails: [{value: '[email protected]'}],
12-
id: '10',
13-
userId: '10',
14-
_json: {domain: 'test.com'}
15-
}
11+
emails: [{ value: "[email protected]" }],
12+
id: "10",
13+
userId: "10",
14+
_json: { domain: "test.com" },
15+
};
1616

17-
describe('Server responses', () => {
18-
beforeEach(() => sinon.stub(app.request, 'session').value({passport: {user: userInfo}}))
19-
afterEach(() => sinon.restore())
17+
describe("Server responses", () => {
18+
beforeEach(() =>
19+
sinon.stub(app.request, "session").value({ passport: { user: userInfo } })
20+
);
21+
afterEach(() => sinon.restore());
2022

21-
describe('that return HTML', () => {
22-
it('should return 200 and content for homepage', () => {
23+
describe("that return HTML", () => {
24+
it("should return 200 and content for homepage", () => {
2325
return request(app)
24-
.get('/')
26+
.get("/")
2527
.expect(200)
26-
.then((res) => expect(res.text).to.include('<title>Team Library</title>'))
27-
})
28+
.then((res) =>
29+
expect(res.text).to.include("<title>Team Library</title>")
30+
);
31+
});
2832

29-
it('should return 200 OK for healthcheck', () => {
33+
it("should return 200 OK for healthcheck", () => {
3034
return request(app)
31-
.get('/healthcheck')
35+
.get("/healthcheck")
3236
.expect(200)
3337
.then((res) => {
34-
expect(res.text).to.equal('OK')
35-
})
36-
})
38+
expect(res.text).to.equal("OK");
39+
});
40+
});
3741

38-
it('should display subfolders for folder', () => {
42+
it("should display subfolders for folder", () => {
3943
return request(app)
40-
.get('/test-folder-1')
44+
.get("/test-folder-1")
4145
.expect(200)
4246
.then((res) => {
4347
// check it resolves name correclty
44-
expect(res.text).to.include('Pages in Test Folder 1')
48+
expect(res.text).to.include("Pages in Test Folder 1");
4549
// check it has links to children
46-
expect(res.text).to.include('Article 1 in test folder 1')
47-
expect(res.text).to.include('Article 2 in test folder 1')
48-
})
49-
})
50+
expect(res.text).to.include("Article 1 in test folder 1");
51+
expect(res.text).to.include("Article 2 in test folder 1");
52+
});
53+
});
5054

51-
it('should remove trailing slash and redirect', () => {
52-
return request(app)
53-
// should strip trailing slash
54-
.get('/test-folder-1/')
55-
.expect(302) // Should be cached at this point
56-
.then((res) => {
57-
expect(res.text).to.equal('Found. Redirecting to /test-folder-1')
58-
})
59-
})
55+
it("should remove trailing slash and redirect", () => {
56+
return (
57+
request(app)
58+
// should strip trailing slash
59+
.get("/test-folder-1/")
60+
.expect(302) // Should be cached at this point
61+
.then((res) => {
62+
expect(res.text).to.equal("Found. Redirecting to /test-folder-1");
63+
})
64+
);
65+
});
6066

61-
it('should render top level folder in categories', () => {
67+
it("should render top level folder in categories", () => {
6268
return request(app)
63-
.get('/categories')
69+
.get("/categories")
6470
.expect(200)
6571
.then((res) => {
66-
expect(res.text).to.include('<h3>\n <a href="/test-folder-1">\n Test Folder 1\n </a>\n </h3>')
67-
})
68-
})
72+
expect(res.text).to.include(
73+
'<h3>\n <a href="/test-folder-1">\n Test Folder 1\n </a>\n </h3>'
74+
);
75+
});
76+
});
6977

7078
// also tests insertion into datastore
71-
it('folder with home doc should render the doc', () => {
79+
it("folder with home doc should render the doc", () => {
7280
return request(app)
73-
.get('/test-folder-9')
81+
.get("/test-folder-9")
7482
.expect(200)
7583
.then((res) => {
76-
expect(res.text).to.include('<h1 class="headline">Home article 10 for test folder 9</h1>')
77-
expect(res.text).to.include('By <span class="author">John Smith</span>')
78-
expect(res.text).to.include('Last edited by <span class="author">Foo Bar</span>')
79-
expect(res.text).to.include('Home article 10 for test folder 9')
80-
})
81-
})
84+
expect(res.text).to.include(
85+
'<h1 class="headline">Home article 10 for test folder 9</h1>'
86+
);
87+
expect(res.text).to.include(
88+
'By <span class="author">John Smith</span>'
89+
);
90+
expect(res.text).to.include(
91+
'Last edited by <span class="author">Foo Bar</span>'
92+
);
93+
expect(res.text).to.include("Home article 10 for test folder 9");
94+
});
95+
});
8296

83-
it('duplicate doc should render a warning', () => {
97+
it("duplicate doc should render a warning", () => {
8498
return request(app)
85-
.get('/test-folder-9/article-3-in-test-folder-9')
99+
.get("/test-folder-9/article-3-in-test-folder-9")
86100
.expect(200)
87101
.then((res) => {
88-
expect(res.text).to.include('<h1 class="headline">Article 3 in test folder 9</h1>')
89-
expect(res.text).to.include('By <span class="author">John Smith</span>')
90-
expect(res.text).to.include('Last edited by <span class="author">Foo Bar</span>')
91-
expect(res.text).to.include('Article 3 in test folder 9')
102+
expect(res.text).to.include(
103+
'<h1 class="headline">Article 3 in test folder 9</h1>'
104+
);
105+
expect(res.text).to.include(
106+
'By <span class="author">John Smith</span>'
107+
);
108+
expect(res.text).to.include(
109+
'Last edited by <span class="author">Foo Bar</span>'
110+
);
111+
expect(res.text).to.include("Article 3 in test folder 9");
92112
expect(res.text).to.include(
93113
'<div class="warning">\n Warning: Multiple resources in ' +
94-
'<a href="https://drive.google.com/drive/u/0/folders/TestFolder9" target="_blank">' +
95-
'this folder</a> share the same name&#58; Article 3 in test folder 9. Only one will be ' +
96-
'accesible through Library.\n</div>'
97-
)
98-
})
99-
})
114+
'<a href="https://drive.google.com/drive/u/0/folders/TestFolder9" target="_blank">' +
115+
"this folder</a> share the same name&#58; Article 3 in test folder 9. Only one will be " +
116+
"accesible through Library.\n</div>"
117+
);
118+
});
119+
});
100120

101-
it('should render an inline <style> tag and no JS on error pages', () => {
121+
it("should render an inline <style> tag and no JS on error pages", () => {
102122
return request(app)
103-
.get('/this-route-does-not-exist')
123+
.get("/this-route-does-not-exist")
104124
.expect(404)
105125
.then((res) => {
106-
expect(res.text).to.match(/<style type="text\/css">[^<]/i)
107-
expect(res.text).to.not.include('<link href="/assets')
108-
expect(res.text).to.not.include('<script src="/assets')
109-
})
110-
})
111-
})
126+
expect(res.text).to.match(/<style type="text\/css">[^<]/i);
127+
expect(res.text).to.not.include('<link href="/assets');
128+
expect(res.text).to.not.include('<script src="/assets');
129+
});
130+
});
131+
});
112132

113-
describe('that return JSON', () => {
114-
it('should contain a complete filename listing', () => {
133+
describe("that return JSON", () => {
134+
it("should contain a complete filename listing", () => {
115135
return request(app)
116-
.get('/filename-listing.json')
136+
.get("/filename-listing.json")
117137
.expect(200)
118138
.then((res) => {
119-
const {filenames} = res.body
120-
expect(Array.isArray(filenames), 'cached file listing should be an array')
121-
expect(filenames).to.include(...allFilenames)
122-
expect(filenames.length).to.equal(allFilenames.length)
123-
})
124-
})
139+
const { filenames } = res.body;
140+
expect(
141+
Array.isArray(filenames),
142+
"cached file listing should be an array"
143+
);
144+
expect(filenames).to.include(...allFilenames);
145+
expect(filenames.length).to.equal(allFilenames.length);
146+
});
147+
});
125148

126-
it('folder with home doc should render the doc', () => {
149+
it("folder with home doc should render the doc", () => {
127150
return request(app)
128-
.get('/test-folder-9')
129-
.set('Accept', 'application/json')
151+
.get("/test-folder-9")
152+
.set("Accept", "application/json")
130153
.expect(200)
131-
.expect('Content-Type', /json/)
154+
.expect("Content-Type", /json/)
132155
.then((res) => {
133-
const data = JSON.parse(res.text)
134-
expect(data).to.deep.include(
135-
{
136-
url: '/test-folder-9',
137-
title: 'Home article 10 for test folder 9',
138-
lastUpdatedBy: 'Foo Bar',
139-
modifiedAt: '2018-03-02T14:13:20.000Z',
140-
createdAt: '2018-02-19T00:26:40.000Z',
141-
id: 'Test10',
142-
resourceType: 'document',
143-
content: '<p> This is a simple test document export.</p>',
144-
byline: 'John Smith',
145-
createdBy: 'John Smith',
146-
sections: []
147-
}
148-
)
149-
})
150-
})
156+
const data = JSON.parse(res.text);
157+
expect(data).to.deep.include({
158+
url: "/test-folder-9",
159+
title: "Home article 10 for test folder 9",
160+
lastUpdatedBy: "Foo Bar",
161+
modifiedAt: "2018-03-02T14:13:20.000Z",
162+
createdAt: "2018-02-19T00:26:40.000Z",
163+
id: "Test10",
164+
resourceType: "document",
165+
content: "<p> This is a simple test document export.</p>",
166+
byline: "John Smith",
167+
createdBy: "John Smith",
168+
sections: [],
169+
});
170+
});
171+
});
151172

152-
it('folder with home doc should render the doc (.json suffix)', () => {
173+
it("folder with home doc should render the doc (.json suffix)", () => {
153174
return request(app)
154-
.get('/test-folder-9.json')
175+
.get("/test-folder-9.json")
155176
.expect(200)
156-
.expect('Content-Type', /json/)
177+
.expect("Content-Type", /json/)
157178
.then((res) => {
158-
const data = JSON.parse(res.text)
159-
expect(data).to.deep.include(
160-
{
161-
url: '/test-folder-9',
162-
title: 'Home article 10 for test folder 9',
163-
lastUpdatedBy: 'Foo Bar',
164-
modifiedAt: '2018-03-02T14:13:20.000Z',
165-
createdAt: '2018-02-19T00:26:40.000Z',
166-
id: 'Test10',
167-
resourceType: 'document',
168-
content: '<p> This is a simple test document export.</p>',
169-
byline: 'John Smith',
170-
createdBy: 'John Smith',
171-
sections: []
172-
}
173-
)
174-
})
175-
})
176-
})
177-
})
179+
const data = JSON.parse(res.text);
180+
expect(data).to.deep.include({
181+
url: "/test-folder-9",
182+
title: "Home article 10 for test folder 9",
183+
lastUpdatedBy: "Foo Bar",
184+
modifiedAt: "2018-03-02T14:13:20.000Z",
185+
createdAt: "2018-02-19T00:26:40.000Z",
186+
id: "Test10",
187+
resourceType: "document",
188+
content: "<p> This is a simple test document export.</p>",
189+
byline: "John Smith",
190+
createdBy: "John Smith",
191+
sections: [],
192+
});
193+
});
194+
});
195+
});
196+
});

0 commit comments

Comments
 (0)