-
Notifications
You must be signed in to change notification settings - Fork 5.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(core): Prevent workflow history saving error from happening #7812
Merged
krynble
merged 4 commits into
master
from
pay-1024-prevent-issues-with-workflow-versioning-on-empty-payload
Dec 13, 2023
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
114 changes: 114 additions & 0 deletions
114
packages/cli/test/unit/services/workflowHistory.service.ee.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
import { User } from '@db/entities/User'; | ||
import { WorkflowHistoryRepository } from '@db/repositories/workflowHistory.repository'; | ||
import { SharedWorkflowRepository } from '@db/repositories/sharedWorkflow.repository'; | ||
import { WorkflowHistoryService } from '@/workflows/workflowHistory/workflowHistory.service.ee'; | ||
import { mockInstance } from '../../shared/mocking'; | ||
import { Logger } from '@/Logger'; | ||
import { getWorkflow } from '../WorkflowHelpers.test'; | ||
import { mockClear } from 'jest-mock-extended'; | ||
|
||
const workflowHistoryRepository = mockInstance(WorkflowHistoryRepository); | ||
const logger = mockInstance(Logger); | ||
const sharedWorkflowRepository = mockInstance(SharedWorkflowRepository); | ||
const workflowHistoryService = new WorkflowHistoryService( | ||
logger, | ||
workflowHistoryRepository, | ||
sharedWorkflowRepository, | ||
); | ||
const testUser = Object.assign(new User(), { | ||
id: '1234', | ||
password: 'passwordHash', | ||
mfaEnabled: false, | ||
firstName: 'John', | ||
lastName: 'Doe', | ||
}); | ||
let isWorkflowHistoryEnabled = true; | ||
|
||
jest.mock('@/workflows/workflowHistory/workflowHistoryHelper.ee', () => { | ||
return { | ||
isWorkflowHistoryEnabled: jest.fn(() => isWorkflowHistoryEnabled), | ||
}; | ||
}); | ||
|
||
describe('WorkflowHistoryService', () => { | ||
beforeEach(() => { | ||
mockClear(workflowHistoryRepository.insert); | ||
}); | ||
|
||
describe('saveVersion', () => { | ||
it('should save a new version when workflow history is enabled and nodes and connections are present', async () => { | ||
// Arrange | ||
isWorkflowHistoryEnabled = true; | ||
const workflow = getWorkflow({ addNodeWithoutCreds: true }); | ||
const workflowId = '123'; | ||
workflow.connections = {}; | ||
workflow.id = workflowId; | ||
workflow.versionId = '456'; | ||
|
||
// Act | ||
await workflowHistoryService.saveVersion(testUser, workflow, workflowId); | ||
|
||
// Assert | ||
expect(workflowHistoryRepository.insert).toHaveBeenCalledWith({ | ||
authors: 'John Doe', | ||
connections: {}, | ||
nodes: workflow.nodes, | ||
versionId: workflow.versionId, | ||
workflowId, | ||
}); | ||
}); | ||
|
||
it('should not save a new version when workflow history is disabled', async () => { | ||
// Arrange | ||
isWorkflowHistoryEnabled = false; | ||
const workflow = getWorkflow({ addNodeWithoutCreds: true }); | ||
const workflowId = '123'; | ||
workflow.connections = {}; | ||
workflow.id = workflowId; | ||
workflow.versionId = '456'; | ||
|
||
// Act | ||
await workflowHistoryService.saveVersion(testUser, workflow, workflowId); | ||
|
||
// Assert | ||
expect(workflowHistoryRepository.insert).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('should not save a new version when nodes or connections are missing', async () => { | ||
// Arrange | ||
isWorkflowHistoryEnabled = true; | ||
const workflow = getWorkflow({ addNodeWithoutCreds: true }); | ||
const workflowId = '123'; | ||
workflow.id = workflowId; | ||
workflow.versionId = '456'; | ||
// Nodes are set but connections is empty | ||
|
||
// Act | ||
await workflowHistoryService.saveVersion(testUser, workflow, workflowId); | ||
|
||
// Assert | ||
expect(workflowHistoryRepository.insert).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('should log an error when failed to save workflow history version', async () => { | ||
// Arrange | ||
isWorkflowHistoryEnabled = true; | ||
const workflow = getWorkflow({ addNodeWithoutCreds: true }); | ||
const workflowId = '123'; | ||
workflow.connections = {}; | ||
workflow.id = workflowId; | ||
workflow.versionId = '456'; | ||
workflowHistoryRepository.insert.mockRejectedValueOnce(new Error('Test error')); | ||
|
||
// Act | ||
await workflowHistoryService.saveVersion(testUser, workflow, workflowId); | ||
|
||
// Assert | ||
expect(workflowHistoryRepository.insert).toHaveBeenCalled(); | ||
expect(logger.error).toHaveBeenCalledWith( | ||
'Failed to save workflow history version for workflow 123', | ||
expect.any(Error), | ||
); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { User } from '@db/entities/User'; | ||
import { Role } from '@db/entities/Role'; | ||
import { CredentialsEntity } from '@db/entities/CredentialsEntity'; | ||
|
||
import { | ||
randomCredentialPayload, | ||
randomEmail, | ||
randomInteger, | ||
randomName, | ||
} from '../../integration/shared/random'; | ||
|
||
export const wfOwnerRole = () => | ||
Object.assign(new Role(), { | ||
scope: 'workflow', | ||
name: 'owner', | ||
id: randomInteger(), | ||
}); | ||
|
||
export const mockCredRole = (name: 'owner' | 'editor'): Role => | ||
Object.assign(new Role(), { | ||
scope: 'credentials', | ||
name, | ||
id: randomInteger(), | ||
}); | ||
|
||
export const mockCredential = (): CredentialsEntity => | ||
Object.assign(new CredentialsEntity(), randomCredentialPayload()); | ||
|
||
export const mockUser = (): User => | ||
Object.assign(new User(), { | ||
id: randomInteger(), | ||
email: randomEmail(), | ||
firstName: randomName(), | ||
lastName: randomName(), | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for stuff like this we should really add different set of methods on the repository, and add strong typing on the payload that gets passed in. That way not only we wouldn't need vague checks with comments like these, but also the method names are going to be self explanatory. a good example is
markAsCrashed
inExecutionRepository