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

1"""Tests for the password module.""" 

2 

3import gws 

4import gws.test.util as u 

5import gws.lib.password as password 

6import unittest.mock 

7 

8# Assuming Salt is random 

9salt = 'hashtest' 

10 

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==' 

14 

15 

16def test_compare_true(): 

17 assert password.compare('foo', 'foo') 

18 

19 

20def test_compare_false(): 

21 assert not password.compare('foo', 'bar') 

22 

23 

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 

28 

29 

30def test_check_true(): 

31 # create encode 

32 enc = '$'.join(['', 'sha512', salt, hvKnown]) 

33 assert password.check('foo', enc) 

34 

35 

36def test_check_false(): 

37 assert not password.check('foo', '$sha512$hashtest$ThisIsJustSomethingDifferent') 

38 

39 

40import string 

41 

42 

43def _cc(s, ls): 

44 return sum(c in ls for c in s) 

45 

46 

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 

55 

56 

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 

64 

65 

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 

70 

71 

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 

80 

81 

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) 

86 

87 

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)