2023-11-03 09:20:54 -07:00
|
|
|
import { Container } from 'typedi';
|
2024-05-17 01:53:15 -07:00
|
|
|
import { In } from '@n8n/typeorm';
|
2023-11-03 09:20:54 -07:00
|
|
|
import { mock } from 'jest-mock-extended';
|
2024-04-05 04:47:49 -07:00
|
|
|
import { hasScope } from '@n8n/permissions';
|
|
|
|
|
2023-11-03 09:20:54 -07:00
|
|
|
import type { User } from '@db/entities/User';
|
|
|
|
import type { CredentialsEntity } from '@db/entities/CredentialsEntity';
|
|
|
|
import { SharedCredentials } from '@db/entities/SharedCredentials';
|
|
|
|
import { SharedCredentialsRepository } from '@db/repositories/sharedCredentials.repository';
|
2024-05-17 01:53:15 -07:00
|
|
|
import { GLOBAL_MEMBER_SCOPES, GLOBAL_OWNER_SCOPES } from '@/permissions/global-roles';
|
2024-04-05 04:47:49 -07:00
|
|
|
import { mockEntityManager } from '../../shared/mocking';
|
2023-11-03 09:20:54 -07:00
|
|
|
|
|
|
|
describe('SharedCredentialsRepository', () => {
|
2024-04-05 04:47:49 -07:00
|
|
|
const entityManager = mockEntityManager(SharedCredentials);
|
2023-11-03 09:20:54 -07:00
|
|
|
const repository = Container.get(SharedCredentialsRepository);
|
|
|
|
|
|
|
|
describe('findCredentialForUser', () => {
|
|
|
|
const credentialsId = 'cred_123';
|
|
|
|
const sharedCredential = mock<SharedCredentials>();
|
|
|
|
sharedCredential.credentials = mock<CredentialsEntity>({ id: credentialsId });
|
2023-11-29 06:48:36 -08:00
|
|
|
const owner = mock<User>({
|
|
|
|
isOwner: true,
|
2023-12-19 04:52:42 -08:00
|
|
|
hasGlobalScope: (scope) =>
|
|
|
|
hasScope(scope, {
|
2024-05-17 01:53:15 -07:00
|
|
|
global: GLOBAL_OWNER_SCOPES,
|
2023-12-19 04:52:42 -08:00
|
|
|
}),
|
2023-11-29 06:48:36 -08:00
|
|
|
});
|
|
|
|
const member = mock<User>({
|
|
|
|
isOwner: false,
|
|
|
|
id: 'test',
|
2023-12-19 04:52:42 -08:00
|
|
|
hasGlobalScope: (scope) =>
|
|
|
|
hasScope(scope, {
|
2024-05-17 01:53:15 -07:00
|
|
|
global: GLOBAL_MEMBER_SCOPES,
|
2023-12-19 04:52:42 -08:00
|
|
|
}),
|
2023-11-29 06:48:36 -08:00
|
|
|
});
|
2023-11-03 09:20:54 -07:00
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
jest.resetAllMocks();
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should allow instance owner access to all credentials', async () => {
|
|
|
|
entityManager.findOne.mockResolvedValueOnce(sharedCredential);
|
2024-05-17 01:53:15 -07:00
|
|
|
const credential = await repository.findCredentialForUser(credentialsId, owner, [
|
|
|
|
'credential:read',
|
|
|
|
]);
|
2023-11-03 09:20:54 -07:00
|
|
|
expect(entityManager.findOne).toHaveBeenCalledWith(SharedCredentials, {
|
2024-05-17 01:53:15 -07:00
|
|
|
relations: { credentials: { shared: { project: { projectRelations: { user: true } } } } },
|
2023-11-03 09:20:54 -07:00
|
|
|
where: { credentialsId },
|
|
|
|
});
|
|
|
|
expect(credential).toEqual(sharedCredential.credentials);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should allow members', async () => {
|
|
|
|
entityManager.findOne.mockResolvedValueOnce(sharedCredential);
|
2024-05-17 01:53:15 -07:00
|
|
|
const credential = await repository.findCredentialForUser(credentialsId, member, [
|
|
|
|
'credential:read',
|
|
|
|
]);
|
2023-11-03 09:20:54 -07:00
|
|
|
expect(entityManager.findOne).toHaveBeenCalledWith(SharedCredentials, {
|
2024-05-17 01:53:15 -07:00
|
|
|
relations: { credentials: { shared: { project: { projectRelations: { user: true } } } } },
|
|
|
|
where: {
|
|
|
|
credentialsId,
|
|
|
|
role: In(['credential:owner', 'credential:user']),
|
|
|
|
project: {
|
|
|
|
projectRelations: {
|
2024-06-06 02:55:48 -07:00
|
|
|
role: In([
|
|
|
|
'project:admin',
|
|
|
|
'project:personalOwner',
|
|
|
|
'project:editor',
|
|
|
|
'project:viewer',
|
|
|
|
]),
|
2024-05-17 01:53:15 -07:00
|
|
|
userId: member.id,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2023-11-03 09:20:54 -07:00
|
|
|
});
|
|
|
|
expect(credential).toEqual(sharedCredential.credentials);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should return null when no shared credential is found', async () => {
|
|
|
|
entityManager.findOne.mockResolvedValueOnce(null);
|
2024-05-17 01:53:15 -07:00
|
|
|
const credential = await repository.findCredentialForUser(credentialsId, member, [
|
|
|
|
'credential:read',
|
|
|
|
]);
|
2023-11-03 09:20:54 -07:00
|
|
|
expect(entityManager.findOne).toHaveBeenCalledWith(SharedCredentials, {
|
2024-05-17 01:53:15 -07:00
|
|
|
relations: { credentials: { shared: { project: { projectRelations: { user: true } } } } },
|
|
|
|
where: {
|
|
|
|
credentialsId,
|
|
|
|
role: In(['credential:owner', 'credential:user']),
|
|
|
|
project: {
|
|
|
|
projectRelations: {
|
2024-06-06 02:55:48 -07:00
|
|
|
role: In([
|
|
|
|
'project:admin',
|
|
|
|
'project:personalOwner',
|
|
|
|
'project:editor',
|
|
|
|
'project:viewer',
|
|
|
|
]),
|
2024-05-17 01:53:15 -07:00
|
|
|
userId: member.id,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2023-11-03 09:20:54 -07:00
|
|
|
});
|
|
|
|
expect(credential).toEqual(null);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|