Skip to content

Commit

Permalink
send correct timestamp with turnstile event (#7381)
Browse files Browse the repository at this point in the history
* send correct timestamp with turnstile event

* update TurnstileEvent tests
  • Loading branch information
mollymerp authored and kkaefer committed Oct 10, 2018
1 parent 067e30e commit 56487f0
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/util/mapbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ export class TurnstileEvent {
if (config.ACCESS_TOKEN &&
Array.isArray(tileUrls) &&
tileUrls.some((url) => { return /(mapbox\.c)(n|om)/i.test(url); })) {
this.queueRequest(browser.now());
this.queueRequest(Date.now());
}
}

Expand Down
38 changes: 21 additions & 17 deletions test/unit/util/mapbox.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -359,9 +359,9 @@ test("mapbox", (t) => {
lastSuccess: now
}));

t.stub(browser, 'now').callsFake(() => now + 5); // A bit later

const dateNow = t.stub(Date, 'now').callsFake(() => now + 5); // A bit later
event.postTurnstileEvent(['a.tiles.mapbox.com']);
dateNow.restore();

t.false(window.server.requests.length);
t.end();
Expand All @@ -375,9 +375,9 @@ test("mapbox", (t) => {
lastSuccess: now
}));

t.stub(browser, 'now').callsFake(() => now + ms25Hours); // next day

const dateNow = t.stub(Date, 'now').callsFake(() => now + ms25Hours);
event.postTurnstileEvent(['a.tiles.mapbox.com']);
dateNow.restore();

const req = window.server.requests[0];
req.respond(200);
Expand All @@ -395,8 +395,6 @@ test("mapbox", (t) => {
lastSuccess: now + ms25Hours // 24-hours later
}));

t.stub(browser, 'now').callsFake(() => now); // Past relative ot lastSuccess

event.postTurnstileEvent(['a.tiles.mapbox.com']);

const req = window.server.requests[0];
Expand All @@ -409,13 +407,14 @@ test("mapbox", (t) => {

t.test('does not POST appuserTurnstile event second time within same calendar day', (t) => {
let now = +Date.now();
t.stub(browser, 'now').callsFake(() => now);
event.postTurnstileEvent(['a.tiles.mapbox.com']);

//Post second event
const firstEvent = now;
now += (60 * 1000); // A bit later
const dateNow = t.stub(Date, 'now').callsFake(() => now);
event.postTurnstileEvent(['a.tiles.mapbox.com']);
dateNow.restore();

const req = window.server.requests[0];
req.respond(200);
Expand All @@ -430,13 +429,14 @@ test("mapbox", (t) => {

t.test('does not POST appuserTurnstile event second time when clock goes backwards less than a day', (t) => {
let now = +Date.now();
t.stub(browser, 'now').callsFake(() => now);
event.postTurnstileEvent(['a.tiles.mapbox.com']);

//Post second event
const firstEvent = now;
now -= (60 * 1000); // A bit earlier
const dateNow = t.stub(Date, 'now').callsFake(() => now);
event.postTurnstileEvent(['a.tiles.mapbox.com']);
dateNow.restore();

const req = window.server.requests[0];
req.respond(200);
Expand Down Expand Up @@ -485,12 +485,13 @@ test("mapbox", (t) => {
t.test('does not POST appuserTurnstile event second time within same calendar day', (t) => {
let now = +Date.now();
const firstEvent = now;
t.stub(browser, 'now').callsFake(() => now);
event.postTurnstileEvent(['a.tiles.mapbox.com']);

//Post second event
now += (60 * 1000); // A bit later
const dateNow = t.stub(Date, 'now').callsFake(() => now);
event.postTurnstileEvent(['a.tiles.mapbox.com']);
dateNow.restore();

const req = window.server.requests[0];
req.respond(200);
Expand All @@ -506,12 +507,13 @@ test("mapbox", (t) => {
t.test('does not POST appuserTurnstile event second time when clock goes backwards less than a day', (t) => {
let now = +Date.now();
const firstEvent = now;
t.stub(browser, 'now').callsFake(() => now);
event.postTurnstileEvent(['a.tiles.mapbox.com']);

//Post second event
now -= (60 * 1000); // A bit earlier
const dateNow = t.stub(Date, 'now').callsFake(() => now);
event.postTurnstileEvent(['a.tiles.mapbox.com']);
dateNow.restore();

const req = window.server.requests[0];
req.respond(200);
Expand Down Expand Up @@ -543,14 +545,13 @@ test("mapbox", (t) => {
});

t.test('POSTs appUserTurnstile event on next calendar day', (t) => {
let now = +Date.now();
t.stub(browser, 'now').callsFake(() => now);

const now = +Date.now();
event.postTurnstileEvent(['a.tiles.mapbox.com']);

now += ms25Hours; // Add a day
const tomorrow = now;
// Add a day
const tomorrow = now + ms25Hours;
const dateNow = t.stub(Date, 'now').callsFake(() => tomorrow);
event.postTurnstileEvent(['a.tiles.mapbox.com']);
dateNow.restore();

let req = window.server.requests[0];
req.respond(200);
Expand All @@ -570,18 +571,21 @@ test("mapbox", (t) => {

t.test('Queues and POSTs appuserTurnstile events when triggerred in quick succession', (t) => {
let now = Date.now();
t.stub(browser, 'now').callsFake(() => now);

const today = now;
event.postTurnstileEvent(['a.tiles.mapbox.com']);

const laterToday = now + 1;
let dateNow = t.stub(Date, 'now').callsFake(() => laterToday);
now = laterToday;
event.postTurnstileEvent(['b.tiles.mapbox.com']);
dateNow.restore();

const tomorrow = laterToday + ms25Hours; // Add a day
now = tomorrow;
dateNow = t.stub(Date, 'now').callsFake(() => tomorrow);
event.postTurnstileEvent(['c.tiles.mapbox.com']);
dateNow.restore();

const reqToday = window.server.requests[0];
reqToday.respond(200);
Expand Down

0 comments on commit 56487f0

Please sign in to comment.