fix(editor): Optionally share credentials used by the workflow when moving the workflow between projects (#12524)

Co-authored-by: Danny Martini <danny@n8n.io>
This commit is contained in:
Csaba Tuncsik
2025-02-21 11:05:37 +01:00
committed by GitHub
parent 29ae2396c9
commit 7bd83d7d33
22 changed files with 1078 additions and 136 deletions

View File

@@ -951,6 +951,57 @@ describe('PUT /credentials/:id/share', () => {
expect(mailer.notifyCredentialsShared).toHaveBeenCalledTimes(1);
});
test('should ignore sharing with owner project', async () => {
// ARRANGE
const project = await projectService.createTeamProject(owner, { name: 'Team Project' });
const credential = await saveCredential(randomCredentialPayload(), { project });
// ACT
const response = await authOwnerAgent
.put(`/credentials/${credential.id}/share`)
.send({ shareWithIds: [project.id] });
const sharedCredentials = await Container.get(SharedCredentialsRepository).find({
where: { credentialsId: credential.id },
});
// ASSERT
expect(response.statusCode).toBe(200);
expect(sharedCredentials).toHaveLength(1);
expect(sharedCredentials[0].projectId).toBe(project.id);
expect(sharedCredentials[0].role).toBe('credential:owner');
});
test('should ignore sharing with project that already has it shared', async () => {
// ARRANGE
const project = await projectService.createTeamProject(owner, { name: 'Team Project' });
const credential = await saveCredential(randomCredentialPayload(), { project });
const project2 = await projectService.createTeamProject(owner, { name: 'Team Project 2' });
await shareCredentialWithProjects(credential, [project2]);
// ACT
const response = await authOwnerAgent
.put(`/credentials/${credential.id}/share`)
.send({ shareWithIds: [project2.id] });
const sharedCredentials = await Container.get(SharedCredentialsRepository).find({
where: { credentialsId: credential.id },
});
// ASSERT
expect(response.statusCode).toBe(200);
expect(sharedCredentials).toHaveLength(2);
expect(sharedCredentials).toEqual(
expect.arrayContaining([
expect.objectContaining({ projectId: project.id, role: 'credential:owner' }),
expect.objectContaining({ projectId: project2.id, role: 'credential:user' }),
]),
);
});
test('should respond 400 if invalid payload is provided', async () => {
const savedCredential = await saveCredential(randomCredentialPayload(), { user: owner });