-
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.
- Loading branch information
1 parent
c45f47a
commit 041fb91
Showing
3 changed files
with
163 additions
and
0 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
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; | ||
}; |
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); | ||
}); | ||
}); | ||
}); |
62 changes: 62 additions & 0 deletions
62
packages/cli/src/credentials/__tests__/credentials.test-data.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,62 @@ | ||
import type { Scope } from '@n8n/permissions'; | ||
import { nanoId, date } from 'minifaker'; | ||
import { randomString } from 'n8n-workflow'; | ||
|
||
import type { CredentialRequest } from '@/requests'; | ||
|
||
type NewCredentialWithSCopes = { | ||
scopes: Scope[]; | ||
name: string; | ||
data: string; | ||
type: string; | ||
isManaged: boolean; | ||
id: string; | ||
createdAt: Date; | ||
updatedAt: Date; | ||
}; | ||
|
||
const name = 'new Credential'; | ||
const type = 'openAiApi'; | ||
const data = { | ||
apiKey: 'apiKey', | ||
url: 'url', | ||
}; | ||
const projectId = nanoId.nanoid(); | ||
|
||
const credentialScopes: Scope[] = [ | ||
'credential:create', | ||
'credential:delete', | ||
'credential:list', | ||
'credential:move', | ||
'credential:read', | ||
'credential:share', | ||
'credential:update', | ||
]; | ||
|
||
export const createNewCredentialsPayload = ( | ||
payload?: Partial<CredentialRequest.CredentialProperties>, | ||
): CredentialRequest.CredentialProperties => { | ||
return { | ||
name, | ||
type, | ||
data, | ||
projectId, | ||
...payload, | ||
}; | ||
}; | ||
|
||
export const createdCredentialsWithScopes = ( | ||
payload?: Partial<NewCredentialWithSCopes>, | ||
): NewCredentialWithSCopes => { | ||
return { | ||
name, | ||
type, | ||
data: randomString(20), | ||
id: nanoId.nanoid(), | ||
createdAt: date(), | ||
updatedAt: date(), | ||
isManaged: false, | ||
scopes: credentialScopes, | ||
...payload, | ||
}; | ||
}; |