From 7ed6ac7c85c6966ad8c24b3d113c7db088e9858b Mon Sep 17 00:00:00 2001 From: OlegIvaniv Date: Thu, 30 Mar 2023 14:34:04 +0200 Subject: [PATCH] fix(editor): Prevent error from showing-up when duplicating unsaved workflow (#5833) * fix(editor): Prevent error from showing-up when duplicating unsaved workflow * Add unsaved workflow duplicate test --- cypress/e2e/7-workflow-actions.cy.ts | 71 +++++++++++-------- .../components/DuplicateWorkflowDialog.vue | 16 +++-- 2 files changed, 53 insertions(+), 34 deletions(-) diff --git a/cypress/e2e/7-workflow-actions.cy.ts b/cypress/e2e/7-workflow-actions.cy.ts index cfce3cc9b0c35..baa38e9f7a205 100644 --- a/cypress/e2e/7-workflow-actions.cy.ts +++ b/cypress/e2e/7-workflow-actions.cy.ts @@ -190,37 +190,48 @@ describe('Workflow Actions', () => { cy.url().should('include', '/workflow/new'); }); - it('should duplicate workflow', () => { - // Stub window.open so new tab is not getting opened - cy.window().then((win) => { - cy.stub(win, 'open').as('open'); + describe('duplicate workflow', () => { + function duplicateWorkflow() { + WorkflowPage.getters.workflowMenu().should('be.visible'); + WorkflowPage.getters.workflowMenu().click(); + WorkflowPage.getters.workflowMenuItemDuplicate().click(); + WorkflowPage.getters.duplicateWorkflowModal().should('be.visible'); + WorkflowPage.getters.duplicateWorkflowModal().find('input').first().should('be.visible'); + WorkflowPage.getters.duplicateWorkflowModal().find('input').first().type('{selectall}'); + WorkflowPage.getters + .duplicateWorkflowModal() + .find('input') + .first() + .type(DUPLICATE_WORKFLOW_NAME); + WorkflowPage.getters + .duplicateWorkflowModal() + .find('.el-select__tags input') + .type(DUPLICATE_WORKFLOW_TAG); + WorkflowPage.getters.duplicateWorkflowModal().find('.el-select__tags input').type('{enter}'); + WorkflowPage.getters.duplicateWorkflowModal().find('.el-select__tags input').type('{enter}'); + WorkflowPage.getters + .duplicateWorkflowModal() + .find('button') + .contains('Duplicate') + .should('be.visible'); + WorkflowPage.getters.duplicateWorkflowModal().find('button').contains('Duplicate').click(); + WorkflowPage.getters.errorToast().should('not.exist'); + } + + beforeEach(() => { + // Stub window.open so new tab is not getting opened + cy.window().then((win) => { + cy.stub(win, 'open').as('open'); + }); + WorkflowPage.actions.addNodeToCanvas(MANUAL_TRIGGER_NODE_NAME); }); - WorkflowPage.actions.addNodeToCanvas(MANUAL_TRIGGER_NODE_NAME); - WorkflowPage.actions.saveWorkflowOnButtonClick(); - WorkflowPage.getters.workflowMenu().should('be.visible'); - WorkflowPage.getters.workflowMenu().click(); - WorkflowPage.getters.workflowMenuItemDuplicate().click(); - WorkflowPage.getters.duplicateWorkflowModal().should('be.visible'); - WorkflowPage.getters.duplicateWorkflowModal().find('input').first().should('be.visible'); - WorkflowPage.getters.duplicateWorkflowModal().find('input').first().type('{selectall}'); - WorkflowPage.getters - .duplicateWorkflowModal() - .find('input') - .first() - .type(DUPLICATE_WORKFLOW_NAME); - WorkflowPage.getters - .duplicateWorkflowModal() - .find('.el-select__tags input') - .type(DUPLICATE_WORKFLOW_TAG); - WorkflowPage.getters.duplicateWorkflowModal().find('.el-select__tags input').type('{enter}'); - WorkflowPage.getters.duplicateWorkflowModal().find('.el-select__tags input').type('{enter}'); - WorkflowPage.getters - .duplicateWorkflowModal() - .find('button') - .contains('Duplicate') - .should('be.visible'); - WorkflowPage.getters.duplicateWorkflowModal().find('button').contains('Duplicate').click(); - WorkflowPage.getters.errorToast().should('not.exist'); + it('should duplicate unsaved workflow', () => { + duplicateWorkflow(); + }); + it('should duplicate saved workflow', () => { + WorkflowPage.actions.saveWorkflowOnButtonClick(); + duplicateWorkflow(); + }); }); }); diff --git a/packages/editor-ui/src/components/DuplicateWorkflowDialog.vue b/packages/editor-ui/src/components/DuplicateWorkflowDialog.vue index c76a2f57fa3dd..ecd62791ee2ab 100644 --- a/packages/editor-ui/src/components/DuplicateWorkflowDialog.vue +++ b/packages/editor-ui/src/components/DuplicateWorkflowDialog.vue @@ -89,10 +89,18 @@ export default mixins(showMessage, workflowHelpers, restApi).extend({ computed: { ...mapStores(useUsersStore, useSettingsStore, useWorkflowsStore), workflowPermissions(): IPermissions { - return getWorkflowPermissions( - this.usersStore.currentUser, - this.workflowsStore.getWorkflowById(this.data.id), - ); + const isEmptyWorkflow = this.data.id === PLACEHOLDER_EMPTY_WORKFLOW_ID; + const isCurrentWorkflowEmpty = + this.workflowsStore.workflow.id === PLACEHOLDER_EMPTY_WORKFLOW_ID; + + // If the workflow to be duplicated is empty and the current workflow is also empty + // we need to use the current workflow to get the permissions + const currentWorkflow = + isEmptyWorkflow && isCurrentWorkflowEmpty + ? this.workflowsStore.workflow + : this.workflowsStore.getWorkflowById(this.data.id); + + return getWorkflowPermissions(this.usersStore.currentUser, currentWorkflow); }, }, watch: {