1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-08-04 13:48:56 +02:00

chore: clean up tests, fix admin type validation, projects* tests

This commit is contained in:
David Leek 2025-05-07 15:12:33 +02:00
parent e93810bc34
commit 46d3c3cab4
No known key found for this signature in database
GPG Key ID: 515EE0F1BB6D0BE1
6 changed files with 10 additions and 45 deletions

View File

@ -1,17 +1,5 @@
import type { FromSchema } from 'json-schema-to-ts';
import { mergeAllOfs } from '../util/all-of';
const adminSchema = {
required: ['type'],
type: 'object',
properties: {
type: {
type: 'string',
pattern: '^[Aa][Dd][Mm][Ii][Nn]$',
description: `An admin token. Must be the string "admin" (not case sensitive).`,
example: 'admin',
},
},
} as const;
const tokenNameSchema = {
type: 'object',
@ -86,10 +74,7 @@ export const createApiTokenSchema = {
type: 'object',
description:
'The data required to create an [Unleash API token](https://docs.getunleash.io/reference/api-tokens-and-client-keys).',
oneOf: [
mergeAllOfs([expireSchema, adminSchema, tokenNameSchema]),
mergeAllOfs([expireSchema, clientFrontendSchema, tokenNameSchema]),
],
oneOf: [mergeAllOfs([expireSchema, clientFrontendSchema, tokenNameSchema])],
components: {},
} as const;

View File

@ -307,11 +307,6 @@ export class ApiTokenController extends Controller {
const permissionRequired = tokenTypeToCreatePermission(
createToken.type,
);
if (createToken.type.toUpperCase() === 'ADMIN') {
throw new OperationDeniedError(
`Admin tokens are disabled in this instance. Use a Service account or a PAT to access admin operations instead`,
);
}
const hasPermission = await this.accessService.hasPermission(
req.user,
permissionRequired,

View File

@ -24,7 +24,7 @@ test('should not have default project set if projects is present', async () => {
expect(token.project).not.toBeDefined();
});
test('should have project set to default if projects is missing', async () => {
test('should have a projects entry consisting of ALL if projects is missing', async () => {
const token = await createApiToken.validateAsync({
tokenName: 'test',
type: 'client',
@ -32,7 +32,7 @@ test('should have project set to default if projects is missing', async () => {
expect(token.projects).toMatchObject([ALL]);
});
test('should not have project set if project is present', async () => {
test('should not have project set after validation if project is present', async () => {
const token = await createApiToken.validateAsync({
tokenName: 'test',
type: 'client',

View File

@ -12,16 +12,11 @@ export const createApiToken = joi
.required()
.valid(ApiTokenType.CLIENT, ApiTokenType.FRONTEND),
expiresAt: joi.date().optional(),
project: joi.when('projects', {
not: joi.required(),
then: joi.string().optional().default(ALL),
}),
projects: joi.array().min(0).optional(),
projects: joi.array().min(0).optional().default([ALL]),
environment: joi.when('type', {
is: joi.string().valid(ApiTokenType.CLIENT, ApiTokenType.FRONTEND),
then: joi.string().optional().default(DEFAULT_ENV),
otherwise: joi.string().optional().default(ALL),
}),
})
.nand('project', 'projects')
.options({ stripUnknown: true, allowUnknown: false, abortEarly: false });

View File

@ -241,9 +241,6 @@ test('A role with only CREATE_PROJECT_API_TOKEN can create project tokens', asyn
type: 'client',
})
.set('Content-Type', 'application/json')
.expect((res) => {
console.log(res.body);
})
.expect(201);
await destroy();
});

View File

@ -176,7 +176,7 @@ test('creates new client token with project & environment set', async () => {
.send({
tokenName: 'default-client',
type: 'client',
project: 'default',
projects: ['default'],
environment: DEFAULT_ENV,
})
.set('Content-Type', 'application/json')
@ -209,7 +209,7 @@ test('should prefix token with "project:environment."', async () => {
.send({
tokenName: 'default-client',
type: 'client',
project: 'default',
projects: ['default'],
environment: DEFAULT_ENV,
})
.set('Content-Type', 'application/json')
@ -225,7 +225,7 @@ test('should not create token for invalid projectId', async () => {
.send({
tokenName: 'default-client',
type: 'client',
project: 'bogus-project-something',
projects: ['bogus-project-something'],
})
.set('Content-Type', 'application/json')
.expect(400)
@ -264,19 +264,15 @@ test('needs tokenName property set', async () => {
.expect(400);
});
test('username can not be set', async () => {
test('can not create token with admin type', async () => {
return app.request
.post('/api/admin/api-tokens')
.send({
username: 'default-client-name',
tokenName: 'default-token-name',
type: 'client',
tokenName: 'default-client',
type: 'admin',
environment: '*',
})
.set('Content-Type', 'application/json')
.expect((res) => {
console.log(res.body);
})
.expect(400);
});
@ -307,9 +303,6 @@ test('should create token for disabled environment', async () => {
environment: 'disabledEnvironment',
})
.set('Content-Type', 'application/json')
.expect((res) => {
console.log(res.body);
})
.expect(201);
});