merchant

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

testing_api_cmd_get_webhook.c (8537B)


      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       {
    109         TALER_TESTING_interpreter_fail (gis->is);
    110         return;
    111       }
    112       if (0 != strcmp (wgr->details.ok.event_type,
    113                        expected_event_type))
    114       {
    115         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    116                     "Event type does not match\n");
    117         TALER_TESTING_interpreter_fail (gis->is);
    118         return;
    119       }
    120     }
    121     {
    122       const char *expected_url;
    123 
    124       if (GNUNET_OK !=
    125           TALER_TESTING_get_trait_url (webhook_cmd,
    126                                        &expected_url))
    127       {
    128         TALER_TESTING_interpreter_fail (gis->is);
    129         return;
    130       }
    131       if (0 != strcmp (wgr->details.ok.url,
    132                        expected_url))
    133       {
    134         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    135                     "URL does not match\n");
    136         TALER_TESTING_interpreter_fail (gis->is);
    137         return;
    138       }
    139     }
    140     {
    141       const char *expected_http_method;
    142 
    143       if (GNUNET_OK !=
    144           TALER_TESTING_get_trait_http_method (webhook_cmd,
    145                                                &expected_http_method))
    146       {
    147         TALER_TESTING_interpreter_fail (gis->is);
    148         return;
    149       }
    150       if (0 != strcmp (wgr->details.ok.http_method,
    151                        expected_http_method))
    152       {
    153         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    154                     "http_method does not match\n");
    155         TALER_TESTING_interpreter_fail (gis->is);
    156         return;
    157       }
    158     }
    159     {
    160       const char *expected_header_template;
    161 
    162       if (GNUNET_OK !=
    163           TALER_TESTING_get_trait_header_template (webhook_cmd,
    164                                                    &expected_header_template))
    165       {
    166         TALER_TESTING_interpreter_fail (gis->is);
    167         return;
    168       }
    169       if ( ( (NULL == wgr->details.ok.header_template) && (NULL !=
    170                                                            expected_header_template))
    171            ||
    172            ( (NULL != wgr->details.ok.header_template) && (NULL ==
    173                                                            expected_header_template))
    174            ||
    175            ( (NULL != wgr->details.ok.header_template) &&
    176              (0 != strcmp (wgr->details.ok.header_template,
    177                            expected_header_template)) ) )
    178       {
    179         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    180                     "header template does not match\n");
    181         TALER_TESTING_interpreter_fail (gis->is);
    182         return;
    183       }
    184     }
    185     {
    186       const char *expected_body_template;
    187 
    188       if (GNUNET_OK !=
    189           TALER_TESTING_get_trait_body_template (webhook_cmd,
    190                                                  &expected_body_template))
    191       {
    192         TALER_TESTING_interpreter_fail (gis->is);
    193         return;
    194       }
    195       if ( ( (NULL == wgr->details.ok.body_template) && (NULL !=
    196                                                          expected_body_template)
    197              ) ||
    198            ( (NULL != wgr->details.ok.body_template) && (NULL ==
    199                                                          expected_body_template)
    200            ) ||
    201            ( (NULL != wgr->details.ok.body_template) &&
    202              (0 != strcmp (wgr->details.ok.body_template,
    203                            expected_body_template)) ) )
    204       {
    205         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    206                     "body template does not match\n");
    207         TALER_TESTING_interpreter_fail (gis->is);
    208         return;
    209       }
    210     }
    211     break;
    212   case MHD_HTTP_UNAUTHORIZED:
    213     break;
    214   case MHD_HTTP_NOT_FOUND:
    215     break;
    216   default:
    217     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
    218                 "Unhandled HTTP status.\n");
    219   }
    220   TALER_TESTING_interpreter_next (gis->is);
    221 }
    222 
    223 
    224 /**
    225  * Run the "GET webhook" CMD.
    226  *
    227  *
    228  * @param cls closure.
    229  * @param cmd command being run now.
    230  * @param is interpreter state.
    231  */
    232 static void
    233 get_webhook_run (void *cls,
    234                  const struct TALER_TESTING_Command *cmd,
    235                  struct TALER_TESTING_Interpreter *is)
    236 {
    237   struct GetWebhookState *gis = cls;
    238 
    239   gis->is = is;
    240   gis->igh = TALER_MERCHANT_get_private_webhook_create (
    241     TALER_TESTING_interpreter_get_context (is),
    242     gis->merchant_url,
    243     gis->webhook_id);
    244   {
    245     enum TALER_ErrorCode ec;
    246 
    247     ec = TALER_MERCHANT_get_private_webhook_start (
    248       gis->igh,
    249       &get_webhook_cb,
    250       gis);
    251     GNUNET_assert (TALER_EC_NONE == ec);
    252   }
    253 }
    254 
    255 
    256 /**
    257  * Free the state of a "GET webhook" CMD, and possibly
    258  * cancel a pending operation thereof.
    259  *
    260  * @param cls closure.
    261  * @param cmd command being run.
    262  */
    263 static void
    264 get_webhook_cleanup (void *cls,
    265                      const struct TALER_TESTING_Command *cmd)
    266 {
    267   struct GetWebhookState *gis = cls;
    268 
    269   if (NULL != gis->igh)
    270   {
    271     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
    272                 "GET /webhooks/$ID operation did not complete\n");
    273     TALER_MERCHANT_get_private_webhook_cancel (gis->igh);
    274   }
    275   GNUNET_free (gis);
    276 }
    277 
    278 
    279 struct TALER_TESTING_Command
    280 TALER_TESTING_cmd_merchant_get_webhook (const char *label,
    281                                         const char *merchant_url,
    282                                         const char *webhook_id,
    283                                         unsigned int http_status,
    284                                         const char *webhook_reference)
    285 {
    286   struct GetWebhookState *gis;
    287 
    288   gis = GNUNET_new (struct GetWebhookState);
    289   gis->merchant_url = merchant_url;
    290   gis->webhook_id = webhook_id;
    291   gis->http_status = http_status;
    292   gis->webhook_reference = webhook_reference;
    293   {
    294     struct TALER_TESTING_Command cmd = {
    295       .cls = gis,
    296       .label = label,
    297       .run = &get_webhook_run,
    298       .cleanup = &get_webhook_cleanup
    299     };
    300 
    301     return cmd;
    302   }
    303 }
    304 
    305 
    306 /* end of testing_api_cmd_get_webhook.c */