more filters

master
Y 2018-01-31 08:30:19 +01:00
parent 2680405c89
commit 298a4c3a11
5 changed files with 77 additions and 1 deletions

View File

@ -0,0 +1,13 @@
# pyruse is intended as a replacement to both fail2ban and epylog
# Copyright © 20172018 Y. Gablin
# Full licensing information in the LICENSE file, or gnu.org/licences/gpl-3.0.txt if the file is missing.
from pyruse import base
class Filter(base.Filter):
def __init__(self, args):
super().__init__()
self.field = args["field"]
self.values = args["values"]
def filter(self, entry):
return entry.get(self.field, None) in self.values

View File

@ -0,0 +1,13 @@
# pyruse is intended as a replacement to both fail2ban and epylog
# Copyright © 20172018 Y. Gablin
# Full licensing information in the LICENSE file, or gnu.org/licences/gpl-3.0.txt if the file is missing.
from pyruse import base
class Filter(base.Filter):
def __init__(self, args):
super().__init__()
self.field = args["field"]
self.value = args["value"]
def filter(self, entry):
return entry[self.field] <= self.value if self.field in entry else False

22
tests/filter_in.py Normal file
View File

@ -0,0 +1,22 @@
# pyruse is intended as a replacement to both fail2ban and epylog
# Copyright © 20172018 Y. Gablin
# Full licensing information in the LICENSE file, or gnu.org/licences/gpl-3.0.txt if the file is missing.
from pyruse.filters.filter_in import Filter
def whenNotInListThenFalse():
assert not Filter({"field": "v", "values": [0, "test"]}).filter({"v": 3})
def whenInListSameTypeThenTrue():
assert Filter({"field": "v", "values": [2]}).filter({"v": 2})
def whenInListDiffTypeThenTrue():
assert Filter({"field": "v", "values": [2.0]}).filter({"v": 2})
def whenNoFieldThenFalse():
assert not Filter({"field": "v", "values": [0]}).filter({"other": 0})
def unitTests():
whenNotInListThenFalse()
whenInListSameTypeThenTrue()
whenInListDiffTypeThenTrue()
whenNoFieldThenFalse()

View File

@ -0,0 +1,26 @@
# pyruse is intended as a replacement to both fail2ban and epylog
# Copyright © 20172018 Y. Gablin
# Full licensing information in the LICENSE file, or gnu.org/licences/gpl-3.0.txt if the file is missing.
from pyruse.filters.filter_lowerOrEquals import Filter
def whenLowerNegIntThenTrue():
assert Filter({"field": "v", "value": -2}).filter({"v": -3})
def whenLowerPosFloatThenTrue():
assert Filter({"field": "v", "value": 2.1}).filter({"v": 1.9})
def whenEqualSameTypeThenTrue():
assert Filter({"field": "v", "value": 2}).filter({"v": 2})
def whenEqualDiffTypeThenTrue():
assert Filter({"field": "v", "value": 2.0}).filter({"v": 2})
def whenGreaterThenFalse():
assert not Filter({"field": "v", "value": 0}).filter({"v": 2})
def unitTests():
whenLowerNegIntThenTrue()
whenLowerPosFloatThenTrue()
whenEqualSameTypeThenTrue()
whenEqualDiffTypeThenTrue()
whenGreaterThenFalse()

View File

@ -28,11 +28,13 @@ def main():
base.actionFallback = None
# Unit tests
import filter_equals, filter_greaterOrEquals, filter_pcre, filter_pcreAny, filter_userExists
import filter_equals, filter_greaterOrEquals, filter_in, filter_lowerOrEquals, filter_pcre, filter_pcreAny, filter_userExists
import action_counterRaise, action_counterReset, action_dailyReport, action_email, action_nftBan
filter_equals.unitTests()
filter_greaterOrEquals.unitTests()
filter_in.unitTests()
filter_lowerOrEquals.unitTests()
filter_pcre.unitTests()
filter_pcreAny.unitTests()
filter_userExists.unitTests()