merchant

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

testing_api_cmd_get_webhook.c (8377B)


      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 src/testing/testing_api_cmd_get_webhook.c
     21  * @brief command to test GET /webhooks/$ID
     22  * @author Priscilla HUANG
     23  */
     24 #include "platform.h"
     25 struct GetWebhookState;
     26 #define TALER_MERCHANT_GET_PRIVATE_WEBHOOK_RESULT_CLOSURE struct GetWebhookState
     27 #include <taler/taler_exchange_service.h>
     28 #include <taler/taler_testing_lib.h>
     29 #include "taler/taler_merchant_service.h"
     30 #include "taler/taler_merchant_testing_lib.h"
     31 #include <taler/merchant/get-private-webhooks-WEBHOOK_ID.h>
     32 
     33 
     34 /**
     35  * State of a "GET webhook" CMD.
     36  */
     37 struct GetWebhookState
     38 {
     39 
     40   /**
     41    * Handle for a "GET webhook" request.
     42    */
     43   struct TALER_MERCHANT_GetPrivateWebhookHandle *igh;
     44 
     45   /**
     46    * The interpreter state.
     47    */
     48   struct TALER_TESTING_Interpreter *is;
     49 
     50   /**
     51    * Base URL of the merchant serving the request.
     52    */
     53   const char *merchant_url;
     54 
     55   /**
     56    * ID of the webhook to run GET for.
     57    */
     58   const char *webhook_id;
     59 
     60   /**
     61    * Reference for a POST or PATCH /webhooks CMD (optional).
     62    */
     63   const char *webhook_reference;
     64 
     65   /**
     66    * Expected HTTP response code.
     67    */
     68   unsigned int http_status;
     69 
     70 };
     71 
     72 
     73 /**
     74  * Callback for a /get/webhooks/$ID operation.
     75  *
     76  * @param cls closure for this function
     77  * @param wgr response details
     78  */
     79 static void
     80 get_webhook_cb (struct GetWebhookState *gis,
     81                 const struct TALER_MERCHANT_GetPrivateWebhookResponse *wgr)
     82 {
     83   const struct TALER_TESTING_Command *webhook_cmd;
     84 
     85   gis->igh = NULL;
     86   if (gis->http_status != wgr->hr.http_status)
     87   {
     88     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     89                 "Unexpected response code %u (%d) to command %s\n",
     90                 wgr->hr.http_status,
     91                 (int) wgr->hr.ec,
     92                 TALER_TESTING_interpreter_get_current_label (gis->is));
     93     TALER_TESTING_interpreter_fail (gis->is);
     94     return;
     95   }
     96   switch (wgr->hr.http_status)
     97   {
     98   case MHD_HTTP_OK:
     99     {
    100       const char *expected_event_type;
    101 
    102       webhook_cmd = TALER_TESTING_interpreter_lookup_command (
    103         gis->is,
    104         gis->webhook_reference);
    105       if (GNUNET_OK !=
    106           TALER_TESTING_get_trait_event_type (webhook_cmd,
    107                                               &expected_event_type))
    108         TALER_TESTING_interpreter_fail (gis->is);
    109       if (0 != strcmp (wgr->details.ok.event_type,
    110                        expected_event_type))
    111       {
    112         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    113                     "Event type does not match\n");
    114         TALER_TESTING_interpreter_fail (gis->is);
    115         return;
    116       }
    117     }
    118     {
    119       const char *expected_url;
    120 
    121       if (GNUNET_OK !=
    122           TALER_TESTING_get_trait_url (webhook_cmd,
    123                                        &expected_url))
    124         TALER_TESTING_interpreter_fail (gis->is);
    125       if (0 != strcmp (wgr->details.ok.url,
    126                        expected_url))
    127       {
    128         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    129                     "URL does not match\n");
    130         TALER_TESTING_interpreter_fail (gis->is);
    131         return;
    132       }
    133     }
    134     {
    135       const char *expected_http_method;
    136 
    137       if (GNUNET_OK !=
    138           TALER_TESTING_get_trait_http_method (webhook_cmd,
    139                                                &expected_http_method))
    140         TALER_TESTING_interpreter_fail (gis->is);
    141       if (0 != strcmp (wgr->details.ok.http_method,
    142                        expected_http_method))
    143       {
    144         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    145                     "http_method does not match\n");
    146         TALER_TESTING_interpreter_fail (gis->is);
    147         return;
    148       }
    149     }
    150     {
    151       const char *expected_header_template;
    152 
    153       if (GNUNET_OK !=
    154           TALER_TESTING_get_trait_header_template (webhook_cmd,
    155                                                    &expected_header_template))
    156         TALER_TESTING_interpreter_fail (gis->is);
    157       if ( ( (NULL == wgr->details.ok.header_template) && (NULL !=
    158                                                            expected_header_template))
    159            ||
    160            ( (NULL != wgr->details.ok.header_template) && (NULL ==
    161                                                            expected_header_template))
    162            ||
    163            ( (NULL != wgr->details.ok.header_template) &&
    164              (0 != strcmp (wgr->details.ok.header_template,
    165                            expected_header_template)) ) )
    166       {
    167         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    168                     "header template does not match\n");
    169         TALER_TESTING_interpreter_fail (gis->is);
    170         return;
    171       }
    172     }
    173     {
    174       const char *expected_body_template;
    175 
    176       if (GNUNET_OK !=
    177           TALER_TESTING_get_trait_body_template (webhook_cmd,
    178                                                  &expected_body_template))
    179         TALER_TESTING_interpreter_fail (gis->is);
    180       if ( ( (NULL == wgr->details.ok.body_template) && (NULL !=
    181                                                          expected_body_template)
    182              ) ||
    183            ( (NULL != wgr->details.ok.body_template) && (NULL ==
    184                                                          expected_body_template)
    185            ) ||
    186            ( (NULL != wgr->details.ok.body_template) &&
    187              (0 != strcmp (wgr->details.ok.body_template,
    188                            expected_body_template)) ) )
    189       {
    190         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    191                     "body template does not match\n");
    192         TALER_TESTING_interpreter_fail (gis->is);
    193         return;
    194       }
    195     }
    196     break;
    197   case MHD_HTTP_UNAUTHORIZED:
    198     break;
    199   case MHD_HTTP_NOT_FOUND:
    200     break;
    201   default:
    202     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
    203                 "Unhandled HTTP status.\n");
    204   }
    205   TALER_TESTING_interpreter_next (gis->is);
    206 }
    207 
    208 
    209 /**
    210  * Run the "GET webhook" CMD.
    211  *
    212  *
    213  * @param cls closure.
    214  * @param cmd command being run now.
    215  * @param is interpreter state.
    216  */
    217 static void
    218 get_webhook_run (void *cls,
    219                  const struct TALER_TESTING_Command *cmd,
    220                  struct TALER_TESTING_Interpreter *is)
    221 {
    222   struct GetWebhookState *gis = cls;
    223 
    224   gis->is = is;
    225   gis->igh = TALER_MERCHANT_get_private_webhook_create (
    226     TALER_TESTING_interpreter_get_context (is),
    227     gis->merchant_url,
    228     gis->webhook_id);
    229   {
    230     enum TALER_ErrorCode ec;
    231 
    232     ec = TALER_MERCHANT_get_private_webhook_start (
    233       gis->igh,
    234       &get_webhook_cb,
    235       gis);
    236     GNUNET_assert (TALER_EC_NONE == ec);
    237   }
    238 }
    239 
    240 
    241 /**
    242  * Free the state of a "GET webhook" CMD, and possibly
    243  * cancel a pending operation thereof.
    244  *
    245  * @param cls closure.
    246  * @param cmd command being run.
    247  */
    248 static void
    249 get_webhook_cleanup (void *cls,
    250                      const struct TALER_TESTING_Command *cmd)
    251 {
    252   struct GetWebhookState *gis = cls;
    253 
    254   if (NULL != gis->igh)
    255   {
    256     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
    257                 "GET /webhooks/$ID operation did not complete\n");
    258     TALER_MERCHANT_get_private_webhook_cancel (gis->igh);
    259   }
    260   GNUNET_free (gis);
    261 }
    262 
    263 
    264 struct TALER_TESTING_Command
    265 TALER_TESTING_cmd_merchant_get_webhook (const char *label,
    266                                         const char *merchant_url,
    267                                         const char *webhook_id,
    268                                         unsigned int http_status,
    269                                         const char *webhook_reference)
    270 {
    271   struct GetWebhookState *gis;
    272 
    273   gis = GNUNET_new (struct GetWebhookState);
    274   gis->merchant_url = merchant_url;
    275   gis->webhook_id = webhook_id;
    276   gis->http_status = http_status;
    277   gis->webhook_reference = webhook_reference;
    278   {
    279     struct TALER_TESTING_Command cmd = {
    280       .cls = gis,
    281       .label = label,
    282       .run = &get_webhook_run,
    283       .cleanup = &get_webhook_cleanup
    284     };
    285 
    286     return cmd;
    287   }
    288 }
    289 
    290 
    291 /* end of testing_api_cmd_get_webhook.c */