File size: 3,917 Bytes
6684cad
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fb70710
 
 
 
 
 
 
 
6684cad
 
 
 
 
 
 
 
 
 
26b4286
 
 
 
 
 
 
 
 
 
6684cad
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fb70710
 
 
 
 
6684cad
 
 
 
 
26b4286
 
 
 
 
 
 
6684cad
 
 
 
 
 
fb70710
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6684cad
 
 
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
from perde import leaked_values, mask, restore
from perde.recognizers import iban_ok, luhn_ok


def entities(text):
    return {s.entity: s.text for s in mask(text).spans}


def test_fin_with_context():
    assert entities("FIN kodum 5ZK3L8M")["FIN"] == "5ZK3L8M"


def test_az_phone_formats():
    for phone in ["+994 50 123 45 67", "+994501234567", "050-123-45-67", "(012) 555 12 34"]:
        assert "PHONE" in entities(f"Əlaqə: {phone}"), phone


def test_bracketed_landline_masked_with_its_bracket():
    assert entities("Tel: (012) 555 12 34")["PHONE"] == "(012) 555 12 34"


def test_house_number_before_full_stop_is_masked():
    assert entities("Ünvan: Sülh küçəsi 12. Tel yoxdur")["ADDRESS"] == "Sülh küçəsi 12"


def test_iban_checksum():
    assert iban_ok("AZ21NABZ00000000137010001944")
    assert not iban_ok("AZ21NABZ00000000137010001945")


def test_voen_needs_context_or_valid_suffix():
    assert entities("VÖEN: 1700123456")["VOEN"] == "1700123456"
    assert "VOEN" not in entities("Sifariş nömrəsi 5550001119")


def test_product_codes_are_not_fin():
    assert "FIN" not in entities("Kod ABC1234, COVID19 testi")
    assert entities("FIN kodum ABC1234")["FIN"] == "ABC1234"
    assert entities("kod 6TR9K2L")["FIN"] == "6TR9K2L"


def test_ten_digits_next_to_phone_words_are_not_voen():
    assert "VOEN" not in entities("telefon 4111111111")


def test_card_luhn():
    assert luhn_ok("4111111111111111")
    assert "CARD" in entities("Ödəniş 4111 1111 1111 1111 ilə")


def test_person_full_name_with_patronymic():
    assert entities("Mən Əli Vəli oğlu Məmmədov")["PERSON"] == "Əli Vəli oğlu Məmmədov"


def test_greeting_word_is_not_a_name():
    assert "PERSON" not in entities("Hörmətli müştəri, sifarişiniz hazırdır.")


def test_same_value_gets_same_tag():
    r = mask("Aygün zəng etdi. Sonra Aygün yazdı.")
    assert r.masked.count("[PERSON_1]") == 2


def test_same_phone_in_two_formats_gets_one_tag():
    r = mask("Zəng: 050 123 45 67 və ya +994501234567")
    assert r.masked.count("[PHONE_1]") == 2


def test_restore_tolerates_model_formatting():
    r = mask("Telefon +994 55 765 43 21")
    assert restore("Nömrə: [ phone_1 ]", r.mapping) == "Nömrə: +994 55 765 43 21"


def test_restore_handles_tags_without_brackets_or_with_dashes():
    r = mask("Aygün, ş/v AZE12345678")
    out = restore("Salam PERSON_1, [PERSON-1], [person 1], ID_CARD_1.", r.mapping)
    assert out == "Salam Aygün, Aygün, Aygün, AZE12345678."
    assert restore("MYPERSON_1 PERSON_12", r.mapping) == "MYPERSON_1 PERSON_12"


def test_no_leak_after_masking():
    text = "Rəşad Həsənov, FIN 7XK2M9P, tel 070 222 33 44, rashad@bank.az"
    r = mask(text)
    assert leaked_values(r.masked, r.mapping) == []


def test_leak_check_ignores_values_inside_other_words_and_numbers():
    r = mask("Əli zəng etdi, tel +994 55 765 43 21")
    assert leaked_values("Bəli/xeyr cavabı ver", r.mapping) == []
    assert leaked_values("Tarix 2024-55-76, kod 54 321", r.mapping) == []


def test_gateway_masks_personal_data_in_the_task_too(tmp_path, monkeypatch):
    from perde import gateway
    monkeypatch.setattr(gateway, "AUDIT_PATH", tmp_path / "audit.jsonl")
    res = gateway.run("Tel 055 412 33 90", "Reply to Rəşad Həsənov, FIN 7XK2M9P", provider="mock")
    for value in ("Rəşad Həsənov", "7XK2M9P", "055 412 33 90"):
        assert value not in res.outgoing


def test_gateway_blocks_when_no_data_types_selected(tmp_path, monkeypatch):
    import pytest
    from perde import gateway
    monkeypatch.setattr(gateway, "AUDIT_PATH", tmp_path / "audit.jsonl")
    with pytest.raises(gateway.LeakBlocked):
        gateway.run("Nərmin Quliyeva FIN 6TR9K2L", "", provider="mock", entities=[])


def test_leak_check_catches_unmasked_digits():
    r = mask("Telefon +994 55 765 43 21")
    assert leaked_values("call 0557654321", r.mapping)