LCOV - code coverage report
Current view: top level - testing - testing_api_cmd_patch_webhook.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 43 56 76.8 %
Date: 2025-06-23 16:22:09 Functions: 5 5 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_patch_webhook.c
      21             :  * @brief command to test PATCH /webhook
      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 "PATCH /webhook" CMD.
      33             :  */
      34             : struct PatchWebhookState
      35             : {
      36             : 
      37             :   /**
      38             :    * Handle for a "GET webhook" request.
      39             :    */
      40             :   struct TALER_MERCHANT_WebhookPatchHandle *iph;
      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 GET for.
      54             :    */
      55             :   const char *webhook_id;
      56             : 
      57             :   /**
      58             :    * event of the webhook
      59             :    */
      60             :   const char *event_type;
      61             : 
      62             :   /**
      63             :    * url use by the customer
      64             :    */
      65             :   const char *url;
      66             : 
      67             :   /**
      68             :    * http_method use by the merchant
      69             :    */
      70             :   const char *http_method;
      71             : 
      72             :   /**
      73             :    * header of the webhook
      74             :    */
      75             :   const char *header_template;
      76             : 
      77             :   /**
      78             :    * body_template of the webhook
      79             :    */
      80             :   const char *body_template;
      81             : 
      82             :   /**
      83             :    * Expected HTTP response code.
      84             :    */
      85             :   unsigned int http_status;
      86             : 
      87             : };
      88             : 
      89             : 
      90             : /**
      91             :  * Callback for a PATCH /webhooks/$ID operation.
      92             :  *
      93             :  * @param cls closure for this function
      94             :  * @param hr response being processed
      95             :  */
      96             : static void
      97           4 : patch_webhook_cb (void *cls,
      98             :                   const struct TALER_MERCHANT_HttpResponse *hr)
      99             : {
     100           4 :   struct PatchWebhookState *pis = cls;
     101             : 
     102           4 :   pis->iph = NULL;
     103           4 :   if (pis->http_status != hr->http_status)
     104             :   {
     105           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     106             :                 "Unexpected response code %u (%d) to command %s\n",
     107             :                 hr->http_status,
     108             :                 (int) hr->ec,
     109             :                 TALER_TESTING_interpreter_get_current_label (pis->is));
     110           0 :     TALER_TESTING_interpreter_fail (pis->is);
     111           0 :     return;
     112             :   }
     113           4 :   switch (hr->http_status)
     114             :   {
     115           2 :   case MHD_HTTP_NO_CONTENT:
     116           2 :     break;
     117           0 :   case MHD_HTTP_UNAUTHORIZED:
     118           0 :     break;
     119           0 :   case MHD_HTTP_FORBIDDEN:
     120           0 :     break;
     121           2 :   case MHD_HTTP_NOT_FOUND:
     122           2 :     break;
     123           0 :   case MHD_HTTP_CONFLICT:
     124           0 :     break;
     125           0 :   default:
     126           0 :     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
     127             :                 "Unhandled HTTP status %u for PATCH /webhooks/ID.\n",
     128             :                 hr->http_status);
     129             :   }
     130           4 :   TALER_TESTING_interpreter_next (pis->is);
     131             : }
     132             : 
     133             : 
     134             : /**
     135             :  * Run the "PATCH /webhooks/$ID" CMD.
     136             :  *
     137             :  *
     138             :  * @param cls closure.
     139             :  * @param cmd command being run now.
     140             :  * @param is interpreter state.
     141             :  */
     142             : static void
     143           4 : patch_webhook_run (void *cls,
     144             :                    const struct TALER_TESTING_Command *cmd,
     145             :                    struct TALER_TESTING_Interpreter *is)
     146             : {
     147           4 :   struct PatchWebhookState *pis = cls;
     148             : 
     149           4 :   pis->is = is;
     150           4 :   pis->iph = TALER_MERCHANT_webhook_patch (
     151             :     TALER_TESTING_interpreter_get_context (is),
     152             :     pis->merchant_url,
     153             :     pis->webhook_id,
     154             :     pis->event_type,
     155             :     pis->url,
     156             :     pis->http_method,
     157             :     pis->header_template,
     158             :     pis->body_template,
     159             :     &patch_webhook_cb,
     160             :     pis);
     161           4 :   GNUNET_assert (NULL != pis->iph);
     162           4 : }
     163             : 
     164             : 
     165             : /**
     166             :  * Offers information from the PATCH /webhooks CMD state to other
     167             :  * commands.
     168             :  *
     169             :  * @param cls closure
     170             :  * @param[out] ret result (could be anything)
     171             :  * @param trait name of the trait
     172             :  * @param index index number of the object to extract.
     173             :  * @return #GNUNET_OK on success
     174             :  */
     175             : static enum GNUNET_GenericReturnValue
     176          10 : patch_webhook_traits (void *cls,
     177             :                       const void **ret,
     178             :                       const char *trait,
     179             :                       unsigned int index)
     180             : {
     181          10 :   struct PatchWebhookState *pws = cls;
     182             :   struct TALER_TESTING_Trait traits[] = {
     183          10 :     TALER_TESTING_make_trait_event_type (pws->event_type),
     184          10 :     TALER_TESTING_make_trait_url (pws->url),
     185          10 :     TALER_TESTING_make_trait_http_method (pws->http_method),
     186          10 :     TALER_TESTING_make_trait_header_template (pws->header_template),
     187          10 :     TALER_TESTING_make_trait_body_template (pws->body_template),
     188          10 :     TALER_TESTING_make_trait_webhook_id (pws->webhook_id),
     189          10 :     TALER_TESTING_trait_end (),
     190             :   };
     191             : 
     192          10 :   return TALER_TESTING_get_trait (traits,
     193             :                                   ret,
     194             :                                   trait,
     195             :                                   index);
     196             : }
     197             : 
     198             : 
     199             : /**
     200             :  * Free the state of a "GET webhook" CMD, and possibly
     201             :  * cancel a pending operation thereof.
     202             :  *
     203             :  * @param cls closure.
     204             :  * @param cmd command being run.
     205             :  */
     206             : static void
     207           4 : patch_webhook_cleanup (void *cls,
     208             :                        const struct TALER_TESTING_Command *cmd)
     209             : {
     210           4 :   struct PatchWebhookState *pis = cls;
     211             : 
     212           4 :   if (NULL != pis->iph)
     213             :   {
     214           0 :     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
     215             :                 "PATCH /webhooks/$ID operation did not complete\n");
     216           0 :     TALER_MERCHANT_webhook_patch_cancel (pis->iph);
     217             :   }
     218           4 :   GNUNET_free (pis);
     219           4 : }
     220             : 
     221             : 
     222             : struct TALER_TESTING_Command
     223           4 : TALER_TESTING_cmd_merchant_patch_webhook (
     224             :   const char *label,
     225             :   const char *merchant_url,
     226             :   const char *webhook_id,
     227             :   const char *event_type,
     228             :   const char *url,
     229             :   const char *http_method,
     230             :   const char *header_template,
     231             :   const char *body_template,
     232             :   unsigned int http_status)
     233             : {
     234             :   struct PatchWebhookState *pis;
     235             : 
     236           4 :   pis = GNUNET_new (struct PatchWebhookState);
     237           4 :   pis->merchant_url = merchant_url;
     238           4 :   pis->webhook_id = webhook_id;
     239           4 :   pis->http_status = http_status;
     240           4 :   pis->event_type = event_type;
     241           4 :   pis->url = url;
     242           4 :   pis->http_method = http_method;
     243           4 :   pis->header_template = (NULL == header_template) ? NULL : header_template;
     244           4 :   pis->body_template = (NULL == body_template) ? NULL : body_template;
     245             :   {
     246           4 :     struct TALER_TESTING_Command cmd = {
     247             :       .cls = pis,
     248             :       .label = label,
     249             :       .run = &patch_webhook_run,
     250             :       .cleanup = &patch_webhook_cleanup,
     251             :       .traits = &patch_webhook_traits
     252             :     };
     253             : 
     254           4 :     return cmd;
     255             :   }
     256             : }
     257             : 
     258             : 
     259             : /* end of testing_api_cmd_patch_webhook.c */

Generated by: LCOV version 1.16