merchant

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

testing_api_cmd_get_webhooks.c (5899B)


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