Repository: privacyidea/privacyidea

Issue:
Handler condition does not work for failing enrollment
It seems like we have no condition for a failed enrollment request.

``result_value = false``

is not contained. We have a result->error. But maybe we should add

``result_status = false|true``


Discussion:

Resolving patch:
diff --git a/privacyidea/lib/eventhandler/base.py b/privacyidea/lib/eventhandler/base.py
--- a/privacyidea/lib/eventhandler/base.py
+++ b/privacyidea/lib/eventhandler/base.py
@@ -39,7 +39,7 @@
 from privacyidea.lib.token import get_token_owner, get_tokens
 from privacyidea.lib.user import User, UserError
 from privacyidea.lib.utils import (compare_condition, compare_value_value,
-                                   parse_time_offset_from_now)
+                                   parse_time_offset_from_now, is_true)
 import datetime
 from dateutil.tz import tzlocal
 import re
@@ -67,6 +67,8 @@ class CONDITION(object):
     TOKENINFO = "tokeninfo"
     DETAIL_ERROR_MESSAGE = "detail_error_message"
     DETAIL_MESSAGE = "detail_message"
+    RESULT_VALUE = "result_value"
+    RESULT_STATUS = "result_status"
 
 
 class BaseEventHandler(object):
@@ -129,12 +131,18 @@ def conditions(cls):
                 "desc": _("The logged in user is of the following type."),
                 "value": (ROLE.ADMIN, ROLE.USER)
             },
-            "result_value": {
+            CONDITION.RESULT_VALUE: {
                 "type": "str",
                 "desc": _("The result.value within the response is "
                           "True or False."),
                 "value": ("True", "False")
             },
+            CONDITION.RESULT_STATUS: {
+                "type": "str",
+                "desc": _("The result.status within the response is "
+                          "True or False."),
+                "value": ("True", "False")
+            },
             "token_locked": {
                 "type": "str",
                 "desc": _("Check if the max failcounter of the token is "
@@ -308,10 +316,16 @@ def check_condition(self, options):
             if user_role != conditions.get("logged_in_user"):
                 return False
 
-        if "result_value" in conditions:
-            condition_value = conditions.get("result_value")
+        if CONDITION.RESULT_VALUE in conditions:
+            condition_value = conditions.get(CONDITION.RESULT_VALUE)
             result_value = content.get("result", {}).get("value")
-            if condition_value != str(result_value):
+            if is_true(condition_value) != is_true(result_value):
+                return False
+
+        if CONDITION.RESULT_STATUS in conditions:
+            condition_value = conditions.get(CONDITION.RESULT_STATUS)
+            result_status = content.get("result", {}).get("status")
+            if is_true(condition_value) != is_true(result_status):
                 return False
 
         # checking of max-failcounter state of the token


Test patch:
diff --git a/tests/test_lib_events.py b/tests/test_lib_events.py
--- a/tests/test_lib_events.py
+++ b/tests/test_lib_events.py
@@ -1447,7 +1447,8 @@ def test_05_check_conditions(self):
 
         uhandler = UserNotificationEventHandler()
         resp = Response()
-        resp.data = """{"result": {"value": false}}"""
+        # The actual result_status is false and the result_value is false.
+        resp.data = """{"result": {"value": false, "status": false}}"""
         builder = EnvironBuilder(method='POST')
         env = builder.get_environ()
         req = Request(env)
@@ -1460,13 +1461,38 @@ def test_05_check_conditions(self):
              "request": req})
         self.assertEqual(r, False)
 
+        # We expect the result_value to be True, but it is not.
         r = uhandler.check_condition(
             {"g": {},
-             "handler_def": {"conditions": {"result_value": True}},
+             "handler_def": {"conditions": {"result_value": "True"}},
              "response": resp,
              "request": req})
         self.assertEqual(r, False)
 
+        # We expect the result_value to be False, and it is.
+        r = uhandler.check_condition(
+            {"g": {},
+             "handler_def": {"conditions": {"result_value": "False"}},
+             "response": resp,
+             "request": req})
+        self.assertEqual(r, True)
+
+        # We expect the result_status to be True, but it is not!
+        r = uhandler.check_condition(
+            {"g": {},
+             "handler_def": {"conditions": {"result_status": "True"}},
+             "response": resp,
+             "request": req})
+        self.assertEqual(r, False)
+
+        # We expect the result_status to be False, and it is!
+        r = uhandler.check_condition(
+            {"g": {},
+             "handler_def": {"conditions": {"result_status": "False"}},
+             "response": resp,
+             "request": req})
+        self.assertEqual(r, True)
+
         # check a locked token with maxfail = failcount
         builder = EnvironBuilder(method='POST',
                                  data={'serial': "OATH123456"},