Use env variables instead of checking if isdev

This commit is contained in:
Adria Navarro 2024-01-03 10:12:22 +01:00
parent 82abdd5f09
commit ba2b54f842
4 changed files with 11 additions and 17 deletions

View File

@ -1,20 +1,15 @@
import { env } from ".." import { env } from ".."
export const PASSWORD_MIN_LENGTH = 8 export const PASSWORD_MIN_LENGTH = +(process.env.PASSWORD_MIN_LENGTH || 8)
export const PASSWORD_MAX_LENGTH = 512 export const PASSWORD_MAX_LENGTH = +(process.env.PASSWORD_MAX_LENGTH || 512)
export function validatePassword( export function validatePassword(
password: string password: string
): { valid: true } | { valid: false; error: string } { ): { valid: true } | { valid: false; error: string } {
if (env.isDev() && !env.isTest() && password) {
// We accept any password while on development
return { valid: true }
}
if (!password || password.length < PASSWORD_MIN_LENGTH) { if (!password || password.length < PASSWORD_MIN_LENGTH) {
return { return {
valid: false, valid: false,
error: "Password invalid. Minimum eight characters.", error: `Password invalid. Minimum ${PASSWORD_MIN_LENGTH} characters.`,
} }
} }

View File

@ -14,21 +14,18 @@ describe("auth", () => {
])("%s returns unsuccessful", (_, password) => { ])("%s returns unsuccessful", (_, password) => {
expect(validatePassword(password as string)).toEqual({ expect(validatePassword(password as string)).toEqual({
valid: false, valid: false,
error: "Password invalid. Minimum eight characters.", error: "Password invalid. Minimum 8 characters.",
}) })
}) })
it.each([ it.each([
generator.word({ length: PASSWORD_MAX_LENGTH }), generator.word({ length: PASSWORD_MAX_LENGTH }),
generator.paragraph().substring(0, PASSWORD_MAX_LENGTH), generator.paragraph().substring(0, PASSWORD_MAX_LENGTH),
])( ])(`can use passwords up to 512 characters in length`, password => {
`can use passwords up to ${PASSWORD_MAX_LENGTH} characters in length`, expect(validatePassword(password)).toEqual({
password => { valid: true,
expect(validatePassword(password)).toEqual({ })
valid: true, })
})
}
)
it.each([ it.each([
generator.word({ length: PASSWORD_MAX_LENGTH + 1 }), generator.word({ length: PASSWORD_MAX_LENGTH + 1 }),

View File

@ -48,6 +48,7 @@ async function init() {
HTTP_MIGRATIONS: "0", HTTP_MIGRATIONS: "0",
HTTP_LOGGING: "0", HTTP_LOGGING: "0",
VERSION: "0.0.0+local", VERSION: "0.0.0+local",
PASSWORD_MIN_LENGTH: "1",
} }
config = { ...config, ...existingConfig } config = { ...config, ...existingConfig }

View File

@ -30,6 +30,7 @@ async function init() {
ENABLE_EMAIL_TEST_MODE: "1", ENABLE_EMAIL_TEST_MODE: "1",
HTTP_LOGGING: "0", HTTP_LOGGING: "0",
VERSION: "0.0.0+local", VERSION: "0.0.0+local",
PASSWORD_MIN_LENGTH: "1",
} }
config = { ...config, ...existingConfig } config = { ...config, ...existingConfig }