1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-12-28 00:06:53 +01:00
unleash.unleash/frontend/src/component/playground/Playground/playground.utils.test.ts
andreas-unleash 8e3863a27e
feat: add input for api token in playground (#5130)
Adds a token input in playground.

In the case of tokens that span multiple projects ie
`[]:development.etc` it will look into the token definitions to find the
token and get the projects

Otherwise it will try to parse the project and environment from the
token itself (without checking for it being a valid token)

Also, does not support admin tokens `*:*.etc`

Closes #
[1-1507](https://linear.app/unleash/issue/1-1507/create-a-token-input-in-the-playground-form)
<img width="1661" alt="Screenshot 2023-10-23 at 16 38 11"
src="https://github.com/Unleash/unleash/assets/104830839/f2d4fb6e-962f-4cc1-b5e4-817fd2de18ff">
<img width="1673" alt="Screenshot 2023-10-23 at 16 38 33"
src="https://github.com/Unleash/unleash/assets/104830839/27645955-d651-41e6-be02-4381c4f00551">

<img width="1377" alt="Screenshot 2023-10-25 at 17 06 43"
src="https://github.com/Unleash/unleash/assets/104830839/c7638366-3634-4521-af65-4f68a4f3b330">

---------

Signed-off-by: andreas-unleash <andreas@getunleash.ai>
2023-10-25 17:55:33 +03:00

125 lines
3.8 KiB
TypeScript

import {
normalizeCustomContextProperties,
NormalizedContextProperties,
validateTokenFormat,
} from './playground.utils';
test('should keep standard properties in their place', () => {
const input: NormalizedContextProperties = {
appName: 'testApp',
environment: 'testEnv',
userId: 'testUser',
sessionId: 'testSession',
remoteAddress: '127.0.0.1',
currentTime: 'now',
};
const output = normalizeCustomContextProperties(input);
expect(output).toEqual(input);
});
test('should move non-standard properties to nested properties field', () => {
const input = {
appName: 'testApp',
customProp: 'customValue',
anotherCustom: 'anotherValue',
};
const output = normalizeCustomContextProperties(input);
expect(output).toEqual({
appName: 'testApp',
properties: {
customProp: 'customValue',
anotherCustom: 'anotherValue',
},
});
});
test('should not have properties field if there are no non-standard properties', () => {
const input = {
appName: 'testApp',
};
const output = normalizeCustomContextProperties(input);
expect(output).toEqual(input);
expect(output.properties).toBeUndefined();
});
test('should combine existing properties field with non-standard properties', () => {
const input = {
appName: 'testApp',
properties: {
existingProp: 'existingValue',
},
customProp: 'customValue',
};
const output = normalizeCustomContextProperties(input);
expect(output).toEqual({
appName: 'testApp',
properties: {
existingProp: 'existingValue',
customProp: 'customValue',
},
});
});
test('should add multiple standard properties without breaking custom properties', () => {
const input = {
appName: 'testApp',
properties: {
existingProp: 'existingValue',
},
currentTime: 'value',
};
const output = normalizeCustomContextProperties(input);
expect(output).toEqual({
appName: 'testApp',
currentTime: 'value',
properties: {
existingProp: 'existingValue',
},
});
});
describe('validateTokenFormat', () => {
it('should throw an error for invalid token format without colon', () => {
const invalidToken = 'invalidToken';
expect(() => validateTokenFormat(invalidToken)).toThrow(
'Invalid token format',
);
});
it('should not throw an error for invalid token format without period', () => {
const invalidToken = 'project:environment';
expect(() => validateTokenFormat(invalidToken)).not.toThrow();
});
it('should throw an error for tokens with an empty project', () => {
const invalidToken = ':environment.abc123';
expect(() => validateTokenFormat(invalidToken)).toThrow(
'Invalid token format',
);
});
it('should throw an error for tokens with an empty environment', () => {
const invalidToken = 'project:.abc123';
expect(() => validateTokenFormat(invalidToken)).toThrow(
'Invalid token format',
);
});
it('should throw an error for admin tokens', () => {
const adminToken = 'project:*.abc123';
expect(() => validateTokenFormat(adminToken)).toThrow(
'Admin tokens are not supported in the playground',
);
});
it('should not throw an error for valid token formats', () => {
const validToken = 'project:environment.abc123';
expect(() => validateTokenFormat(validToken)).not.toThrow();
});
it('should not throw an error for valid token format and all projects', () => {
const validToken = '*:environment.abc123';
expect(() => validateTokenFormat(validToken)).not.toThrow();
});
});