remove un-used /hibp/breaches endpoint
This commit is contained in:
Родитель
9e8bd76e59
Коммит
a24f3d2205
|
@ -102,51 +102,6 @@ async function notify (req, res) {
|
|||
}
|
||||
|
||||
|
||||
/*
|
||||
* Endpoint for clients to request latest breaches data.
|
||||
* Clients should send the date-time of the most recent breach they know in the
|
||||
* If-Modified-Since HTTP header.
|
||||
*
|
||||
> GET /hibp/breaches HTTP/1.1
|
||||
> If-Modified-Since: 2018-08-25T00:00:00
|
||||
*
|
||||
* If the client already has the latest breach, it will receive a 304:
|
||||
*
|
||||
* < HTTP/1.1 304 Not Modified
|
||||
*
|
||||
* ... or, if the client needs new breach data, it will receive a 200 with the
|
||||
* latest breach date in the Last-Modified header with a full body of breach data:
|
||||
*
|
||||
* < HTTP/1.1 200 OK
|
||||
* < Last-Modified: Thu Aug 23 2018 23:36:24 GMT-0500 (CDT)
|
||||
* <
|
||||
* [{"Title":"000webhost","Name":"000webhost", ...}]
|
||||
*
|
||||
* The client should store the 'Last-Modified' value and start sending it in
|
||||
* its 'If-Modified-Since' header in future requests.
|
||||
*/
|
||||
async function breaches (req, res, next) {
|
||||
const serverMostRecentBreachDateTime = req.app.locals.mostRecentBreachDateTime;
|
||||
const clientMostRecentBreachDateTime = req.headers["if-modified-since"] ? new Date(req.headers["if-modified-since"]) : new Date(0);
|
||||
|
||||
if (clientMostRecentBreachDateTime < serverMostRecentBreachDateTime) {
|
||||
res.append("Last-Modified", serverMostRecentBreachDateTime);
|
||||
res.json(req.app.locals.breaches);
|
||||
} else {
|
||||
res.sendStatus(304);
|
||||
}
|
||||
|
||||
if (Date.now() - req.app.locals.breachesLoadedDateTime >= AppConstants.HIBP_RELOAD_BREACHES_TIMER * 1000) {
|
||||
await HIBP.loadBreachesIntoApp(req.app);
|
||||
const freshBreachesLatestBreachDateTime = HIBP.getLatestBreachDateTime(req.app.locals.breaches);
|
||||
if (freshBreachesLatestBreachDateTime > req.app.locals.mostRecentBreachDateTime) {
|
||||
req.app.locals.mostRecentBreachDateTime = freshBreachesLatestBreachDateTime;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
module.exports = {
|
||||
notify,
|
||||
breaches,
|
||||
};
|
||||
|
|
|
@ -127,61 +127,3 @@ test("notify POST for subscriber with no signup_language should default to en",
|
|||
const mockFluentFormatCallArgs = mockFluentFormatCalls[0];
|
||||
expect (mockFluentFormatCallArgs[0]).toEqual(["en"]);
|
||||
});
|
||||
|
||||
|
||||
test("GET breaches with no if-modified-since responds with Last-Modified and json", async () => {
|
||||
const testDate = new Date();
|
||||
const mockReq = { headers: [], app: { locals: { breaches: testBreaches, mostRecentBreachDateTime: testDate } } };
|
||||
const mockResp = { append: jest.fn(), json: jest.fn() };
|
||||
|
||||
await hibp.breaches(mockReq, mockResp);
|
||||
|
||||
const mockAppendCallArgs = mockResp.append.mock.calls[0];
|
||||
expect(mockAppendCallArgs[0]).toBe("Last-Modified");
|
||||
expect(mockAppendCallArgs[1]).toBe(testDate);
|
||||
const mockJsonCallArgs = mockResp.json.mock.calls[0];
|
||||
expect(mockJsonCallArgs[0]).toBe(testBreaches);
|
||||
});
|
||||
|
||||
|
||||
test("GET breaches with expired if-modified-since responds with Last-Modified and json", async () => {
|
||||
const testDate = new Date();
|
||||
const mockReq = { headers: {"if-modified-since": "Mon, 10 Sep 2016 00:00:00 GMT"}, app: { locals: { breaches: testBreaches, mostRecentBreachDateTime: testDate } } };
|
||||
const mockResp = { append: jest.fn(), json: jest.fn() };
|
||||
|
||||
await hibp.breaches(mockReq, mockResp);
|
||||
|
||||
const mockAppendCallArgs = mockResp.append.mock.calls[0];
|
||||
expect(mockAppendCallArgs[0]).toBe("Last-Modified");
|
||||
expect(mockAppendCallArgs[1]).toBe(testDate);
|
||||
const mockJsonCallArgs = mockResp.json.mock.calls[0];
|
||||
expect(mockJsonCallArgs[0]).toBe(testBreaches);
|
||||
});
|
||||
|
||||
|
||||
test("GET breaches with up-to-date if-modified-since responds 304", async () => {
|
||||
const mockReq = { headers: {"if-modified-since": "Mon, 10 Sep 2018 00:00:00 GMT"}, app: { locals: { breaches: testBreaches } } };
|
||||
const mockResp = { sendStatus: jest.fn() };
|
||||
|
||||
await hibp.breaches(mockReq, mockResp);
|
||||
|
||||
const mockSendStatusCallArgs = mockResp.sendStatus.mock.calls[0];
|
||||
expect(mockSendStatusCallArgs[0]).toBe(304);
|
||||
});
|
||||
|
||||
|
||||
test("GET breaches after HIBP_RELOAD_BREACHES_TIMER triggers HIBP reload", async () => {
|
||||
jest.mock("../../hibp");
|
||||
HIBPLib.loadBreachesIntoApp = jest.fn();
|
||||
const breachesLoaded20mAgoDateTime = new Date();
|
||||
breachesLoaded20mAgoDateTime.setMinutes(breachesLoaded20mAgoDateTime.getMinutes() - 20);
|
||||
const mockReq = { headers: {"if-modified-since": "Mon, 10 Sep 2018 00:00:00 GMT"}, app: { locals: { breaches: testBreaches, breachesLoadedDateTime: breachesLoaded20mAgoDateTime } } };
|
||||
const mockResp = { sendStatus: jest.fn() };
|
||||
|
||||
await hibp.breaches(mockReq, mockResp);
|
||||
|
||||
const mockSendStatusCallArgs = mockResp.sendStatus.mock.calls[0];
|
||||
expect(mockSendStatusCallArgs[0]).toBe(304);
|
||||
const mockLoadBreachesCallArgs = HIBPLib.loadBreachesIntoApp.mock.calls[0];
|
||||
expect(mockLoadBreachesCallArgs[0]).toBe(mockReq.app);
|
||||
});
|
||||
|
|
Загрузка…
Ссылка в новой задаче