-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathredact_tests.py
More file actions
1639 lines (1308 loc) · 66.4 KB
/
Copy pathredact_tests.py
File metadata and controls
1639 lines (1308 loc) · 66.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
"""
redact_tests.py — Unit and integration tests.
Run all: python3 -m pytest redact_tests.py -v
Run fast: python3 -m pytest redact_tests.py -v -m "not slow"
"""
import json
import re
import sys
import tempfile
import unittest
from argparse import Namespace
from pathlib import Path
from unittest.mock import MagicMock, patch
sys.path.insert(0, str(Path(__file__).parent))
from redact import (
_apply_template,
_overlaps,
_passes_exclusions,
_process_docx_para,
PseudonymRegistry,
PatternDef,
build_patterns,
cmd_redact,
cmd_restore,
main,
process_pdf,
redact_text,
restore_text,
load_config,
DEFAULT_CONFIG,
)
# ── Test fixtures ──────────────────────────────────────────────────────────────
def _pat_email():
return PatternDef(
name='email',
template='person{n:03d}@anon.invalid',
regex=re.compile(r'\b[a-zA-Z0-9._%+\-]+@[a-zA-Z0-9.\-]+\.[a-zA-Z]{2,}\b'),
)
def _pat_name():
return PatternDef(
name='name',
template='Person{n:03d}',
regex=re.compile(r'\b[A-Z][a-z]{1,25}(?:\s+[A-Z][a-z]{1,25}){1,3}\b'),
exclusions=frozenset({'January', 'Monday', 'English', 'Figure', 'Table',
'Section', 'Page', 'True', 'False', 'Microsoft'}),
min_word_length=2,
)
def _pat_id():
return PatternDef(
name='id',
template='preserve_length',
regex=re.compile(r'\b\d{7,8}\b'),
)
def _pat_username():
return PatternDef(
name='username',
template='user{n:04d}xx',
regex=re.compile(r'\b(?=[a-zA-Z0-9]*[a-zA-Z])(?=[a-zA-Z0-9]*\d)[a-zA-Z0-9]{8}\b'),
)
def _all_patterns():
return [_pat_email(), _pat_name(), _pat_id(), _pat_username()]
def _redact(text, patterns=None):
"""Convenience: redact with a fresh registry, return (result, registry)."""
registry = PseudonymRegistry()
result = redact_text(text, registry, patterns or _all_patterns(), nlp=None)
return result, registry
# ── _apply_template ────────────────────────────────────────────────────────────
class TestApplyTemplate(unittest.TestCase):
def test_counter_zero_padded(self):
self.assertEqual(_apply_template('Person{n:03d}', 1, 'x'), 'Person001')
self.assertEqual(_apply_template('Person{n:03d}', 42, 'x'), 'Person042')
def test_counter_no_padding(self):
self.assertEqual(_apply_template('X{n}', 7, 'x'), 'X7')
def test_orig_substitution(self):
self.assertEqual(_apply_template('({orig})', 1, 'secret'), '(secret)')
def test_orig_len_substitution(self):
self.assertEqual(_apply_template('len={orig_len}', 1, 'hello'), 'len=5')
def test_email_template(self):
self.assertEqual(_apply_template('person{n:03d}@anon.invalid', 5, 'x'),
'person005@anon.invalid')
def test_preserve_length_7_digit(self):
result = _apply_template('preserve_length', 1, '1234567')
self.assertEqual(result, '0000001')
self.assertEqual(len(result), 7)
def test_preserve_length_8_digit(self):
result = _apply_template('preserve_length', 99, '12345678')
self.assertEqual(result, '00000099')
self.assertEqual(len(result), 8)
def test_preserve_length_large_counter(self):
result = _apply_template('preserve_length', 10_000_000, '1234567')
self.assertEqual(result, '10000000') # counter > orig length — no truncation
# ── _overlaps ──────────────────────────────────────────────────────────────────
class TestOverlaps(unittest.TestCase):
def setUp(self):
self.spans = [(5, 10, 'A'), (20, 25, 'B')]
def test_before_all_spans(self):
self.assertFalse(_overlaps(self.spans, 0, 4))
def test_between_spans(self):
self.assertFalse(_overlaps(self.spans, 11, 19))
def test_after_all_spans(self):
self.assertFalse(_overlaps(self.spans, 26, 30))
def test_adjacent_left_not_overlapping(self):
self.assertFalse(_overlaps(self.spans, 0, 5)) # ends where span starts
def test_adjacent_right_not_overlapping(self):
self.assertFalse(_overlaps(self.spans, 10, 15)) # starts where span ends
def test_overlaps_at_start(self):
self.assertTrue(_overlaps(self.spans, 3, 7))
def test_overlaps_at_end(self):
self.assertTrue(_overlaps(self.spans, 8, 15))
def test_contained_within_span(self):
self.assertTrue(_overlaps(self.spans, 6, 9))
def test_span_contained_within_candidate(self):
self.assertTrue(_overlaps(self.spans, 4, 12))
def test_empty_span_list(self):
self.assertFalse(_overlaps([], 0, 10))
# ── _passes_exclusions ─────────────────────────────────────────────────────────
class TestPassesExclusions(unittest.TestCase):
def setUp(self):
self.pat = PatternDef(
name='name', template='X',
exclusions=frozenset({'January', 'Monday', 'Table'}),
min_word_length=2,
)
def test_clean_name_passes(self):
self.assertTrue(_passes_exclusions('John Smith', self.pat))
def test_excluded_first_word(self):
self.assertFalse(_passes_exclusions('January Report', self.pat))
def test_excluded_second_word(self):
self.assertFalse(_passes_exclusions('Sales Table', self.pat))
def test_excluded_single_word(self):
self.assertFalse(_passes_exclusions('Table', self.pat))
def test_short_word_rejected_by_min_length(self):
self.assertFalse(_passes_exclusions('Jo A', self.pat)) # 'A' is 1 char
def test_min_length_exactly_met(self):
self.assertTrue(_passes_exclusions('Jo Sm', self.pat)) # both ≥ 2 chars
def test_no_exclusions_set(self):
pat = PatternDef(name='id', template='X', regex=None)
self.assertTrue(_passes_exclusions('anything here', pat))
def test_min_word_length_1_allows_single_char(self):
pat = PatternDef(name='x', template='X', min_word_length=1)
self.assertTrue(_passes_exclusions('A B', pat))
# ── PseudonymRegistry ──────────────────────────────────────────────────────────
class TestPseudonymRegistry(unittest.TestCase):
def setUp(self):
self.pat = _pat_email()
self.reg = PseudonymRegistry()
def test_same_original_same_pseudonym(self):
a = self.reg.get_or_create('user@x.com', self.pat)
b = self.reg.get_or_create('user@x.com', self.pat)
self.assertEqual(a, b)
def test_different_originals_different_pseudonyms(self):
a = self.reg.get_or_create('a@x.com', self.pat)
b = self.reg.get_or_create('b@x.com', self.pat)
self.assertNotEqual(a, b)
def test_counter_increments_per_category(self):
a = self.reg.get_or_create('a@x.com', self.pat)
b = self.reg.get_or_create('b@x.com', self.pat)
self.assertEqual(a, 'person001@anon.invalid')
self.assertEqual(b, 'person002@anon.invalid')
def test_whitespace_stripped_for_key(self):
a = self.reg.get_or_create('user@x.com', self.pat)
b = self.reg.get_or_create(' user@x.com ', self.pat)
self.assertEqual(a, b)
def test_separate_counters_per_category(self):
name_pat = _pat_name()
self.reg.get_or_create('a@x.com', self.pat)
self.reg.get_or_create('Alice Smith', name_pat)
self.assertEqual(self.reg.get_or_create('b@x.com', self.pat),
'person002@anon.invalid')
self.assertEqual(self.reg.get_or_create('Bob Jones', name_pat),
'Person002')
def test_mapping_file_dict_inverted(self):
self.reg.get_or_create('user@x.com', self.pat)
d = self.reg.mapping_file_dict()
self.assertEqual(d['person001@anon.invalid'], 'user@x.com')
def test_stats(self):
name_pat = _pat_name()
self.reg.get_or_create('a@x.com', self.pat)
self.reg.get_or_create('b@x.com', self.pat)
self.reg.get_or_create('Alice Smith', name_pat)
self.assertEqual(self.reg.stats(), {'email': 2, 'name': 1})
# ── redact_text ────────────────────────────────────────────────────────────────
class TestAnonymizeText(unittest.TestCase):
# ── email ──────────────────────────────────────────────────────────────────
def test_email_replaced(self):
result, _ = _redact('Contact user@example.com today')
self.assertNotIn('user@example.com', result)
self.assertIn('@anon.invalid', result)
def test_email_with_plus_addressing(self):
result, _ = _redact('Send to user+tag@example.com please')
self.assertNotIn('user+tag@example.com', result)
self.assertIn('@anon.invalid', result)
def test_email_with_subdomain(self):
result, _ = _redact('Reply to user@mail.dept.ac.uk here')
self.assertNotIn('@mail.dept.ac.uk', result)
# ── IDs ────────────────────────────────────────────────────────────────────
def test_7_digit_id_replaced(self):
result, _ = _redact('Student 1234567 enrolled')
self.assertNotIn('1234567', result)
self.assertIn('0000001', result)
def test_8_digit_id_replaced(self):
result, _ = _redact('Ref 12345678 filed')
self.assertNotIn('12345678', result)
self.assertIn('00000001', result)
def test_7_digit_preserves_length(self):
result, _ = _redact('ID: 1234567')
replaced = [t for t in result.split() if t.isdigit()]
self.assertTrue(all(len(t) == 7 for t in replaced))
def test_8_digit_preserves_length(self):
result, _ = _redact('ID: 12345678')
replaced = [t for t in result.split() if t.isdigit()]
self.assertTrue(all(len(t) == 8 for t in replaced))
def test_6_digit_not_caught(self):
result, _ = _redact('Code 123456 here')
self.assertIn('123456', result)
def test_9_digit_not_caught(self):
result, _ = _redact('Number 123456789 here')
self.assertIn('123456789', result)
def test_id_embedded_in_longer_number_not_caught(self):
# \b ensures 1234567 inside 012345678 is not extracted
result, _ = _redact('Code 012345678 here')
self.assertIn('012345678', result)
# ── usernames ──────────────────────────────────────────────────────────────
def test_8_char_mixed_username_replaced(self):
result, _ = _redact('Login: jsmith01 via portal')
self.assertNotIn('jsmith01', result)
self.assertIn('user0001xx', result)
def test_all_digit_8_char_not_a_username(self):
result, _ = _redact('Code: 12345678')
self.assertNotIn('user', result) # caught as id, not username
def test_all_alpha_8_char_not_a_username(self):
result, _ = _redact('Word: password')
self.assertNotIn('user', result)
def test_7_char_mixed_not_a_username(self):
result, _ = _redact('ref: abc1234') # only 7 chars
self.assertNotIn('user', result)
def test_9_char_mixed_not_a_username(self):
result, _ = _redact('ref: abc123def') # 9 chars
self.assertNotIn('user', result)
# ── names (heuristic, no spaCy) ────────────────────────────────────────────
def test_name_heuristic_two_words(self):
result, _ = _redact('Signed by John Smith', patterns=[_pat_name()])
self.assertNotIn('John Smith', result)
self.assertIn('Person', result)
def test_name_heuristic_three_words(self):
result, _ = _redact('Written by Mary Anne Jones', patterns=[_pat_name()])
self.assertNotIn('Mary Anne Jones', result)
def test_excluded_word_not_detected_as_name(self):
result, _ = _redact('Date is January Monday')
self.assertIn('January', result)
self.assertIn('Monday', result)
def test_single_title_case_word_not_caught(self):
# A capitalised word not adjacent to another title-case word is not a name
result, _ = _redact('Submit the Report today', patterns=[_pat_name()])
self.assertIn('Report', result)
# ── overlap / ordering ─────────────────────────────────────────────────────
def test_email_not_double_detected_as_name(self):
result, _ = _redact('Email: John.Smith@example.com here')
self.assertEqual(result.count('@anon.invalid'), 1)
self.assertNotIn('Person', result)
def test_first_pattern_wins_on_overlap(self):
# Email pattern runs before name; the email address wins
result, _ = _redact('Contact John.Smith@example.com')
self.assertIn('@anon.invalid', result)
self.assertNotIn('Person', result)
# ── consistency ────────────────────────────────────────────────────────────
def test_consistent_replacement_across_calls(self):
registry = PseudonymRegistry()
pats = _all_patterns()
r1 = redact_text('user@x.com', registry, pats, nlp=None)
r2 = redact_text('user@x.com', registry, pats, nlp=None)
self.assertEqual(r1, r2)
def test_two_occurrences_get_same_pseudonym(self):
registry = PseudonymRegistry()
result = redact_text(
'From user@x.com to user@x.com', registry, _all_patterns(), nlp=None
)
hits = [w for w in result.split() if '@anon.invalid' in w]
self.assertEqual(len(hits), 2)
self.assertEqual(hits[0], hits[1])
def test_different_values_get_different_pseudonyms(self):
result, _ = _redact('a@x.com and b@x.com')
self.assertIn('person001@anon.invalid', result)
self.assertIn('person002@anon.invalid', result)
# ── edge cases ─────────────────────────────────────────────────────────────
def test_empty_string(self):
result, _ = _redact('')
self.assertEqual(result, '')
def test_whitespace_only(self):
result, _ = _redact(' ')
self.assertEqual(result, ' ')
def test_no_pii_unchanged(self):
text = 'No sensitive data here at all.'
result, _ = _redact(text, patterns=[_pat_email(), _pat_id(), _pat_username()])
self.assertEqual(result, text)
def test_pii_at_start(self):
result, _ = _redact('user@x.com is the contact', patterns=[_pat_email()])
self.assertNotIn('user@x.com', result)
def test_pii_at_end(self):
result, _ = _redact('contact is user@x.com', patterns=[_pat_email()])
self.assertNotIn('user@x.com', result)
def test_multiple_pii_types_on_one_line(self):
result, _ = _redact('user@x.com 1234567 ab12cd34')
self.assertNotIn('user@x.com', result)
self.assertNotIn('1234567', result)
self.assertNotIn('ab12cd34', result)
def test_unicode_context_untouched(self):
result, _ = _redact('Ünïcödé context, email user@x.com here')
self.assertIn('Ünïcödé', result)
self.assertNotIn('user@x.com', result)
# ── restore_text ───────────────────────────────────────────────────────────────
class TestDeanonymizeText(unittest.TestCase):
def setUp(self):
self.mapping = {
'Person001': 'John Smith',
'person001@anon.invalid': 'john@example.com',
'0000001': '1234567',
'user0001xx': 'jsmith01',
}
def test_name_reversal(self):
self.assertEqual(restore_text('Hello Person001', self.mapping),
'Hello John Smith')
def test_email_reversal(self):
self.assertEqual(restore_text('Email: person001@anon.invalid', self.mapping),
'Email: john@example.com')
def test_id_reversal(self):
self.assertEqual(restore_text('ID 0000001 enrolled', self.mapping),
'ID 1234567 enrolled')
def test_multiple_types_reversed(self):
result = restore_text('Person001 <person001@anon.invalid>', self.mapping)
self.assertEqual(result, 'John Smith <john@example.com>')
def test_empty_string(self):
self.assertEqual(restore_text('', self.mapping), '')
def test_no_pseudonyms_unchanged(self):
text = 'Nothing to replace here.'
self.assertEqual(restore_text(text, self.mapping), text)
def test_longest_first_prevents_partial_match(self):
# 'person001@anon.invalid' contains 'person001' as a substring.
# Longest pseudonym must be replaced first or the partial match corrupts it.
mapping = {
'person001@anon.invalid': 'real@email.com',
'person001': 'PARTIAL',
}
result = restore_text('person001@anon.invalid', mapping)
self.assertEqual(result, 'real@email.com')
# ── round-trips ────────────────────────────────────────────────────────────────
class TestRoundTrip(unittest.TestCase):
"""redact → restore must exactly restore the original."""
def _trip(self, text):
registry = PseudonymRegistry()
redacted = redact_text(text, registry, _all_patterns(), nlp=None)
return restore_text(redacted, registry.mapping_file_dict())
def test_email(self):
t = 'Contact user@example.com for details'
self.assertEqual(self._trip(t), t)
def test_7_digit_id(self):
t = 'Student 1234567 enrolled'
self.assertEqual(self._trip(t), t)
def test_8_digit_id(self):
t = 'Reference 87654321 filed'
self.assertEqual(self._trip(t), t)
def test_username(self):
t = 'Login ab12cd34 active'
self.assertEqual(self._trip(t), t)
def test_mixed_pii(self):
t = 'User ab12cd34 <user@dept.org> ID 1234567 enrolled'
self.assertEqual(self._trip(t), t)
def test_repeated_values(self):
t = 'user@x.com and again user@x.com'
self.assertEqual(self._trip(t), t)
def test_no_pii_unchanged(self):
t = 'Nothing sensitive in this sentence.'
self.assertEqual(self._trip(t), t)
def test_multiple_emails(self):
t = 'From a@x.com to b@x.com cc c@x.com'
self.assertEqual(self._trip(t), t)
# ── build_patterns ─────────────────────────────────────────────────────────────
class TestBuildPatterns(unittest.TestCase):
def _cfg(self, patterns):
return {'patterns': patterns}
def test_basic_regex_compiled(self):
pats = build_patterns(self._cfg([
{'name': 'test', 'regex': r'\bfoo\b', 'template': 'BAR{n}'}
]))
self.assertEqual(len(pats), 1)
self.assertIsNotNone(pats[0].regex)
self.assertIsNotNone(pats[0].regex.search('foo'))
def test_name_and_template_set(self):
pats = build_patterns(self._cfg([
{'name': 'myid', 'regex': r'\d+', 'template': 'ID{n:04d}'}
]))
self.assertEqual(pats[0].name, 'myid')
self.assertEqual(pats[0].template, 'ID{n:04d}')
def test_spacy_ner_flag(self):
pats = build_patterns(self._cfg([
{'name': 'name', 'spacy_ner': True, 'regex': r'\b[A-Z]\w+\b', 'template': 'X'}
]))
self.assertTrue(pats[0].spacy_ner)
def test_default_spacy_labels(self):
pats = build_patterns(self._cfg([
{'name': 'name', 'spacy_ner': True, 'template': 'X'}
]))
self.assertEqual(pats[0].spacy_labels, ['PERSON'])
def test_custom_spacy_labels(self):
pats = build_patterns(self._cfg([
{'name': 'org', 'spacy_ner': True, 'spacy_labels': ['ORG', 'GPE'], 'template': 'X'}
]))
self.assertEqual(pats[0].spacy_labels, ['ORG', 'GPE'])
def test_exclusions_become_frozenset(self):
pats = build_patterns(self._cfg([
{'name': 'n', 'regex': r'\w+', 'template': 'X', 'exclusions': ['January', 'Monday']}
]))
self.assertIsInstance(pats[0].exclusions, frozenset)
self.assertIn('January', pats[0].exclusions)
def test_regex_ignorecase_flag(self):
pats = build_patterns(self._cfg([
{'name': 'x', 'regex': r'foo', 'template': 'X', 'regex_flags': ['IGNORECASE']}
]))
self.assertTrue(pats[0].regex.flags & re.IGNORECASE)
self.assertIsNotNone(pats[0].regex.search('FOO'))
def test_no_regex_when_omitted(self):
pats = build_patterns(self._cfg([
{'name': 'name', 'spacy_ner': True, 'template': 'X'}
]))
self.assertIsNone(pats[0].regex)
def test_order_preserved(self):
pats = build_patterns(self._cfg([
{'name': 'a', 'regex': r'x', 'template': 'A'},
{'name': 'b', 'regex': r'y', 'template': 'B'},
{'name': 'c', 'regex': r'z', 'template': 'C'},
]))
self.assertEqual([p.name for p in pats], ['a', 'b', 'c'])
def test_empty_config(self):
self.assertEqual(build_patterns({}), [])
# ── load_config ────────────────────────────────────────────────────────────────
class TestLoadConfig(unittest.TestCase):
def test_loads_default_config(self):
config = load_config(DEFAULT_CONFIG)
self.assertIn('patterns', config)
def test_default_has_email_pattern(self):
config = load_config(DEFAULT_CONFIG)
names = [p['name'] for p in config['patterns']]
self.assertIn('email', names)
def test_default_has_name_pattern(self):
config = load_config(DEFAULT_CONFIG)
names = [p['name'] for p in config['patterns']]
self.assertIn('name', names)
def test_default_has_id_pattern(self):
config = load_config(DEFAULT_CONFIG)
names = [p['name'] for p in config['patterns']]
self.assertIn('id', names)
def test_default_has_username_pattern(self):
config = load_config(DEFAULT_CONFIG)
names = [p['name'] for p in config['patterns']]
self.assertIn('username', names)
def test_name_pattern_has_spacy_ner(self):
config = load_config(DEFAULT_CONFIG)
name_pat = next(p for p in config['patterns'] if p['name'] == 'name')
self.assertTrue(name_pat.get('spacy_ner', False))
def test_id_pattern_uses_preserve_length(self):
config = load_config(DEFAULT_CONFIG)
id_pat = next(p for p in config['patterns'] if p['name'] == 'id')
self.assertEqual(id_pat['template'], 'preserve_length')
def test_missing_config_exits(self):
with self.assertRaises(SystemExit):
load_config(Path('/nonexistent/no_such_file.toml'))
def test_custom_config_file(self):
content = b'[[patterns]]\nname = "test"\nregex = \'foo\'\ntemplate = "BAR{n}"\n'
with tempfile.NamedTemporaryFile(suffix='.toml', delete=False) as f:
f.write(content)
tmp = Path(f.name)
try:
config = load_config(tmp)
self.assertEqual(config['patterns'][0]['name'], 'test')
finally:
tmp.unlink()
# ── file handler: docx ─────────────────────────────────────────────────────────
def _docx_available():
try:
from docx import Document # noqa: F401
return True
except ImportError:
return False
def _make_docx(path, paragraphs=(), table_rows=()):
from docx import Document
doc = Document()
for text in paragraphs:
doc.add_paragraph(text)
if table_rows:
table = doc.add_table(rows=len(table_rows), cols=len(table_rows[0]))
for r, row in enumerate(table_rows):
for c, val in enumerate(row):
table.cell(r, c).text = val
doc.save(str(path))
def _read_docx_para(path, index=0):
from docx import Document
return Document(str(path)).paragraphs[index].text
class TestFileHandlerDocx(unittest.TestCase):
def setUp(self):
if not _docx_available():
self.skipTest('python-docx not installed')
def test_paragraph_anonymized(self):
from redact import process_docx
with tempfile.TemporaryDirectory() as d:
src = Path(d) / 'src.docx'
out = Path(d) / 'out.docx'
_make_docx(src, paragraphs=['John Smith emailed user@example.com ID 1234567'])
registry = PseudonymRegistry()
process_docx(src, out, lambda t: redact_text(t, registry, _all_patterns(), nlp=None))
text = _read_docx_para(out)
self.assertNotIn('user@example.com', text)
self.assertNotIn('1234567', text)
def test_table_cells_anonymized(self):
from docx import Document
from redact import process_docx
with tempfile.TemporaryDirectory() as d:
src = Path(d) / 'src.docx'
out = Path(d) / 'out.docx'
_make_docx(src, table_rows=[['user@example.com', '1234567']])
registry = PseudonymRegistry()
process_docx(src, out, lambda t: redact_text(t, registry, _all_patterns(), nlp=None))
doc2 = Document(str(out))
self.assertNotIn('user@example.com', doc2.tables[0].cell(0, 0).text)
self.assertNotIn('1234567', doc2.tables[0].cell(0, 1).text)
def test_round_trip(self):
from redact import process_docx
original = 'user@example.com ID 1234567'
with tempfile.TemporaryDirectory() as d:
src = Path(d) / 'src.docx'
redacted = Path(d) / 'redacted.docx'
restored_path = Path(d) / 'restored.docx'
_make_docx(src, paragraphs=[original])
registry = PseudonymRegistry()
process_docx(src, redacted, lambda t: redact_text(t, registry, _all_patterns(), nlp=None))
mapping = registry.mapping_file_dict()
process_docx(redacted, restored_path, lambda t: restore_text(t, mapping))
text = _read_docx_para(restored_path)
self.assertIn('user@example.com', text)
self.assertIn('1234567', text)
def test_empty_paragraphs_untouched(self):
from docx import Document
from redact import process_docx
with tempfile.TemporaryDirectory() as d:
src = Path(d) / 'src.docx'
out = Path(d) / 'out.docx'
_make_docx(src, paragraphs=['', 'normal text', ''])
registry = PseudonymRegistry()
process_docx(src, out, lambda t: redact_text(t, registry, _all_patterns(), nlp=None))
paras = [p.text for p in Document(str(out)).paragraphs]
self.assertIn('normal text', paras)
# ── file handler: xlsx ─────────────────────────────────────────────────────────
class TestFileHandlerXlsx(unittest.TestCase):
@classmethod
def setUpClass(cls):
try:
import openpyxl
cls.openpyxl = openpyxl
except ImportError:
cls.openpyxl = None
def setUp(self):
if self.openpyxl is None:
self.skipTest('openpyxl not installed')
def test_string_cells_anonymized(self):
from redact import process_xlsx
with tempfile.TemporaryDirectory() as d:
src = Path(d) / 'src.xlsx'
out = Path(d) / 'out.xlsx'
wb = self.openpyxl.Workbook()
ws = wb.active
ws.append(['Name', 'Email', 'ID'])
ws.append(['John Smith', 'j@example.com', '1234567'])
wb.save(str(src))
registry = PseudonymRegistry()
process_xlsx(src, out, lambda t: redact_text(t, registry, _all_patterns(), nlp=None))
wb2 = self.openpyxl.load_workbook(str(out))
flat = [str(v) for row in wb2.active.iter_rows(values_only=True)
for v in row if v is not None]
self.assertNotIn('j@example.com', flat)
self.assertNotIn('1234567', flat)
def test_numeric_cells_untouched(self):
from redact import process_xlsx
with tempfile.TemporaryDirectory() as d:
src = Path(d) / 'src.xlsx'
out = Path(d) / 'out.xlsx'
wb = self.openpyxl.Workbook()
ws = wb.active
ws['A1'] = 42
ws['B1'] = 3.14
wb.save(str(src))
registry = PseudonymRegistry()
process_xlsx(src, out, lambda t: redact_text(t, registry, _all_patterns(), nlp=None))
wb2 = self.openpyxl.load_workbook(str(out))
self.assertEqual(wb2.active['A1'].value, 42)
self.assertAlmostEqual(wb2.active['B1'].value, 3.14)
def test_multiple_sheets_processed(self):
from redact import process_xlsx
with tempfile.TemporaryDirectory() as d:
src = Path(d) / 'src.xlsx'
out = Path(d) / 'out.xlsx'
wb = self.openpyxl.Workbook()
wb.active['A1'] = 'Sheet1: user@a.com'
ws2 = wb.create_sheet('Sheet2')
ws2['A1'] = 'Sheet2: user@b.com'
wb.save(str(src))
registry = PseudonymRegistry()
process_xlsx(src, out, lambda t: redact_text(t, registry, _all_patterns(), nlp=None))
wb2 = self.openpyxl.load_workbook(str(out))
self.assertNotIn('user@a.com', str(wb2.active['A1'].value))
self.assertNotIn('user@b.com', str(wb2['Sheet2']['A1'].value))
def test_round_trip(self):
from redact import process_xlsx
with tempfile.TemporaryDirectory() as d:
src = Path(d) / 'src.xlsx'
red = Path(d) / 'redacted.xlsx'
rest = Path(d) / 'rest.xlsx'
wb = self.openpyxl.Workbook()
ws = wb.active
ws.append(['j@example.com', '1234567', 'ab12cd34'])
wb.save(str(src))
registry = PseudonymRegistry()
process_xlsx(src, red, lambda t: redact_text(t, registry, _all_patterns(), nlp=None))
mapping = registry.mapping_file_dict()
process_xlsx(red, rest, lambda t: restore_text(t, mapping))
wb3 = self.openpyxl.load_workbook(str(rest))
flat = [str(v) for row in wb3.active.iter_rows(values_only=True) for v in row if v]
self.assertIn('j@example.com', flat)
self.assertIn('1234567', flat)
self.assertIn('ab12cd34', flat)
# ── file handler: odt ──────────────────────────────────────────────────────────
def _odt_available():
try:
from odf.opendocument import OpenDocumentText # noqa: F401
return True
except ImportError:
return False
def _make_odt(path, text):
from odf.opendocument import OpenDocumentText
from odf.text import P
doc = OpenDocumentText()
doc.text.addElement(P(text=text))
doc.save(str(path))
def _read_odt(path):
from odf.opendocument import load as odf_load
doc = odf_load(str(path))
parts = []
def _walk(el):
for child in el.childNodes:
if hasattr(child, 'data'):
parts.append(child.data)
else:
_walk(child)
_walk(doc.text)
return ' '.join(parts)
class TestFileHandlerOdt(unittest.TestCase):
def setUp(self):
if not _odt_available():
self.skipTest('odfpy not installed')
def test_text_anonymized(self):
from redact import process_odt
with tempfile.TemporaryDirectory() as d:
src = Path(d) / 'src.odt'
out = Path(d) / 'out.odt'
_make_odt(src, 'Contact user@example.com ID 1234567')
registry = PseudonymRegistry()
process_odt(src, out, lambda t: redact_text(t, registry, _all_patterns(), nlp=None))
content = _read_odt(out)
self.assertNotIn('user@example.com', content)
self.assertNotIn('1234567', content)
def test_round_trip(self):
from redact import process_odt
with tempfile.TemporaryDirectory() as d:
src = Path(d) / 'src.odt'
red = Path(d) / 'redacted.odt'
rest = Path(d) / 'rest.odt'
_make_odt(src, 'user@example.com 1234567')
registry = PseudonymRegistry()
process_odt(src, red, lambda t: redact_text(t, registry, _all_patterns(), nlp=None))
mapping = registry.mapping_file_dict()
process_odt(red, rest, lambda t: restore_text(t, mapping))
content = _read_odt(rest)
self.assertIn('user@example.com', content)
self.assertIn('1234567', content)
# ── file handler: pdf ──────────────────────────────────────────────────────────
class TestFileHandlerPdf(unittest.TestCase):
def setUp(self):
try:
import pdfplumber # noqa: F401
except ImportError:
self.skipTest('pdfplumber not installed')
# Use the PDF that lives next to the script; skip if absent
self.pdf = Path(__file__).parent / 'Academic Calendar 2026-27.pdf'
if not self.pdf.exists():
self.skipTest('no test PDF found in project directory')
def test_produces_text_file(self):
from redact import process_pdf
with tempfile.TemporaryDirectory() as d:
out = Path(d) / 'out.txt'
registry = PseudonymRegistry()
process_pdf(self.pdf, out,
lambda t: redact_text(t, registry, _all_patterns(), nlp=None))
self.assertTrue(out.exists())
self.assertGreater(out.stat().st_size, 0)
def test_emails_replaced_in_output(self):
from redact import process_pdf
with tempfile.TemporaryDirectory() as d:
out = Path(d) / 'out.txt'
registry = PseudonymRegistry()
process_pdf(self.pdf, out,
lambda t: redact_text(t, registry, _all_patterns(), nlp=None))
text = out.read_text(encoding='utf-8')
# The calendar contains manchester.ac.uk addresses
self.assertNotIn('@manchester.ac.uk', text)
self.assertIn('@anon.invalid', text)
# ── spaCy NER (skipped unless model is present) ────────────────────────────────
try:
import spacy as _spacy
_spacy.load('en_core_web_sm')
_SPACY_OK = True
except Exception:
_SPACY_OK = False
@unittest.skipUnless(_SPACY_OK, 'spaCy en_core_web_sm not installed')
class TestSpacyNER(unittest.TestCase):
@classmethod
def setUpClass(cls):
import spacy
cls.nlp = spacy.load('en_core_web_sm')
cls.patterns = _all_patterns()
def _run(self, text):
registry = PseudonymRegistry()
return redact_text(text, registry, self.patterns, nlp=self.nlp), registry
def test_person_name_detected(self):
result, _ = self._run('The report was written by Alice Johnson.')
self.assertNotIn('Alice Johnson', result)
self.assertIn('Person', result)
def test_email_still_replaced(self):
result, _ = self._run('Email alice@example.com for details.')
self.assertNotIn('alice@example.com', result)
self.assertIn('@anon.invalid', result)
def test_name_and_email_both_replaced(self):
result, _ = self._run('Alice Johnson <alice@example.com> submitted')
self.assertNotIn('Alice Johnson', result)
self.assertNotIn('alice@example.com', result)
def test_date_not_replaced(self):
# Dates are tagged DATE by spaCy, not PERSON — should be unchanged
result, _ = self._run('The deadline is 15 March 2025')
self.assertNotIn('Person', result)
self.assertIn('2025', result)
def test_round_trip_with_spacy(self):
original = 'Alice Johnson <alice@example.com> ID 1234567'
registry = PseudonymRegistry()
redacted = redact_text(original, registry, self.patterns, nlp=self.nlp)
restored = restore_text(redacted, registry.mapping_file_dict())
self.assertEqual(restored, original)
def test_id_and_username_still_caught(self):
result, _ = self._run('ID 1234567 login ab12cd34')
self.assertNotIn('1234567', result)
self.assertNotIn('ab12cd34', result)
def _pat_proper_noun():
return PatternDef(
name='proper_noun',
template='Entity{n:03d}',
regex=re.compile(r'\b[A-Z][a-z]{1,30}(?:\s+[A-Z][a-z]{1,30}){0,3}\b'),
exclusions=frozenset({'January', 'Monday', 'The', 'This', 'Section', 'Table',
'True', 'False', 'Introduction', 'However'}),
min_word_length=2,
pn_only=True,
)