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

gazpacho backport: send correct timestamp with turnstile event #7393

Merged
merged 1 commit into from
Oct 10, 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 @@ -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