Coverage for gws-app/gws/lib/password/_test.py: 0%
57 statements
« prev ^ index » next coverage.py v7.8.0, created at 2025-04-17 01:37 +0200
« prev ^ index » next coverage.py v7.8.0, created at 2025-04-17 01:37 +0200
1"""Tests for the password module."""
3import gws
4import gws.test.util as u
5import gws.lib.password as password
6import unittest.mock
8# Assuming Salt is random
9salt = 'hashtest'
11# known_hv from https://www.dcode.fr/pbkdf2-hash with sha512 alg, foo pw, hashtest salt, base 64 output
12# known URL safe hashvalue to foo
13hvKnown = 'MFyuRkTcnEZd9qB35nslGy_W3a7REHFzpYwYcCTtw1TxRZvqeHwNd0g1DA1DghfkN-OEAcGn32zparlum12UbA=='
16def test_compare_true():
17 assert password.compare('foo', 'foo')
20def test_compare_false():
21 assert not password.compare('foo', 'bar')
24def test_encode():
25 with unittest.mock.patch('gws.lib.password._random_string', return_value=salt):
26 enc = '$'.join(['', 'sha512', salt, hvKnown])
27 assert password.encode('foo', 'sha512') == enc
30def test_check_true():
31 # create encode
32 enc = '$'.join(['', 'sha512', salt, hvKnown])
33 assert password.check('foo', enc)
36def test_check_false():
37 assert not password.check('foo', '$sha512$hashtest$ThisIsJustSomethingDifferent')
40import string
43def _cc(s, ls):
44 return sum(c in ls for c in s)
47def test_generate_min():
48 for _ in range(50):
49 g = password.generate(min_len=20, max_len=40, min_lower=5, min_upper=6, min_digit=7, min_punct=8)
50 assert 20 <= len(g) <= 40
51 assert _cc(g, string.ascii_lowercase) >= 5
52 assert _cc(g, string.ascii_uppercase) >= 6
53 assert _cc(g, string.digits) >= 7
54 assert _cc(g, string.punctuation) >= 8
57def test_generate_max():
58 for _ in range(50):
59 g = password.generate(min_len=20, max_len=40, max_lower=5, max_upper=6, max_digit=7)
60 assert 20 <= len(g) <= 40
61 assert _cc(g, string.ascii_lowercase) <= 5
62 assert _cc(g, string.ascii_uppercase) <= 6
63 assert _cc(g, string.digits) <= 7
66def test_generate_zero():
67 for _ in range(50):
68 g = password.generate(min_len=20, max_len=40, max_lower=0)
69 assert _cc(g, string.ascii_lowercase) == 0
72def test_generate_exact():
73 for _ in range(50):
74 g = password.generate(min_len=12, max_len=12, min_lower=3, min_upper=3, min_digit=3, min_punct=3)
75 assert len(g) == 12
76 assert _cc(g, string.ascii_lowercase) == 3
77 assert _cc(g, string.ascii_uppercase) == 3
78 assert _cc(g, string.digits) == 3
79 assert _cc(g, string.punctuation) == 3
82def test_generate_mins_greater_than_len():
83 for _ in range(50):
84 with u.raises(ValueError):
85 password.generate(min_len=10, max_len=10, min_lower=3, min_upper=3, min_digit=3, min_punct=3)
88def test_generate_maxes_less_than_len():
89 for _ in range(50):
90 with u.raises(ValueError):
91 password.generate(min_len=20, max_len=20, max_lower=3, max_upper=3, max_digit=3, max_punct=3)