password.test.ts 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import { describe, it, expect } from 'vitest';
  2. import { checkPasswordComplexity } from '../../utils/password';
  3. describe('checkPasswordComplexity', () => {
  4. it('rejects passwords shorter than 8 characters', () => {
  5. expect(checkPasswordComplexity('Ab1!def')).toBe('tooShort');
  6. expect(checkPasswordComplexity('')).toBe('tooShort');
  7. });
  8. it('flags missing uppercase first (matches backend validator order)', () => {
  9. // Matches backend/app/schemas/auth.py:_validate_password_complexity which
  10. // returns the uppercase error before checking lowercase/digit/special.
  11. expect(checkPasswordComplexity('abcdefgh')).toBe('needsUppercase');
  12. expect(checkPasswordComplexity('abcdefg1')).toBe('needsUppercase');
  13. expect(checkPasswordComplexity('abcdefg!')).toBe('needsUppercase');
  14. });
  15. it('flags missing lowercase when uppercase is present', () => {
  16. expect(checkPasswordComplexity('ABCDEFGH')).toBe('needsLowercase');
  17. expect(checkPasswordComplexity('ABCDEFG1')).toBe('needsLowercase');
  18. });
  19. it('flags missing digit when letters are present', () => {
  20. expect(checkPasswordComplexity('Abcdefgh')).toBe('needsDigit');
  21. expect(checkPasswordComplexity('Abcdefg!')).toBe('needsDigit');
  22. });
  23. it('flags missing special character', () => {
  24. expect(checkPasswordComplexity('Abcdefg1')).toBe('needsSpecial');
  25. });
  26. it('returns null for a password that meets every rule', () => {
  27. expect(checkPasswordComplexity('Aa1!aaaa')).toBeNull();
  28. expect(checkPasswordComplexity('Aa1!Aa1!Aa1!')).toBeNull();
  29. });
  30. it('handles a password from the #1303 user (8 digits) — the original failure mode', () => {
  31. // The reporter typed an 8-character all-digits password and the backend
  32. // returned 422 "Password must contain at least one uppercase letter".
  33. // The FE check now produces the same verdict locally without a round-trip.
  34. expect(checkPasswordComplexity('12345678')).toBe('needsUppercase');
  35. });
  36. });