Coverage for tests / unit / parsers / gitleaks_parser / test_field_parsing.py: 100%

34 statements  

« prev     ^ index     » next       coverage.py v7.13.0, created at 2026-04-03 18:53 +0000

1"""Unit tests for gitleaks parser field-specific parsing.""" 

2 

3from __future__ import annotations 

4 

5import json 

6 

7from assertpy import assert_that 

8 

9from lintro.parsers.gitleaks.gitleaks_parser import parse_gitleaks_output 

10 

11 

12def test_parse_gitleaks_git_history_fields() -> None: 

13 """Parser should handle git history fields from commit scanning.""" 

14 sample_output = json.dumps( 

15 [ 

16 { 

17 "Description": "API Key", 

18 "StartLine": 1, 

19 "EndLine": 1, 

20 "StartColumn": 1, 

21 "EndColumn": 30, 

22 "File": "secret.py", 

23 "Commit": "abc123def456", 

24 "Author": "John Doe", 

25 "Email": "john@example.com", 

26 "Date": "2024-01-15T10:30:00Z", 

27 "Message": "Add configuration", 

28 "RuleID": "generic-api-key", 

29 "Fingerprint": "secret.py:generic-api-key:1:abc123def456", 

30 }, 

31 ], 

32 ) 

33 

34 issues = parse_gitleaks_output(output=sample_output) 

35 

36 assert_that(len(issues)).is_equal_to(1) 

37 issue = issues[0] 

38 assert_that(issue.commit).is_equal_to("abc123def456") 

39 assert_that(issue.author).is_equal_to("John Doe") 

40 assert_that(issue.email).is_equal_to("john@example.com") 

41 assert_that(issue.date).is_equal_to("2024-01-15T10:30:00Z") 

42 assert_that(issue.commit_message).is_equal_to("Add configuration") 

43 

44 

45def test_gitleaks_entropy_parsing() -> None: 

46 """Parser should correctly handle entropy as float.""" 

47 sample_output = json.dumps( 

48 [ 

49 { 

50 "File": "test.py", 

51 "StartLine": 1, 

52 "Entropy": 4.25, 

53 "RuleID": "test", 

54 }, 

55 ], 

56 ) 

57 

58 issues = parse_gitleaks_output(output=sample_output) 

59 

60 assert_that(len(issues)).is_equal_to(1) 

61 assert_that(issues[0].entropy).is_equal_to(4.25) 

62 

63 

64def test_gitleaks_entropy_as_int() -> None: 

65 """Parser should handle entropy as integer.""" 

66 sample_output = json.dumps( 

67 [ 

68 { 

69 "File": "test.py", 

70 "StartLine": 1, 

71 "Entropy": 4, 

72 "RuleID": "test", 

73 }, 

74 ], 

75 ) 

76 

77 issues = parse_gitleaks_output(output=sample_output) 

78 

79 assert_that(len(issues)).is_equal_to(1) 

80 assert_that(issues[0].entropy).is_equal_to(4.0) 

81 

82 

83def test_gitleaks_tags_empty_list() -> None: 

84 """Parser should handle empty tags list.""" 

85 sample_output = json.dumps( 

86 [ 

87 { 

88 "File": "test.py", 

89 "StartLine": 1, 

90 "Tags": [], 

91 "RuleID": "test", 

92 }, 

93 ], 

94 ) 

95 

96 issues = parse_gitleaks_output(output=sample_output) 

97 

98 assert_that(len(issues)).is_equal_to(1) 

99 assert_that(issues[0].tags).is_equal_to([]) 

100 

101 

102def test_gitleaks_tags_none() -> None: 

103 """Parser should handle missing tags field.""" 

104 sample_output = json.dumps( 

105 [ 

106 { 

107 "File": "test.py", 

108 "StartLine": 1, 

109 "RuleID": "test", 

110 }, 

111 ], 

112 ) 

113 

114 issues = parse_gitleaks_output(output=sample_output) 

115 

116 assert_that(len(issues)).is_equal_to(1) 

117 assert_that(issues[0].tags).is_equal_to([])