Added base_sld_domains filter

This commit is contained in:
2025-05-17 12:19:50 +02:00
parent 76f303da27
commit 1c0224d1df
6 changed files with 171 additions and 109 deletions

View File

@@ -0,0 +1,48 @@
import unittest
import sys
import os
# Ensure filter_plugins directory is on the path
sys.path.insert(
0,
os.path.abspath(os.path.join(os.path.dirname(__file__), '../../filter_plugins'))
)
from domain_filters import FilterModule
class TestGenerateAllDomains(unittest.TestCase):
def setUp(self):
self.filter = FilterModule().generate_all_domains
def test_simple_string_values(self):
domains = {'app': 'example.com'}
result = self.filter(domains)
expected = ['example.com', 'www.example.com']
self.assertEqual(result, expected)
def test_list_and_dict_values(self):
domains = {
'app1': ['one.com', 'two.com'],
'app2': {'x': 'x.com', 'y': 'y.com'}
}
result = self.filter(domains)
expected = sorted([
'one.com', 'two.com', 'x.com', 'y.com',
'www.one.com', 'www.two.com', 'www.x.com', 'www.y.com'
])
self.assertEqual(result, expected)
def test_include_www_false(self):
domains = {'app': 'no-www.com'}
result = self.filter(domains, include_www=False)
self.assertEqual(result, ['no-www.com'])
def test_deduplicate_and_sort(self):
domains = {
'a': 'dup.com',
'b': 'dup.com',
'c': ['b.com', 'a.com'],
}
result = self.filter(domains)
expected = ['a.com', 'b.com', 'dup.com', 'www.a.com', 'www.b.com', 'www.dup.com']
self.assertEqual(result, expected)

View File

@@ -0,0 +1,49 @@
import unittest
import sys
import os
# Ensure filter_plugins directory is on the path
sys.path.insert(
0,
os.path.abspath(os.path.join(os.path.dirname(__file__), '../../filter_plugins'))
)
from domain_filters import FilterModule
class TestGenerateBaseSldDomains(unittest.TestCase):
def setUp(self):
self.filter = FilterModule().generate_base_sld_domains
def test_simple_string_and_redirect(self):
domains = {'app': 'sub.example.com'}
redirects = [{'source': 'alias.example.com'}]
result = self.filter(domains, redirects)
self.assertEqual(result, ['example.com'])
def test_without_redirect_mappings(self):
domains = {
'a': 'a.co',
'b': ['b.co', 'sub.c.co'],
'c': {'x': 'x.co'}
}
result = self.filter(domains, None)
self.assertEqual(result, ['a.co', 'b.co', 'c.co', 'x.co'])
def test_redirect_list_sources(self):
domains = {'app': 'app.domain.org'}
redirects = [{'source': ['alias.domain.org', 'deep.sub.example.net']}]
result = self.filter(domains, redirects)
self.assertEqual(result, ['domain.org', 'example.net'])
def test_duplicate_entries_and_sorting(self):
domains = {
'x': ['one.com', 'sub.one.com'],
'y': 'two.com',
'z': {'k': 'one.com'}
}
redirects = [{'source': 'deep.two.com'}]
result = self.filter(domains, redirects)
self.assertEqual(result, ['one.com', 'two.com'])
if __name__ == '__main__':
unittest.main()

View File

@@ -1,52 +0,0 @@
import sys
import os
import pytest
from filter_plugins.generate_all_domains import FilterModule
@pytest.fixture
def generate_filter():
"""
Fixture to return the generate_all_domains filter function.
"""
fm = FilterModule()
return fm.generate_all_domains
def test_simple_string_values(generate_filter):
domains = {'app': 'example.com'}
result = generate_filter(domains)
# Expect original and www-prefixed, deduped and sorted
expected = ['example.com', 'www.example.com']
assert result == expected
def test_list_and_dict_values(generate_filter):
domains = {
'app1': ['one.com', 'two.com'],
'app2': {'x': 'x.com', 'y': 'y.com'}
}
result = generate_filter(domains)
expected = sorted([
'one.com', 'two.com', 'x.com', 'y.com',
'www.one.com', 'www.two.com', 'www.x.com', 'www.y.com'
])
assert result == expected
def test_include_www_false(generate_filter):
domains = {'app': 'no-www.com'}
result = generate_filter(domains, include_www=False)
# Only the original domain
assert result == ['no-www.com']
def test_deduplicate_and_sort(generate_filter):
domains = {
'a': 'dup.com',
'b': 'dup.com',
'c': ['b.com', 'a.com'],
}
result = generate_filter(domains)
# Should contain unique domains sorted alphabetically
expected = ['a.com', 'b.com', 'dup.com', 'www.a.com', 'www.b.com', 'www.dup.com']
assert result == expected
if __name__ == '__main__':
pytest.main()