-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/master' into CAT-455-port-over-m…
…ore-dtos-part-4
- Loading branch information
Showing
54 changed files
with
1,300 additions
and
317 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
packages/@n8n/api-types/src/dto/ai/__tests__/ai-free-credits-request.dto.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { nanoId } from 'minifaker'; | ||
|
||
import { AiFreeCreditsRequestDto } from '../ai-free-credits-request.dto'; | ||
import 'minifaker/locales/en'; | ||
|
||
describe('AiChatRequestDto', () => { | ||
it('should succeed if projectId is a valid nanoid', () => { | ||
const validRequest = { | ||
projectId: nanoId.nanoid(), | ||
}; | ||
|
||
const result = AiFreeCreditsRequestDto.safeParse(validRequest); | ||
|
||
expect(result.success).toBe(true); | ||
}); | ||
|
||
it('should succeed if no projectId is sent', () => { | ||
const result = AiFreeCreditsRequestDto.safeParse({}); | ||
|
||
expect(result.success).toBe(true); | ||
}); | ||
|
||
it('should fail is projectId invalid value', () => { | ||
const validRequest = { | ||
projectId: '', | ||
}; | ||
|
||
const result = AiFreeCreditsRequestDto.safeParse(validRequest); | ||
|
||
expect(result.success).toBe(false); | ||
}); | ||
}); |
6 changes: 6 additions & 0 deletions
6
packages/@n8n/api-types/src/dto/ai/ai-free-credits-request.dto.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { z } from 'zod'; | ||
import { Z } from 'zod-class'; | ||
|
||
export class AiFreeCreditsRequestDto extends Z.class({ | ||
projectId: z.string().min(1).optional(), | ||
}) {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { nanoId, date, firstName, lastName, email } from 'minifaker'; | ||
import 'minifaker/locales/en'; | ||
|
||
import type { Project, ProjectType } from '@/databases/entities/project'; | ||
|
||
type RawProjectData = Pick<Project, 'name' | 'type' | 'createdAt' | 'updatedAt' | 'id'>; | ||
|
||
const projectName = `${firstName()} ${lastName()} <${email}>`; | ||
|
||
export const createRawProjectData = (payload: Partial<RawProjectData>): Project => { | ||
return { | ||
createdAt: date(), | ||
updatedAt: date(), | ||
id: nanoId.nanoid(), | ||
name: projectName, | ||
type: 'personal' as ProjectType, | ||
...payload, | ||
} as Project; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
packages/cli/src/credentials/__tests__/credentials.controller.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import { mock } from 'jest-mock-extended'; | ||
|
||
import { createRawProjectData } from '@/__tests__/project.test-data'; | ||
import type { SharedCredentialsRepository } from '@/databases/repositories/shared-credentials.repository'; | ||
import type { EventService } from '@/events/event.service'; | ||
import type { AuthenticatedRequest } from '@/requests'; | ||
|
||
import { createdCredentialsWithScopes, createNewCredentialsPayload } from './credentials.test-data'; | ||
import { CredentialsController } from '../credentials.controller'; | ||
import type { CredentialsService } from '../credentials.service'; | ||
|
||
describe('CredentialsController', () => { | ||
const eventService = mock<EventService>(); | ||
const credentialsService = mock<CredentialsService>(); | ||
const sharedCredentialsRepository = mock<SharedCredentialsRepository>(); | ||
|
||
const credentialsController = new CredentialsController( | ||
mock(), | ||
credentialsService, | ||
mock(), | ||
mock(), | ||
mock(), | ||
mock(), | ||
mock(), | ||
sharedCredentialsRepository, | ||
mock(), | ||
eventService, | ||
); | ||
|
||
let req: AuthenticatedRequest; | ||
beforeAll(() => { | ||
req = { user: { id: '123' } } as AuthenticatedRequest; | ||
}); | ||
|
||
describe('createCredentials', () => { | ||
it('it should create new credentials and emit "credentials-created"', async () => { | ||
// Arrange | ||
|
||
const newCredentialsPayload = createNewCredentialsPayload(); | ||
|
||
req.body = newCredentialsPayload; | ||
|
||
const { data, ...payloadWithoutData } = newCredentialsPayload; | ||
|
||
const createdCredentials = createdCredentialsWithScopes(payloadWithoutData); | ||
|
||
const projectOwningCredentialData = createRawProjectData({ | ||
id: newCredentialsPayload.projectId, | ||
}); | ||
|
||
credentialsService.createCredential.mockResolvedValue(createdCredentials); | ||
|
||
sharedCredentialsRepository.findCredentialOwningProject.mockResolvedValue( | ||
projectOwningCredentialData, | ||
); | ||
|
||
// Act | ||
|
||
const newApiKey = await credentialsController.createCredentials(req); | ||
|
||
// Assert | ||
|
||
expect(credentialsService.createCredential).toHaveBeenCalledWith( | ||
newCredentialsPayload, | ||
req.user, | ||
); | ||
expect(sharedCredentialsRepository.findCredentialOwningProject).toHaveBeenCalledWith( | ||
createdCredentials.id, | ||
); | ||
expect(eventService.emit).toHaveBeenCalledWith('credentials-created', { | ||
user: expect.objectContaining({ id: req.user.id }), | ||
credentialId: createdCredentials.id, | ||
credentialType: createdCredentials.type, | ||
projectId: projectOwningCredentialData.id, | ||
projectType: projectOwningCredentialData.type, | ||
publicApi: false, | ||
}); | ||
|
||
expect(newApiKey).toEqual(createdCredentials); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.