merchant

Merchant backend to process payments, run by merchants
Log | Files | Refs | Submodules | README | LICENSE

testing_api_cmd_get_webhooks.c (6126B)


      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_get_webhooks.c
     21  * @brief command to test GET /webhooks
     22  * @author Priscilla HUANG
     23  */
     24 #include "taler/platform.h"
     25 #include <taler/taler_exchange_service.h>
     26 #include <taler/taler_testing_lib.h>
     27 #include "taler/taler_merchant_service.h"
     28 #include "taler/taler_merchant_testing_lib.h"
     29 #include <taler/taler-merchant/get-private-webhooks.h>
     30 
     31 
     32 /**
     33  * State of a "GET webhooks" CMD.
     34  */
     35 struct GetWebhooksState
     36 {
     37 
     38   /**
     39    * Handle for a "GET webhook" request.
     40    */
     41   struct TALER_MERCHANT_GetPrivateWebhooksHandle *igh;
     42 
     43   /**
     44    * The interpreter state.
     45    */
     46   struct TALER_TESTING_Interpreter *is;
     47 
     48   /**
     49    * Base URL of the merchant serving the request.
     50    */
     51   const char *merchant_url;
     52 
     53   /**
     54    * Expected HTTP response code.
     55    */
     56   unsigned int http_status;
     57 
     58   /**
     59    * The list of webhook references.
     60    */
     61   const char **webhooks;
     62 
     63   /**
     64    * Length of @e webhooks.
     65    */
     66   unsigned int webhooks_length;
     67 
     68 };
     69 
     70 
     71 /**
     72  * Callback for a GET /webhooks operation.
     73  *
     74  * @param cls closure for this function
     75  * @param wgr response details
     76  */
     77 static void
     78 get_webhooks_cb (void *cls,
     79                  const struct TALER_MERCHANT_GetPrivateWebhooksResponse *wgr)
     80 {
     81   struct GetWebhooksState *gis = cls;
     82 
     83   gis->igh = NULL;
     84   if (gis->http_status != wgr->hr.http_status)
     85   {
     86     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     87                 "Unexpected response code %u (%d) to command %s\n",
     88                 wgr->hr.http_status,
     89                 (int) wgr->hr.ec,
     90                 TALER_TESTING_interpreter_get_current_label (gis->is));
     91     TALER_TESTING_interpreter_fail (gis->is);
     92     return;
     93   }
     94   switch (wgr->hr.http_status)
     95   {
     96   case MHD_HTTP_OK:
     97     if (wgr->details.ok.webhooks_length != gis->webhooks_length)
     98     {
     99       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    100                   "Length of webhooks found does not match\n");
    101       TALER_TESTING_interpreter_fail (gis->is);
    102       return;
    103     }
    104     for (unsigned int i = 0; i < gis->webhooks_length; ++i)
    105     {
    106       const struct TALER_TESTING_Command *webhook_cmd;
    107 
    108       webhook_cmd = TALER_TESTING_interpreter_lookup_command (
    109         gis->is,
    110         gis->webhooks[i]);
    111 
    112       {
    113         const char *webhook_id;
    114 
    115         if (GNUNET_OK !=
    116             TALER_TESTING_get_trait_webhook_id (webhook_cmd,
    117                                                 &webhook_id))
    118         {
    119           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    120                       "Could not fetch webhook id\n");
    121           TALER_TESTING_interpreter_fail (gis->is);
    122           return;
    123         }
    124         if (0 != strcmp (wgr->details.ok.webhooks[i].webhook_id,
    125                          webhook_id))
    126         {
    127           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    128                       "Webhook id does not match\n");
    129           TALER_TESTING_interpreter_fail (gis->is);
    130           return;
    131         }
    132       }
    133     }
    134     break;
    135   case MHD_HTTP_UNAUTHORIZED:
    136     break;
    137   case MHD_HTTP_NOT_FOUND:
    138     /* instance does not exist */
    139     break;
    140   default:
    141     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
    142                 "Unhandled HTTP status %u (%d).\n",
    143                 wgr->hr.http_status,
    144                 wgr->hr.ec);
    145   }
    146   TALER_TESTING_interpreter_next (gis->is);
    147 }
    148 
    149 
    150 /**
    151  * Run the "GET /webhooks" CMD.
    152  *
    153  *
    154  * @param cls closure.
    155  * @param cmd command being run now.
    156  * @param is interpreter state.
    157  */
    158 static void
    159 get_webhooks_run (void *cls,
    160                   const struct TALER_TESTING_Command *cmd,
    161                   struct TALER_TESTING_Interpreter *is)
    162 {
    163   struct GetWebhooksState *gis = cls;
    164 
    165   gis->is = is;
    166   gis->igh = TALER_MERCHANT_get_private_webhooks_create (
    167     TALER_TESTING_interpreter_get_context (is),
    168     gis->merchant_url);
    169   {
    170     enum TALER_ErrorCode ec;
    171 
    172     ec = TALER_MERCHANT_get_private_webhooks_start (
    173       gis->igh,
    174       &get_webhooks_cb,
    175       gis);
    176     GNUNET_assert (TALER_EC_NONE == ec);
    177   }
    178 }
    179 
    180 
    181 /**
    182  * Free the state of a "GET webhook" CMD, and possibly
    183  * cancel a pending operation thereof.
    184  *
    185  * @param cls closure.
    186  * @param cmd command being run.
    187  */
    188 static void
    189 get_webhooks_cleanup (void *cls,
    190                       const struct TALER_TESTING_Command *cmd)
    191 {
    192   struct GetWebhooksState *gis = cls;
    193 
    194   if (NULL != gis->igh)
    195   {
    196     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
    197                 "GET /webhooks operation did not complete\n");
    198     TALER_MERCHANT_get_private_webhooks_cancel (gis->igh);
    199   }
    200   GNUNET_array_grow (gis->webhooks,
    201                      gis->webhooks_length,
    202                      0);
    203   GNUNET_free (gis);
    204 }
    205 
    206 
    207 struct TALER_TESTING_Command
    208 TALER_TESTING_cmd_merchant_get_webhooks (const char *label,
    209                                          const char *merchant_url,
    210                                          unsigned int http_status,
    211                                          ...)
    212 {
    213   struct GetWebhooksState *gis;
    214 
    215   gis = GNUNET_new (struct GetWebhooksState);
    216   gis->merchant_url = merchant_url;
    217   gis->http_status = http_status;
    218   {
    219     const char *clabel;
    220     va_list ap;
    221 
    222     va_start (ap, http_status);
    223     while (NULL != (clabel = va_arg (ap, const char *)))
    224     {
    225       GNUNET_array_append (gis->webhooks,
    226                            gis->webhooks_length,
    227                            clabel);
    228     }
    229     va_end (ap);
    230   }
    231   {
    232     struct TALER_TESTING_Command cmd = {
    233       .cls = gis,
    234       .label = label,
    235       .run = &get_webhooks_run,
    236       .cleanup = &get_webhooks_cleanup
    237     };
    238 
    239     return cmd;
    240   }
    241 }
    242 
    243 
    244 /* end of testing_api_cmd_get_webhooks.c */