LCOV - code coverage report
Current view: top level - testing - testing_api_cmd_forget_order.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 44 51 86.3 %
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) 2020 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             : /**
      21             :  * @file testing_api_cmd_forget_order.c
      22             :  * @brief command to forget fields of an order
      23             :  * @author Jonathan Buchanan
      24             :  */
      25             : #include "platform.h"
      26             : #include <taler/taler_exchange_service.h>
      27             : #include <taler/taler_testing_lib.h>
      28             : #include "taler_merchant_service.h"
      29             : #include "taler_merchant_testing_lib.h"
      30             : 
      31             : 
      32             : /**
      33             :  * State for a "order forget" CMD.
      34             :  */
      35             : struct OrderForgetState
      36             : {
      37             :   /**
      38             :    * The interpreter state.
      39             :    */
      40             :   struct TALER_TESTING_Interpreter *is;
      41             : 
      42             :   /**
      43             :    * URL of the merchant backend.
      44             :    */
      45             :   const char *merchant_url;
      46             : 
      47             :   /**
      48             :    * Expected status code.
      49             :    */
      50             :   unsigned int http_status;
      51             : 
      52             :   /**
      53             :    * PATCH /orders/$ORDER_ID/forget operation handle.
      54             :    */
      55             :   struct TALER_MERCHANT_OrderForgetHandle *ofh;
      56             : 
      57             :   /**
      58             :    * Reference to a order operation.
      59             :    */
      60             :   const char *order_reference;
      61             : 
      62             :   /**
      63             :    * Order id to forget for. If NULL, the @a order_reference
      64             :    * will offer this value.
      65             :    */
      66             :   const char *order_id;
      67             : 
      68             :   /**
      69             :    * The list of paths to forget in the contract terms.
      70             :    */
      71             :   const char **paths;
      72             : 
      73             :   /**
      74             :    * The length of @e paths.
      75             :    */
      76             :   unsigned int paths_length;
      77             : };
      78             : 
      79             : 
      80             : /**
      81             :  * Free the state of a "order forget" CMD, and possibly
      82             :  * cancel it if it did not complete.
      83             :  *
      84             :  * @param cls closure.
      85             :  * @param cmd command being freed.
      86             :  */
      87             : static void
      88          12 : order_forget_cleanup (void *cls,
      89             :                       const struct TALER_TESTING_Command *cmd)
      90             : {
      91          12 :   struct OrderForgetState *ofs = cls;
      92             : 
      93          12 :   if (NULL != ofs->ofh)
      94             :   {
      95           0 :     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
      96             :                 "Command '%s' did not complete\n",
      97             :                 cmd->label);
      98           0 :     TALER_MERCHANT_order_forget_cancel (ofs->ofh);
      99           0 :     ofs->ofh = NULL;
     100             :   }
     101          12 :   GNUNET_array_grow (ofs->paths,
     102             :                      ofs->paths_length,
     103             :                      0);
     104          12 :   GNUNET_free (ofs);
     105          12 : }
     106             : 
     107             : 
     108             : /**
     109             :  * Callback for "order forget" operation, to check the
     110             :  * response code is as expected.
     111             :  *
     112             :  * @param cls closure
     113             :  * @param hr HTTP response we got
     114             :  */
     115             : static void
     116          12 : order_forget_cb (void *cls,
     117             :                  const struct TALER_MERCHANT_HttpResponse *hr)
     118             : {
     119          12 :   struct OrderForgetState *ofs = cls;
     120             : 
     121          12 :   ofs->ofh = NULL;
     122          12 :   if (ofs->http_status != hr->http_status)
     123             :   {
     124           0 :     TALER_TESTING_unexpected_status_with_body (ofs->is,
     125             :                                                hr->http_status,
     126             :                                                ofs->http_status,
     127             :                                                hr->reply);
     128           0 :     return;
     129             :   }
     130          12 :   TALER_TESTING_interpreter_next (ofs->is);
     131             : }
     132             : 
     133             : 
     134             : /**
     135             :  * Run the "order forget" CMD.
     136             :  *
     137             :  * @param cls closure.
     138             :  * @param cmd command currently being run.
     139             :  * @param is interpreter state.
     140             :  */
     141             : static void
     142          12 : order_forget_run (void *cls,
     143             :                   const struct TALER_TESTING_Command *cmd,
     144             :                   struct TALER_TESTING_Interpreter *is)
     145             : {
     146          12 :   struct OrderForgetState *ofs = cls;
     147             :   const char *order_id;
     148             : 
     149          12 :   ofs->is = is;
     150          12 :   if (NULL != ofs->order_id)
     151             :   {
     152           2 :     order_id = ofs->order_id;
     153             :   }
     154             :   else
     155             :   {
     156             :     const struct TALER_TESTING_Command *order_cmd;
     157             : 
     158             :     order_cmd
     159          10 :       = TALER_TESTING_interpreter_lookup_command (is,
     160             :                                                   ofs->order_reference);
     161          10 :     if (NULL == order_cmd)
     162           0 :       TALER_TESTING_FAIL (is);
     163          10 :     if (GNUNET_OK !=
     164          10 :         TALER_TESTING_get_trait_order_id (order_cmd,
     165             :                                           &order_id))
     166           0 :       TALER_TESTING_FAIL (is);
     167             :   }
     168          12 :   ofs->ofh = TALER_MERCHANT_order_forget (
     169             :     TALER_TESTING_interpreter_get_context (is),
     170             :     ofs->merchant_url,
     171             :     order_id,
     172             :     ofs->paths_length,
     173             :     ofs->paths,
     174             :     &order_forget_cb,
     175             :     ofs);
     176          12 :   GNUNET_assert (NULL != ofs->ofh);
     177             : }
     178             : 
     179             : 
     180             : /**
     181             :  * Offer internal data to other commands.
     182             :  *
     183             :  * @param cls closure
     184             :  * @param[out] ret result (could be anything)
     185             :  * @param trait name of the trait
     186             :  * @param index index number of the object to extract.
     187             :  * @return #GNUNET_OK on success
     188             :  */
     189             : static enum GNUNET_GenericReturnValue
     190          24 : order_forget_traits (void *cls,
     191             :                      const void **ret,
     192             :                      const char *trait,
     193             :                      unsigned int index)
     194          24 : {
     195          24 :   struct OrderForgetState *ofs = cls;
     196          24 :   struct TALER_TESTING_Trait traits[ofs->paths_length + 2];
     197             : 
     198          24 :   traits[0] = TALER_TESTING_make_trait_paths_length (&ofs->paths_length);
     199          48 :   for (unsigned int i = 0; i < ofs->paths_length; ++i)
     200          24 :     traits[i + 1] = TALER_TESTING_make_trait_paths (i,
     201          24 :                                                     ofs->paths[i]);
     202          24 :   traits[ofs->paths_length + 1] = TALER_TESTING_trait_end ();
     203             : 
     204          24 :   return TALER_TESTING_get_trait (traits,
     205             :                                   ret,
     206             :                                   trait,
     207             :                                   index);
     208             : }
     209             : 
     210             : 
     211             : struct TALER_TESTING_Command
     212          12 : TALER_TESTING_cmd_merchant_forget_order (
     213             :   const char *label,
     214             :   const char *merchant_url,
     215             :   unsigned int http_status,
     216             :   const char *order_reference,
     217             :   const char *order_id,
     218             :   ...)
     219             : {
     220             :   struct OrderForgetState *ofs;
     221             : 
     222          12 :   ofs = GNUNET_new (struct OrderForgetState);
     223          12 :   ofs->http_status = http_status;
     224          12 :   ofs->order_reference = order_reference;
     225          12 :   ofs->merchant_url = merchant_url;
     226          12 :   ofs->order_id = order_id;
     227             :   {
     228             :     const char *path;
     229             :     va_list ap;
     230             : 
     231          12 :     va_start (ap, order_id);
     232          24 :     while (NULL != (path = va_arg (ap, const char *)))
     233             :     {
     234          12 :       GNUNET_array_append (ofs->paths,
     235             :                            ofs->paths_length,
     236             :                            path);
     237             :     }
     238          12 :     va_end (ap);
     239             :   }
     240             :   {
     241          12 :     struct TALER_TESTING_Command cmd = {
     242             :       .cls = ofs,
     243             :       .label = label,
     244             :       .run = &order_forget_run,
     245             :       .cleanup = &order_forget_cleanup,
     246             :       .traits = &order_forget_traits
     247             :     };
     248             : 
     249          12 :     return cmd;
     250             :   }
     251             : }

Generated by: LCOV version 1.16