LCOV - code coverage report
Current view: top level - testing - testing_api_cmd_wallet_post_orders_refund.c (source / functions) Hit Total Coverage
Test: GNU Taler merchant coverage report Lines: 54 74 73.0 %
Date: 2021-08-30 06:54:17 Functions: 4 4 100.0 %
Legend: Lines: hit not hit

          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             :  * @file testing_api_cmd_wallet_post_orders_refund.c
      21             :  * @brief command to test refunds.
      22             :  * @author Marcello Stanisci
      23             :  * @author Christian Grothoff
      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 an "obtain refunds" CMD.
      34             :  */
      35             : struct WalletRefundState
      36             : {
      37             :   /**
      38             :    * Operation handle for a (public) POST /orders/$ID/refund request.
      39             :    */
      40             :   struct TALER_MERCHANT_WalletOrderRefundHandle *orh;
      41             : 
      42             :   /**
      43             :    * Base URL of the merchant serving the request.
      44             :    */
      45             :   const char *merchant_url;
      46             : 
      47             :   /**
      48             :    * Interpreter state.
      49             :    */
      50             :   struct TALER_TESTING_Interpreter *is;
      51             : 
      52             :   /**
      53             :    * Expected HTTP response code.
      54             :    */
      55             :   unsigned int http_code;
      56             : 
      57             :   /**
      58             :    * Label of the command that created the order we want to obtain refunds for.
      59             :    */
      60             :   const char *proposal_reference;
      61             : 
      62             :   /**
      63             :    * A list of refunds associated with this order.
      64             :    */
      65             :   const char **refunds;
      66             : 
      67             :   /**
      68             :    * The length of @e refunds.
      69             :    */
      70             :   unsigned int refunds_length;
      71             : };
      72             : 
      73             : 
      74             : /**
      75             :  * Process POST /refund (increase) response; just checking
      76             :  * if the HTTP response code is the one expected.
      77             :  *
      78             :  * @param cls closure
      79             :  * @param hr HTTP response
      80             :  * @param refund_amount refund amount
      81             :  * @param merchant_pub public key of the merchant giving the refund
      82             :  * @param refunds the given refunds
      83             :  * @param refunds_length how many refunds were given
      84             :  */
      85             : static void
      86           1 : refund_cb (
      87             :   void *cls,
      88             :   const struct TALER_MERCHANT_HttpResponse *hr,
      89             :   const struct TALER_Amount *refund_amount,
      90             :   const struct TALER_MerchantPublicKeyP *merchant_pub,
      91             :   struct TALER_MERCHANT_RefundDetail refunds[],
      92             :   unsigned int refunds_length)
      93             : {
      94           1 :   struct WalletRefundState *wrs = cls;
      95             : 
      96           1 :   wrs->orh = NULL;
      97           1 :   if (wrs->http_code != hr->http_status)
      98             :   {
      99           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     100             :                 "Expected status %u, got %u(%d) for refund increase\n",
     101             :                 wrs->http_code,
     102             :                 hr->http_status,
     103             :                 (int) hr->ec);
     104           0 :     TALER_TESTING_FAIL (wrs->is);
     105             :   }
     106           1 :   switch (hr->http_status)
     107             :   {
     108           1 :   case MHD_HTTP_OK:
     109             :     {
     110             :       struct TALER_Amount refunded_total;
     111           1 :       if (refunds_length > 0)
     112           1 :         GNUNET_assert (GNUNET_OK ==
     113             :                        TALER_amount_set_zero (refunds[0].refund_amount.currency,
     114             :                                               &refunded_total));
     115           3 :       for (unsigned int i = 0; i < refunds_length; ++i)
     116             :       {
     117             :         const struct TALER_TESTING_Command *refund_cmd;
     118             :         const char *expected_amount_str;
     119             :         struct TALER_Amount expected_amount;
     120             : 
     121           2 :         refund_cmd = TALER_TESTING_interpreter_lookup_command (
     122             :           wrs->is,
     123           2 :           wrs->refunds[i]);
     124             : 
     125           2 :         if (GNUNET_OK !=
     126           2 :             TALER_TESTING_get_trait_string (refund_cmd,
     127             :                                             0,
     128             :                                             &expected_amount_str))
     129             :         {
     130           0 :           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     131             :                       "Could not fetch refund amount\n");
     132           0 :           TALER_TESTING_interpreter_fail (wrs->is);
     133           0 :           return;
     134             :         }
     135           2 :         GNUNET_assert (GNUNET_OK ==
     136             :                        TALER_string_to_amount (expected_amount_str,
     137             :                                                &expected_amount));
     138             :         /* The most recent refunds are returned first */
     139           2 :         GNUNET_assert (0 <= TALER_amount_add (&refunded_total,
     140             :                                               &refunded_total,
     141             :                                               &refunds[refunds_length - 1
     142             :                                                        - i].refund_amount));
     143           2 :         if ((GNUNET_OK !=
     144           2 :              TALER_amount_cmp_currency (&expected_amount,
     145           2 :                                         &refunded_total)) ||
     146           2 :             (0 != TALER_amount_cmp (&expected_amount,
     147             :                                     &refunded_total)))
     148             :         {
     149           0 :           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     150             :                       "Refund amounts do not match\n");
     151           0 :           TALER_TESTING_interpreter_fail (wrs->is);
     152           0 :           return;
     153             :         }
     154             :       }
     155             :     }
     156           1 :     break;
     157           0 :   default:
     158             : 
     159           0 :     break;
     160             :   }
     161           1 :   TALER_TESTING_interpreter_next (wrs->is);
     162             : }
     163             : 
     164             : 
     165             : /**
     166             :  * Run the "refund increase" CMD.
     167             :  *
     168             :  * @param cls closure.
     169             :  * @param cmd command currently being run.
     170             :  * @param is the interpreter state.
     171             :  */
     172             : static void
     173           1 : obtain_refunds_run (void *cls,
     174             :                     const struct TALER_TESTING_Command *cmd,
     175             :                     struct TALER_TESTING_Interpreter *is)
     176             : {
     177           1 :   struct WalletRefundState *wrs = cls;
     178             :   const struct TALER_TESTING_Command *proposal_cmd =
     179           1 :     TALER_TESTING_interpreter_lookup_command (is,
     180             :                                               wrs->proposal_reference);
     181             :   const struct GNUNET_HashCode *h_contract_terms;
     182             :   const char *order_id;
     183             : 
     184           1 :   if (NULL == proposal_cmd)
     185           0 :     TALER_TESTING_FAIL (is);
     186           1 :   if (GNUNET_OK !=
     187           1 :       TALER_TESTING_get_trait_h_contract_terms (proposal_cmd,
     188             :                                                 0,
     189             :                                                 &h_contract_terms))
     190           0 :     TALER_TESTING_FAIL (is);
     191             : 
     192             :   {
     193             :     const json_t *contract_terms;
     194             :     const char *error_name;
     195             :     unsigned int error_line;
     196             : 
     197           1 :     if (GNUNET_OK !=
     198           1 :         TALER_TESTING_get_trait_contract_terms (proposal_cmd,
     199             :                                                 0,
     200             :                                                 &contract_terms))
     201           0 :       TALER_TESTING_FAIL (is);
     202             :     {
     203             :       /* Get information that needs to be put verbatim in the
     204             :        * deposit permission */
     205             :       struct GNUNET_JSON_Specification spec[] = {
     206           1 :         GNUNET_JSON_spec_string ("order_id",
     207             :                                  &order_id),
     208           1 :         GNUNET_JSON_spec_end ()
     209             :       };
     210             : 
     211           1 :       if (GNUNET_OK !=
     212           1 :           GNUNET_JSON_parse (contract_terms,
     213             :                              spec,
     214             :                              &error_name,
     215             :                              &error_line))
     216             :       {
     217             :         char *js;
     218             : 
     219           0 :         js = json_dumps (contract_terms,
     220             :                          JSON_INDENT (1));
     221           0 :         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     222             :                     "Parser failed on %s:%u for input `%s'\n",
     223             :                     error_name,
     224             :                     error_line,
     225             :                     js);
     226           0 :         free (js);
     227           0 :         TALER_TESTING_FAIL (is);
     228             :       }
     229             :     }
     230             :   }
     231             : 
     232           1 :   wrs->is = is;
     233           1 :   wrs->orh = TALER_MERCHANT_wallet_post_order_refund (
     234             :     is->ctx,
     235             :     wrs->merchant_url,
     236             :     order_id,
     237             :     h_contract_terms,
     238             :     &refund_cb,
     239             :     wrs);
     240           1 :   if (NULL == wrs->orh)
     241           0 :     TALER_TESTING_FAIL (is);
     242             : }
     243             : 
     244             : 
     245             : /**
     246             :  * Free the state of a "refund increase" CMD, and
     247             :  * possibly cancel a pending "refund increase" operation.
     248             :  *
     249             :  * @param cls closure
     250             :  * @param cmd command currently being freed.
     251             :  */
     252             : static void
     253           1 : obtain_refunds_cleanup (void *cls,
     254             :                         const struct TALER_TESTING_Command *cmd)
     255             : {
     256           1 :   struct WalletRefundState *wrs = cls;
     257             : 
     258           1 :   if (NULL != wrs->orh)
     259             :   {
     260           0 :     TALER_LOG_WARNING ("Refund operation did not complete\n");
     261           0 :     TALER_MERCHANT_wallet_post_order_refund_cancel (wrs->orh);
     262             :   }
     263           1 :   GNUNET_array_grow (wrs->refunds,
     264             :                      wrs->refunds_length,
     265             :                      0);
     266           1 :   GNUNET_free (wrs);
     267           1 : }
     268             : 
     269             : 
     270             : struct TALER_TESTING_Command
     271           1 : TALER_TESTING_cmd_wallet_order_refund (const char *label,
     272             :                                        const char *merchant_url,
     273             :                                        const char *order_ref,
     274             :                                        unsigned int http_code,
     275             :                                        ...)
     276             : {
     277             :   struct WalletRefundState *wrs;
     278             : 
     279           1 :   wrs = GNUNET_new (struct WalletRefundState);
     280           1 :   wrs->merchant_url = merchant_url;
     281           1 :   wrs->proposal_reference = order_ref;
     282           1 :   wrs->http_code = http_code;
     283           1 :   wrs->refunds_length = 0;
     284             :   {
     285             :     const char *clabel;
     286             :     va_list ap;
     287             : 
     288           1 :     va_start (ap, http_code);
     289           3 :     while (NULL != (clabel = va_arg (ap, const char *)))
     290             :     {
     291           2 :       GNUNET_array_append (wrs->refunds,
     292             :                            wrs->refunds_length,
     293             :                            clabel);
     294             :     }
     295           1 :     va_end (ap);
     296             :   }
     297             :   {
     298           1 :     struct TALER_TESTING_Command cmd = {
     299             :       .cls = wrs,
     300             :       .label = label,
     301             :       .run = &obtain_refunds_run,
     302             :       .cleanup = &obtain_refunds_cleanup
     303             :     };
     304             : 
     305           1 :     return cmd;
     306             :   }
     307             : }

Generated by: LCOV version 1.14