Skip to content

Commit 7c960d2

Browse files
feat: ignore data from suspended user
1 parent 557027e commit 7c960d2

4 files changed

Lines changed: 111 additions & 2 deletions

File tree

migrations/dashboard/create-tables.js.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ CREATE TABLE IF NOT EXISTS directus_users (
33
github_username VARCHAR(255),
44
github_organizations longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL DEFAULT '[]' CHECK (json_valid(`github_organizations`)),
55
user_type VARCHAR(255) NOT NULL DEFAULT 'member',
6+
status VARCHAR(255) NOT NULL DEFAULT 'active',
67
public_probes BOOLEAN DEFAULT 0,
78
adoption_token VARCHAR(255) NOT NULL,
89
default_prefix VARCHAR(255) NOT NULL,

src/lib/override/adopted-probes.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -363,11 +363,14 @@ export class AdoptedProbes {
363363

364364
public async fetchDProbes () {
365365
const rows = await this.sql(DASH_PROBES_TABLE)
366-
.leftJoin(USERS_TABLE, `${DASH_PROBES_TABLE}.userId`, `${USERS_TABLE}.id`)
366+
.leftJoin(USERS_TABLE, function () {
367+
this.on(`${DASH_PROBES_TABLE}.userId`, `${USERS_TABLE}.id`)
368+
.andOnVal(`${USERS_TABLE}.status`, '=', 'active');
369+
})
367370
// First item will be preserved, so we are prioritizing adopted and online probes.
368371
// Sorting by id at the end so order is the same in any table state.
369372
.orderByRaw(`IF (${DASH_PROBES_TABLE}.userId IS NOT NULL, 1, 2), ${DASH_PROBES_TABLE}.lastSyncDate DESC, ${DASH_PROBES_TABLE}.onlineTimesToday DESC, FIELD(${DASH_PROBES_TABLE}.status, 'ready') DESC, ${DASH_PROBES_TABLE}.id DESC`)
370-
.select<Row[]>(`${DASH_PROBES_TABLE}.*`, `${USERS_TABLE}.default_prefix AS defaultPrefix`, `${USERS_TABLE}.deprecated_prefix AS deprecatedPrefix`, `${USERS_TABLE}.public_probes as publicProbes`, `${USERS_TABLE}.adoption_token AS adoptionToken`);
373+
.select<Row[]>(`${DASH_PROBES_TABLE}.*`, `${USERS_TABLE}.id AS userId`, `${USERS_TABLE}.default_prefix AS defaultPrefix`, `${USERS_TABLE}.deprecated_prefix AS deprecatedPrefix`, `${USERS_TABLE}.public_probes as publicProbes`, `${USERS_TABLE}.adoption_token AS adoptionToken`);
371374

372375
const dProbes: DProbe[] = rows.map(row => ({
373376
...row,

test/tests/integration/measurement/create-measurement.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -717,6 +717,13 @@ describe('Create measurement', () => {
717717

718718
describe('adopted probes', () => {
719719
before(async () => {
720+
await dashboardClient('directus_users').insert({
721+
id: '89da69bd-a236-4ab7-9c5d-b5f52ce09959',
722+
status: 'active',
723+
adoption_token: 'adoptionTokenValue',
724+
default_prefix: 'jsdelivr',
725+
});
726+
720727
await dashboardClient(DASH_PROBES_TABLE).insert({
721728
id: randomUUID(),
722729
userId: '89da69bd-a236-4ab7-9c5d-b5f52ce09959',
@@ -756,6 +763,7 @@ describe('Create measurement', () => {
756763

757764
after(async () => {
758765
await dashboardClient(DASH_PROBES_TABLE).where({ city: 'Oklahoma City' }).delete();
766+
await dashboardClient('directus_users').delete();
759767
});
760768

761769
it('should create measurement with adopted "city: Oklahoma City" location', async () => {
@@ -899,6 +907,13 @@ describe('Create measurement', () => {
899907

900908
describe('adopted probes + admin overrides', () => {
901909
before(async () => {
910+
await dashboardClient('directus_users').insert({
911+
id: '89da69bd-a236-4ab7-9c5d-b5f52ce09959',
912+
status: 'active',
913+
adoption_token: 'adoptionTokenValue',
914+
default_prefix: 'jsdelivr',
915+
});
916+
902917
await dashboardClient(DASH_PROBES_TABLE).insert({
903918
id: randomUUID(),
904919
userId: '89da69bd-a236-4ab7-9c5d-b5f52ce09959',
@@ -947,6 +962,7 @@ describe('Create measurement', () => {
947962

948963
after(async () => {
949964
await dashboardClient(DASH_PROBES_TABLE).where({ city: 'Oklahoma City' }).delete();
965+
await dashboardClient('directus_users').delete();
950966
await dashboardClient('gp_location_overrides').where({ city: 'Paris' }).delete();
951967
});
952968

test/tests/integration/probes/get-probes.test.ts

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,13 @@ describe('Get Probes', () => {
249249

250250
describe('adopted probes', () => {
251251
before(async () => {
252+
await dashboardClient('directus_users').insert({
253+
id: '89da69bd-a236-4ab7-9c5d-b5f52ce09959',
254+
status: 'active',
255+
adoption_token: 'adoptionTokenValue',
256+
default_prefix: 'jimaek',
257+
});
258+
252259
await dashboardClient(DASH_PROBES_TABLE).insert({
253260
id: randomUUID(),
254261
userId: '89da69bd-a236-4ab7-9c5d-b5f52ce09959',
@@ -286,6 +293,7 @@ describe('Get Probes', () => {
286293

287294
after(async () => {
288295
await dashboardClient(DASH_PROBES_TABLE).where({ city: 'Cordoba' }).delete();
296+
await dashboardClient('directus_users').delete();
289297
});
290298

291299
it('should update probes data', async () => {
@@ -319,5 +327,86 @@ describe('Get Probes', () => {
319327
});
320328
});
321329
});
330+
331+
describe('probes adopted by a suspended user', () => {
332+
before(async () => {
333+
await dashboardClient('directus_users').insert({
334+
id: '89da69bd-a236-4ab7-9c5d-b5f52ce09959',
335+
status: 'suspended',
336+
adoption_token: 'adoptionTokenValue',
337+
default_prefix: 'jimaek',
338+
});
339+
340+
await dashboardClient(DASH_PROBES_TABLE).insert({
341+
id: randomUUID(),
342+
userId: '89da69bd-a236-4ab7-9c5d-b5f52ce09959',
343+
lastSyncDate: new Date(),
344+
ip: '1.2.3.4',
345+
uuid: '11111111-1111-4111-8111-111111111111',
346+
tags: '[{"prefix":"jimaek","value":"dashboardtag1"}]',
347+
status: 'ready',
348+
isIPv4Supported: false,
349+
isIPv6Supported: false,
350+
version: '0.26.0',
351+
nodeVersion: 'v18.14.2',
352+
country: 'AR',
353+
countryName: 'Argentina',
354+
continent: 'SA',
355+
continentName: 'South America',
356+
region: 'South America',
357+
city: 'Cordoba',
358+
latitude: -31.41,
359+
longitude: -64.18,
360+
network: 'InterBS S.R.L. (BAEHOST)',
361+
asn: 61004,
362+
allowedCountries: '["AR"]',
363+
customLocation: JSON.stringify({
364+
country: 'AR',
365+
city: 'Cordoba',
366+
latitude: -31.41,
367+
longitude: -64.18,
368+
state: null,
369+
}),
370+
});
371+
372+
await getIoContext().probeOverride.fetchDashboardData();
373+
});
374+
375+
after(async () => {
376+
await dashboardClient(DASH_PROBES_TABLE).where({ city: 'Cordoba' }).delete();
377+
await dashboardClient('directus_users').delete();
378+
});
379+
380+
it('should ignore the custom location and tags', async () => {
381+
nockGeoIpProviders({ ip2location: 'argentina', ipmap: 'argentina', maxmind: 'argentina', ipinfo: 'argentina', fastly: 'argentina' });
382+
const probe = await addFakeProbe();
383+
probe.emit('probe:status:update', 'ready');
384+
await waitForProbesUpdate();
385+
386+
await requestAgent.get('/v1/probes')
387+
.send()
388+
.expect(200)
389+
.expect((response) => {
390+
expect(response.body[0]).to.deep.equal({
391+
version: '0.39.0',
392+
location: {
393+
continent: 'SA',
394+
region: 'South America',
395+
country: 'AR',
396+
state: null,
397+
city: 'Buenos Aires',
398+
latitude: -34.61,
399+
longitude: -58.38,
400+
asn: 61004,
401+
network: 'InterBS S.R.L. (BAEHOST)',
402+
},
403+
tags: [],
404+
resolvers: [],
405+
});
406+
407+
expect(response).to.matchApiSchema();
408+
});
409+
});
410+
});
322411
});
323412
});

0 commit comments

Comments
 (0)