mirror of
https://github.com/Abdulazizzn/n8n-enterprise-unlocked.git
synced 2025-12-20 19:32:15 +00:00
refactor(core): Consolidate CredentialsService.getMany() (no-changelog) (#7028)
Consolidate `CredentialsService.getMany()` in preparation for adding list query middleware to `GET /credentials`.
This commit is contained in:
@@ -5,7 +5,7 @@ import type { IUser } from 'n8n-workflow';
|
||||
|
||||
import * as Db from '@/Db';
|
||||
import { RESPONSE_ERROR_MESSAGES } from '@/constants';
|
||||
import type { CredentialWithSharings } from '@/credentials/credentials.types';
|
||||
import type { Credentials } from '@/requests';
|
||||
import * as UserManagementHelpers from '@/UserManagement/UserManagementHelper';
|
||||
import type { Role } from '@db/entities/Role';
|
||||
import type { User } from '@db/entities/User';
|
||||
@@ -92,10 +92,10 @@ describe('GET /credentials', () => {
|
||||
expect(response.statusCode).toBe(200);
|
||||
expect(response.body.data).toHaveLength(2); // owner retrieved owner cred and member cred
|
||||
const ownerCredential = response.body.data.find(
|
||||
(e: CredentialWithSharings) => e.ownedBy?.id === owner.id,
|
||||
(e: Credentials.WithOwnedByAndSharedWith) => e.ownedBy?.id === owner.id,
|
||||
);
|
||||
const memberCredential = response.body.data.find(
|
||||
(e: CredentialWithSharings) => e.ownedBy?.id === member1.id,
|
||||
(e: Credentials.WithOwnedByAndSharedWith) => e.ownedBy?.id === member1.id,
|
||||
);
|
||||
|
||||
validateMainCredentialData(ownerCredential);
|
||||
@@ -497,7 +497,7 @@ describe('PUT /credentials/:id/share', () => {
|
||||
});
|
||||
});
|
||||
|
||||
function validateMainCredentialData(credential: CredentialWithSharings) {
|
||||
function validateMainCredentialData(credential: Credentials.WithOwnedByAndSharedWith) {
|
||||
expect(typeof credential.name).toBe('string');
|
||||
expect(typeof credential.type).toBe('string');
|
||||
expect(typeof credential.nodesAccess[0].nodeType).toBe('string');
|
||||
|
||||
@@ -5,7 +5,7 @@ import * as Db from '@/Db';
|
||||
import config from '@/config';
|
||||
import { RESPONSE_ERROR_MESSAGES } from '@/constants';
|
||||
import * as UserManagementHelpers from '@/UserManagement/UserManagementHelper';
|
||||
import type { CredentialsEntity } from '@db/entities/CredentialsEntity';
|
||||
import type { Credentials } from '@/requests';
|
||||
import type { Role } from '@db/entities/Role';
|
||||
import type { User } from '@db/entities/User';
|
||||
import { randomCredentialPayload, randomName, randomString } from './shared/random';
|
||||
@@ -59,9 +59,9 @@ describe('GET /credentials', () => {
|
||||
expect(response.body.data.length).toBe(2); // owner retrieved owner cred and member cred
|
||||
|
||||
const savedCredentialsIds = [savedOwnerCredentialId, savedMemberCredentialId];
|
||||
response.body.data.forEach((credential: CredentialsEntity) => {
|
||||
response.body.data.forEach((credential: Credentials.WithOwnedByAndSharedWith) => {
|
||||
validateMainCredentialData(credential);
|
||||
expect(credential.data).toBeUndefined();
|
||||
expect('data' in credential).toBe(false);
|
||||
expect(savedCredentialsIds).toContain(credential.id);
|
||||
});
|
||||
});
|
||||
@@ -532,14 +532,25 @@ describe('GET /credentials/:id', () => {
|
||||
});
|
||||
});
|
||||
|
||||
function validateMainCredentialData(credential: CredentialsEntity) {
|
||||
expect(typeof credential.name).toBe('string');
|
||||
expect(typeof credential.type).toBe('string');
|
||||
expect(typeof credential.nodesAccess[0].nodeType).toBe('string');
|
||||
// @ts-ignore
|
||||
expect(credential.ownedBy).toBeUndefined();
|
||||
// @ts-ignore
|
||||
expect(credential.sharedWith).toBeUndefined();
|
||||
function validateMainCredentialData(credential: Credentials.WithOwnedByAndSharedWith) {
|
||||
const { name, type, nodesAccess, sharedWith, ownedBy } = credential;
|
||||
|
||||
expect(typeof name).toBe('string');
|
||||
expect(typeof type).toBe('string');
|
||||
expect(typeof nodesAccess?.[0].nodeType).toBe('string');
|
||||
|
||||
if (sharedWith) {
|
||||
expect(Array.isArray(sharedWith)).toBe(true);
|
||||
}
|
||||
|
||||
if (ownedBy) {
|
||||
const { id, email, firstName, lastName } = ownedBy;
|
||||
|
||||
expect(typeof id).toBe('string');
|
||||
expect(typeof email).toBe('string');
|
||||
expect(typeof firstName).toBe('string');
|
||||
expect(typeof lastName).toBe('string');
|
||||
}
|
||||
}
|
||||
|
||||
const INVALID_PAYLOADS = [
|
||||
|
||||
@@ -2,12 +2,19 @@ import { OwnershipService } from '@/services/ownership.service';
|
||||
import { SharedWorkflowRepository } from '@/databases/repositories';
|
||||
import { mockInstance } from '../../integration/shared/utils';
|
||||
import { Role } from '@/databases/entities/Role';
|
||||
import { randomInteger } from '../../integration/shared/random';
|
||||
import {
|
||||
randomCredentialPayload,
|
||||
randomEmail,
|
||||
randomInteger,
|
||||
randomName,
|
||||
} from '../../integration/shared/random';
|
||||
import { SharedWorkflow } from '@/databases/entities/SharedWorkflow';
|
||||
import { CacheService } from '@/services/cache.service';
|
||||
import { User } from '@/databases/entities/User';
|
||||
import { RoleService } from '@/services/role.service';
|
||||
import { UserService } from '@/services/user.service';
|
||||
import { CredentialsEntity } from '@/databases/entities/CredentialsEntity';
|
||||
import type { SharedCredentials } from '@/databases/entities/SharedCredentials';
|
||||
|
||||
const wfOwnerRole = () =>
|
||||
Object.assign(new Role(), {
|
||||
@@ -16,6 +23,24 @@ const wfOwnerRole = () =>
|
||||
id: randomInteger(),
|
||||
});
|
||||
|
||||
const mockCredRole = (name: 'owner' | 'editor'): Role =>
|
||||
Object.assign(new Role(), {
|
||||
scope: 'credentials',
|
||||
name,
|
||||
id: randomInteger(),
|
||||
});
|
||||
|
||||
const mockCredential = (): CredentialsEntity =>
|
||||
Object.assign(new CredentialsEntity(), randomCredentialPayload());
|
||||
|
||||
const mockUser = (): User =>
|
||||
Object.assign(new User(), {
|
||||
id: randomInteger(),
|
||||
email: randomEmail(),
|
||||
firstName: randomName(),
|
||||
lastName: randomName(),
|
||||
});
|
||||
|
||||
describe('OwnershipService', () => {
|
||||
const cacheService = mockInstance(CacheService);
|
||||
const roleService = mockInstance(RoleService);
|
||||
@@ -67,4 +92,55 @@ describe('OwnershipService', () => {
|
||||
await expect(ownershipService.getWorkflowOwnerCached('some-workflow-id')).rejects.toThrow();
|
||||
});
|
||||
});
|
||||
|
||||
describe('addOwnedByAndSharedWith()', () => {
|
||||
test('should add ownedBy and sharedWith to credential', async () => {
|
||||
const owner = mockUser();
|
||||
const editor = mockUser();
|
||||
|
||||
const credential = mockCredential();
|
||||
|
||||
credential.shared = [
|
||||
{ role: mockCredRole('owner'), user: owner },
|
||||
{ role: mockCredRole('editor'), user: editor },
|
||||
] as SharedCredentials[];
|
||||
|
||||
const { ownedBy, sharedWith } = ownershipService.addOwnedByAndSharedWith(credential);
|
||||
|
||||
expect(ownedBy).toStrictEqual({
|
||||
id: owner.id,
|
||||
email: owner.email,
|
||||
firstName: owner.firstName,
|
||||
lastName: owner.lastName,
|
||||
});
|
||||
|
||||
expect(sharedWith).toStrictEqual([
|
||||
{
|
||||
id: editor.id,
|
||||
email: editor.email,
|
||||
firstName: editor.firstName,
|
||||
lastName: editor.lastName,
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
test('should produce an empty sharedWith if no sharee', async () => {
|
||||
const owner = mockUser();
|
||||
|
||||
const credential = mockCredential();
|
||||
|
||||
credential.shared = [{ role: mockCredRole('owner'), user: owner }] as SharedCredentials[];
|
||||
|
||||
const { ownedBy, sharedWith } = ownershipService.addOwnedByAndSharedWith(credential);
|
||||
|
||||
expect(ownedBy).toStrictEqual({
|
||||
id: owner.id,
|
||||
email: owner.email,
|
||||
firstName: owner.firstName,
|
||||
lastName: owner.lastName,
|
||||
});
|
||||
|
||||
expect(sharedWith).toHaveLength(0);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user