Merge pull request #2166 from automatisch/aut-1349-7
test(step): write test for updateFor method in model
This commit is contained in:
@@ -314,13 +314,13 @@ class Step extends Base {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async updateFor(user, newStepData) {
|
async updateFor(user, newStepData) {
|
||||||
const { connectionId, appKey, key, parameters } = newStepData;
|
const { appKey = this.appKey, connectionId, key, parameters } = newStepData;
|
||||||
|
|
||||||
if (connectionId && (appKey || this.appKey)) {
|
if (connectionId && appKey) {
|
||||||
await user.authorizedConnections
|
await user.authorizedConnections
|
||||||
.findOne({
|
.findOne({
|
||||||
id: connectionId,
|
id: connectionId,
|
||||||
key: appKey || this.appKey,
|
key: appKey,
|
||||||
})
|
})
|
||||||
.throwIfNotFound();
|
.throwIfNotFound();
|
||||||
}
|
}
|
||||||
@@ -334,8 +334,8 @@ class Step extends Base {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const updatedStep = await this.$query().patchAndFetch({
|
const updatedStep = await this.$query().patchAndFetch({
|
||||||
key: key,
|
key,
|
||||||
appKey: appKey,
|
appKey,
|
||||||
connectionId: connectionId,
|
connectionId: connectionId,
|
||||||
parameters: parameters,
|
parameters: parameters,
|
||||||
status: 'incomplete',
|
status: 'incomplete',
|
||||||
|
@@ -1,4 +1,4 @@
|
|||||||
import { describe, it, expect, vi } from 'vitest';
|
import { beforeEach, describe, it, expect, vi } from 'vitest';
|
||||||
import appConfig from '../config/app.js';
|
import appConfig from '../config/app.js';
|
||||||
import App from './app.js';
|
import App from './app.js';
|
||||||
import Base from './base.js';
|
import Base from './base.js';
|
||||||
@@ -9,6 +9,10 @@ import ExecutionStep from './execution-step.js';
|
|||||||
import Telemetry from '../helpers/telemetry/index.js';
|
import Telemetry from '../helpers/telemetry/index.js';
|
||||||
import * as testRunModule from '../services/test-run.js';
|
import * as testRunModule from '../services/test-run.js';
|
||||||
import { createFlow } from '../../test/factories/flow.js';
|
import { createFlow } from '../../test/factories/flow.js';
|
||||||
|
import { createUser } from '../../test/factories/user.js';
|
||||||
|
import { createRole } from '../../test/factories/role.js';
|
||||||
|
import { createPermission } from '../../test/factories/permission.js';
|
||||||
|
import { createConnection } from '../../test/factories/connection.js';
|
||||||
import { createStep } from '../../test/factories/step.js';
|
import { createStep } from '../../test/factories/step.js';
|
||||||
import { createExecutionStep } from '../../test/factories/execution-step.js';
|
import { createExecutionStep } from '../../test/factories/execution-step.js';
|
||||||
|
|
||||||
@@ -353,6 +357,107 @@ describe('Step model', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('updateFor', async () => {
|
||||||
|
let step,
|
||||||
|
userRole,
|
||||||
|
user,
|
||||||
|
userConnection,
|
||||||
|
anotherUser,
|
||||||
|
anotherUserConnection;
|
||||||
|
|
||||||
|
beforeEach(async () => {
|
||||||
|
userRole = await createRole({ name: 'User' });
|
||||||
|
anotherUser = await createUser({ roleId: userRole.id });
|
||||||
|
user = await createUser({ roleId: userRole.id });
|
||||||
|
|
||||||
|
userConnection = await createConnection({
|
||||||
|
key: 'deepl',
|
||||||
|
userId: user.id,
|
||||||
|
});
|
||||||
|
|
||||||
|
anotherUserConnection = await createConnection({
|
||||||
|
key: 'deepl',
|
||||||
|
userId: anotherUser.id,
|
||||||
|
});
|
||||||
|
|
||||||
|
await createPermission({
|
||||||
|
roleId: userRole.id,
|
||||||
|
action: 'read',
|
||||||
|
subject: 'Connection',
|
||||||
|
conditions: ['isCreator'],
|
||||||
|
});
|
||||||
|
|
||||||
|
step = await createStep();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should update step with the given payload and mark it as incomplete', async () => {
|
||||||
|
const stepData = {
|
||||||
|
appKey: 'deepl',
|
||||||
|
key: 'translateText',
|
||||||
|
connectionId: anotherUserConnection.id,
|
||||||
|
parameters: {
|
||||||
|
key: 'value',
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
const anotherUserWithRoleAndPermissions = await anotherUser
|
||||||
|
.$query()
|
||||||
|
.withGraphFetched({ permissions: true, role: true });
|
||||||
|
|
||||||
|
const updatedStep = await step.updateFor(
|
||||||
|
anotherUserWithRoleAndPermissions,
|
||||||
|
stepData
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(updatedStep).toMatchObject({
|
||||||
|
...stepData,
|
||||||
|
status: 'incomplete',
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should invoke updateWebhookUrl', async () => {
|
||||||
|
const updateWebhookUrlSpy = vi
|
||||||
|
.spyOn(Step.prototype, 'updateWebhookUrl')
|
||||||
|
.mockResolvedValue();
|
||||||
|
|
||||||
|
const stepData = {
|
||||||
|
appKey: 'deepl',
|
||||||
|
key: 'translateText',
|
||||||
|
};
|
||||||
|
|
||||||
|
await step.updateFor(user, stepData);
|
||||||
|
|
||||||
|
expect(updateWebhookUrlSpy).toHaveBeenCalledOnce();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should not update step when inaccessible connection is given', async () => {
|
||||||
|
const stepData = {
|
||||||
|
appKey: 'deepl',
|
||||||
|
key: 'translateText',
|
||||||
|
connectionId: userConnection.id,
|
||||||
|
};
|
||||||
|
|
||||||
|
const anotherUserWithRoleAndPermissions = await anotherUser
|
||||||
|
.$query()
|
||||||
|
.withGraphFetched({ permissions: true, role: true });
|
||||||
|
|
||||||
|
await expect(() =>
|
||||||
|
step.updateFor(anotherUserWithRoleAndPermissions, stepData)
|
||||||
|
).rejects.toThrowError('NotFoundError');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should not update step when given app key and key do not exist', async () => {
|
||||||
|
const stepData = {
|
||||||
|
appKey: 'deepl',
|
||||||
|
key: 'not-existing-key',
|
||||||
|
};
|
||||||
|
|
||||||
|
await expect(() => step.updateFor(user, stepData)).rejects.toThrowError(
|
||||||
|
'DeepL does not have an action with the "not-existing-key" key!'
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
describe('$afterInsert', () => {
|
describe('$afterInsert', () => {
|
||||||
it('should call super.$afterInsert', async () => {
|
it('should call super.$afterInsert', async () => {
|
||||||
const superAfterInsertSpy = vi.spyOn(Base.prototype, '$afterInsert');
|
const superAfterInsertSpy = vi.spyOn(Base.prototype, '$afterInsert');
|
||||||
|
@@ -1,5 +1,5 @@
|
|||||||
import { faker } from '@faker-js/faker';
|
import { faker } from '@faker-js/faker';
|
||||||
import Role from '../../src/models/role';
|
import Role from '../../src/models/role.js';
|
||||||
|
|
||||||
export const createRole = async (params = {}) => {
|
export const createRole = async (params = {}) => {
|
||||||
const name = faker.lorem.word();
|
const name = faker.lorem.word();
|
||||||
|
Reference in New Issue
Block a user