password_strength_helpers.ts 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. /**
  2. * Copyright © 2016-2020 Threema GmbH (https://threema.ch/).
  3. *
  4. * This file is part of Threema Web.
  5. *
  6. * Threema Web is free software: you can redistribute it and/or modify it
  7. * under the terms of the GNU Affero General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or (at
  9. * your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Affero General Public License
  17. * along with Threema Web. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. import {scorePassword, Strength} from '../../src/helpers/password_strength';
  20. describe('Password Strength Helpers', () => {
  21. describe('scorePassword', () => {
  22. it('returns 0 for empty passwords', function() {
  23. expect(scorePassword('')).toEqual({score: 0, strength: Strength.BAD});
  24. });
  25. it('increases points depending on character count', function() {
  26. expect(scorePassword('a')).toEqual({score: 5, strength: Strength.BAD});
  27. expect(scorePassword('abcd')).toEqual({score: 20, strength: Strength.BAD});
  28. expect(scorePassword('aaaa')).toEqual({score: 10, strength: Strength.BAD});
  29. });
  30. it('awards points for multiple character classes', function() {
  31. expect(scorePassword('abc')).toEqual({score: 15, strength: Strength.BAD});
  32. expect(scorePassword('aBc')).toEqual({score: 25, strength: Strength.BAD});
  33. expect(scorePassword('aB3')).toEqual({score: 35, strength: Strength.BAD});
  34. });
  35. it('assigns strength based on score', function() {
  36. expect(scorePassword('aB3')).toEqual({score: 35, strength: Strength.BAD});
  37. expect(scorePassword('aB3cde')).toEqual({score: 50, strength: Strength.WEAK});
  38. expect(scorePassword('aB3cdefgh')).toEqual({score: 65, strength: Strength.GOOD});
  39. expect(scorePassword('aB3cdefghijkl')).toEqual({score: 85, strength: Strength.STRONG});
  40. });
  41. });
  42. });