Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

send correct timestamp with turnstile event #7381

Merged
merged 2 commits into from
Oct 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/util/mapbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ export class TurnstileEvent {
if (config.ACCESS_TOKEN &&
Array.isArray(tileUrls) &&
tileUrls.some(url => isMapboxHTTPURL(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 @@ -381,9 +381,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(mapboxTileURLs);
dateNow.restore();

t.false(window.server.requests.length);
t.end();
Expand All @@ -397,9 +397,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(mapboxTileURLs);
dateNow.restore();

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

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

event.postTurnstileEvent(mapboxTileURLs);

const req = window.server.requests[0];
Expand All @@ -431,13 +429,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(mapboxTileURLs);

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

const req = window.server.requests[0];
req.respond(200);
Expand All @@ -452,13 +451,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(mapboxTileURLs);

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

const req = window.server.requests[0];
req.respond(200);
Expand Down Expand Up @@ -507,12 +507,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(mapboxTileURLs);

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

const req = window.server.requests[0];
req.respond(200);
Expand All @@ -528,12 +529,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(mapboxTileURLs);

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

const req = window.server.requests[0];
req.respond(200);
Expand Down Expand Up @@ -565,14 +567,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(mapboxTileURLs);

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(mapboxTileURLs);
dateNow.restore();

let req = window.server.requests[0];
req.respond(200);
Expand All @@ -592,18 +593,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(mapboxTileURLs);

const laterToday = now + 1;
let dateNow = t.stub(Date, 'now').callsFake(() => laterToday);
now = laterToday;
event.postTurnstileEvent(mapboxTileURLs);
dateNow.restore();

const tomorrow = laterToday + ms25Hours; // Add a day
now = tomorrow;
dateNow = t.stub(Date, 'now').callsFake(() => tomorrow);
event.postTurnstileEvent(mapboxTileURLs);
dateNow.restore();

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