Repository: privacyidea/privacyidea

Issue:
Failed token resync appears as succeeded in audit log
When a token resync request fails, the entry in the audit log is marked as "succeeded".
According to [`api/token.py`](https://github.com/privacyidea/privacyidea/blob/333d704397623ca20798f505b39f7278ba24416e/privacyidea/api/token.py#L618) the result of the resync is not checked.



Discussion:
I think marking this as "fail" in the audit log is totally ok.

So it probably would be something like:

~~~~python
res = resync_token(serial, otp1, otp2, user=user)
g.audit_object.log({"success": bool(res)})
return send_result(res)
~~~~

can you please change this and adapt the tests (if needed)?

Resolving patch:
diff --git a/privacyidea/api/token.py b/privacyidea/api/token.py
--- a/privacyidea/api/token.py
+++ b/privacyidea/api/token.py
@@ -613,7 +613,7 @@ def resync_api(serial=None):
     otp2 = getParam(request.all_data, "otp2", required)
 
     res = resync_token(serial, otp1, otp2, user=user)
-    g.audit_object.log({"success": True})
+    g.audit_object.log({"success": bool(res)})
     return send_result(res)
 
 


Test patch:
diff --git a/tests/test_api_token.py b/tests/test_api_token.py
--- a/tests/test_api_token.py
+++ b/tests/test_api_token.py
@@ -544,6 +544,18 @@ def test_07_resync(self):
             result = json.loads(res.data.decode('utf8')).get("result")
             self.assertTrue(result.get("value") is False, result)
 
+        # check that we have a failed request in the audit log
+        with self.app.test_request_context('/audit/',
+                                           method='GET',
+                                           data={'action': "POST /token/resync/<serial>",
+                                                 'serial': 'Resync01',
+                                                 'success': '0'},
+                                           headers={'Authorization': self.at}):
+            res = self.app.full_dispatch_request()
+            self.assertEquals(res.status_code, 200, res)
+            self.assertEquals(len(res.json['result']['value']['auditdata']), 1, res.json)
+            self.assertEquals(res.json['result']['value']['auditdata'][0]['success'], 0, res.json)
+
         # Successful resync with consecutive values
         with self.app.test_request_context('/token/resync',
                                             method="POST",
@@ -556,6 +568,18 @@ def test_07_resync(self):
             result = json.loads(res.data.decode('utf8')).get("result")
             self.assertTrue(result.get("value") is True, result)
 
+        # Check for a successful request in the audit log
+        with self.app.test_request_context('/audit/',
+                                           method='GET',
+                                           data={'action': "POST /token/resync",
+                                                 'serial': 'Resync01',
+                                                 'success': '1'},
+                                           headers={'Authorization': self.at}):
+            res = self.app.full_dispatch_request()
+            self.assertEquals(res.status_code, 200, res)
+            self.assertEquals(len(res.json['result']['value']['auditdata']), 1, res.json)
+            self.assertEquals(res.json['result']['value']['auditdata'][0]['success'], 1, res.json)
+
         # Get the OTP token and inspect the counter
         with self.app.test_request_context('/token/',
                                             method="GET",