core: add DebugRule which takes random amount of time to process

This commit is contained in:
Jens Langhammer 2018-12-09 21:06:21 +01:00
parent af3df16b90
commit 60b1c1b493
No known key found for this signature in database
GPG key ID: BEBC05297D92821B
3 changed files with 71 additions and 5 deletions

View file

@ -2,8 +2,9 @@
from django import forms from django import forms
from passbook.core.models import FieldMatcherRule, WebhookRule from passbook.core.models import DebugRule, FieldMatcherRule, WebhookRule
GENERAL_FIELDS = ['name', 'action', 'negate', 'order', ]
class FieldMatcherRuleForm(forms.ModelForm): class FieldMatcherRuleForm(forms.ModelForm):
"""FieldMatcherRule Form""" """FieldMatcherRule Form"""
@ -11,8 +12,7 @@ class FieldMatcherRuleForm(forms.ModelForm):
class Meta: class Meta:
model = FieldMatcherRule model = FieldMatcherRule
fields = ['name', 'action', 'negate', 'order', fields = GENERAL_FIELDS + ['user_field', 'match_action', 'value', ]
'user_field', 'match_action', 'value', ]
widgets = { widgets = {
'name': forms.TextInput(), 'name': forms.TextInput(),
'user_field': forms.TextInput(), 'user_field': forms.TextInput(),
@ -26,11 +26,24 @@ class WebhookRuleForm(forms.ModelForm):
class Meta: class Meta:
model = WebhookRule model = WebhookRule
fields = ['url', 'method', 'json_body', 'json_headers', fields = GENERAL_FIELDS + ['url', 'method', 'json_body', 'json_headers',
'result_jsonpath', 'result_json_value', ] 'result_jsonpath', 'result_json_value', ]
widgets = { widgets = {
'name': forms.TextInput(),
'json_body': forms.TextInput(), 'json_body': forms.TextInput(),
'json_headers': forms.TextInput(), 'json_headers': forms.TextInput(),
'result_jsonpath': forms.TextInput(), 'result_jsonpath': forms.TextInput(),
'result_json_value': forms.TextInput(), 'result_json_value': forms.TextInput(),
} }
class DebugRuleForm(forms.ModelForm):
"""DebugRuleForm Form"""
class Meta:
model = DebugRule
fields = GENERAL_FIELDS + ['result', 'wait_min', 'wait_max']
widgets = {
'name': forms.TextInput(),
}

View file

@ -0,0 +1,28 @@
# Generated by Django 2.1.4 on 2018-12-09 18:58
import django.db.models.deletion
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('passbook_core', '0006_user_uuid'),
]
operations = [
migrations.CreateModel(
name='DebugRule',
fields=[
('rule_ptr', models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, serialize=False, to='passbook_core.Rule')),
('result', models.BooleanField(default=False)),
('wait_min', models.IntegerField(default=5)),
('wait_max', models.IntegerField(default=30)),
],
options={
'verbose_name': 'Debug Rule',
'verbose_name_plural': 'Debug Rules',
},
bases=('passbook_core.rule',),
),
]

View file

@ -1,6 +1,8 @@
"""passbook core models""" """passbook core models"""
import re import re
from logging import getLogger from logging import getLogger
from random import randrange
from time import sleep
from uuid import uuid4 from uuid import uuid4
import reversion import reversion
@ -213,3 +215,26 @@ class WebhookRule(Rule):
verbose_name = _('Webhook Rule') verbose_name = _('Webhook Rule')
verbose_name_plural = _('Webhook Rules') verbose_name_plural = _('Webhook Rules')
@reversion.register()
class DebugRule(Rule):
"""Rule used for debugging the RuleEngine. Returns a fixed result,
but takes a random time to process."""
result = models.BooleanField(default=False)
wait_min = models.IntegerField(default=5)
wait_max = models.IntegerField(default=30)
form = 'passbook.core.forms.rules.DebugRuleForm'
def passes(self, user: User):
"""Wait random time then return result"""
wait = randrange(self.wait_min, self.wait_max)
LOGGER.debug("Rule '%s' waiting for %ds", self.name, wait)
sleep(wait)
return self.result
class Meta:
verbose_name = _('Debug Rule')
verbose_name_plural = _('Debug Rules')