refactor: Rename fixtures as factories to differentiate dynamic data
This commit is contained in:
23
packages/backend/test/factories/config.ts
Normal file
23
packages/backend/test/factories/config.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
import { IJSONObject } from '@automatisch/types';
|
||||
import { faker } from '@faker-js/faker';
|
||||
|
||||
type ConfigParams = {
|
||||
key?: string;
|
||||
value?: IJSONObject;
|
||||
};
|
||||
|
||||
const createConfig = async (params: ConfigParams = {}) => {
|
||||
const configData = {
|
||||
key: params?.key || faker.lorem.word(),
|
||||
value: params?.value || { data: 'sampleConfig' },
|
||||
};
|
||||
|
||||
const [config] = await global.knex
|
||||
.table('config')
|
||||
.insert(configData)
|
||||
.returning('*');
|
||||
|
||||
return config;
|
||||
};
|
||||
|
||||
export default createConfig;
|
26
packages/backend/test/factories/connection.ts
Normal file
26
packages/backend/test/factories/connection.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
import Connection from '../../src/models/connection';
|
||||
import appConfig from '../../src/config/app';
|
||||
import { AES } from 'crypto-js';
|
||||
|
||||
const createConnection = async (params: Partial<Connection> = {}) => {
|
||||
params.key = params?.key || 'deepl';
|
||||
|
||||
const formattedData = params.formattedData || {
|
||||
screenName: 'Test - DeepL Connection',
|
||||
authenticationKey: 'test key',
|
||||
};
|
||||
|
||||
params.data = AES.encrypt(
|
||||
JSON.stringify(formattedData),
|
||||
appConfig.encryptionKey
|
||||
).toString();
|
||||
|
||||
const [connection] = await global.knex
|
||||
.table('connections')
|
||||
.insert(params)
|
||||
.returning('*');
|
||||
|
||||
return connection;
|
||||
};
|
||||
|
||||
export default createConnection;
|
27
packages/backend/test/factories/permission.ts
Normal file
27
packages/backend/test/factories/permission.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
import { IPermission } from '@automatisch/types';
|
||||
import createRole from './role';
|
||||
|
||||
type PermissionParams = {
|
||||
roleId?: string;
|
||||
action?: string;
|
||||
subject?: string;
|
||||
};
|
||||
|
||||
const createPermission = async (
|
||||
params: PermissionParams = {}
|
||||
): Promise<IPermission> => {
|
||||
const permissionData = {
|
||||
roleId: params?.roleId || (await createRole()).id,
|
||||
action: params?.action || 'read',
|
||||
subject: params?.subject || 'User',
|
||||
};
|
||||
|
||||
const [permission] = await global.knex
|
||||
.table('permissions')
|
||||
.insert(permissionData)
|
||||
.returning('*');
|
||||
|
||||
return permission;
|
||||
};
|
||||
|
||||
export default createPermission;
|
17
packages/backend/test/factories/role.ts
Normal file
17
packages/backend/test/factories/role.ts
Normal file
@@ -0,0 +1,17 @@
|
||||
import { IRole } from '@automatisch/types';
|
||||
|
||||
type RoleParams = {
|
||||
name?: string;
|
||||
key?: string;
|
||||
};
|
||||
|
||||
const createRole = async (params: RoleParams = {}): Promise<IRole> => {
|
||||
params.name = params?.name || 'Viewer';
|
||||
params.key = params?.key || 'viewer';
|
||||
|
||||
const [role] = await knex.table('roles').insert(params).returning('*');
|
||||
|
||||
return role;
|
||||
};
|
||||
|
||||
export default createRole;
|
16
packages/backend/test/factories/user.ts
Normal file
16
packages/backend/test/factories/user.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
import createRole from './role';
|
||||
import { faker } from '@faker-js/faker';
|
||||
import User from '../../src/models/user';
|
||||
|
||||
const createUser = async (params: Partial<User> = {}) => {
|
||||
params.roleId = params?.roleId || (await createRole()).id;
|
||||
params.fullName = params?.fullName || faker.person.fullName();
|
||||
params.email = params?.email || faker.internet.email();
|
||||
params.password = params?.password || faker.internet.password();
|
||||
|
||||
const [user] = await global.knex.table('users').insert(params).returning('*');
|
||||
|
||||
return user;
|
||||
};
|
||||
|
||||
export default createUser;
|
Reference in New Issue
Block a user