mirror of
				https://github.com/Unleash/unleash.git
				synced 2025-10-27 11:02:16 +01:00 
			
		
		
		
	feat: Add suppport for variant overrides
This commit is contained in:
		
							parent
							
								
									4d6fab8ea0
								
							
						
					
					
						commit
						b36fc73a7b
					
				@ -24,6 +24,15 @@ const variantsSchema = joi.object().keys({
 | 
				
			|||||||
            value: joi.string().required(),
 | 
					            value: joi.string().required(),
 | 
				
			||||||
        })
 | 
					        })
 | 
				
			||||||
        .optional(),
 | 
					        .optional(),
 | 
				
			||||||
 | 
					    overrides: joi.array().items(
 | 
				
			||||||
 | 
					        joi
 | 
				
			||||||
 | 
					            .object()
 | 
				
			||||||
 | 
					            .keys({
 | 
				
			||||||
 | 
					                field: joi.string().required(),
 | 
				
			||||||
 | 
					                values: joi.array().items(joi.string()),
 | 
				
			||||||
 | 
					            })
 | 
				
			||||||
 | 
					            .optional()
 | 
				
			||||||
 | 
					    ),
 | 
				
			||||||
});
 | 
					});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const featureShema = joi
 | 
					const featureShema = joi
 | 
				
			||||||
 | 
				
			|||||||
@ -25,3 +25,71 @@ test('should be valid toggle name', t => {
 | 
				
			|||||||
    const { value } = joi.validate(toggle, featureShema);
 | 
					    const { value } = joi.validate(toggle, featureShema);
 | 
				
			||||||
    t.deepEqual(value, toggle);
 | 
					    t.deepEqual(value, toggle);
 | 
				
			||||||
});
 | 
					});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					test('should strip extra variant fields', t => {
 | 
				
			||||||
 | 
					    const toggle = {
 | 
				
			||||||
 | 
					        name: 'app.name',
 | 
				
			||||||
 | 
					        enabled: false,
 | 
				
			||||||
 | 
					        strategies: [{ name: 'default' }],
 | 
				
			||||||
 | 
					        variants: [
 | 
				
			||||||
 | 
					            {
 | 
				
			||||||
 | 
					                name: 'variant-a',
 | 
				
			||||||
 | 
					                weight: 1,
 | 
				
			||||||
 | 
					                unkown: 'not-allowed',
 | 
				
			||||||
 | 
					            },
 | 
				
			||||||
 | 
					        ],
 | 
				
			||||||
 | 
					    };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    const { value } = joi.validate(toggle, featureShema);
 | 
				
			||||||
 | 
					    t.notDeepEqual(value, toggle);
 | 
				
			||||||
 | 
					    t.falsy(value.variants[0].unkown);
 | 
				
			||||||
 | 
					});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					test('should be possible to define variant overrides', t => {
 | 
				
			||||||
 | 
					    const toggle = {
 | 
				
			||||||
 | 
					        name: 'app.name',
 | 
				
			||||||
 | 
					        enabled: false,
 | 
				
			||||||
 | 
					        strategies: [{ name: 'default' }],
 | 
				
			||||||
 | 
					        variants: [
 | 
				
			||||||
 | 
					            {
 | 
				
			||||||
 | 
					                name: 'variant-a',
 | 
				
			||||||
 | 
					                weight: 1,
 | 
				
			||||||
 | 
					                overrides: [
 | 
				
			||||||
 | 
					                    {
 | 
				
			||||||
 | 
					                        field: 'userId',
 | 
				
			||||||
 | 
					                        values: ['123'],
 | 
				
			||||||
 | 
					                    },
 | 
				
			||||||
 | 
					                ],
 | 
				
			||||||
 | 
					            },
 | 
				
			||||||
 | 
					        ],
 | 
				
			||||||
 | 
					    };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    const { value, error } = joi.validate(toggle, featureShema);
 | 
				
			||||||
 | 
					    t.deepEqual(value, toggle);
 | 
				
			||||||
 | 
					    t.falsy(error);
 | 
				
			||||||
 | 
					});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					test('variant overrides must have corect shape', async t => {
 | 
				
			||||||
 | 
					    t.plan(1);
 | 
				
			||||||
 | 
					    const toggle = {
 | 
				
			||||||
 | 
					        name: 'app.name',
 | 
				
			||||||
 | 
					        enabled: false,
 | 
				
			||||||
 | 
					        strategies: [{ name: 'default' }],
 | 
				
			||||||
 | 
					        variants: [
 | 
				
			||||||
 | 
					            {
 | 
				
			||||||
 | 
					                name: 'variant-a',
 | 
				
			||||||
 | 
					                weight: 1,
 | 
				
			||||||
 | 
					                overrides: {
 | 
				
			||||||
 | 
					                    userId: ['not-alloed'],
 | 
				
			||||||
 | 
					                    sessionId: ['not-alloed'],
 | 
				
			||||||
 | 
					                },
 | 
				
			||||||
 | 
					            },
 | 
				
			||||||
 | 
					        ],
 | 
				
			||||||
 | 
					    };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    try {
 | 
				
			||||||
 | 
					        await joi.validate(toggle, featureShema);
 | 
				
			||||||
 | 
					    } catch (error) {
 | 
				
			||||||
 | 
					        t.is(error.details[0].message, '"overrides" must be an array');
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					});
 | 
				
			||||||
 | 
				
			|||||||
@ -234,3 +234,31 @@ test.serial('should not be possible to create archived toggle', async t => {
 | 
				
			|||||||
        .expect(400)
 | 
					        .expect(400)
 | 
				
			||||||
        .then(destroy);
 | 
					        .then(destroy);
 | 
				
			||||||
});
 | 
					});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					test.serial('creates new feature toggle with variant overrides', async t => {
 | 
				
			||||||
 | 
					    t.plan(0);
 | 
				
			||||||
 | 
					    const { request, destroy } = await setupApp('feature_api_serial');
 | 
				
			||||||
 | 
					    return request
 | 
				
			||||||
 | 
					        .post('/api/admin/features')
 | 
				
			||||||
 | 
					        .send({
 | 
				
			||||||
 | 
					            name: 'com.test.variants',
 | 
				
			||||||
 | 
					            enabled: false,
 | 
				
			||||||
 | 
					            strategies: [{ name: 'default' }],
 | 
				
			||||||
 | 
					            variants: [
 | 
				
			||||||
 | 
					                {
 | 
				
			||||||
 | 
					                    name: 'variant1',
 | 
				
			||||||
 | 
					                    weight: 50,
 | 
				
			||||||
 | 
					                    overrides: [
 | 
				
			||||||
 | 
					                        {
 | 
				
			||||||
 | 
					                            field: 'userId',
 | 
				
			||||||
 | 
					                            values: ['123'],
 | 
				
			||||||
 | 
					                        },
 | 
				
			||||||
 | 
					                    ],
 | 
				
			||||||
 | 
					                },
 | 
				
			||||||
 | 
					                { name: 'variant2', weight: 50 },
 | 
				
			||||||
 | 
					            ],
 | 
				
			||||||
 | 
					        })
 | 
				
			||||||
 | 
					        .set('Content-Type', 'application/json')
 | 
				
			||||||
 | 
					        .expect(201)
 | 
				
			||||||
 | 
					        .then(destroy);
 | 
				
			||||||
 | 
					});
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
		Reference in New Issue
	
	Block a user