LCOV - code coverage report
Current view: top level - testing - testing_api_cmd_delete_webhook.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 28 39 71.8 %
Date: 2025-06-23 16:22:09 Functions: 4 4 100.0 %

          Line data    Source code
       1             : /*
       2             :   This file is part of TALER
       3             :   Copyright (C) 2022 Taler Systems SA
       4             : 
       5             :   TALER is free software; you can redistribute it and/or modify
       6             :   it under the terms of the GNU General Public License as
       7             :   published by the Free Software Foundation; either version 3, or
       8             :   (at your option) any later version.
       9             : 
      10             :   TALER is distributed in the hope that it will be useful, but
      11             :   WITHOUT ANY WARRANTY; without even the implied warranty of
      12             :   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      13             :   GNU General Public License for more details.
      14             : 
      15             :   You should have received a copy of the GNU General Public
      16             :   License along with TALER; see the file COPYING.  If not, see
      17             :   <http://www.gnu.org/licenses/>
      18             : */
      19             : /**
      20             :  * @file testing_api_cmd_delete_webhook.c
      21             :  * @brief command to test DELETE /webhooks/$ID
      22             :  * @author Priscilla HUANG
      23             :  */
      24             : #include "platform.h"
      25             : #include <taler/taler_exchange_service.h>
      26             : #include <taler/taler_testing_lib.h>
      27             : #include "taler_merchant_service.h"
      28             : #include "taler_merchant_testing_lib.h"
      29             : 
      30             : 
      31             : /**
      32             :  * State of a "DELETE /webhooks/$ID" CMD.
      33             :  */
      34             : struct DeleteWebhookState
      35             : {
      36             : 
      37             :   /**
      38             :    * Handle for a "DELETE webhook" request.
      39             :    */
      40             :   struct TALER_MERCHANT_WebhookDeleteHandle *wdh;
      41             : 
      42             :   /**
      43             :    * The interpreter state.
      44             :    */
      45             :   struct TALER_TESTING_Interpreter *is;
      46             : 
      47             :   /**
      48             :    * Base URL of the merchant serving the request.
      49             :    */
      50             :   const char *merchant_url;
      51             : 
      52             :   /**
      53             :    * ID of the webhook to run DELETE for.
      54             :    */
      55             :   const char *webhook_id;
      56             : 
      57             :   /**
      58             :    * Expected HTTP response code.
      59             :    */
      60             :   unsigned int http_status;
      61             : 
      62             : };
      63             : 
      64             : 
      65             : /**
      66             :  * Callback for a /delete/webhooks/$ID operation.
      67             :  *
      68             :  * @param cls closure for this function
      69             :  * @param hr response being processed
      70             :  */
      71             : static void
      72           6 : delete_webhook_cb (void *cls,
      73             :                    const struct TALER_MERCHANT_HttpResponse *hr)
      74             : {
      75           6 :   struct DeleteWebhookState *dis = cls;
      76             : 
      77           6 :   dis->wdh = NULL;
      78           6 :   if (dis->http_status != hr->http_status)
      79             :   {
      80           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
      81             :                 "Unexpected response code %u (%d) to command %s\n",
      82             :                 hr->http_status,
      83             :                 (int) hr->ec,
      84             :                 TALER_TESTING_interpreter_get_current_label (dis->is));
      85           0 :     TALER_TESTING_interpreter_fail (dis->is);
      86           0 :     return;
      87             :   }
      88           6 :   switch (hr->http_status)
      89             :   {
      90           4 :   case MHD_HTTP_NO_CONTENT:
      91           4 :     break;
      92           0 :   case MHD_HTTP_UNAUTHORIZED:
      93           0 :     break;
      94           2 :   case MHD_HTTP_NOT_FOUND:
      95           2 :     break;
      96           0 :   case MHD_HTTP_CONFLICT:
      97           0 :     break;
      98           0 :   default:
      99           0 :     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
     100             :                 "Unhandled HTTP status %u for DELETE webhook.\n",
     101             :                 hr->http_status);
     102             :   }
     103           6 :   TALER_TESTING_interpreter_next (dis->is);
     104             : }
     105             : 
     106             : 
     107             : /**
     108             :  * Run the "DELETE webhook" CMD.
     109             :  *
     110             :  *
     111             :  * @param cls closure.
     112             :  * @param cmd command being run now.
     113             :  * @param is interpreter state.
     114             :  */
     115             : static void
     116           6 : delete_webhook_run (void *cls,
     117             :                     const struct TALER_TESTING_Command *cmd,
     118             :                     struct TALER_TESTING_Interpreter *is)
     119             : {
     120           6 :   struct DeleteWebhookState *dis = cls;
     121             : 
     122           6 :   dis->is = is;
     123           6 :   dis->wdh = TALER_MERCHANT_webhook_delete (
     124             :     TALER_TESTING_interpreter_get_context (is),
     125             :     dis->merchant_url,
     126             :     dis->webhook_id,
     127             :     &delete_webhook_cb,
     128             :     dis);
     129           6 :   GNUNET_assert (NULL != dis->wdh);
     130           6 : }
     131             : 
     132             : 
     133             : /**
     134             :  * Free the state of a "DELETE webhook" CMD, and possibly
     135             :  * cancel a pending operation thereof.
     136             :  *
     137             :  * @param cls closure.
     138             :  * @param cmd command being run.
     139             :  */
     140             : static void
     141           6 : delete_webhook_cleanup (void *cls,
     142             :                         const struct TALER_TESTING_Command *cmd)
     143             : {
     144           6 :   struct DeleteWebhookState *dis = cls;
     145             : 
     146           6 :   if (NULL != dis->wdh)
     147             :   {
     148           0 :     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
     149             :                 "DELETE /webhooks/$ID operation did not complete\n");
     150           0 :     TALER_MERCHANT_webhook_delete_cancel (dis->wdh);
     151             :   }
     152           6 :   GNUNET_free (dis);
     153           6 : }
     154             : 
     155             : 
     156             : struct TALER_TESTING_Command
     157           6 : TALER_TESTING_cmd_merchant_delete_webhook (const char *label,
     158             :                                            const char *merchant_url,
     159             :                                            const char *webhook_id,
     160             :                                            unsigned int http_status)
     161             : {
     162             :   struct DeleteWebhookState *dis;
     163             : 
     164           6 :   dis = GNUNET_new (struct DeleteWebhookState);
     165           6 :   dis->merchant_url = merchant_url;
     166           6 :   dis->webhook_id = webhook_id;
     167           6 :   dis->http_status = http_status;
     168             :   {
     169           6 :     struct TALER_TESTING_Command cmd = {
     170             :       .cls = dis,
     171             :       .label = label,
     172             :       .run = &delete_webhook_run,
     173             :       .cleanup = &delete_webhook_cleanup
     174             :     };
     175             : 
     176           6 :     return cmd;
     177             :   }
     178             : }
     179             : 
     180             : 
     181             : /* end of testing_api_cmd_delete_webhook.c */

Generated by: LCOV version 1.16