fix(editor): Remove unknown credentials on pasting workflow (#7582)

https://linear.app/n8n/issue/PAY-881
This commit is contained in:
Iván Ovejero 2023-11-02 17:26:39 +01:00 committed by GitHub
parent 81f43805aa
commit d633753687
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 90 additions and 5 deletions

View file

@ -34,10 +34,10 @@ describe('Canvas Node Manipulation and Navigation', () => {
WorkflowPage.actions.addNodeToCanvas(SWITCH_NODE_NAME, true, true); WorkflowPage.actions.addNodeToCanvas(SWITCH_NODE_NAME, true, true);
for (let i = 0; i < desiredOutputs; i++) { for (let i = 0; i < desiredOutputs; i++) {
cy.contains('Add Routing Rule').click() cy.contains('Add Routing Rule').click();
} }
NDVDialog.actions.close() NDVDialog.actions.close();
for (let i = 0; i < desiredOutputs; i++) { for (let i = 0; i < desiredOutputs; i++) {
WorkflowPage.getters.canvasNodePlusEndpointByName(SWITCH_NODE_NAME, i).click({ force: true }); WorkflowPage.getters.canvasNodePlusEndpointByName(SWITCH_NODE_NAME, i).click({ force: true });
WorkflowPage.getters.nodeCreatorSearchBar().should('be.visible'); WorkflowPage.getters.nodeCreatorSearchBar().should('be.visible');
@ -51,7 +51,9 @@ describe('Canvas Node Manipulation and Navigation', () => {
cy.reload(); cy.reload();
cy.waitForLoad(); cy.waitForLoad();
// Make sure outputless switch was connected correctly // Make sure outputless switch was connected correctly
cy.get(`[data-target-node="${SWITCH_NODE_NAME}1"][data-source-node="${EDIT_FIELDS_SET_NODE_NAME}3"]`).should('be.visible'); cy.get(
`[data-target-node="${SWITCH_NODE_NAME}1"][data-source-node="${EDIT_FIELDS_SET_NODE_NAME}3"]`,
).should('be.visible');
// Make sure all connections are there after reload // Make sure all connections are there after reload
for (let i = 0; i < desiredOutputs; i++) { for (let i = 0; i < desiredOutputs; i++) {
const setName = `${EDIT_FIELDS_SET_NODE_NAME}${i > 0 ? i : ''}`; const setName = `${EDIT_FIELDS_SET_NODE_NAME}${i > 0 ? i : ''}`;
@ -179,7 +181,7 @@ describe('Canvas Node Manipulation and Navigation', () => {
.canvasNodes() .canvasNodes()
.last() .last()
.should('have.css', 'left', '740px') .should('have.css', 'left', '740px')
.should('have.css', 'top', '320px') .should('have.css', 'top', '320px');
}); });
it('should zoom in', () => { it('should zoom in', () => {
@ -350,5 +352,17 @@ describe('Canvas Node Manipulation and Navigation', () => {
cy.waitForLoad(); cy.waitForLoad();
WorkflowPage.getters.canvasNodes().should('have.length', 2); WorkflowPage.getters.canvasNodes().should('have.length', 2);
cy.get('.rect-input-endpoint.jtk-endpoint-connected').should('have.length', 1); cy.get('.rect-input-endpoint.jtk-endpoint-connected').should('have.length', 1);
}) });
it('should remove unknown credentials on pasting workflow', () => {
cy.fixture('workflow-with-unknown-credentials.json').then((data) => {
cy.get('body').paste(JSON.stringify(data));
WorkflowPage.getters.canvasNodes().should('have.have.length', 2);
WorkflowPage.actions.openNode('n8n');
cy.get('[class*=hasIssues]').should('have.length', 1);
NDVDialog.actions.close();
});
});
}); });

View file

@ -0,0 +1,52 @@
{
"meta": {
"instanceId": "123"
},
"nodes": [
{
"parameters": {
"resource": "credential",
"name": "123",
"credentialTypeName": "123"
},
"id": "a01f79f6-e8c3-44c5-be5e-4bc482e23172",
"name": "n8n",
"type": "n8n-nodes-base.n8n",
"typeVersion": 1,
"position": [
540,
240
],
"credentials": {
"n8nApi": {
"id": "10",
"name": "n8n account"
}
}
},
{
"parameters": {},
"id": "acdd1bdc-c642-4ea6-ad67-f4201b640cfa",
"name": "When clicking \"Execute Workflow\"",
"type": "n8n-nodes-base.manualTrigger",
"typeVersion": 1,
"position": [
300,
240
]
}
],
"connections": {
"When clicking \"Execute Workflow\"": {
"main": [
[
{
"node": "n8n",
"type": "main",
"index": 0
}
]
]
}
}
}

View file

@ -1677,6 +1677,8 @@ export default defineComponent({
}); });
} }
this.removeUnknownCredentials(workflowData);
const currInstanceId = this.rootStore.instanceId; const currInstanceId = this.rootStore.instanceId;
const nodeGraph = JSON.stringify( const nodeGraph = JSON.stringify(
@ -1756,6 +1758,23 @@ export default defineComponent({
this.showError(error, this.$locale.baseText('nodeView.showError.importWorkflowData.title')); this.showError(error, this.$locale.baseText('nodeView.showError.importWorkflowData.title'));
} }
}, },
removeUnknownCredentials(workflow: IWorkflowToShare) {
if (!workflow?.nodes) return;
for (const node of workflow.nodes) {
if (!node.credentials) continue;
for (const [name, credential] of Object.entries(node.credentials)) {
if (credential.id === null) continue;
if (!this.credentialsStore.getCredentialById(credential.id)) {
delete node.credentials[name];
}
}
}
},
onDragOver(event: DragEvent) { onDragOver(event: DragEvent) {
event.preventDefault(); event.preventDefault();
}, },