From 2ede29ed584db02bc44cd6b2ae551dd12e8a7978 Mon Sep 17 00:00:00 2001 From: Thomas Heartman Date: Tue, 12 Mar 2024 11:14:39 +0100 Subject: [PATCH 1/8] chore: add test cases for certain edge cases regarding the context --- src/test/create-context.test.ts | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/test/create-context.test.ts b/src/test/create-context.test.ts index 8c9272c..88b49e8 100644 --- a/src/test/create-context.test.ts +++ b/src/test/create-context.test.ts @@ -25,6 +25,29 @@ test('should move rest props to properties', () => { expect(context.properties?.tenantId).toBe('some-tenant'); }); +test('when an extra property is both on the top-level and under properties, the properties-level wins', () => { + const context = createContext({ + customProperty: 'top-level', + properties: { + customProperty: 'properties-level', + } + }); + + expect(context).not.toHaveProperty('customProperty'); + expect(context.properties?.customProperty).toBe('properties-level'); +}); + +test('If you specify top-level properties under properties, they do not get moved up', () => { + const context = createContext({ + properties: { + appName: 'name' + } + }); + + expect(context.properties?.appName).toBe('name'); + expect(context.appName).toBe(undefined); +}); + test('should keep properties', () => { const context = createContext({ userId: '123', From 4b85aa55eb183817975b180a454c217819125583 Mon Sep 17 00:00:00 2001 From: Thomas Heartman Date: Tue, 12 Mar 2024 11:16:30 +0100 Subject: [PATCH 2/8] chore: add other test --- src/test/unleash-proxy.test.ts | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/test/unleash-proxy.test.ts b/src/test/unleash-proxy.test.ts index b5092f4..7bc6289 100644 --- a/src/test/unleash-proxy.test.ts +++ b/src/test/unleash-proxy.test.ts @@ -338,6 +338,33 @@ test('Should remove "undefined" environment field from context', async () => { expect(client.queriedContexts[0]).not.toHaveProperty('environment'); }); +test('Multiple properties get collapsed', async () => { + const client = new MockClient([]); + + const proxySecrets = ['sdf']; + const app = createApp( + { + unleashUrl, + unleashApiToken, + proxySecrets, + environment: 'test', + }, + client, + ); + client.emit('ready'); + + await request(app) + .get('/proxy?userId=123&properties=test&properties[otherprop]=other') + .set('Authorization', 'sdf') + .expect(200) + .expect('Content-Type', /json/); + + console.log(JSON.stringify(client.queriedContexts[0], null, 2)) + const queriedProperties = client.queriedContexts[0].properties; + expect(queriedProperties?.properties).toBe('test') + expect(queriedProperties?.otherProp).toBe('other') +}); + test('Should register metrics', () => { const toggles = [ { From e2b7bcd1ff07d836da516c0edec35b6610c14305 Mon Sep 17 00:00:00 2001 From: Thomas Heartman Date: Tue, 12 Mar 2024 11:19:42 +0100 Subject: [PATCH 3/8] chore: formatting --- src/test/create-context.test.ts | 20 ++++++++++---------- src/test/unleash-proxy.test.ts | 8 ++++---- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/test/create-context.test.ts b/src/test/create-context.test.ts index 88b49e8..191fa6f 100644 --- a/src/test/create-context.test.ts +++ b/src/test/create-context.test.ts @@ -27,25 +27,25 @@ test('should move rest props to properties', () => { test('when an extra property is both on the top-level and under properties, the properties-level wins', () => { const context = createContext({ - customProperty: 'top-level', - properties: { - customProperty: 'properties-level', - } + customProperty: 'top-level', + properties: { + customProperty: 'properties-level', + }, }); expect(context).not.toHaveProperty('customProperty'); - expect(context.properties?.customProperty).toBe('properties-level'); + expect(context.properties?.customProperty).toBe('properties-level'); }); test('If you specify top-level properties under properties, they do not get moved up', () => { const context = createContext({ - properties: { - appName: 'name' - } + properties: { + appName: 'name', + }, }); - expect(context.properties?.appName).toBe('name'); - expect(context.appName).toBe(undefined); + expect(context.properties?.appName).toBe('name'); + expect(context.appName).toBe(undefined); }); test('should keep properties', () => { diff --git a/src/test/unleash-proxy.test.ts b/src/test/unleash-proxy.test.ts index 7bc6289..1c26212 100644 --- a/src/test/unleash-proxy.test.ts +++ b/src/test/unleash-proxy.test.ts @@ -359,10 +359,10 @@ test('Multiple properties get collapsed', async () => { .expect(200) .expect('Content-Type', /json/); - console.log(JSON.stringify(client.queriedContexts[0], null, 2)) - const queriedProperties = client.queriedContexts[0].properties; - expect(queriedProperties?.properties).toBe('test') - expect(queriedProperties?.otherProp).toBe('other') + console.log(JSON.stringify(client.queriedContexts[0], null, 2)); + const queriedProperties = client.queriedContexts[0].properties; + expect(queriedProperties?.properties).toBe('test'); + expect(queriedProperties?.otherProp).toBe('other'); }); test('Should register metrics', () => { From bfbd41774cb55a1bd54a1025085aad4d68bd808f Mon Sep 17 00:00:00 2001 From: Thomas Heartman Date: Tue, 12 Mar 2024 11:23:11 +0100 Subject: [PATCH 4/8] chore: url-encode --- src/test/unleash-proxy.test.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/test/unleash-proxy.test.ts b/src/test/unleash-proxy.test.ts index 1c26212..70ae28e 100644 --- a/src/test/unleash-proxy.test.ts +++ b/src/test/unleash-proxy.test.ts @@ -354,7 +354,9 @@ test('Multiple properties get collapsed', async () => { client.emit('ready'); await request(app) - .get('/proxy?userId=123&properties=test&properties[otherprop]=other') + .get( + '/proxy?userId=123&properties=test&properties%5Botherprop%5D=other', + ) .set('Authorization', 'sdf') .expect(200) .expect('Content-Type', /json/); From ca4d594e7bbe41e4b2d03acf7c8d7db0bd773fd0 Mon Sep 17 00:00:00 2001 From: Thomas Heartman Date: Tue, 12 Mar 2024 11:37:23 +0100 Subject: [PATCH 5/8] chore: only sub-property --- src/test/unleash-proxy.test.ts | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/test/unleash-proxy.test.ts b/src/test/unleash-proxy.test.ts index 70ae28e..646cd8e 100644 --- a/src/test/unleash-proxy.test.ts +++ b/src/test/unleash-proxy.test.ts @@ -354,9 +354,7 @@ test('Multiple properties get collapsed', async () => { client.emit('ready'); await request(app) - .get( - '/proxy?userId=123&properties=test&properties%5Botherprop%5D=other', - ) + .get('/proxy?userId=123&properties%5Botherprop%5D=other') .set('Authorization', 'sdf') .expect(200) .expect('Content-Type', /json/); From beea211a2246e339d81144ab9352d1391b7b8ce5 Mon Sep 17 00:00:00 2001 From: Thomas Heartman Date: Tue, 12 Mar 2024 11:39:23 +0100 Subject: [PATCH 6/8] chore: use flags --- src/test/unleash-proxy.test.ts | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/test/unleash-proxy.test.ts b/src/test/unleash-proxy.test.ts index 646cd8e..e42a2d8 100644 --- a/src/test/unleash-proxy.test.ts +++ b/src/test/unleash-proxy.test.ts @@ -339,7 +339,14 @@ test('Should remove "undefined" environment field from context', async () => { }); test('Multiple properties get collapsed', async () => { - const client = new MockClient([]); + const toggles = [ + { + name: 'test', + enabled: true, + impressionData: true, + }, + ]; + const client = new MockClient(toggles); const proxySecrets = ['sdf']; const app = createApp( From 26bcb0c77512e1f71d04d3b503ec4f46f02318f6 Mon Sep 17 00:00:00 2001 From: Thomas Heartman Date: Tue, 12 Mar 2024 11:44:40 +0100 Subject: [PATCH 7/8] chore: test string value --- src/test/unleash-proxy.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/unleash-proxy.test.ts b/src/test/unleash-proxy.test.ts index e42a2d8..585153c 100644 --- a/src/test/unleash-proxy.test.ts +++ b/src/test/unleash-proxy.test.ts @@ -361,7 +361,7 @@ test('Multiple properties get collapsed', async () => { client.emit('ready'); await request(app) - .get('/proxy?userId=123&properties%5Botherprop%5D=other') + .get('/proxy?userId=123&properties=string') .set('Authorization', 'sdf') .expect(200) .expect('Content-Type', /json/); From 69beb7a7a297cb258709fb994e3787347fd853af Mon Sep 17 00:00:00 2001 From: Thomas Heartman Date: Tue, 12 Mar 2024 11:51:14 +0100 Subject: [PATCH 8/8] chore: change test --- src/test/unleash-proxy.test.ts | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/src/test/unleash-proxy.test.ts b/src/test/unleash-proxy.test.ts index 585153c..2814373 100644 --- a/src/test/unleash-proxy.test.ts +++ b/src/test/unleash-proxy.test.ts @@ -338,7 +338,7 @@ test('Should remove "undefined" environment field from context', async () => { expect(client.queriedContexts[0]).not.toHaveProperty('environment'); }); -test('Multiple properties get collapsed', async () => { +test('Providing a string for `properties` yields a 400', async () => { const toggles = [ { name: 'test', @@ -363,13 +363,7 @@ test('Multiple properties get collapsed', async () => { await request(app) .get('/proxy?userId=123&properties=string') .set('Authorization', 'sdf') - .expect(200) - .expect('Content-Type', /json/); - - console.log(JSON.stringify(client.queriedContexts[0], null, 2)); - const queriedProperties = client.queriedContexts[0].properties; - expect(queriedProperties?.properties).toBe('test'); - expect(queriedProperties?.otherProp).toBe('other'); + .expect(400); }); test('Should register metrics', () => {