From 60b1c1b493bf6bfcf5c350a84a4554fdd5d3190b Mon Sep 17 00:00:00 2001 From: Jens Langhammer Date: Sun, 9 Dec 2018 21:06:21 +0100 Subject: [PATCH] core: add DebugRule which takes random amount of time to process --- passbook/core/forms/rules.py | 23 ++++++++++++++---- passbook/core/migrations/0007_debugrule.py | 28 ++++++++++++++++++++++ passbook/core/models.py | 25 +++++++++++++++++++ 3 files changed, 71 insertions(+), 5 deletions(-) create mode 100644 passbook/core/migrations/0007_debugrule.py diff --git a/passbook/core/forms/rules.py b/passbook/core/forms/rules.py index c965ed4da..9b179acac 100644 --- a/passbook/core/forms/rules.py +++ b/passbook/core/forms/rules.py @@ -2,8 +2,9 @@ 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): """FieldMatcherRule Form""" @@ -11,8 +12,7 @@ class FieldMatcherRuleForm(forms.ModelForm): class Meta: model = FieldMatcherRule - fields = ['name', 'action', 'negate', 'order', - 'user_field', 'match_action', 'value', ] + fields = GENERAL_FIELDS + ['user_field', 'match_action', 'value', ] widgets = { 'name': forms.TextInput(), 'user_field': forms.TextInput(), @@ -26,11 +26,24 @@ class WebhookRuleForm(forms.ModelForm): class Meta: model = WebhookRule - fields = ['url', 'method', 'json_body', 'json_headers', - 'result_jsonpath', 'result_json_value', ] + fields = GENERAL_FIELDS + ['url', 'method', 'json_body', 'json_headers', + 'result_jsonpath', 'result_json_value', ] widgets = { + 'name': forms.TextInput(), 'json_body': forms.TextInput(), 'json_headers': forms.TextInput(), 'result_jsonpath': 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(), + } diff --git a/passbook/core/migrations/0007_debugrule.py b/passbook/core/migrations/0007_debugrule.py new file mode 100644 index 000000000..c18216d6f --- /dev/null +++ b/passbook/core/migrations/0007_debugrule.py @@ -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',), + ), + ] diff --git a/passbook/core/models.py b/passbook/core/models.py index 6e1fd7211..2fc1aac86 100644 --- a/passbook/core/models.py +++ b/passbook/core/models.py @@ -1,6 +1,8 @@ """passbook core models""" import re from logging import getLogger +from random import randrange +from time import sleep from uuid import uuid4 import reversion @@ -213,3 +215,26 @@ class WebhookRule(Rule): verbose_name = _('Webhook Rule') 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')