LCOV - code coverage report
Current view: top level - testing - testing_api_cmd_patch_template.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 42 55 76.4 %
Date: 2025-06-23 16:22:09 Functions: 5 5 100.0 %

          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_patch_template.c
      21             :  * @brief command to test PATCH /template
      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 "PATCH /template" CMD.
      33             :  */
      34             : struct PatchTemplateState
      35             : {
      36             : 
      37             :   /**
      38             :    * Handle for a "GET template" request.
      39             :    */
      40             :   struct TALER_MERCHANT_TemplatePatchHandle *iph;
      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 template to run GET for.
      54             :    */
      55             :   const char *template_id;
      56             : 
      57             :   /**
      58             :    * description of the template
      59             :    */
      60             :   const char *template_description;
      61             : 
      62             :   /**
      63             :    * OTP device ID
      64             :    */
      65             :   char *otp_id;
      66             : 
      67             :   /**
      68             :    * Contract of the company
      69             :    */
      70             :   json_t *template_contract;
      71             : 
      72             :   /**
      73             :    * Expected HTTP response code.
      74             :    */
      75             :   unsigned int http_status;
      76             : 
      77             : };
      78             : 
      79             : 
      80             : /**
      81             :  * Callback for a PATCH /templates/$ID operation.
      82             :  *
      83             :  * @param cls closure for this function
      84             :  * @param hr response being processed
      85             :  */
      86             : static void
      87           4 : patch_template_cb (void *cls,
      88             :                    const struct TALER_MERCHANT_HttpResponse *hr)
      89             : {
      90           4 :   struct PatchTemplateState *pis = cls;
      91             : 
      92           4 :   pis->iph = NULL;
      93           4 :   if (pis->http_status != hr->http_status)
      94             :   {
      95           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
      96             :                 "Unexpected response code %u (%d) to command %s\n",
      97             :                 hr->http_status,
      98             :                 (int) hr->ec,
      99             :                 TALER_TESTING_interpreter_get_current_label (pis->is));
     100           0 :     TALER_TESTING_interpreter_fail (pis->is);
     101           0 :     return;
     102             :   }
     103           4 :   switch (hr->http_status)
     104             :   {
     105           2 :   case MHD_HTTP_NO_CONTENT:
     106           2 :     break;
     107           0 :   case MHD_HTTP_UNAUTHORIZED:
     108           0 :     break;
     109           0 :   case MHD_HTTP_FORBIDDEN:
     110           0 :     break;
     111           2 :   case MHD_HTTP_NOT_FOUND:
     112           2 :     break;
     113           0 :   case MHD_HTTP_CONFLICT:
     114           0 :     break;
     115           0 :   default:
     116           0 :     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
     117             :                 "Unhandled HTTP status %u for PATCH /templates/ID.\n",
     118             :                 hr->http_status);
     119             :   }
     120           4 :   TALER_TESTING_interpreter_next (pis->is);
     121             : }
     122             : 
     123             : 
     124             : /**
     125             :  * Run the "PATCH /templates/$ID" CMD.
     126             :  *
     127             :  *
     128             :  * @param cls closure.
     129             :  * @param cmd command being run now.
     130             :  * @param is interpreter state.
     131             :  */
     132             : static void
     133           4 : patch_template_run (void *cls,
     134             :                     const struct TALER_TESTING_Command *cmd,
     135             :                     struct TALER_TESTING_Interpreter *is)
     136             : {
     137           4 :   struct PatchTemplateState *pis = cls;
     138             : 
     139           4 :   pis->is = is;
     140           4 :   pis->iph = TALER_MERCHANT_template_patch (
     141             :     TALER_TESTING_interpreter_get_context (is),
     142             :     pis->merchant_url,
     143             :     pis->template_id,
     144             :     pis->template_description,
     145           4 :     pis->otp_id,
     146             :     pis->template_contract,
     147             :     &patch_template_cb,
     148             :     pis);
     149           4 :   GNUNET_assert (NULL != pis->iph);
     150           4 : }
     151             : 
     152             : 
     153             : /**
     154             :  * Offers information from the PATCH /templates CMD state to other
     155             :  * commands.
     156             :  *
     157             :  * @param cls closure
     158             :  * @param[out] ret result (could be anything)
     159             :  * @param trait name of the trait
     160             :  * @param index index number of the object to extract.
     161             :  * @return #GNUNET_OK on success
     162             :  */
     163             : static enum GNUNET_GenericReturnValue
     164           6 : patch_template_traits (void *cls,
     165             :                        const void **ret,
     166             :                        const char *trait,
     167             :                        unsigned int index)
     168             : {
     169           6 :   struct PatchTemplateState *pts = cls;
     170             :   struct TALER_TESTING_Trait traits[] = {
     171           6 :     TALER_TESTING_make_trait_template_description (pts->template_description),
     172           6 :     TALER_TESTING_make_trait_otp_id (pts->otp_id),
     173           6 :     TALER_TESTING_make_trait_template_contract (pts->template_contract),
     174           6 :     TALER_TESTING_make_trait_template_id (pts->template_id),
     175           6 :     TALER_TESTING_trait_end (),
     176             :   };
     177             : 
     178           6 :   return TALER_TESTING_get_trait (traits,
     179             :                                   ret,
     180             :                                   trait,
     181             :                                   index);
     182             : }
     183             : 
     184             : 
     185             : /**
     186             :  * Free the state of a "GET template" CMD, and possibly
     187             :  * cancel a pending operation thereof.
     188             :  *
     189             :  * @param cls closure.
     190             :  * @param cmd command being run.
     191             :  */
     192             : static void
     193           4 : patch_template_cleanup (void *cls,
     194             :                         const struct TALER_TESTING_Command *cmd)
     195             : {
     196           4 :   struct PatchTemplateState *pis = cls;
     197             : 
     198           4 :   if (NULL != pis->iph)
     199             :   {
     200           0 :     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
     201             :                 "PATCH /templates/$ID operation did not complete\n");
     202           0 :     TALER_MERCHANT_template_patch_cancel (pis->iph);
     203             :   }
     204           4 :   GNUNET_free (pis->otp_id);
     205           4 :   json_decref (pis->template_contract);
     206           4 :   GNUNET_free (pis);
     207           4 : }
     208             : 
     209             : 
     210             : struct TALER_TESTING_Command
     211           4 : TALER_TESTING_cmd_merchant_patch_template (
     212             :   const char *label,
     213             :   const char *merchant_url,
     214             :   const char *template_id,
     215             :   const char *template_description,
     216             :   const char *otp_id,
     217             :   json_t *template_contract,
     218             :   unsigned int http_status)
     219             : {
     220             :   struct PatchTemplateState *pis;
     221             : 
     222           4 :   pis = GNUNET_new (struct PatchTemplateState);
     223           4 :   pis->merchant_url = merchant_url;
     224           4 :   pis->template_id = template_id;
     225           4 :   pis->http_status = http_status;
     226           4 :   pis->template_description = template_description;
     227           4 :   pis->otp_id = (NULL == otp_id) ? NULL : GNUNET_strdup (otp_id);
     228           4 :   pis->template_contract = template_contract; /* ownership taken */
     229             :   {
     230           4 :     struct TALER_TESTING_Command cmd = {
     231             :       .cls = pis,
     232             :       .label = label,
     233             :       .run = &patch_template_run,
     234             :       .cleanup = &patch_template_cleanup,
     235             :       .traits = &patch_template_traits
     236             :     };
     237             : 
     238           4 :     return cmd;
     239             :   }
     240             : }
     241             : 
     242             : 
     243             : /* end of testing_api_cmd_patch_template.c */

Generated by: LCOV version 1.16