LCOV - code coverage report
Current view: top level - testing - testing_api_cmd_refund_order.c (source / functions) Hit Total Coverage
Test: GNU Taler coverage report Lines: 0 38 0.0 %
Date: 2020-08-15 06:12:35 Functions: 0 5 0.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :   This file is part of TALER
       3             :   Copyright (C) 2014-2019 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 lib/testing_api_cmd_refund_order.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 a "refund increase" CMD.
      34             :  */
      35             : struct RefundState
      36             : {
      37             :   /**
      38             :    * Operation handle for a POST /orders/$ID/refund request.
      39             :    */
      40             :   struct TALER_MERCHANT_OrderRefundHandle *orh;
      41             : 
      42             :   /**
      43             :    * Base URL of the merchant serving the request.
      44             :    */
      45             :   const char *merchant_url;
      46             : 
      47             :   /**
      48             :    * Order id of the contract to refund.
      49             :    */
      50             :   const char *order_id;
      51             : 
      52             :   /**
      53             :    * The amount to refund.
      54             :    */
      55             :   const char *refund_amount;
      56             : 
      57             :   /**
      58             :    * Human-readable justification for the refund.
      59             :    */
      60             :   const char *reason;
      61             : 
      62             :   /**
      63             :    * Interpreter state.
      64             :    */
      65             :   struct TALER_TESTING_Interpreter *is;
      66             : 
      67             :   /**
      68             :    * Expected HTTP response code.
      69             :    */
      70             :   unsigned int http_code;
      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             :  */
      81             : static void
      82           0 : refund_cb (void *cls,
      83             :            const struct TALER_MERCHANT_HttpResponse *hr)
      84             : {
      85           0 :   struct RefundState *ris = cls;
      86             : 
      87           0 :   ris->orh = NULL;
      88           0 :   if (ris->http_code != hr->http_status)
      89             :   {
      90           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
      91             :                 "Expected status %u, got %u(%d) for refund increase\n",
      92             :                 ris->http_code,
      93             :                 hr->http_status,
      94             :                 (int) hr->ec);
      95           0 :     TALER_TESTING_FAIL (ris->is);
      96             :   }
      97           0 :   TALER_TESTING_interpreter_next (ris->is);
      98             : }
      99             : 
     100             : 
     101             : /**
     102             :  * Run the "refund increase" CMD.
     103             :  *
     104             :  * @param cls closure.
     105             :  * @param cmd command currently being run.
     106             :  * @param is the interpreter state.
     107             :  */
     108             : static void
     109           0 : refund_increase_run (void *cls,
     110             :                      const struct TALER_TESTING_Command *cmd,
     111             :                      struct TALER_TESTING_Interpreter *is)
     112             : {
     113           0 :   struct RefundState *ris = cls;
     114             :   struct TALER_Amount refund_amount;
     115             : 
     116           0 :   ris->is = is;
     117           0 :   if (GNUNET_OK !=
     118           0 :       TALER_string_to_amount (ris->refund_amount,
     119             :                               &refund_amount))
     120           0 :     TALER_TESTING_FAIL (is);
     121           0 :   ris->orh = TALER_MERCHANT_post_order_refund (is->ctx,
     122             :                                                ris->merchant_url,
     123             :                                                ris->order_id,
     124             :                                                &refund_amount,
     125             :                                                ris->reason,
     126             :                                                &refund_cb,
     127             :                                                ris);
     128           0 :   if (NULL == ris->orh)
     129           0 :     TALER_TESTING_FAIL (is);
     130             : }
     131             : 
     132             : 
     133             : /**
     134             :  * Offer internal data from the "refund increase" CMD
     135             :  * state to other commands.
     136             :  *
     137             :  * @param cls closure
     138             :  * @param ret[out] result (could be anything)
     139             :  * @param trait name of the trait
     140             :  * @param index index number of the object to extract.
     141             :  * @return #GNUNET_OK on success
     142             :  */
     143             : static int
     144           0 : refund_increase_traits (void *cls,
     145             :                         const void **ret,
     146             :                         const char *trait,
     147             :                         unsigned int index)
     148             : {
     149           0 :   struct RefundState *ris = cls;
     150             :   struct TALER_TESTING_Trait traits[] = {
     151           0 :     TALER_TESTING_make_trait_string (0,
     152             :                                      ris->refund_amount),
     153           0 :     TALER_TESTING_make_trait_string (1,
     154             :                                      ris->reason),
     155           0 :     TALER_TESTING_trait_end ()
     156             :   };
     157             : 
     158           0 :   return TALER_TESTING_get_trait (traits,
     159             :                                   ret,
     160             :                                   trait,
     161             :                                   index);
     162             : }
     163             : 
     164             : 
     165             : /**
     166             :  * Free the state of a "refund increase" CMD, and
     167             :  * possibly cancel a pending "refund increase" operation.
     168             :  *
     169             :  * @param cls closure
     170             :  * @param cmd command currently being freed.
     171             :  */
     172             : static void
     173           0 : refund_increase_cleanup (void *cls,
     174             :                          const struct TALER_TESTING_Command *cmd)
     175             : {
     176           0 :   struct RefundState *ris = cls;
     177             : 
     178           0 :   if (NULL != ris->orh)
     179             :   {
     180           0 :     TALER_LOG_WARNING ("Refund operation did not complete\n");
     181           0 :     TALER_MERCHANT_post_order_refund_cancel (ris->orh);
     182             :   }
     183           0 :   GNUNET_free (ris);
     184           0 : }
     185             : 
     186             : 
     187             : /**
     188             :  * Define a "refund order" CMD.
     189             :  *
     190             :  * @param label command label.
     191             :  * @param merchant_url base URL of the backend serving the
     192             :  *        "refund increase" request.
     193             :  * @param reason refund justification, human-readable.
     194             :  * @param order_id order id of the contract to refund.
     195             :  * @param refund_amount amount to be refund-increased.
     196             :  * @param http_code expected HTTP response code.
     197             :  * @return the command.
     198             :  */
     199             : struct TALER_TESTING_Command
     200           0 : TALER_TESTING_cmd_merchant_order_refund (const char *label,
     201             :                                          const char *merchant_url,
     202             :                                          const char *reason,
     203             :                                          const char *order_id,
     204             :                                          const char *refund_amount,
     205             :                                          unsigned int http_code)
     206             : {
     207             :   struct RefundState *ris;
     208             : 
     209           0 :   ris = GNUNET_new (struct RefundState);
     210           0 :   ris->merchant_url = merchant_url;
     211           0 :   ris->order_id = order_id;
     212           0 :   ris->refund_amount = refund_amount;
     213           0 :   ris->reason = reason;
     214           0 :   ris->http_code = http_code;
     215             :   {
     216           0 :     struct TALER_TESTING_Command cmd = {
     217             :       .cls = ris,
     218             :       .label = label,
     219             :       .run = &refund_increase_run,
     220             :       .cleanup = &refund_increase_cleanup,
     221             :       .traits = &refund_increase_traits
     222             :     };
     223             : 
     224           0 :     return cmd;
     225             :   }
     226             : }
     227             : 
     228             : 
     229             : /* end of testing_api_cmd_refund_order.c */

Generated by: LCOV version 1.14