From de52c31ca7e8217e4d16cae411bfdfd0128c9f9f Mon Sep 17 00:00:00 2001 From: Alex Yarmosh Date: Mon, 27 Nov 2023 13:37:25 +0100 Subject: [PATCH] feat: allow any case for user tags filter (#449) --- src/probe/sockets-location-filter.ts | 2 +- .../measurement/create-measurement.test.ts | 18 +++++++++++++++++- .../unit/measurement/schema/schema.test.ts | 14 ++++++++++++++ test/tests/unit/probe/router.test.ts | 4 ++-- 4 files changed, 34 insertions(+), 4 deletions(-) diff --git a/src/probe/sockets-location-filter.ts b/src/probe/sockets-location-filter.ts index 9035bb83..29322521 100644 --- a/src/probe/sockets-location-filter.ts +++ b/src/probe/sockets-location-filter.ts @@ -51,7 +51,7 @@ export class SocketsLocationFilter { } static hasTag (socket: RemoteProbeSocket, tag: string) { - return socket.data.probe.tags.some(({ value }) => value === tag); + return socket.data.probe.tags.some(({ value }) => value.toLowerCase() === tag); } public filterGloballyDistibuted (sockets: RemoteProbeSocket[], limit: number): RemoteProbeSocket[] { diff --git a/test/tests/integration/measurement/create-measurement.test.ts b/test/tests/integration/measurement/create-measurement.test.ts index c1db2fab..a777c845 100644 --- a/test/tests/integration/measurement/create-measurement.test.ts +++ b/test/tests/integration/measurement/create-measurement.test.ts @@ -378,7 +378,7 @@ describe('Create measurement', () => { ip: '1.2.3.4', uuid: '1-1-1-1-1', isCustomCity: 1, - tags: '["dashboard-tag"]', + tags: '["Dashboard-Tag"]', status: 'ready', version: '0.26.0', country: 'US', @@ -429,6 +429,22 @@ describe('Create measurement', () => { }); }); + it('should create measurement with adopted "tags: ["u-jimaek-Dashboard-Tag"]" in any letter case', async () => { + await requestAgent.post('/v1/measurements') + .send({ + type: 'ping', + target: 'example.com', + locations: [{ tags: [ 'u-jimaek-Dashboard-Tag' ], limit: 2 }], + }) + .expect(202) + .expect((response) => { + expect(response.body.id).to.exist; + expect(response.header.location).to.exist; + expect(response.body.probesCount).to.equal(1); + expect(response).to.matchApiSchema(); + }); + }); + it('should not use create measurement with adopted tag in magic field "magic: ["u-jimaek-dashboard-tag"]" location', async () => { await requestAgent.post('/v1/measurements') .send({ diff --git a/test/tests/unit/measurement/schema/schema.test.ts b/test/tests/unit/measurement/schema/schema.test.ts index 28ba9286..617f0979 100644 --- a/test/tests/unit/measurement/schema/schema.test.ts +++ b/test/tests/unit/measurement/schema/schema.test.ts @@ -245,6 +245,20 @@ describe('command schema', async () => { expect(valid.value![0].region).to.equal('Northern America'); }); + it('should correct tag value (lowercase)', () => { + const input = [ + { + tags: [ 'DifferentCase-tag' ], + limit: 1, + }, + ]; + + const valid = locationSchema.validate(input); + + expect(valid.value![0].tags).to.not.equal(input[0]!.tags); + expect(valid.value![0].tags).to.deep.equal([ 'differentcase-tag' ]); + }); + it('should fail (wrong region)', () => { const input = [ { diff --git a/test/tests/unit/probe/router.test.ts b/test/tests/unit/probe/router.test.ts index 0632f390..52b351f2 100644 --- a/test/tests/unit/probe/router.test.ts +++ b/test/tests/unit/probe/router.test.ts @@ -822,13 +822,13 @@ describe('probe router', () => { const socket = await buildSocket(String(Date.now()), location); socket.data.probe.tags = [ ...socket.data.probe.tags, - { type: 'user', value: 'u-jimaek-dashboardtag' }, + { type: 'user', value: 'u-MartinKolarik-DashboardTag' }, ]; const sockets: DeepPartial = [ socket ]; const locations: Location[] = [ - { tags: [ 'u-jimaek-dashboardtag' ] }, + { tags: [ 'u-martinkolarik-dashboardtag' ] }, ]; fetchSocketsMock.resolves(sockets as never);