mirror of
https://github.com/Abdulazizzn/n8n-enterprise-unlocked.git
synced 2025-12-19 11:01:15 +00:00
feat: Allow multi API creation via the UI (#12845)
This commit is contained in:
@@ -3,8 +3,10 @@ import { mock } from 'jest-mock-extended';
|
||||
|
||||
import type { ApiKey } from '@/databases/entities/api-key';
|
||||
import type { User } from '@/databases/entities/user';
|
||||
import { ApiKeyRepository } from '@/databases/repositories/api-key.repository';
|
||||
import { EventService } from '@/events/event.service';
|
||||
import type { ApiKeysRequest, AuthenticatedRequest } from '@/requests';
|
||||
import { License } from '@/license';
|
||||
import type { AuthenticatedRequest } from '@/requests';
|
||||
import { PublicApiKeyService } from '@/services/public-api-key.service';
|
||||
import { mockInstance } from '@test/mocking';
|
||||
|
||||
@@ -13,6 +15,8 @@ import { ApiKeysController } from '../api-keys.controller';
|
||||
describe('ApiKeysController', () => {
|
||||
const publicApiKeyService = mockInstance(PublicApiKeyService);
|
||||
const eventService = mockInstance(EventService);
|
||||
mockInstance(ApiKeyRepository);
|
||||
mockInstance(License);
|
||||
const controller = Container.get(ApiKeysController);
|
||||
|
||||
let req: AuthenticatedRequest;
|
||||
@@ -28,7 +32,7 @@ describe('ApiKeysController', () => {
|
||||
id: '123',
|
||||
userId: '123',
|
||||
label: 'My API Key',
|
||||
apiKey: 'apiKey********',
|
||||
apiKey: 'apiKey123',
|
||||
createdAt: new Date(),
|
||||
} as ApiKey;
|
||||
|
||||
@@ -36,14 +40,25 @@ describe('ApiKeysController', () => {
|
||||
|
||||
publicApiKeyService.createPublicApiKeyForUser.mockResolvedValue(apiKeyData);
|
||||
|
||||
publicApiKeyService.redactApiKey.mockImplementation(() => '***123');
|
||||
|
||||
// Act
|
||||
|
||||
const newApiKey = await controller.createAPIKey(req);
|
||||
const newApiKey = await controller.createAPIKey(req, mock(), mock());
|
||||
|
||||
// Assert
|
||||
|
||||
expect(publicApiKeyService.createPublicApiKeyForUser).toHaveBeenCalled();
|
||||
expect(apiKeyData).toEqual(newApiKey);
|
||||
expect(newApiKey).toEqual(
|
||||
expect.objectContaining({
|
||||
id: '123',
|
||||
userId: '123',
|
||||
label: 'My API Key',
|
||||
apiKey: '***123',
|
||||
createdAt: expect.any(Date),
|
||||
rawApiKey: 'apiKey123',
|
||||
}),
|
||||
);
|
||||
expect(eventService.emit).toHaveBeenCalledWith(
|
||||
'public-api-key-created',
|
||||
expect.objectContaining({ user: req.user, publicApi: false }),
|
||||
@@ -91,11 +106,11 @@ describe('ApiKeysController', () => {
|
||||
mfaEnabled: false,
|
||||
});
|
||||
|
||||
const req = mock<ApiKeysRequest.DeleteAPIKey>({ user, params: { id: user.id } });
|
||||
const req = mock<AuthenticatedRequest>({ user, params: { id: user.id } });
|
||||
|
||||
// Act
|
||||
|
||||
await controller.deleteAPIKey(req);
|
||||
await controller.deleteAPIKey(req, mock(), user.id);
|
||||
|
||||
publicApiKeyService.deleteApiKeyForUser.mockResolvedValue();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user