LCOV - code coverage report
Current view: top level - testing - testing_api_cmd_wallet_get_order.c (source / functions) Hit Total Coverage
Test: GNU Taler coverage report Lines: 0 109 0.0 %
Date: 2020-08-15 06:12:35 Functions: 0 4 0.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 lib/testing_api_cmd_wallet_get_order.c
      21             :  * @brief command to test GET /order/$ORDER_ID
      22             :  * @author Jonathan Buchanan
      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 for a GET /orders/$ORDER_ID CMD.
      33             :  */
      34             : struct WalletGetOrderState
      35             : {
      36             :   /**
      37             :    * The merchant base URL.
      38             :    */
      39             :   const char *merchant_url;
      40             : 
      41             :   /**
      42             :    * Expected HTTP response code for this CMD.
      43             :    */
      44             :   unsigned int http_status;
      45             : 
      46             :   /**
      47             :    * The handle to the current GET /orders/$ORDER_ID request.
      48             :    */
      49             :   struct TALER_MERCHANT_OrderWalletGetHandle *ogh;
      50             : 
      51             :   /**
      52             :    * The interpreter state.
      53             :    */
      54             :   struct TALER_TESTING_Interpreter *is;
      55             : 
      56             :   /**
      57             :    * Reference to a command that created an order.
      58             :    */
      59             :   const char *order_reference;
      60             : 
      61             :   /**
      62             :    * Whether the order was paid or not.
      63             :    */
      64             :   bool paid;
      65             : 
      66             :   /**
      67             :    * Whether the order was refunded or not.
      68             :    */
      69             :   bool refunded;
      70             : 
      71             :   /**
      72             :    * A NULL-terminated list of refunds associated with this order.
      73             :    */
      74             :   const char **refunds;
      75             : 
      76             :   /**
      77             :    * The length of @e refunds.
      78             :    */
      79             :   unsigned int refunds_length;
      80             : };
      81             : 
      82             : 
      83             : /**
      84             :  * Callback to process a GET /orders/$ID request
      85             :  *
      86             :  * @param cls closure
      87             :  * @param hr HTTP response details
      88             :  * @param paid #GNUNET_YES if the payment is settled, #GNUNET_NO if not
      89             :  *        settled, #GNUNET_SYSERR on error
      90             :  *        (note that refunded payments are returned as paid!)
      91             :  * @param refunded #GNUNET_YES if there is at least on refund on this payment,
      92             :  *        #GNUNET_NO if refunded, #GNUNET_SYSERR or error
      93             :  * @param refunded_amount amount that was refunded, NULL if there
      94             :  *        was no refund
      95             :  * @param taler_pay_uri the URI that instructs the wallets to process
      96             :  *                      the payment
      97             :  * @param already_paid_order_id equivalent order that this customer
      98             :  *                      paid already, or NULL for none
      99             :  * @param merchant_pub public key of the merchant
     100             :  * @param num_refunds length of the @a refunds array
     101             :  * @param refunds details about the refund processing
     102             :  */
     103             : static void
     104           0 : wallet_get_order_cb (
     105             :   void *cls,
     106             :   const struct TALER_MERCHANT_HttpResponse *hr,
     107             :   enum GNUNET_GenericReturnValue paid,
     108             :   enum GNUNET_GenericReturnValue refunded,
     109             :   struct TALER_Amount *refund_amount,
     110             :   const char *taler_pay_uri,
     111             :   const char *already_paid_order_id,
     112             :   const struct TALER_MerchantPublicKeyP *merchant_pub,
     113             :   unsigned int num_refunds,
     114             :   const struct TALER_MERCHANT_RefundDetail *refunds)
     115             : {
     116             :   /* FIXME, deeper checks should be implemented here. */
     117           0 :   struct WalletGetOrderState *gos = cls;
     118           0 :   bool paid_b = (paid == GNUNET_YES);
     119           0 :   bool refunded_b = (refunded == GNUNET_YES);
     120             : 
     121           0 :   gos->ogh = NULL;
     122           0 :   if (gos->http_status != hr->http_status)
     123             :   {
     124           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     125             :                 "Unexpected response code %u (%d) to command %s\n",
     126             :                 hr->http_status,
     127             :                 (int) hr->ec,
     128             :                 TALER_TESTING_interpreter_get_current_label (gos->is));
     129           0 :     TALER_TESTING_interpreter_fail (gos->is);
     130           0 :     return;
     131             :   }
     132           0 :   switch (hr->http_status)
     133             :   {
     134           0 :   case MHD_HTTP_OK:
     135             :     // FIXME: use gos->order_reference here to
     136             :     // check if the data returned matches that from the POST / PATCH
     137           0 :     if (gos->paid != paid_b)
     138             :     {
     139           0 :       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     140             :                   "Order paid does not match\n");
     141           0 :       TALER_TESTING_interpreter_fail (gos->is);
     142           0 :       return;
     143             :     }
     144           0 :     if (gos->refunded != refunded_b)
     145             :     {
     146           0 :       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     147             :                   "Order refunded does not match\n");
     148           0 :       TALER_TESTING_interpreter_fail (gos->is);
     149           0 :       return;
     150             :     }
     151           0 :     if (gos->refunds_length != num_refunds)
     152             :     {
     153           0 :       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     154             :                   "Order refunds count does not match\n");
     155           0 :       TALER_TESTING_interpreter_fail (gos->is);
     156           0 :       return;
     157             :     }
     158             :     {
     159             :       struct TALER_Amount refunded_total;
     160           0 :       if (num_refunds > 0)
     161           0 :         GNUNET_assert (GNUNET_OK ==
     162             :                        TALER_amount_get_zero (refund_amount->currency,
     163             :                                               &refunded_total));
     164           0 :       for (unsigned int i = 0; i < num_refunds; ++i)
     165             :       {
     166             :         const struct TALER_TESTING_Command *refund_cmd;
     167             :         const char *expected_amount_str;
     168             :         struct TALER_Amount expected_amount;
     169             : 
     170           0 :         refund_cmd = TALER_TESTING_interpreter_lookup_command (
     171             :           gos->is,
     172           0 :           gos->refunds[i]);
     173             : 
     174           0 :         if (GNUNET_OK !=
     175           0 :             TALER_TESTING_get_trait_string (refund_cmd,
     176             :                                             0,
     177             :                                             &expected_amount_str))
     178             :         {
     179           0 :           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     180             :                       "Could not fetch refund amount\n");
     181           0 :           TALER_TESTING_interpreter_fail (gos->is);
     182           0 :           return;
     183             :         }
     184           0 :         GNUNET_assert (GNUNET_OK ==
     185             :                        TALER_string_to_amount (expected_amount_str,
     186             :                                                &expected_amount));
     187             :         /* The most recent refunds are returned first */
     188           0 :         GNUNET_assert (0 <= TALER_amount_add (&refunded_total,
     189             :                                               &refunded_total,
     190             :                                               &refunds[num_refunds - 1 - i].refund_amount));
     191           0 :         if ((GNUNET_OK !=
     192           0 :              TALER_amount_cmp_currency (&expected_amount,
     193           0 :                                         &refunded_total)) ||
     194           0 :             (0 != TALER_amount_cmp (&expected_amount,
     195             :                                     &refunded_total)))
     196             :         {
     197           0 :           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     198             :                       "Refund amounts do not match\n");
     199           0 :           TALER_TESTING_interpreter_fail (gos->is);
     200           0 :           return;
     201             :         }
     202             :       }
     203             :     }
     204           0 :     if (!paid_b)
     205             :     {
     206             :       /* FIXME: Check all of the members of `pud` */
     207             :       struct TALER_MERCHANT_PayUriData pud;
     208             :       const struct TALER_TESTING_Command *order_cmd;
     209             :       const char *order_id;
     210             :       const struct TALER_ClaimTokenP *claim_token;
     211             : 
     212           0 :       if (GNUNET_OK !=
     213           0 :           TALER_MERCHANT_parse_pay_uri (taler_pay_uri,
     214             :                                         &pud))
     215             :       {
     216           0 :         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     217             :                     "Taler pay uri is malformed\n");
     218           0 :         TALER_TESTING_interpreter_fail (gos->is);
     219           0 :         return;
     220             :       }
     221             : 
     222           0 :       order_cmd = TALER_TESTING_interpreter_lookup_command (
     223             :         gos->is,
     224             :         gos->order_reference);
     225             : 
     226           0 :       if (GNUNET_OK !=
     227           0 :           TALER_TESTING_get_trait_order_id (order_cmd,
     228             :                                             0,
     229             :                                             &order_id))
     230           0 :         TALER_TESTING_FAIL (gos->is);
     231             : 
     232           0 :       if (GNUNET_OK !=
     233           0 :           TALER_TESTING_get_trait_claim_token (order_cmd,
     234             :                                                0,
     235             :                                                &claim_token))
     236           0 :         TALER_TESTING_FAIL (gos->is);
     237             : 
     238           0 :       if ((0 != strcmp ("localhost:8080",
     239           0 :                         pud.merchant_host)) ||
     240           0 :           (NULL != pud.merchant_prefix_path) ||
     241           0 :           (0 != strcmp (order_id,
     242           0 :                         pud.order_id)) ||
     243           0 :           (NULL != pud.ssid))
     244             :       {
     245           0 :         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     246             :                     "Order pay uri does not match\n");
     247           0 :         TALER_TESTING_interpreter_fail (gos->is);
     248           0 :         TALER_MERCHANT_parse_pay_uri_free (&pud);
     249           0 :         return;
     250             :       }
     251             :       /* The claim token is not given in the pay uri if the order
     252             :          has been claimed already. */
     253           0 :       if ((NULL != pud.claim_token) &&
     254           0 :           ((NULL == claim_token) ||
     255           0 :            (0 != GNUNET_memcmp (claim_token,
     256             :                                 pud.claim_token))))
     257             :       {
     258           0 :         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     259             :                     "Order pay uri does not match\n");
     260           0 :         TALER_TESTING_interpreter_fail (gos->is);
     261           0 :         TALER_MERCHANT_parse_pay_uri_free (&pud);
     262           0 :         return;
     263             :       }
     264             :     }
     265           0 :     break;
     266           0 :   default:
     267           0 :     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
     268             :                 "Unhandled HTTP status.\n");
     269             :   }
     270           0 :   TALER_TESTING_interpreter_next (gos->is);
     271             : }
     272             : 
     273             : 
     274             : /**
     275             :  * Run the "GET order" CMD.
     276             :  *
     277             :  * @param cls closure.
     278             :  * @param cmd command being run now.
     279             :  * @param is interpreter state.
     280             :  */
     281             : static void
     282           0 : wallet_get_order_run (void *cls,
     283             :                       const struct TALER_TESTING_Command *cmd,
     284             :                       struct TALER_TESTING_Interpreter *is)
     285             : {
     286           0 :   struct WalletGetOrderState *gos = cls;
     287             :   const struct TALER_TESTING_Command *order_cmd;
     288             :   const char *order_id;
     289             :   const struct GNUNET_HashCode *h_contract;
     290             : 
     291           0 :   order_cmd = TALER_TESTING_interpreter_lookup_command (
     292             :     is,
     293             :     gos->order_reference);
     294             : 
     295           0 :   if (GNUNET_OK !=
     296           0 :       TALER_TESTING_get_trait_order_id (order_cmd,
     297             :                                         0,
     298             :                                         &order_id))
     299           0 :     TALER_TESTING_FAIL (is);
     300             : 
     301           0 :   if (GNUNET_OK !=
     302           0 :       TALER_TESTING_get_trait_h_contract_terms (order_cmd,
     303             :                                                 0,
     304             :                                                 &h_contract))
     305           0 :     TALER_TESTING_FAIL (is);
     306             : 
     307           0 :   gos->is = is;
     308           0 :   gos->ogh = TALER_MERCHANT_wallet_order_get (is->ctx,
     309             :                                               gos->merchant_url,
     310             :                                               order_id,
     311             :                                               h_contract,
     312             :                                               GNUNET_TIME_UNIT_ZERO,
     313             :                                               NULL,
     314             :                                               NULL,
     315             :                                               false,
     316             :                                               &wallet_get_order_cb,
     317             :                                               gos);
     318             : }
     319             : 
     320             : 
     321             : /**
     322             :  * Free the state of a "GET order" CMD, and possibly
     323             :  * cancel a pending operation thereof.
     324             :  *
     325             :  * @param cls closure.
     326             :  * @param cmd command being run.
     327             :  */
     328             : static void
     329           0 : wallet_get_order_cleanup (void *cls,
     330             :                           const struct TALER_TESTING_Command *cmd)
     331             : {
     332           0 :   struct WalletGetOrderState *gos = cls;
     333             : 
     334           0 :   if (NULL != gos->ogh)
     335             :   {
     336           0 :     TALER_LOG_WARNING ("Get tip operation did not complete\n");
     337           0 :     TALER_MERCHANT_wallet_order_get_cancel (gos->ogh);
     338             :   }
     339           0 :   GNUNET_free (gos);
     340           0 : }
     341             : 
     342             : 
     343             : /**
     344             :  * Define a GET /orders/$ORDER_ID CMD.
     345             :  *
     346             :  * @param label the command label
     347             :  * @param merchant_url base URL of the merchant which will
     348             :  *        serve the request.
     349             :  * @param order_reference reference to a command that created an order.
     350             :  * @param paid whether the order has been paid for or not.
     351             :  * @param refunded whether the order has been refunded.
     352             :  * @param http_status expected HTTP response code for the request.
     353             :  * @param ... NULL-terminated list of labels (const char *) of
     354             :  *        refunds (commands) we expect to be aggregated in the transfer
     355             :  *        (assuming @a http_code is #MHD_HTTP_OK). If @e refunded is false,
     356             :  *        this parameter is ignored.
     357             :  */
     358             : struct TALER_TESTING_Command
     359           0 : TALER_TESTING_cmd_wallet_get_order (const char *label,
     360             :                                     const char *merchant_url,
     361             :                                     const char *order_reference,
     362             :                                     bool paid,
     363             :                                     bool refunded,
     364             :                                     unsigned int http_status,
     365             :                                     ...)
     366             : {
     367             :   struct WalletGetOrderState *gos;
     368             : 
     369           0 :   gos = GNUNET_new (struct WalletGetOrderState);
     370           0 :   gos->merchant_url = merchant_url;
     371           0 :   gos->order_reference = order_reference;
     372           0 :   gos->http_status = http_status;
     373           0 :   gos->paid = paid;
     374           0 :   gos->refunded = refunded;
     375           0 :   gos->refunds_length = 0;
     376           0 :   if (refunded)
     377             :   {
     378             :     const char *clabel;
     379             :     va_list ap;
     380             : 
     381           0 :     va_start (ap, http_status);
     382           0 :     while (NULL != (clabel = va_arg (ap, const char *)))
     383             :     {
     384           0 :       GNUNET_array_append (gos->refunds,
     385             :                            gos->refunds_length,
     386             :                            clabel);
     387             :     }
     388           0 :     va_end (ap);
     389             :   }
     390             :   {
     391           0 :     struct TALER_TESTING_Command cmd = {
     392             :       .cls = gos,
     393             :       .label = label,
     394             :       .run = &wallet_get_order_run,
     395             :       .cleanup = &wallet_get_order_cleanup
     396             :     };
     397             : 
     398           0 :     return cmd;
     399             :   }
     400             : }
     401             : 
     402             : 
     403             : /* end of testing_api_cmd_wallet_get_order.c */

Generated by: LCOV version 1.14