merchant

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

testing_api_cmd_post_webhooks.c (7437B)


      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_post_webhooks.c
     21  * @brief command to test POST /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/post-private-webhooks.h>
     30 
     31 
     32 /**
     33  * State of a "POST /webhooks" CMD.
     34  */
     35 struct PostWebhooksState
     36 {
     37 
     38   /**
     39    * Handle for a "GET webhook" request.
     40    */
     41   struct TALER_MERCHANT_PostPrivateWebhooksHandle *iph;
     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    * ID of the webhook to run POST for.
     55    */
     56   const char *webhook_id;
     57 
     58   /**
     59    * event of the webhook
     60    */
     61   const char *event_type;
     62 
     63   /**
     64    * url use by the customer
     65    */
     66   const char *url;
     67 
     68   /**
     69   * http_method use by the merchant
     70   */
     71   const char *http_method;
     72 
     73   /**
     74   * header of the webhook
     75   */
     76   const char *header_template;
     77 
     78   /**
     79   * body of the webhook
     80   */
     81   const char *body_template;
     82 
     83   /**
     84    * Expected HTTP response code.
     85    */
     86   unsigned int http_status;
     87 
     88 };
     89 
     90 
     91 /**
     92  * Callback for a POST /webhooks operation.
     93  *
     94  * @param cls closure for this function
     95  * @param hr response being processed
     96  */
     97 static void
     98 post_webhooks_cb (void *cls,
     99                   const struct TALER_MERCHANT_PostPrivateWebhooksResponse *wpr)
    100 {
    101   struct PostWebhooksState *wis = cls;
    102 
    103   wis->iph = NULL;
    104   if (wis->http_status != wpr->hr.http_status)
    105   {
    106     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    107                 "Unexpected response code %u (%d) to command %s\n",
    108                 wpr->hr.http_status,
    109                 (int) wpr->hr.ec,
    110                 TALER_TESTING_interpreter_get_current_label (wis->is));
    111     TALER_TESTING_interpreter_fail (wis->is);
    112     return;
    113   }
    114   switch (wpr->hr.http_status)
    115   {
    116   case MHD_HTTP_NO_CONTENT:
    117     break;
    118   case MHD_HTTP_UNAUTHORIZED:
    119     break;
    120   case MHD_HTTP_FORBIDDEN:
    121     break;
    122   case MHD_HTTP_NOT_FOUND:
    123     break;
    124   case MHD_HTTP_CONFLICT:
    125     break;
    126   default:
    127     GNUNET_break (0);
    128     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
    129                 "Unhandled HTTP status %u for POST /templates.\n",
    130                 wpr->hr.http_status);
    131   }
    132   TALER_TESTING_interpreter_next (wis->is);
    133 }
    134 
    135 
    136 /**
    137  * Run the "POST /webhooks" CMD.
    138  *
    139  *
    140  * @param cls closure.
    141  * @param cmd command being run now.
    142  * @param is interpreter state.
    143  */
    144 static void
    145 post_webhooks_run (void *cls,
    146                    const struct TALER_TESTING_Command *cmd,
    147                    struct TALER_TESTING_Interpreter *is)
    148 {
    149   struct PostWebhooksState *wis = cls;
    150 
    151   wis->is = is;
    152   wis->iph = TALER_MERCHANT_post_private_webhooks_create (
    153     TALER_TESTING_interpreter_get_context (is),
    154     wis->merchant_url,
    155     wis->webhook_id,
    156     wis->event_type,
    157     wis->url,
    158     wis->http_method);
    159   if (NULL != wis->header_template)
    160     TALER_MERCHANT_post_private_webhooks_set_options (
    161       wis->iph,
    162       TALER_MERCHANT_post_private_webhooks_option_header_template (
    163         wis->header_template));
    164   if (NULL != wis->body_template)
    165     TALER_MERCHANT_post_private_webhooks_set_options (
    166       wis->iph,
    167       TALER_MERCHANT_post_private_webhooks_option_body_template (
    168         wis->body_template));
    169   {
    170     enum TALER_ErrorCode ec;
    171 
    172     ec = TALER_MERCHANT_post_private_webhooks_start (
    173       wis->iph,
    174       &post_webhooks_cb,
    175       wis);
    176     GNUNET_assert (TALER_EC_NONE == ec);
    177   }
    178 }
    179 
    180 
    181 /**
    182  * Offers information from the POST /webhooks CMD state to other
    183  * commands.
    184  *
    185  * @param cls closure
    186  * @param[out] ret result (could be anything)
    187  * @param trait name of the trait
    188  * @param index index number of the object to extract.
    189  * @return #GNUNET_OK on success
    190  */
    191 static int
    192 post_webhooks_traits (void *cls,
    193                       const void **ret,
    194                       const char *trait,
    195                       unsigned int index)
    196 {
    197   struct PostWebhooksState *pws = cls;
    198   struct TALER_TESTING_Trait traits[] = {
    199     TALER_TESTING_make_trait_event_type (pws->event_type),
    200     TALER_TESTING_make_trait_url (pws->url),
    201     TALER_TESTING_make_trait_http_method (pws->http_method),
    202     TALER_TESTING_make_trait_header_template (pws->header_template),
    203     TALER_TESTING_make_trait_body_template (pws->body_template),
    204     TALER_TESTING_make_trait_webhook_id (pws->webhook_id),
    205     TALER_TESTING_trait_end (),
    206   };
    207 
    208   return TALER_TESTING_get_trait (traits,
    209                                   ret,
    210                                   trait,
    211                                   index);
    212 }
    213 
    214 
    215 /**
    216  * Free the state of a "POST webhook" CMD, and possibly
    217  * cancel a pending operation thereof.
    218  *
    219  * @param cls closure.
    220  * @param cmd command being run.
    221  */
    222 static void
    223 post_webhooks_cleanup (void *cls,
    224                        const struct TALER_TESTING_Command *cmd)
    225 {
    226   struct PostWebhooksState *wis = cls;
    227 
    228   if (NULL != wis->iph)
    229   {
    230     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
    231                 "POST /webhooks operation did not complete\n");
    232     TALER_MERCHANT_post_private_webhooks_cancel (wis->iph);
    233   }
    234   GNUNET_free (wis);
    235 }
    236 
    237 
    238 struct TALER_TESTING_Command
    239 TALER_TESTING_cmd_merchant_post_webhooks2 (
    240   const char *label,
    241   const char *merchant_url,
    242   const char *webhook_id,
    243   const char *event_type,
    244   const char *url,
    245   const char *http_method,
    246   const char *header_template,
    247   const char *body_template,
    248   unsigned int http_status)
    249 {
    250   struct PostWebhooksState *wis;
    251 
    252   wis = GNUNET_new (struct PostWebhooksState);
    253   wis->merchant_url = merchant_url;
    254   wis->webhook_id = webhook_id;
    255   wis->http_status = http_status;
    256   wis->event_type = event_type;
    257   wis->url = url;
    258   wis->http_method = http_method;
    259   wis->header_template = (NULL==header_template) ? NULL : header_template;
    260   wis->body_template = (NULL==body_template) ? NULL : body_template;
    261   {
    262     struct TALER_TESTING_Command cmd = {
    263       .cls = wis,
    264       .label = label,
    265       .run = &post_webhooks_run,
    266       .cleanup = &post_webhooks_cleanup,
    267       .traits = &post_webhooks_traits
    268     };
    269 
    270     return cmd;
    271   }
    272 }
    273 
    274 
    275 struct TALER_TESTING_Command
    276 TALER_TESTING_cmd_merchant_post_webhooks (const char *label,
    277                                           const char *merchant_url,
    278                                           const char *webhook_id,
    279                                           const char *event_type,
    280                                           unsigned int http_status)
    281 {
    282   return TALER_TESTING_cmd_merchant_post_webhooks2 (
    283     label,
    284     merchant_url,
    285     webhook_id,
    286     event_type,
    287     "http://localhost:12345/",
    288     "POST",
    289     "Taler-test-header: EFEHYJS-Bakery",
    290     "5.0 EUR",
    291     http_status);
    292 }
    293 
    294 
    295 /* end of testing_api_cmd_post_webhooks.c */