1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-12-28 00:06:53 +01:00
unleash.unleash/lib/routes/admin-api/feature-schema.test.js

175 lines
4.2 KiB
JavaScript
Raw Normal View History

'use strict';
2018-12-17 09:24:49 +01:00
const test = require('ava');
const { featureShema } = require('./feature-schema');
test('should require URL firendly name', t => {
const toggle = {
name: 'io`dasd',
enabled: false,
strategies: [{ name: 'default' }],
};
2020-01-02 19:23:52 +01:00
const { error } = featureShema.validate(toggle);
t.deepEqual(error.details[0].message, '"name" must be URL friendly');
});
test('should be valid toggle name', t => {
const toggle = {
name: 'app.name',
type: 'release',
enabled: false,
stale: false,
strategies: [{ name: 'default' }],
};
2020-01-02 19:23:52 +01:00
const { value } = featureShema.validate(toggle);
t.deepEqual(value, toggle);
});
test('should strip extra variant fields', t => {
const toggle = {
name: 'app.name',
type: 'release',
enabled: false,
stale: false,
strategies: [{ name: 'default' }],
variants: [
{
name: 'variant-a',
weight: 1,
unkown: 'not-allowed',
},
],
};
2020-01-02 19:23:52 +01:00
const { value } = featureShema.validate(toggle);
t.notDeepEqual(value, toggle);
t.falsy(value.variants[0].unkown);
});
test('should allow weightType=fix', t => {
const toggle = {
name: 'app.name',
type: 'release',
enabled: false,
stale: false,
strategies: [{ name: 'default' }],
variants: [
{
name: 'variant-a',
weight: 1,
weightType: 'fix',
},
],
};
const { value } = featureShema.validate(toggle);
t.deepEqual(value, toggle);
});
test('should disallow weightType=unknown', t => {
const toggle = {
name: 'app.name',
type: 'release',
enabled: false,
stale: false,
strategies: [{ name: 'default' }],
variants: [
{
name: 'variant-a',
weight: 1,
weightType: 'unknown',
},
],
};
const { error } = featureShema.validate(toggle);
t.deepEqual(
error.details[0].message,
'"variants[0].weightType" must be one of [variable, fix]',
);
});
test('should be possible to define variant overrides', t => {
const toggle = {
name: 'app.name',
type: 'release',
enabled: false,
stale: false,
strategies: [{ name: 'default' }],
variants: [
{
name: 'variant-a',
weight: 1,
weightType: 'variable',
overrides: [
{
contextName: 'userId',
values: ['123'],
},
],
},
],
};
2020-01-02 19:23:52 +01:00
const { value, error } = featureShema.validate(toggle);
t.deepEqual(value, toggle);
t.falsy(error);
});
test('variant overrides must have corect shape', async t => {
t.plan(1);
const toggle = {
name: 'app.name',
type: 'release',
enabled: false,
stale: false,
strategies: [{ name: 'default' }],
variants: [
{
name: 'variant-a',
weight: 1,
overrides: {
userId: ['not-alloed'],
sessionId: ['not-alloed'],
},
},
],
};
try {
2020-01-02 19:23:52 +01:00
await featureShema.validateAsync(toggle);
} catch (error) {
2020-01-02 19:23:52 +01:00
t.is(
error.details[0].message,
'"variants[0].overrides" must be an array',
2020-01-02 19:23:52 +01:00
);
}
});
test('should keep constraints', t => {
const toggle = {
name: 'app.constraints',
type: 'release',
enabled: false,
stale: false,
strategies: [
{
name: 'default',
constraints: [
{
contextName: 'environment',
operator: 'IN',
values: ['asd'],
},
],
},
],
};
2020-01-02 19:23:52 +01:00
const { value, error } = featureShema.validate(toggle);
t.deepEqual(value, toggle);
t.falsy(error);
});