This commit is contained in:
91
tests/test_patterns.py
Normal file
91
tests/test_patterns.py
Normal file
@@ -0,0 +1,91 @@
|
|||||||
|
import pytest
|
||||||
|
from code_privacy_shield.patterns import PatternLibrary
|
||||||
|
|
||||||
|
|
||||||
|
class TestPatternLibrary:
|
||||||
|
def setup_method(self):
|
||||||
|
self.library = PatternLibrary()
|
||||||
|
|
||||||
|
def test_api_key_patterns_compile(self):
|
||||||
|
patterns = self.library.get_api_key_patterns()
|
||||||
|
assert len(patterns) > 0
|
||||||
|
for pattern, name in patterns:
|
||||||
|
assert pattern is not None
|
||||||
|
assert name is not None
|
||||||
|
|
||||||
|
def test_pii_patterns_compile(self):
|
||||||
|
patterns = self.library.get_pii_patterns()
|
||||||
|
assert len(patterns) > 0
|
||||||
|
for pattern, name in patterns:
|
||||||
|
assert pattern is not None
|
||||||
|
assert name is not None
|
||||||
|
|
||||||
|
def test_database_patterns_compile(self):
|
||||||
|
patterns = self.library.get_database_patterns()
|
||||||
|
assert len(patterns) > 0
|
||||||
|
for pattern, name in patterns:
|
||||||
|
assert pattern is not None
|
||||||
|
assert name is not None
|
||||||
|
|
||||||
|
def test_env_var_patterns_compile(self):
|
||||||
|
patterns = self.library.get_env_var_patterns()
|
||||||
|
assert len(patterns) > 0
|
||||||
|
for pattern, name in patterns:
|
||||||
|
assert pattern is not None
|
||||||
|
assert name is not None
|
||||||
|
|
||||||
|
def test_ip_patterns_compile(self):
|
||||||
|
patterns = self.library.get_ip_patterns()
|
||||||
|
assert len(patterns) > 0
|
||||||
|
for pattern, name in patterns:
|
||||||
|
assert pattern is not None
|
||||||
|
assert name is not None
|
||||||
|
|
||||||
|
def test_authorization_patterns_compile(self):
|
||||||
|
patterns = self.library.get_authorization_patterns()
|
||||||
|
assert len(patterns) > 0
|
||||||
|
for pattern, name in patterns:
|
||||||
|
assert pattern is not None
|
||||||
|
assert name is not None
|
||||||
|
|
||||||
|
def test_openai_api_key_detection(self):
|
||||||
|
patterns = self.library.get_api_key_patterns()
|
||||||
|
test_key = "sk-abc123def456ghi789jkl012mno345pq"
|
||||||
|
found = False
|
||||||
|
for pattern, name in patterns:
|
||||||
|
match = pattern.search(test_key)
|
||||||
|
if match and name == "OpenAI API Key":
|
||||||
|
found = True
|
||||||
|
break
|
||||||
|
assert found
|
||||||
|
|
||||||
|
def test_email_detection(self):
|
||||||
|
patterns = self.library.get_pii_patterns()
|
||||||
|
test_email = "user@example.com"
|
||||||
|
found = False
|
||||||
|
for pattern, name in patterns:
|
||||||
|
match = pattern.search(test_email)
|
||||||
|
if match and name == "Email Address":
|
||||||
|
found = True
|
||||||
|
break
|
||||||
|
assert found
|
||||||
|
|
||||||
|
def test_postgresql_connection_detection(self):
|
||||||
|
patterns = self.library.get_database_patterns()
|
||||||
|
test_conn = "postgresql://user:password@localhost:5432/mydb"
|
||||||
|
found = False
|
||||||
|
for pattern, name in patterns:
|
||||||
|
match = pattern.search(test_conn)
|
||||||
|
if match and name == "PostgreSQL Connection":
|
||||||
|
found = True
|
||||||
|
break
|
||||||
|
assert found
|
||||||
|
|
||||||
|
def test_get_all_patterns(self):
|
||||||
|
all_patterns = self.library.get_all_patterns()
|
||||||
|
assert "api_keys" in all_patterns
|
||||||
|
assert "pii" in all_patterns
|
||||||
|
assert "database" in all_patterns
|
||||||
|
assert "env_var" in all_patterns
|
||||||
|
assert "ip" in all_patterns
|
||||||
|
assert "authorization" in all_patterns
|
||||||
Reference in New Issue
Block a user