LCOV - code coverage report
Current view: top level - testing - testing_api_cmd_get_webhook.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 63.8 % 80 51
Test Date: 2025-11-13 17:46:01 Functions: 100.0 % 4 4

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

Generated by: LCOV version 2.0-1