feat: Check each zod schema requirement (User & Password updater)

This commit is contained in:
April Hall 2025-02-27 10:57:20 -05:00
parent 34e2ad0748
commit e7af60555e
4 changed files with 106 additions and 34 deletions

View File

@ -6,10 +6,10 @@ export const changePasswordSchema = z
newPassword: z newPassword: z
.string() .string()
.min(8, 'New password must be at least 8 characters.') .min(8, 'New password must be at least 8 characters.')
.regex(/(?=.*[A-Z])/gm, 'New password must contain at uppercase letter.') .regex(/(?=.*[A-Z])/gm, 'New password must contain an uppercase letter.')
.regex(/(?=.*[a-z])/gm, 'New password must contain at lowercase letter.') .regex(/(?=.*[a-z])/gm, 'New password must contain a lowercase letter.')
.regex(/(?=.*\d)/gm, 'New password must contain at least one number.') .regex(/(?=.*\d)/gm, 'New password must contain at least one number.')
.regex(/(?=.*\W)/gm, 'New password must contain at least one special character'), .regex(/(?=.*\W)/gm, 'New password must contain at least one special character.'),
}) })
.refine((schema) => schema.newPassword !== 'Password123!', { .refine((schema) => schema.newPassword !== 'Password123!', {
message: "You can't use the example password, silly", message: "You can't use the example password, silly",
@ -25,8 +25,8 @@ export const changeUsernameSchema = z.object({
.string() .string()
.min(3, 'Username must be at least 3 characters.') .min(3, 'Username must be at least 3 characters.')
.max(15, 'Username must be no more than 15 characters.') .max(15, 'Username must be no more than 15 characters.')
.regex(/^(?![A-Z])/gm, 'Username cannot contain uppercase letters') .regex(/^(?![A-Z])/gm, 'Username cannot contain uppercase letters.')
.regex(/^(?=[a-z0-9-_]+$)/gm, 'Username cannot contain special characters'), .regex(/^(?=[a-z0-9-_]+$)/gm, 'Username cannot contain special characters.'),
}); });
export type ChangePasswordSchema = typeof changePasswordSchema; export type ChangePasswordSchema = typeof changePasswordSchema;

View File

@ -7,15 +7,15 @@ export const signupSchema = z
.string() .string()
.min(3, 'Username must be at least 3 characters.') .min(3, 'Username must be at least 3 characters.')
.max(15, 'Username must be no more than 15 characters.') .max(15, 'Username must be no more than 15 characters.')
.regex(/^(?![A-Z])/gm, 'Username cannot contain uppercase letters') .regex(/^(?![A-Z])/gm, 'Username cannot contain uppercase letters.')
.regex(/^(?=[a-z0-9-_]+$)/gm, 'Username cannot contain special characters'), .regex(/^(?=[a-z0-9-_]+$)/gm, 'Username cannot contain special characters.'),
password: z password: z
.string() .string()
.min(8, 'Password must be at least 8 characters.') .min(8, 'Password must be at least 8 characters.')
.regex(/(?=.*[A-Z])/gm, 'Password must contain at uppercase letter.') .regex(/(?=.*[A-Z])/gm, 'Password must contain an uppercase letter.')
.regex(/(?=.*[a-z])/gm, 'Password must contain at lowercase letter.') .regex(/(?=.*[a-z])/gm, 'Password must contain a lowercase letter.')
.regex(/(?=.*\d)/gm, 'Password must contain at least one number.') .regex(/(?=.*\d)/gm, 'Password must contain at least one number.')
.regex(/(?=.*\W)/gm, 'Password must contain at least one special character'), .regex(/(?=.*\W)/gm, 'Password must contain at least one special character.'),
verify: z.string().nonempty('Passwords do not match.'), verify: z.string().nonempty('Passwords do not match.'),
}) })
.refine((schema) => schema.password !== 'Password123!', { .refine((schema) => schema.password !== 'Password123!', {

View File

@ -46,16 +46,6 @@ test.describe('Password Update Form', () => {
await expectError('Invalid password', page); await expectError('Invalid password', page);
}); });
// Test validation error for weak new password
test('should show validation error for weak new password', async () => {
await currentPasswordInput.fill(currentPassword);
await newPasswordInput.fill('weak');
await submitButton.click();
// Check for error message
await expectError('New password must be at least 8 characters.', page);
});
// Test empty fields validation // Test empty fields validation
test('should show validation errors when fields are empty', async () => { test('should show validation errors when fields are empty', async () => {
// Leave fields empty and try to submit // Leave fields empty and try to submit
@ -65,6 +55,56 @@ test.describe('Password Update Form', () => {
await expectError('Password must not be empty.', page); await expectError('Password must not be empty.', page);
}); });
// Test validation error for missing uppercase letter
test('should show validation error for password without uppercase letter', async () => {
await currentPasswordInput.fill(currentPassword);
await newPasswordInput.fill('password123!');
await submitButton.click();
// Check for error message
await expectError('New password must contain an uppercase letter.', page);
});
// Test validation error for missing lowercase letter
test('should show validation error for password without lowercase letter', async () => {
await currentPasswordInput.fill(currentPassword);
await newPasswordInput.fill('PASSWORD123!');
await submitButton.click();
// Check for error message
await expectError('New password must contain a lowercase letter.', page);
});
// Test validation error for missing number
test('should show validation error for password without number', async () => {
await currentPasswordInput.fill(currentPassword);
await newPasswordInput.fill('Password!!!');
await submitButton.click();
// Check for error message
await expectError('New password must contain at least one number.', page);
});
// Test validation error for missing special character
test('should show validation error for password without special character', async () => {
await currentPasswordInput.fill(currentPassword);
await newPasswordInput.fill('Password123');
await submitButton.click();
// Check for error message
await expectError('New password must contain at least one special character.', page);
});
// Test validation error for using example password
test('should show validation error for using example password', async () => {
await currentPasswordInput.fill(currentPassword);
await newPasswordInput.fill('Password123!');
await submitButton.click();
// Check for error message
await expectError("You can't use the example password, silly", page);
});
// Test update functionality // Test update functionality
test('should successfully update user password', async () => { test('should successfully update user password', async () => {
await currentPasswordInput.fill(currentPassword); await currentPasswordInput.fill(currentPassword);

View File

@ -32,7 +32,7 @@ test.describe('Username Update Form', () => {
await submitButton.click(); await submitButton.click();
// Check for success message // Check for success message
const successMessageLocator = page.locator('p.text-sm.text-green-500:has-text("Username updated.")'); const successMessageLocator = page.getByText('Username updated.');
await expect(successMessageLocator).toBeVisible(); await expect(successMessageLocator).toBeVisible();
// Verify the username displayed in the UI has been updated // Verify the username displayed in the UI has been updated
@ -40,19 +40,6 @@ test.describe('Username Update Form', () => {
expect(updatedUsername).toBe(newUsername); expect(updatedUsername).toBe(newUsername);
}); });
// Test invalidator
test('should show validation error for invalid username', async () => {
await usernameInput.fill('a');
await submitButton.click();
// Check for error message
await expectError('Username must be at least 3 characters.', page);
// Ensure the username wasn't updated
const currentUsername: string = (await currentUsernameElement.textContent()) || '';
expect(currentUsername).not.toBe('a');
});
// Test that new and old username can't be the same // Test that new and old username can't be the same
test('should not allow same username', async () => { test('should not allow same username', async () => {
const currentUsername = await currentUsernameElement.textContent(); const currentUsername = await currentUsernameElement.textContent();
@ -72,4 +59,49 @@ test.describe('Username Update Form', () => {
await expectError('Username taken.', page); await expectError('Username taken.', page);
}); });
// Test validation error for username less than 3 characters
test('should show validation error for username less than 3 characters', async () => {
await usernameInput.fill('ab');
await submitButton.click();
// Check for error message
await expectError('Username must be at least 3 characters.', page);
});
// Test validation error for username more than 15 characters
test('should show validation error for username more than 15 characters', async () => {
await usernameInput.fill('abcdefghijklmnopq');
await submitButton.click();
// Check for error message
await expectError('Username must be no more than 15 characters.', page);
});
// Test validation error for username with uppercase letters
test('should show validation error for username with uppercase letters', async () => {
await usernameInput.fill('Username');
await submitButton.click();
// Check for error message
await expectError('Username cannot contain uppercase letters.', page);
});
// Test validation error for username with special characters
test('should show validation error for username with special characters', async () => {
await usernameInput.fill('user@name');
await submitButton.click();
// Check for error message
await expectError('Username cannot contain special characters.', page);
});
// Test validation error for empty username
test('should show validation error for empty username', async () => {
await usernameInput.fill('');
await submitButton.click();
// Check for error message - assuming there's a custom message or using the min length one
await expectError('Username must be at least 3 characters.', page);
});
}); });