1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-02-09 00:18:00 +01:00

fix: resolve correct environment for admin tokens (#1951)

This commit is contained in:
olav 2022-08-22 14:44:38 +02:00 committed by GitHub
parent 651994a5a0
commit 655c0dea2c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 3 deletions

View File

@ -8,7 +8,7 @@ import {
mapFeaturesForClient, mapFeaturesForClient,
mapSegmentsForClient, mapSegmentsForClient,
} from '../util/offline-unleash-client'; } from '../util/offline-unleash-client';
import { ALL_PROJECTS } from '../util/constants'; import { ALL_ENVS, ALL_PROJECTS } from '../util/constants';
import { UnleashEvents } from 'unleash-client'; import { UnleashEvents } from 'unleash-client';
import { ANY_EVENT } from '../util/anyEventEmitter'; import { ANY_EVENT } from '../util/anyEventEmitter';
import { Logger } from '../logger'; import { Logger } from '../logger';
@ -99,7 +99,7 @@ export class ProxyRepository
return mapFeaturesForClient( return mapFeaturesForClient(
await this.services.featureToggleServiceV2.getClientFeatures({ await this.services.featureToggleServiceV2.getClientFeatures({
project: await this.projectIdsForToken(), project: await this.projectIdsForToken(),
environment: this.token.environment, environment: this.environmentNameForToken(),
}), }),
); );
} }
@ -118,4 +118,12 @@ export class ProxyRepository
return this.token.projects; return this.token.projects;
} }
private environmentNameForToken(): string {
if (this.token.environment === ALL_ENVS) {
return 'default';
}
return this.token.environment;
}
} }

View File

@ -190,6 +190,12 @@ test('should allow requests with a token secret alias', async () => {
}); });
test('should allow requests with an admin token', async () => { test('should allow requests with an admin token', async () => {
const featureA = randomId();
await createFeatureToggle({
name: featureA,
enabled: true,
strategies: [{ name: 'default', constraints: [], parameters: {} }],
});
const adminToken = await createApiToken(ApiTokenType.ADMIN, { const adminToken = await createApiToken(ApiTokenType.ADMIN, {
projects: ['*'], projects: ['*'],
environment: '*', environment: '*',
@ -199,7 +205,8 @@ test('should allow requests with an admin token', async () => {
.set('Authorization', adminToken.secret) .set('Authorization', adminToken.secret)
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
.expect(200) .expect(200)
.expect((res) => expect(res.body).toEqual({ toggles: [] })); .expect((res) => expect(res.body.toggles).toHaveLength(1))
.expect((res) => expect(res.body.toggles[0].name).toEqual(featureA));
}); });
test('should not allow admin requests with a frontend token', async () => { test('should not allow admin requests with a frontend token', async () => {