Added boolean fields like 'reject_reports' to mergeplan handling.

Clarified logic in apply_mergeplan() for boolean fields.
Added tests for boolean merge situations.
This commit is contained in:
Justin Warren 2023-01-12 08:08:09 +11:00
parent 0a6eec4bf2
commit 66f0373cbe
No known key found for this signature in database
4 changed files with 85 additions and 39 deletions

View File

@ -171,10 +171,13 @@ def apply_mergeplan(oldblock: DomainBlock, newblock: DomainBlock, mergeplan: str
log.debug(f"New block severity is higher. Using that.") log.debug(f"New block severity is higher. Using that.")
blockdata['severity'] = newblock.severity blockdata['severity'] = newblock.severity
# If obfuscate is set and is True for the domain in # For 'reject_media', 'reject_reports', and 'obfuscate' if
# any blocklist then obfuscate is set to True. # the value is set and is True for the domain in
if getattr(newblock, 'obfuscate', False): # any blocklist then the value is set to True.
blockdata['obfuscate'] = True for key in ['reject_media', 'reject_reports', 'obfuscate']:
newval = getattr(newblock, key)
if newval == True:
blockdata[key] = True
elif mergeplan in ['min']: elif mergeplan in ['min']:
# Use the lowest block level found # Use the lowest block level found
@ -183,10 +186,13 @@ def apply_mergeplan(oldblock: DomainBlock, newblock: DomainBlock, mergeplan: str
if newblock.severity < oldblock.severity: if newblock.severity < oldblock.severity:
blockdata['severity'] = newblock.severity blockdata['severity'] = newblock.severity
# If obfuscate is set and is False for the domain in # For 'reject_media', 'reject_reports', and 'obfuscate' if
# any blocklist then obfuscate is set to False. # the value is set and is False for the domain in
if not getattr(newblock, 'obfuscate', True): # any blocklist then the value is set to False.
blockdata['obfuscate'] = False for key in ['reject_media', 'reject_reports', 'obfuscate']:
newval = getattr(newblock, key)
if newval == False:
blockdata[key] = False
else: else:
raise NotImplementedError(f"Mergeplan '{mergeplan}' not implemented.") raise NotImplementedError(f"Mergeplan '{mergeplan}' not implemented.")

View File

@ -1,14 +1,14 @@
"domain","severity","public_comment","private_comment","reject_media","reject_reports","obfuscate" "domain","severity","public_comment","private_comment","reject_media","reject_reports","obfuscate"
"public-comment.example.org","noop","This is a public comment","This is a private comment",TRUE,TRUE,TRUE "public-comment.example.org","noop","This is a public comment","This is a private comment",FALSE,FALSE,FALSE
"private-comment.example.org","noop",,"This is a private comment",TRUE,TRUE,TRUE "private-comment.example.org","noop",,"This is a private comment",FALSE,FALSE,FALSE
"diff-comment.example.org","noop","Noop public comment","Noop private comment",TRUE,TRUE,TRUE "diff-comment.example.org","noop","Noop public comment","Noop private comment",FALSE,FALSE,FALSE
"2diff-comment.example.org","noop","Public duplicate","Private duplicate",TRUE,TRUE,TRUE "2diff-comment.example.org","noop","Public duplicate","Private duplicate",FALSE,FALSE,FALSE
"qoto.org","noop",,,TRUE,TRUE,TRUE "qoto.org","noop",,,FALSE,FALSE,FALSE
"sealion.club","noop",,,TRUE,TRUE,TRUE "sealion.club","noop",,,FALSE,FALSE,FALSE
"develop.gab.com","noop",,,TRUE,TRUE,TRUE "develop.gab.com","noop",,,FALSE,FALSE,FALSE
"gab.ai","noop",,,TRUE,TRUE,TRUE "gab.ai","noop",,,FALSE,FALSE,FALSE
"gab.sleeck.eu","noop",,,TRUE,TRUE,TRUE "gab.sleeck.eu","noop",,,FALSE,FALSE,FALSE
"gab.com","noop",,,TRUE,TRUE,TRUE "gab.com","noop",,,FALSE,FALSE,FALSE
"kiwifarms.is","noop",,,TRUE,TRUE,TRUE "kiwifarms.is","noop",,,FALSE,FALSE,FALSE
"kiwifarms.net","noop",,,TRUE,TRUE,TRUE "kiwifarms.net","noop",,,FALSE,FALSE,FALSE
"gabfed.com","noop",,,TRUE,TRUE,TRUE "gabfed.com","noop",,,FALSE,FALSE,FALSE

1 domain severity public_comment private_comment reject_media reject_reports obfuscate
2 public-comment.example.org noop This is a public comment This is a private comment TRUE FALSE TRUE FALSE TRUE FALSE
3 private-comment.example.org noop This is a private comment TRUE FALSE TRUE FALSE TRUE FALSE
4 diff-comment.example.org noop Noop public comment Noop private comment TRUE FALSE TRUE FALSE TRUE FALSE
5 2diff-comment.example.org noop Public duplicate Private duplicate TRUE FALSE TRUE FALSE TRUE FALSE
6 qoto.org noop TRUE FALSE TRUE FALSE TRUE FALSE
7 sealion.club noop TRUE FALSE TRUE FALSE TRUE FALSE
8 develop.gab.com noop TRUE FALSE TRUE FALSE TRUE FALSE
9 gab.ai noop TRUE FALSE TRUE FALSE TRUE FALSE
10 gab.sleeck.eu noop TRUE FALSE TRUE FALSE TRUE FALSE
11 gab.com noop TRUE FALSE TRUE FALSE TRUE FALSE
12 kiwifarms.is noop TRUE FALSE TRUE FALSE TRUE FALSE
13 kiwifarms.net noop TRUE FALSE TRUE FALSE TRUE FALSE
14 gabfed.com noop TRUE FALSE TRUE FALSE TRUE FALSE

View File

@ -1,14 +1,14 @@
"domain","severity","public_comment","private_comment","reject_media","reject_reports","obfuscate" "domain","severity","public_comment","private_comment","reject_media","reject_reports","obfuscate"
"public-comment.example.org","silence","This is a public comment","This is a private comment",TRUE,TRUE,TRUE "public-comment.example.org","silence","This is a public comment","This is a private comment",FALSE,FALSE,FALSE
"private-comment.example.org","silence",,"This is a private comment",TRUE,TRUE,TRUE "private-comment.example.org","silence",,"This is a private comment",FALSE,FALSE,FALSE
"diff-comment.example.org","silence","Silence public comment","Silence private comment",TRUE,TRUE,TRUE "diff-comment.example.org","silence","Silence public comment","Silence private comment",FALSE,FALSE,FALSE
"2diff-comment.example.org","silence","Public duplicate","Private duplicate",TRUE,TRUE,TRUE "2diff-comment.example.org","silence","Public duplicate","Private duplicate",FALSE,FALSE,FALSE
"qoto.org","silence",,,TRUE,TRUE,TRUE "qoto.org","silence",,,FALSE,FALSE,FALSE
"sealion.club","silence",,,TRUE,TRUE,TRUE "sealion.club","silence",,,FALSE,FALSE,FALSE
"develop.gab.com","silence",,,TRUE,TRUE,TRUE "develop.gab.com","silence",,,FALSE,FALSE,FALSE
"gab.ai","silence",,,TRUE,TRUE,TRUE "gab.ai","silence",,,FALSE,FALSE,FALSE
"gab.sleeck.eu","silence",,,TRUE,TRUE,TRUE "gab.sleeck.eu","silence",,,FALSE,FALSE,FALSE
"gab.com","silence",,,TRUE,TRUE,TRUE "gab.com","silence",,,FALSE,FALSE,FALSE
"kiwifarms.is","silence",,,TRUE,TRUE,TRUE "kiwifarms.is","silence",,,FALSE,FALSE,FALSE
"kiwifarms.net","silence",,,TRUE,TRUE,TRUE "kiwifarms.net","silence",,,FALSE,FALSE,FALSE
"gabfed.com","silence",,,TRUE,TRUE,TRUE "gabfed.com","silence",,,FALSE,FALSE,FALSE

1 domain severity public_comment private_comment reject_media reject_reports obfuscate
2 public-comment.example.org silence This is a public comment This is a private comment TRUE FALSE TRUE FALSE TRUE FALSE
3 private-comment.example.org silence This is a private comment TRUE FALSE TRUE FALSE TRUE FALSE
4 diff-comment.example.org silence Silence public comment Silence private comment TRUE FALSE TRUE FALSE TRUE FALSE
5 2diff-comment.example.org silence Public duplicate Private duplicate TRUE FALSE TRUE FALSE TRUE FALSE
6 qoto.org silence TRUE FALSE TRUE FALSE TRUE FALSE
7 sealion.club silence TRUE FALSE TRUE FALSE TRUE FALSE
8 develop.gab.com silence TRUE FALSE TRUE FALSE TRUE FALSE
9 gab.ai silence TRUE FALSE TRUE FALSE TRUE FALSE
10 gab.sleeck.eu silence TRUE FALSE TRUE FALSE TRUE FALSE
11 gab.com silence TRUE FALSE TRUE FALSE TRUE FALSE
12 kiwifarms.is silence TRUE FALSE TRUE FALSE TRUE FALSE
13 kiwifarms.net silence TRUE FALSE TRUE FALSE TRUE FALSE
14 gabfed.com silence TRUE FALSE TRUE FALSE TRUE FALSE

View File

@ -2,9 +2,9 @@
""" """
from fediblockhole.blocklist_parser import parse_blocklist from fediblockhole.blocklist_parser import parse_blocklist
from fediblockhole import merge_blocklists, merge_comments from fediblockhole import merge_blocklists, merge_comments, apply_mergeplan
from fediblockhole.const import SeverityLevel from fediblockhole.const import SeverityLevel, DomainBlock
datafile01 = "data-suspends-01.csv" datafile01 = "data-suspends-01.csv"
datafile02 = "data-silences-01.csv" datafile02 = "data-silences-01.csv"
@ -71,9 +71,12 @@ def test_mergeplan_3_max():
for key in bl: for key in bl:
assert bl[key].severity.level == SeverityLevel.SUSPEND assert bl[key].severity.level == SeverityLevel.SUSPEND
assert bl[key].reject_media == True
assert bl[key].reject_reports == True
assert bl[key].obfuscate == True
def test_mergeplan_3_max(): def test_mergeplan_3_min():
"""3 datafiles and mergeplan of 'max'""" """3 datafiles and mergeplan of 'min'"""
blocklists = load_test_blocklist_data([datafile01, datafile02, datafile03]) blocklists = load_test_blocklist_data([datafile01, datafile02, datafile03])
bl = merge_blocklists(blocklists, 'min') bl = merge_blocklists(blocklists, 'min')
@ -81,6 +84,9 @@ def test_mergeplan_3_max():
for key in bl: for key in bl:
assert bl[key].severity.level == SeverityLevel.NONE assert bl[key].severity.level == SeverityLevel.NONE
assert bl[key].reject_media == False
assert bl[key].reject_reports == False
assert bl[key].obfuscate == False
def test_mergeplan_noop_v_silence_max(): def test_mergeplan_noop_v_silence_max():
"""Mergeplan of max should choose silence over noop""" """Mergeplan of max should choose silence over noop"""
@ -198,4 +204,38 @@ def test_merge_comments_dups():
r = merge_comments(a, b) r = merge_comments(a, b)
assert r == 'boring, nazis, lack of moderation, flagged, special, spoon, happy, fork' assert r == 'boring, nazis, lack of moderation, flagged, special, spoon, happy, fork'
def test_mergeplan_same_min_bools_false():
"""Test merging with mergeplan 'max' and False values doesn't change them
"""
a = DomainBlock('example.org', 'noop', '', '', False, False, False)
b = DomainBlock('example.org', 'noop', '', '', False, False, False)
r = apply_mergeplan(a, b, 'max')
assert r.reject_media == False
assert r.reject_reports == False
assert r.obfuscate == False
def test_mergeplan_same_min_bools_true():
"""Test merging with mergeplan 'max' and True values doesn't change them
"""
a = DomainBlock('example.org', 'noop', '', '', True, False, True)
b = DomainBlock('example.org', 'noop', '', '', True, False, True)
r = apply_mergeplan(a, b, 'max')
assert r.reject_media == True
assert r.reject_reports == False
assert r.obfuscate == True
def test_mergeplan_max_bools():
a = DomainBlock('example.org', 'suspend', '', '', True, False, True)
b = DomainBlock('example.org', 'noop', '', '', False, False, False)
r = apply_mergeplan(a, b, 'max')
assert r.reject_media == True
assert r.reject_reports == False
assert r.obfuscate == True