2023-11-03 09:20:54 -07:00
|
|
|
import { Container } from 'typedi';
|
|
|
|
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';
|
2023-11-29 06:48:36 -08:00
|
|
|
import { memberPermissions, ownerPermissions } from '@/permissions/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, {
|
2023-11-29 06:48:36 -08:00
|
|
|
global: ownerPermissions,
|
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, {
|
2023-11-29 06:48:36 -08:00
|
|
|
global: memberPermissions,
|
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);
|
|
|
|
const credential = await repository.findCredentialForUser(credentialsId, owner);
|
|
|
|
expect(entityManager.findOne).toHaveBeenCalledWith(SharedCredentials, {
|
|
|
|
relations: ['credentials'],
|
|
|
|
where: { credentialsId },
|
|
|
|
});
|
|
|
|
expect(credential).toEqual(sharedCredential.credentials);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should allow members', async () => {
|
|
|
|
entityManager.findOne.mockResolvedValueOnce(sharedCredential);
|
|
|
|
const credential = await repository.findCredentialForUser(credentialsId, member);
|
|
|
|
expect(entityManager.findOne).toHaveBeenCalledWith(SharedCredentials, {
|
|
|
|
relations: ['credentials'],
|
|
|
|
where: { credentialsId, userId: member.id },
|
|
|
|
});
|
|
|
|
expect(credential).toEqual(sharedCredential.credentials);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should return null when no shared credential is found', async () => {
|
|
|
|
entityManager.findOne.mockResolvedValueOnce(null);
|
|
|
|
const credential = await repository.findCredentialForUser(credentialsId, member);
|
|
|
|
expect(entityManager.findOne).toHaveBeenCalledWith(SharedCredentials, {
|
|
|
|
relations: ['credentials'],
|
|
|
|
where: { credentialsId, userId: member.id },
|
|
|
|
});
|
|
|
|
expect(credential).toEqual(null);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|