mirror of
https://github.com/Unleash/unleash.git
synced 2025-09-19 17:52:45 +02:00
chore: support for all envs token
This commit is contained in:
parent
d9fb624c39
commit
151dd42bf3
@ -15,7 +15,7 @@ import type {
|
|||||||
IEventSearchParams,
|
IEventSearchParams,
|
||||||
IEventStore,
|
IEventStore,
|
||||||
} from '../../types/stores/event-store.js';
|
} from '../../types/stores/event-store.js';
|
||||||
import { sharedEventEmitter } from '../../util/index.js';
|
import { ALL_ENVS, sharedEventEmitter } from '../../util/index.js';
|
||||||
import type { Db } from '../../db/db.js';
|
import type { Db } from '../../db/db.js';
|
||||||
import type { Knex } from 'knex';
|
import type { Knex } from 'knex';
|
||||||
import type EventEmitter from 'node:events';
|
import type EventEmitter from 'node:events';
|
||||||
@ -199,7 +199,7 @@ export class EventStore implements IEventStore {
|
|||||||
.whereNotNull('feature_name')
|
.whereNotNull('feature_name')
|
||||||
.whereNotIn('type', [FEATURE_CREATED, FEATURE_TAGGED])
|
.whereNotIn('type', [FEATURE_CREATED, FEATURE_TAGGED])
|
||||||
.whereNot('type', 'LIKE', 'change-%');
|
.whereNot('type', 'LIKE', 'change-%');
|
||||||
if (environment) {
|
if (environment && environment !== ALL_ENVS) {
|
||||||
inner.where('environment', environment);
|
inner.where('environment', environment);
|
||||||
}
|
}
|
||||||
return inner;
|
return inner;
|
||||||
|
@ -25,9 +25,19 @@ const validTokens = [
|
|||||||
type: ApiTokenType.CLIENT,
|
type: ApiTokenType.CLIENT,
|
||||||
secret: '*:production.client',
|
secret: '*:production.client',
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
tokenName: 'all-envs-client',
|
||||||
|
permissions: [CLIENT],
|
||||||
|
projects: ['*'],
|
||||||
|
environment: '*',
|
||||||
|
type: ApiTokenType.CLIENT,
|
||||||
|
secret: '*:*.hungry-client',
|
||||||
|
},
|
||||||
];
|
];
|
||||||
const devTokenSecret = validTokens[0].secret;
|
const devTokenSecret = validTokens[0].secret;
|
||||||
const prodTokenSecret = validTokens[1].secret;
|
const prodTokenSecret = validTokens[1].secret;
|
||||||
|
const allEnvsTokenSecret = validTokens[2].secret;
|
||||||
|
|
||||||
async function setup({
|
async function setup({
|
||||||
etagVariantName,
|
etagVariantName,
|
||||||
enabled,
|
enabled,
|
||||||
@ -96,53 +106,54 @@ async function initialize({ app, db }: { app: IUnleashTest; db: ITestDb }) {
|
|||||||
featureName,
|
featureName,
|
||||||
type,
|
type,
|
||||||
}));
|
}));
|
||||||
|
let nextId = 8; // this is the first id after the token creation events
|
||||||
const expectedEvents = [
|
const expectedEvents = [
|
||||||
{
|
{
|
||||||
id: 7,
|
id: nextId++,
|
||||||
featureName: 'X',
|
featureName: 'X',
|
||||||
type: 'feature-created',
|
type: 'feature-created',
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
id: 8,
|
id: nextId++,
|
||||||
featureName: 'Y',
|
featureName: 'Y',
|
||||||
type: 'feature-created',
|
type: 'feature-created',
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
id: 9,
|
id: nextId++,
|
||||||
featureName: 'Y',
|
featureName: 'Y',
|
||||||
type: 'feature-archived',
|
type: 'feature-archived',
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
id: 10,
|
id: nextId++,
|
||||||
featureName: 'Z',
|
featureName: 'Z',
|
||||||
type: 'feature-created',
|
type: 'feature-created',
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
id: 11,
|
id: nextId++,
|
||||||
environment: 'development',
|
environment: 'development',
|
||||||
featureName: 'Z',
|
featureName: 'Z',
|
||||||
type: 'feature-strategy-add',
|
type: 'feature-strategy-add',
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
id: 12,
|
id: nextId++,
|
||||||
environment: 'development',
|
environment: 'development',
|
||||||
featureName: 'Z',
|
featureName: 'Z',
|
||||||
type: 'feature-environment-enabled',
|
type: 'feature-environment-enabled',
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
id: 13,
|
id: nextId++,
|
||||||
environment: 'production',
|
environment: 'production',
|
||||||
featureName: 'Z',
|
featureName: 'Z',
|
||||||
type: 'feature-strategy-add',
|
type: 'feature-strategy-add',
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
id: 14,
|
id: nextId++,
|
||||||
environment: 'production',
|
environment: 'production',
|
||||||
featureName: 'Z',
|
featureName: 'Z',
|
||||||
type: 'feature-environment-enabled',
|
type: 'feature-environment-enabled',
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
id: 15,
|
id: nextId++,
|
||||||
featureName: 'X',
|
featureName: 'X',
|
||||||
type: 'change-request-created',
|
type: 'change-request-created',
|
||||||
},
|
},
|
||||||
@ -191,11 +202,11 @@ describe.each([
|
|||||||
.expect(200);
|
.expect(200);
|
||||||
|
|
||||||
if (enabled) {
|
if (enabled) {
|
||||||
expect(res.headers.etag).toBe(`"76d8bb0e:12:${name}"`);
|
expect(res.headers.etag).toBe(`"76d8bb0e:13:${name}"`);
|
||||||
expect(res.body.meta.etag).toBe(`"76d8bb0e:12:${name}"`);
|
expect(res.body.meta.etag).toBe(`"76d8bb0e:13:${name}"`);
|
||||||
} else {
|
} else {
|
||||||
expect(res.headers.etag).toBe('"76d8bb0e:12"');
|
expect(res.headers.etag).toBe('"76d8bb0e:13"');
|
||||||
expect(res.body.meta.etag).toBe('"76d8bb0e:12"');
|
expect(res.body.meta.etag).toBe('"76d8bb0e:13"');
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -203,12 +214,12 @@ describe.each([
|
|||||||
const res = await app.request
|
const res = await app.request
|
||||||
.get('/api/client/features')
|
.get('/api/client/features')
|
||||||
.set('Authorization', devTokenSecret)
|
.set('Authorization', devTokenSecret)
|
||||||
.set('if-none-match', '"76d8bb0e:12"')
|
.set('if-none-match', '"76d8bb0e:13"')
|
||||||
.expect(enabled ? 200 : 304);
|
.expect(enabled ? 200 : 304);
|
||||||
|
|
||||||
if (enabled) {
|
if (enabled) {
|
||||||
expect(res.headers.etag).toBe(`"76d8bb0e:12:${name}"`);
|
expect(res.headers.etag).toBe(`"76d8bb0e:13:${name}"`);
|
||||||
expect(res.body.meta.etag).toBe(`"76d8bb0e:12:${name}"`);
|
expect(res.body.meta.etag).toBe(`"76d8bb0e:13:${name}"`);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -219,35 +230,47 @@ describe.each([
|
|||||||
await app.request
|
await app.request
|
||||||
.get('/api/client/features')
|
.get('/api/client/features')
|
||||||
.set('Authorization', devTokenSecret)
|
.set('Authorization', devTokenSecret)
|
||||||
.set('if-none-match', `"76d8bb0e:12${enabled ? `:${name}` : ''}"`)
|
.set('if-none-match', `"76d8bb0e:13${enabled ? `:${name}` : ''}"`)
|
||||||
.expect(304);
|
.expect(304);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('a token with all envs should get the max id regardless of the environment', async () => {
|
||||||
|
const currentProdEtag = `"67e24428:15${enabled ? `:${name}` : ''}"`;
|
||||||
|
const { headers } = await app.request
|
||||||
|
.get('/api/client/features')
|
||||||
|
.set('if-none-match', currentProdEtag)
|
||||||
|
.set('Authorization', allEnvsTokenSecret)
|
||||||
|
.expect(200);
|
||||||
|
|
||||||
|
// it's a different hash than prod, but gets the max id
|
||||||
|
expect(headers.etag).toEqual(
|
||||||
|
`"ae443048:15${enabled ? `:${name}` : ''}"`,
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
test('production environment gets a different etag than development', async () => {
|
test('production environment gets a different etag than development', async () => {
|
||||||
const { headers: prodHeaders } = await app.request
|
const { headers: prodHeaders } = await app.request
|
||||||
.get('/api/client/features?bla=1')
|
.get('/api/client/features?bla=1')
|
||||||
.set('Authorization', prodTokenSecret)
|
.set('Authorization', prodTokenSecret)
|
||||||
.expect(200);
|
.expect(200);
|
||||||
if (enabled) {
|
|
||||||
expect(prodHeaders.etag).toEqual('"67e24428:14:v2"');
|
expect(prodHeaders.etag).toEqual(
|
||||||
} else {
|
`"67e24428:15${enabled ? `:${name}` : ''}"`,
|
||||||
expect(prodHeaders.etag).toEqual('"67e24428:14"');
|
);
|
||||||
}
|
|
||||||
|
|
||||||
const { headers: devHeaders } = await app.request
|
const { headers: devHeaders } = await app.request
|
||||||
.get('/api/client/features')
|
.get('/api/client/features')
|
||||||
.set('Authorization', devTokenSecret)
|
.set('Authorization', devTokenSecret)
|
||||||
.expect(200);
|
.expect(200);
|
||||||
if (enabled) {
|
|
||||||
expect(devHeaders.etag).toEqual('"76d8bb0e:12:v2"');
|
expect(devHeaders.etag).toEqual(
|
||||||
} else {
|
`"76d8bb0e:13${enabled ? `:${name}` : ''}"`,
|
||||||
expect(devHeaders.etag).toEqual('"76d8bb0e:12"');
|
);
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
test('modifying dev environment should only invalidate dev tokens', async () => {
|
test('modifying dev environment should only invalidate dev tokens', async () => {
|
||||||
const currentDevEtag = `"76d8bb0e:12${enabled ? `:${name}` : ''}"`;
|
const currentDevEtag = `"76d8bb0e:13${enabled ? `:${name}` : ''}"`;
|
||||||
const currentProdEtag = `"67e24428:14${enabled ? `:${name}` : ''}"`;
|
const currentProdEtag = `"67e24428:15${enabled ? `:${name}` : ''}"`;
|
||||||
await app.request
|
await app.request
|
||||||
.get('/api/client/features')
|
.get('/api/client/features')
|
||||||
.set('if-none-match', currentProdEtag)
|
.set('if-none-match', currentProdEtag)
|
||||||
@ -276,10 +299,10 @@ describe.each([
|
|||||||
.expect(200);
|
.expect(200);
|
||||||
|
|
||||||
// Note: this test yields a different result if run in isolation
|
// Note: this test yields a different result if run in isolation
|
||||||
// this is because the id 18 depends on a previous test adding a feature
|
// this is because the id 19 depends on a previous test adding a feature
|
||||||
// otherwise the id will be 17
|
// otherwise the id will be 18
|
||||||
expect(devHeaders.etag).toEqual(
|
expect(devHeaders.etag).toEqual(
|
||||||
`"76d8bb0e:18${enabled ? `:${name}` : ''}"`,
|
`"76d8bb0e:19${enabled ? `:${name}` : ''}"`,
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user