Skip to content

Commit ee5960a

Browse files
author
Dan Hertz
committed
Add test with all the bells and whistles
1 parent 66a8914 commit ee5960a

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

nightfall/detection_rules.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class WordList:
3030
is_case_sensitive: bool
3131

3232
def as_dict(self):
33-
return {"wordList": self.word_list, "isCaseSensitive": self.is_case_sensitive}
33+
return {"values": self.word_list, "isCaseSensitive": self.is_case_sensitive}
3434

3535

3636
class Confidence(Enum):
@@ -60,7 +60,7 @@ class ContextRule:
6060

6161
def as_dict(self):
6262
return {
63-
"regex": self.regex,
63+
"regex": self.regex.as_dict(),
6464
"proximity": {"windowBefore": self.window_before, "windowAfter": self.window_after},
6565
"confidenceAdjustment": {"fixedConfidence": self.fixed_confidence.value}
6666
}
@@ -92,8 +92,10 @@ def as_dict(self):
9292
result = {"matchType": self.match_type.value}
9393
if self.regex:
9494
result["regex"] = self.regex.as_dict()
95+
result["exclusionType"] = "REGEX"
9596
if self.word_list:
9697
result["wordList"] = self.word_list.as_dict()
98+
result["exclusionType"] = "WORD_LIST"
9799
return result
98100

99101

tests/test_api.py

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
import pytest
33

44
from nightfall.api import Nightfall
5-
from nightfall.detection_rules import DetectionRule, Detector, LogicalOp, Confidence, ExclusionRule
5+
from nightfall.detection_rules import DetectionRule, Detector, LogicalOp, Confidence, ExclusionRule, ContextRule, \
6+
WordList, MatchType, RedactionConfig, MaskConfig, Regex
67
from nightfall.findings import Finding, Range
78

89

@@ -12,22 +13,38 @@ def nightfall():
1213

1314

1415
def test_scan_text_detection_rules_v3(nightfall):
15-
result, _ = nightfall.scan_text(
16+
result, redactions = nightfall.scan_text(
1617
["4916-6734-7572-5015 is my credit card number"],
1718
detection_rules=[DetectionRule(logical_op=LogicalOp.ANY, detectors=[
18-
Detector(min_confidence=Confidence.LIKELY, min_num_findings=1,
19-
display_name="Credit Card Number", nightfall_detector="CREDIT_CARD_NUMBER")])]
19+
Detector(min_confidence=Confidence.LIKELY,
20+
min_num_findings=1,
21+
display_name="Credit Card Number",
22+
nightfall_detector="CREDIT_CARD_NUMBER",
23+
context_rules=[ContextRule(regex=Regex("fake regex", is_case_sensitive=False),
24+
window_before=10, window_after=10,
25+
fixed_confidence=Confidence.VERY_UNLIKELY)],
26+
exclusion_rules=[ExclusionRule(MatchType.FULL,
27+
word_list=WordList(["never", "match"],
28+
is_case_sensitive=True))],
29+
redaction_config=RedactionConfig(remove_finding=False,
30+
mask_config=MaskConfig(masking_char='👀',
31+
num_chars_to_leave_unmasked=3,
32+
chars_to_ignore=["-"]))
33+
)])]
2034
)
2135

2236
assert len(result) == 1
2337
assert result[0][0] == Finding(
2438
"4916-6734-7572-5015",
25-
None, None, None,
39+
'491👀-👀👀👀👀-👀👀👀👀-👀👀👀👀',
40+
None, None,
2641
"Credit Card Number",
2742
result[0][0].detector_uuid,
2843
Confidence.VERY_LIKELY,
2944
Range(0, 19), Range(0, 19),
3045
[], ["Inline Detection Rule #1"])
46+
assert len(redactions) == 1
47+
assert redactions[0] == "491👀-👀👀👀👀-👀👀👀👀-👀👀👀👀 is my credit card number"
3148

3249

3350
@pytest.mark.filetest

0 commit comments

Comments
 (0)