mirror of
https://github.com/n8n-io/n8n.git
synced 2024-12-24 04:04:06 -08:00
ci(core): Avoid slow bcrypt calls in tests (no-changelog) (#8570)
This commit is contained in:
parent
b79d6749d5
commit
40eee3aa49
|
@ -1,18 +1,17 @@
|
||||||
import { BadRequestError } from '@/errors/response-errors/bad-request.error';
|
import { BadRequestError } from '@/errors/response-errors/bad-request.error';
|
||||||
import { Service as Utility } from 'typedi';
|
import { Service as Utility } from 'typedi';
|
||||||
import { compare, genSaltSync, hash } from 'bcryptjs';
|
import { compare, hash } from 'bcryptjs';
|
||||||
import {
|
import {
|
||||||
MAX_PASSWORD_CHAR_LENGTH as maxLength,
|
MAX_PASSWORD_CHAR_LENGTH as maxLength,
|
||||||
MIN_PASSWORD_CHAR_LENGTH as minLength,
|
MIN_PASSWORD_CHAR_LENGTH as minLength,
|
||||||
} from '@/constants';
|
} from '@/constants';
|
||||||
|
|
||||||
|
const SALT_ROUNDS = 10;
|
||||||
|
|
||||||
@Utility()
|
@Utility()
|
||||||
export class PasswordUtility {
|
export class PasswordUtility {
|
||||||
async hash(plaintext: string) {
|
async hash(plaintext: string) {
|
||||||
const SALT_ROUNDS = 10;
|
return await hash(plaintext, SALT_ROUNDS);
|
||||||
const salt = genSaltSync(SALT_ROUNDS);
|
|
||||||
|
|
||||||
return await hash(plaintext, salt);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async compare(plaintext: string, hashed: string) {
|
async compare(plaintext: string, hashed: string) {
|
||||||
|
|
|
@ -47,7 +47,7 @@ const testServer = utils.setupTestServer({
|
||||||
});
|
});
|
||||||
|
|
||||||
beforeAll(async () => {
|
beforeAll(async () => {
|
||||||
owner = await createUser({ role: 'global:owner', password: 'password' });
|
owner = await createUser({ role: 'global:owner' });
|
||||||
authOwnerAgent = testServer.authAgentFor(owner);
|
authOwnerAgent = testServer.authAgentFor(owner);
|
||||||
|
|
||||||
defaultLdapConfig.bindingAdminPassword = Container.get(Cipher).encrypt(
|
defaultLdapConfig.bindingAdminPassword = Container.get(Cipher).encrypt(
|
||||||
|
|
|
@ -9,6 +9,9 @@ import { MfaService } from '@/Mfa/mfa.service';
|
||||||
|
|
||||||
import { randomApiKey, randomEmail, randomName, randomValidPassword } from '../random';
|
import { randomApiKey, randomEmail, randomName, randomValidPassword } from '../random';
|
||||||
|
|
||||||
|
// pre-computed bcrypt hash for the string 'password', using `await hash('password', 10)`
|
||||||
|
const passwordHash = '$2a$10$njedH7S6V5898mj6p0Jr..IGY9Ms.qNwR7RbSzzX9yubJocKfvGGK';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Store a user in the DB, defaulting to a `member`.
|
* Store a user in the DB, defaulting to a `member`.
|
||||||
*/
|
*/
|
||||||
|
@ -16,7 +19,7 @@ export async function createUser(attributes: Partial<User> = {}): Promise<User>
|
||||||
const { email, password, firstName, lastName, role, ...rest } = attributes;
|
const { email, password, firstName, lastName, role, ...rest } = attributes;
|
||||||
const user = Container.get(UserRepository).create({
|
const user = Container.get(UserRepository).create({
|
||||||
email: email ?? randomEmail(),
|
email: email ?? randomEmail(),
|
||||||
password: await hash(password ?? randomValidPassword(), 10),
|
password: password ? await hash(password, 1) : passwordHash,
|
||||||
firstName: firstName ?? randomName(),
|
firstName: firstName ?? randomName(),
|
||||||
lastName: lastName ?? randomName(),
|
lastName: lastName ?? randomName(),
|
||||||
role: role ?? 'global:member',
|
role: role ?? 'global:member',
|
||||||
|
@ -101,7 +104,7 @@ export async function createManyUsers(
|
||||||
[...Array(amount)].map(async () =>
|
[...Array(amount)].map(async () =>
|
||||||
Container.get(UserRepository).create({
|
Container.get(UserRepository).create({
|
||||||
email: email ?? randomEmail(),
|
email: email ?? randomEmail(),
|
||||||
password: await hash(password ?? randomValidPassword(), 10),
|
password: password ? await hash(password, 1) : passwordHash,
|
||||||
firstName: firstName ?? randomName(),
|
firstName: firstName ?? randomName(),
|
||||||
lastName: lastName ?? randomName(),
|
lastName: lastName ?? randomName(),
|
||||||
role: role ?? 'global:member',
|
role: role ?? 'global:member',
|
||||||
|
|
Loading…
Reference in a new issue