LCOV - code coverage report
Current view: top level - testing - testing_api_cmd_wallet_get_order.c (source / functions) Hit Total Coverage
Test: GNU Taler merchant coverage report Lines: 166 232 71.6 %
Date: 2021-08-30 06:54:17 Functions: 14 14 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_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             :    * Whether the order has refunds pending.
      73             :    */
      74             :   bool refund_pending;
      75             : };
      76             : 
      77             : 
      78             : /**
      79             :  * Callback to process a GET /orders/$ID request
      80             :  *
      81             :  * @param cls closure
      82             :  * @param hr HTTP response details
      83             :  * @param paid #GNUNET_YES if the payment is settled, #GNUNET_NO if not
      84             :  *        settled, #GNUNET_SYSERR on error
      85             :  *        (note that refunded payments are returned as paid!)
      86             :  * @param refunded #GNUNET_YES if there is at least on refund on this payment,
      87             :  *        #GNUNET_NO if refunded, #GNUNET_SYSERR or error
      88             :  * @param refund_pending #GNUNET_YES if there are refunds waiting to be
      89             :  *        obtained, #GNUNET_NO if all refunds have been obtained, #GNUNET_SYSERR
      90             :  *        on error.
      91             :  * @param refund_amount amount that was refunded, NULL if there
      92             :  *        was no refund
      93             :  * @param taler_pay_uri the URI that instructs the wallets to process
      94             :  *                      the payment
      95             :  * @param already_paid_order_id equivalent order that this customer
      96             :  *                      paid already, or NULL for none
      97             :  */
      98             : static void
      99           5 : wallet_get_order_cb (
     100             :   void *cls,
     101             :   const struct TALER_MERCHANT_HttpResponse *hr,
     102             :   enum GNUNET_GenericReturnValue paid,
     103             :   enum GNUNET_GenericReturnValue refunded,
     104             :   enum GNUNET_GenericReturnValue refund_pending,
     105             :   struct TALER_Amount *refund_amount,
     106             :   const char *taler_pay_uri,
     107             :   const char *already_paid_order_id)
     108             : {
     109             :   /* FIXME, deeper checks should be implemented here. */
     110           5 :   struct WalletGetOrderState *gos = cls;
     111           5 :   bool paid_b = (paid == GNUNET_YES);
     112           5 :   bool refunded_b = (refunded == GNUNET_YES);
     113           5 :   bool refund_pending_b = (refund_pending == GNUNET_YES);
     114             : 
     115           5 :   gos->ogh = NULL;
     116           5 :   if (gos->http_status != hr->http_status)
     117             :   {
     118           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     119             :                 "Unexpected response code %u (%d) to command %s\n",
     120             :                 hr->http_status,
     121             :                 (int) hr->ec,
     122             :                 TALER_TESTING_interpreter_get_current_label (gos->is));
     123           0 :     TALER_TESTING_interpreter_fail (gos->is);
     124           0 :     return;
     125             :   }
     126           5 :   switch (hr->http_status)
     127             :   {
     128           5 :   case MHD_HTTP_OK:
     129             :     // FIXME: use gos->order_reference here to
     130             :     // check if the data returned matches that from the POST / PATCH
     131           5 :     if (gos->paid != paid_b)
     132             :     {
     133           0 :       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     134             :                   "Order paid does not match\n");
     135           0 :       TALER_TESTING_interpreter_fail (gos->is);
     136           0 :       return;
     137             :     }
     138           5 :     if (gos->refunded != refunded_b)
     139             :     {
     140           0 :       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     141             :                   "Order refunded does not match\n");
     142           0 :       TALER_TESTING_interpreter_fail (gos->is);
     143           0 :       return;
     144             :     }
     145           5 :     if (gos->refund_pending != refund_pending_b)
     146             :     {
     147           0 :       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     148             :                   "Order refund pending does not match\n");
     149           0 :       TALER_TESTING_interpreter_fail (gos->is);
     150           0 :       return;
     151             :     }
     152           5 :     if (! paid_b)
     153             :     {
     154             :       /* FIXME: Check all of the members of `pud` */
     155             :       struct TALER_MERCHANT_PayUriData pud;
     156             :       const struct TALER_TESTING_Command *order_cmd;
     157             :       const char *order_id;
     158             :       const struct TALER_ClaimTokenP *claim_token;
     159             : 
     160           1 :       if (GNUNET_OK !=
     161           1 :           TALER_MERCHANT_parse_pay_uri (taler_pay_uri,
     162             :                                         &pud))
     163             :       {
     164           0 :         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     165             :                     "Taler pay uri `%s' is malformed\n",
     166             :                     taler_pay_uri);
     167           0 :         TALER_TESTING_interpreter_fail (gos->is);
     168           0 :         return;
     169             :       }
     170             : 
     171           1 :       order_cmd = TALER_TESTING_interpreter_lookup_command (
     172             :         gos->is,
     173             :         gos->order_reference);
     174             : 
     175           1 :       if (GNUNET_OK !=
     176           1 :           TALER_TESTING_get_trait_order_id (order_cmd,
     177             :                                             0,
     178             :                                             &order_id))
     179             :       {
     180           0 :         TALER_MERCHANT_parse_pay_uri_free (&pud);
     181           0 :         TALER_TESTING_FAIL (gos->is);
     182             :       }
     183             : 
     184           1 :       if (GNUNET_OK !=
     185           1 :           TALER_TESTING_get_trait_claim_token (order_cmd,
     186             :                                                0,
     187             :                                                &claim_token))
     188             :       {
     189           0 :         TALER_MERCHANT_parse_pay_uri_free (&pud);
     190           0 :         TALER_TESTING_FAIL (gos->is);
     191             :       }
     192             : 
     193             :       {
     194             :         char *port;
     195             :         char *host;
     196             : 
     197           1 :         if (GNUNET_OK !=
     198           1 :             GNUNET_CONFIGURATION_get_value_string (gos->is->cfg,
     199             :                                                    "merchant",
     200             :                                                    "PORT",
     201             :                                                    &port))
     202             :         {
     203             :           /* How did we get here without a configured port? */
     204           0 :           GNUNET_break (0);
     205           0 :           TALER_TESTING_interpreter_fail (gos->is);
     206           0 :           TALER_MERCHANT_parse_pay_uri_free (&pud);
     207           0 :           return;
     208             :         }
     209           1 :         GNUNET_asprintf (&host,
     210             :                          "localhost:%s",
     211             :                          port);
     212           1 :         GNUNET_free (port);
     213           1 :         if ((0 != strcmp (host,
     214           1 :                           pud.merchant_host)) ||
     215           1 :             (NULL != pud.merchant_prefix_path) ||
     216           1 :             (0 != strcmp (order_id,
     217           1 :                           pud.order_id)) ||
     218           1 :             (NULL != pud.ssid))
     219             :         {
     220           0 :           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     221             :                       "Order pay uri `%s' does not match `%s'\n",
     222             :                       taler_pay_uri,
     223             :                       pud.order_id);
     224           0 :           TALER_TESTING_interpreter_fail (gos->is);
     225           0 :           TALER_MERCHANT_parse_pay_uri_free (&pud);
     226           0 :           GNUNET_free (host);
     227           0 :           return;
     228             :         }
     229           1 :         GNUNET_free (host);
     230             :       }
     231             :       /* The claim token is not given in the pay uri if the order
     232             :          has been claimed already. */
     233           1 :       if ((NULL != pud.claim_token) &&
     234           0 :           ((NULL == claim_token) ||
     235           0 :            (0 != GNUNET_memcmp (claim_token,
     236             :                                 pud.claim_token))))
     237             :       {
     238           0 :         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     239             :                     "Order pay uri claim token does not match (%d/%d)\n",
     240             :                     NULL == pud.claim_token,
     241             :                     NULL == claim_token);
     242           0 :         TALER_TESTING_interpreter_fail (gos->is);
     243           0 :         TALER_MERCHANT_parse_pay_uri_free (&pud);
     244           0 :         return;
     245             :       }
     246           1 :       TALER_MERCHANT_parse_pay_uri_free (&pud);
     247             :     }
     248           5 :     break;
     249           0 :   default:
     250           0 :     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
     251             :                 "Unhandled HTTP status.\n");
     252             :   }
     253           5 :   TALER_TESTING_interpreter_next (gos->is);
     254             : }
     255             : 
     256             : 
     257             : /**
     258             :  * Run the "GET order" CMD.
     259             :  *
     260             :  * @param cls closure.
     261             :  * @param cmd command being run now.
     262             :  * @param is interpreter state.
     263             :  */
     264             : static void
     265           5 : wallet_get_order_run (void *cls,
     266             :                       const struct TALER_TESTING_Command *cmd,
     267             :                       struct TALER_TESTING_Interpreter *is)
     268             : {
     269           5 :   struct WalletGetOrderState *gos = cls;
     270             :   const struct TALER_TESTING_Command *order_cmd;
     271             :   const char *order_id;
     272             :   const struct GNUNET_HashCode *h_contract;
     273             : 
     274           5 :   order_cmd = TALER_TESTING_interpreter_lookup_command (
     275             :     is,
     276             :     gos->order_reference);
     277             : 
     278           5 :   if (GNUNET_OK !=
     279           5 :       TALER_TESTING_get_trait_order_id (order_cmd,
     280             :                                         0,
     281             :                                         &order_id))
     282           0 :     TALER_TESTING_FAIL (is);
     283             : 
     284           5 :   if (GNUNET_OK !=
     285           5 :       TALER_TESTING_get_trait_h_contract_terms (order_cmd,
     286             :                                                 0,
     287             :                                                 &h_contract))
     288           0 :     TALER_TESTING_FAIL (is);
     289             : 
     290           5 :   gos->is = is;
     291           5 :   gos->ogh = TALER_MERCHANT_wallet_order_get (is->ctx,
     292             :                                               gos->merchant_url,
     293             :                                               order_id,
     294             :                                               h_contract,
     295             :                                               GNUNET_TIME_UNIT_ZERO,
     296             :                                               NULL,
     297             :                                               NULL,
     298             :                                               false,
     299             :                                               &wallet_get_order_cb,
     300             :                                               gos);
     301             : }
     302             : 
     303             : 
     304             : /**
     305             :  * Free the state of a "GET order" CMD, and possibly
     306             :  * cancel a pending operation thereof.
     307             :  *
     308             :  * @param cls closure.
     309             :  * @param cmd command being run.
     310             :  */
     311             : static void
     312           5 : wallet_get_order_cleanup (void *cls,
     313             :                           const struct TALER_TESTING_Command *cmd)
     314             : {
     315           5 :   struct WalletGetOrderState *gos = cls;
     316             : 
     317           5 :   if (NULL != gos->ogh)
     318             :   {
     319           0 :     TALER_LOG_WARNING ("Get tip operation did not complete\n");
     320           0 :     TALER_MERCHANT_wallet_order_get_cancel (gos->ogh);
     321             :   }
     322           5 :   GNUNET_free (gos);
     323           5 : }
     324             : 
     325             : 
     326             : struct TALER_TESTING_Command
     327           5 : TALER_TESTING_cmd_wallet_get_order (const char *label,
     328             :                                     const char *merchant_url,
     329             :                                     const char *order_reference,
     330             :                                     bool paid,
     331             :                                     bool refunded,
     332             :                                     bool refund_pending,
     333             :                                     unsigned int http_status)
     334             : {
     335             :   struct WalletGetOrderState *gos;
     336             : 
     337           5 :   gos = GNUNET_new (struct WalletGetOrderState);
     338           5 :   gos->merchant_url = merchant_url;
     339           5 :   gos->order_reference = order_reference;
     340           5 :   gos->http_status = http_status;
     341           5 :   gos->paid = paid;
     342           5 :   gos->refunded = refunded;
     343           5 :   gos->refund_pending = refund_pending;
     344             :   {
     345           5 :     struct TALER_TESTING_Command cmd = {
     346             :       .cls = gos,
     347             :       .label = label,
     348             :       .run = &wallet_get_order_run,
     349             :       .cleanup = &wallet_get_order_cleanup
     350             :     };
     351             : 
     352           5 :     return cmd;
     353             :   }
     354             : }
     355             : 
     356             : 
     357             : struct WalletPollOrderConcludeState
     358             : {
     359             :   /**
     360             :    * The interpreter state.
     361             :    */
     362             :   struct TALER_TESTING_Interpreter *is;
     363             : 
     364             :   /**
     365             :    * Reference to a command that can provide a poll order start command.
     366             :    */
     367             :   const char *start_reference;
     368             : 
     369             :   /**
     370             :    * Already paid order ID expected, or NULL for none.
     371             :    */
     372             :   const char *already_paid_order_id;
     373             : 
     374             :   /**
     375             :    * Task to wait for the deadline.
     376             :    */
     377             :   struct GNUNET_SCHEDULER_Task *task;
     378             : 
     379             :   /**
     380             :    * Amount of a refund expected.
     381             :    */
     382             :   struct TALER_Amount expected_refund_amount;
     383             : 
     384             :   /**
     385             :    * Expected HTTP response status code.
     386             :    */
     387             :   unsigned int expected_http_status;
     388             : 
     389             :   /**
     390             :    * Are we expecting a refund?
     391             :    */
     392             :   bool expected_refund;
     393             : };
     394             : 
     395             : 
     396             : struct WalletPollOrderStartState
     397             : {
     398             :   /**
     399             :    * The merchant base URL.
     400             :    */
     401             :   const char *merchant_url;
     402             : 
     403             :   /**
     404             :    * The handle to the current GET /orders/$ORDER_ID request.
     405             :    */
     406             :   struct TALER_MERCHANT_OrderWalletGetHandle *ogh;
     407             : 
     408             :   /**
     409             :    * The interpreter state.
     410             :    */
     411             :   struct TALER_TESTING_Interpreter *is;
     412             : 
     413             :   /**
     414             :    * Reference to a command that created an order.
     415             :    */
     416             :   const char *order_ref;
     417             : 
     418             :   /**
     419             :    * Which session ID to poll for.
     420             :    */
     421             :   const char *session_id;
     422             : 
     423             :   /**
     424             :    * How long to wait for server to return a response.
     425             :    */
     426             :   struct GNUNET_TIME_Relative timeout;
     427             : 
     428             :   /**
     429             :    * Conclude state waiting for completion (if any).
     430             :    */
     431             :   struct WalletPollOrderConcludeState *cs;
     432             : 
     433             :   /**
     434             :    * The HTTP status code returned by the backend.
     435             :    */
     436             :   unsigned int http_status;
     437             : 
     438             :   /**
     439             :    * When the request should be completed by.
     440             :    */
     441             :   struct GNUNET_TIME_Absolute deadline;
     442             : 
     443             :   /**
     444             :    * Minimum refund to wait for.
     445             :    */
     446             :   struct TALER_Amount refund_threshold;
     447             : 
     448             :   /**
     449             :    * Available refund as returned by the merchant.
     450             :    */
     451             :   struct TALER_Amount refund_available;
     452             : 
     453             :   /**
     454             :    * Already paid order ID returned, or NULL for none.
     455             :    */
     456             :   char *already_paid_order_id;
     457             : 
     458             :   /**
     459             :    * Should we poll for a refund?
     460             :    */
     461             :   bool wait_for_refund;
     462             : 
     463             :   /**
     464             :    * Did we receive a refund according to response from the merchant?
     465             :    */
     466             :   bool refunded;
     467             : 
     468             :   /**
     469             :    * Was the order paid according to response from the merchant?
     470             :    */
     471             :   bool paid;
     472             : 
     473             :   /**
     474             :    * Has the order a pending refund according to response from the merchant?
     475             :    */
     476             :   bool refund_pending;
     477             : };
     478             : 
     479             : 
     480             : /**
     481             :  * Task called when either the timeout for the GET /private/order/$ID command
     482             :  * expired or we got a response.  Checks if the result is what we expected.
     483             :  *
     484             :  * @param cls a `struct WalletPollOrderConcludeState`
     485             :  */
     486             : static void
     487           4 : conclude_task (void *cls)
     488             : {
     489           4 :   struct WalletPollOrderConcludeState *ppc = cls;
     490             :   const struct TALER_TESTING_Command *poll_cmd;
     491             :   struct WalletPollOrderStartState *cps;
     492             :   struct GNUNET_TIME_Absolute now;
     493             : 
     494           4 :   ppc->task = NULL;
     495             :   poll_cmd =
     496           4 :     TALER_TESTING_interpreter_lookup_command (ppc->is,
     497             :                                               ppc->start_reference);
     498           4 :   if (NULL == poll_cmd)
     499           0 :     TALER_TESTING_FAIL (ppc->is);
     500           4 :   cps = poll_cmd->cls;
     501           4 :   if (NULL != cps->ogh)
     502             :   {
     503           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     504             :                 "Expected poll GET /orders/$ORDER_ID to have completed, but it did not!\n");
     505           0 :     TALER_TESTING_FAIL (ppc->is);
     506             :   }
     507           4 :   if (cps->http_status != ppc->expected_http_status)
     508             :   {
     509           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     510             :                 "Expected HTTP status %u, got %u\n",
     511             :                 ppc->expected_http_status,
     512             :                 cps->http_status);
     513           0 :     TALER_TESTING_FAIL (ppc->is);
     514             :   }
     515           4 :   if (ppc->expected_refund != cps->refunded)
     516             :   {
     517           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     518             :                 "Order was %srefunded, contrary to our expectations\n",
     519             :                 cps->refunded ? "" : "NOT ");
     520           0 :     TALER_TESTING_FAIL (ppc->is);
     521             :   }
     522           4 :   if ( (NULL == ppc->already_paid_order_id)
     523           4 :        ^ (NULL == cps->already_paid_order_id) )
     524             :   {
     525           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     526             :                 "Mismatch in already paid order IDs: %s vs %s\n",
     527             :                 ppc->already_paid_order_id,
     528             :                 cps->already_paid_order_id);
     529           0 :     TALER_TESTING_FAIL (ppc->is);
     530             :   }
     531           4 :   if ( (NULL != ppc->already_paid_order_id) &&
     532           1 :        (0 != strcmp (ppc->already_paid_order_id,
     533           1 :                      cps->already_paid_order_id) ) )
     534             :   {
     535           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     536             :                 "Mismatch in already paid order IDs: %s vs %s\n",
     537             :                 ppc->already_paid_order_id,
     538             :                 cps->already_paid_order_id);
     539           0 :     TALER_TESTING_FAIL (ppc->is);
     540             :   }
     541             : 
     542           4 :   if (cps->refunded)
     543             :   {
     544           2 :     if (0 != TALER_amount_cmp (&ppc->expected_refund_amount,
     545           2 :                                &cps->refund_available))
     546             :     {
     547           0 :       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     548             :                   "Refund amount %s does not match our expectation!\n",
     549             :                   TALER_amount2s (&cps->refund_available));
     550           0 :       TALER_TESTING_FAIL (ppc->is);
     551             :     }
     552             :   }
     553             :   // FIXME: add checks for cps->paid/refund_available status flags?
     554           4 :   now = GNUNET_TIME_absolute_get ();
     555           4 :   if ((GNUNET_TIME_absolute_add (cps->deadline,
     556           4 :                                  GNUNET_TIME_UNIT_SECONDS).abs_value_us <
     557           4 :        now.abs_value_us) )
     558             :   {
     559           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     560             :                 "Expected answer to be delayed until %llu, but got response at %llu\n",
     561             :                 (unsigned long long) cps->deadline.abs_value_us,
     562             :                 (unsigned long long) now.abs_value_us);
     563           0 :     TALER_TESTING_FAIL (ppc->is);
     564             :   }
     565           4 :   TALER_TESTING_interpreter_next (ppc->is);
     566             : }
     567             : 
     568             : 
     569             : /**
     570             :  * Process response from a GET /orders/$ID request
     571             :  *
     572             :  * @param cls a `struct WalletPollOrderStartState *`
     573             :  * @param hr HTTP response details
     574             :  * @param paid #GNUNET_YES if the payment is settled, #GNUNET_NO if not
     575             :  *        settled, #GNUNET_SYSERR on error
     576             :  *        (note that refunded payments are returned as paid!)
     577             :  * @param refunded #GNUNET_YES if there is at least on refund on this payment,
     578             :  *        #GNUNET_NO if refunded, #GNUNET_SYSERR or error
     579             :  * @param refund_pending #GNUNET_YES if there are refunds waiting to be
     580             :  *        obtained, #GNUNET_NO if all refunds have been obtained, #GNUNET_SYSERR
     581             :  *        on error.
     582             :  * @param refund_amount amount that was refunded, NULL if there
     583             :  *        was no refund
     584             :  * @param taler_pay_uri the URI that instructs the wallets to process
     585             :  *                      the payment
     586             :  * @param already_paid_order_id equivalent order that this customer
     587             :  *                      paid already, or NULL for none
     588             :  */
     589             : static void
     590           4 : wallet_poll_order_cb (
     591             :   void *cls,
     592             :   const struct TALER_MERCHANT_HttpResponse *hr,
     593             :   enum GNUNET_GenericReturnValue paid,
     594             :   enum GNUNET_GenericReturnValue refunded,
     595             :   enum GNUNET_GenericReturnValue refund_pending,
     596             :   struct TALER_Amount *refund_amount,
     597             :   const char *taler_pay_uri,
     598             :   const char *already_paid_order_id)
     599             : {
     600           4 :   struct WalletPollOrderStartState *pos = cls;
     601             : 
     602           4 :   pos->ogh = NULL;
     603           4 :   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
     604             :               "GET /orders/$ID finished with status %u.\n",
     605             :               hr->http_status);
     606           4 :   pos->http_status = hr->http_status;
     607           4 :   switch (hr->http_status)
     608             :   {
     609           4 :   case MHD_HTTP_OK:
     610           4 :     pos->paid = (GNUNET_YES == paid);
     611           4 :     pos->refunded = (GNUNET_YES == refunded);
     612           4 :     pos->refund_pending = (GNUNET_YES == refund_pending);
     613           4 :     if (NULL != refund_amount)
     614           2 :       pos->refund_available = *refund_amount;
     615           4 :     if (NULL != already_paid_order_id)
     616           1 :       pos->already_paid_order_id = GNUNET_strdup (already_paid_order_id);
     617           4 :     break;
     618           0 :   default:
     619           0 :     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
     620             :                 "Unhandled HTTP status.\n");
     621           0 :     break;
     622             :   }
     623           4 :   if (NULL != pos->cs)
     624             :   {
     625           3 :     GNUNET_SCHEDULER_cancel (pos->cs->task);
     626           3 :     pos->cs->task = GNUNET_SCHEDULER_add_now (&conclude_task,
     627           3 :                                               pos->cs);
     628             :   }
     629           4 : }
     630             : 
     631             : 
     632             : /**
     633             :  * Run the "GET order" CMD.
     634             :  *
     635             :  * @param cls closure.
     636             :  * @param cmd command being run now.
     637             :  * @param is interpreter state.
     638             :  */
     639             : static void
     640           4 : wallet_poll_order_start_run (void *cls,
     641             :                              const struct TALER_TESTING_Command *cmd,
     642             :                              struct TALER_TESTING_Interpreter *is)
     643             : {
     644           4 :   struct WalletPollOrderStartState *pos = cls;
     645             :   const struct TALER_TESTING_Command *order_cmd;
     646             :   const char *order_id;
     647             :   const struct GNUNET_HashCode *h_contract;
     648             : 
     649           4 :   order_cmd = TALER_TESTING_interpreter_lookup_command (
     650             :     is,
     651             :     pos->order_ref);
     652             : 
     653           4 :   if (GNUNET_OK !=
     654           4 :       TALER_TESTING_get_trait_order_id (order_cmd,
     655             :                                         0,
     656             :                                         &order_id))
     657           0 :     TALER_TESTING_FAIL (is);
     658             : 
     659           4 :   if (GNUNET_OK !=
     660           4 :       TALER_TESTING_get_trait_h_contract_terms (order_cmd,
     661             :                                                 0,
     662             :                                                 &h_contract))
     663           0 :     TALER_TESTING_FAIL (is);
     664             : 
     665             :   /* add 1s grace time to timeout */
     666             :   pos->deadline
     667           4 :     = GNUNET_TIME_absolute_add (GNUNET_TIME_relative_to_absolute (pos->timeout),
     668             :                                 GNUNET_TIME_UNIT_SECONDS);
     669           4 :   pos->is = is;
     670           4 :   pos->ogh = TALER_MERCHANT_wallet_order_get (is->ctx,
     671             :                                               pos->merchant_url,
     672             :                                               order_id,
     673             :                                               h_contract,
     674             :                                               pos->timeout,
     675             :                                               pos->session_id,
     676           4 :                                               pos->wait_for_refund
     677             :                                               ? &pos->refund_threshold
     678             :                                               : NULL,
     679             :                                               false, /* await_refund_obtained */
     680             :                                               &wallet_poll_order_cb,
     681             :                                               pos);
     682           4 :   GNUNET_assert (NULL != pos->ogh);
     683             :   /* We CONTINUE to run the interpreter while the long-polled command
     684             :      completes asynchronously! */
     685           4 :   TALER_TESTING_interpreter_next (pos->is);
     686             : }
     687             : 
     688             : 
     689             : /**
     690             :  * Free the state of a "GET order" CMD, and possibly
     691             :  * cancel a pending operation thereof.
     692             :  *
     693             :  * @param cls closure.
     694             :  * @param cmd command being run.
     695             :  */
     696             : static void
     697           4 : wallet_poll_order_start_cleanup (void *cls,
     698             :                                  const struct TALER_TESTING_Command *cmd)
     699             : {
     700           4 :   struct WalletPollOrderStartState *pos = cls;
     701             : 
     702           4 :   if (NULL != pos->ogh)
     703             :   {
     704           0 :     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
     705             :                 "Command `%s' was not terminated\n",
     706             :                 TALER_TESTING_interpreter_get_current_label (
     707             :                   pos->is));
     708           0 :     TALER_MERCHANT_wallet_order_get_cancel (pos->ogh);
     709             :   }
     710           4 :   GNUNET_free (pos->already_paid_order_id);
     711           4 :   GNUNET_free (pos);
     712           4 : }
     713             : 
     714             : 
     715             : struct TALER_TESTING_Command
     716           4 : TALER_TESTING_cmd_wallet_poll_order_start (
     717             :   const char *label,
     718             :   const char *merchant_url,
     719             :   const char *order_ref,
     720             :   struct GNUNET_TIME_Relative timeout,
     721             :   const char *await_refund)
     722             : {
     723             :   struct WalletPollOrderStartState *pos;
     724             : 
     725           4 :   pos = GNUNET_new (struct WalletPollOrderStartState);
     726           4 :   pos->order_ref = order_ref;
     727           4 :   pos->merchant_url = merchant_url;
     728           4 :   pos->timeout = timeout;
     729           4 :   if (NULL != await_refund)
     730             :   {
     731           2 :     pos->wait_for_refund = true;
     732           2 :     GNUNET_assert (GNUNET_OK ==
     733             :                    TALER_string_to_amount (await_refund,
     734             :                                            &pos->refund_threshold));
     735             :   }
     736             :   {
     737           4 :     struct TALER_TESTING_Command cmd = {
     738             :       .cls = pos,
     739             :       .label = label,
     740             :       .run = &wallet_poll_order_start_run,
     741             :       .cleanup = &wallet_poll_order_start_cleanup
     742             :     };
     743             : 
     744           4 :     return cmd;
     745             :   }
     746             : }
     747             : 
     748             : 
     749             : struct TALER_TESTING_Command
     750           1 : TALER_TESTING_cmd_wallet_poll_order_start2 (
     751             :   const char *label,
     752             :   const char *merchant_url,
     753             :   const char *order_ref,
     754             :   struct GNUNET_TIME_Relative timeout,
     755             :   const char *await_refund,
     756             :   const char *session_id)
     757             : {
     758             :   struct WalletPollOrderStartState *pos;
     759             :   struct TALER_TESTING_Command cmd;
     760             : 
     761           1 :   cmd = TALER_TESTING_cmd_wallet_poll_order_start (label,
     762             :                                                    merchant_url,
     763             :                                                    order_ref,
     764             :                                                    timeout,
     765             :                                                    await_refund);
     766           1 :   pos = cmd.cls;
     767           1 :   pos->session_id = session_id;
     768           1 :   return cmd;
     769             : }
     770             : 
     771             : 
     772             : /**
     773             :  * Run the "GET order conclude" CMD.
     774             :  *
     775             :  * @param cls closure.
     776             :  * @param cmd command being run now.
     777             :  * @param is interpreter state.
     778             :  */
     779             : static void
     780           4 : wallet_poll_order_conclude_run (void *cls,
     781             :                                 const struct TALER_TESTING_Command *cmd,
     782             :                                 struct TALER_TESTING_Interpreter *is)
     783             : {
     784           4 :   struct WalletPollOrderConcludeState *poc = cls;
     785             :   const struct TALER_TESTING_Command *poll_cmd;
     786             :   struct WalletPollOrderStartState *pos;
     787             : 
     788           4 :   poc->is = is;
     789             :   poll_cmd =
     790           4 :     TALER_TESTING_interpreter_lookup_command (is,
     791             :                                               poc->start_reference);
     792           4 :   if (NULL == poll_cmd)
     793           0 :     TALER_TESTING_FAIL (poc->is);
     794           4 :   GNUNET_assert (poll_cmd->run == &wallet_poll_order_start_run);
     795           4 :   pos = poll_cmd->cls;
     796           4 :   pos->cs = poc;
     797           4 :   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
     798             :               "Waiting on GET /orders/$ID of %s (%s)\n",
     799             :               poc->start_reference,
     800             :               (NULL == pos->ogh)
     801             :               ? "finished"
     802             :               : "active");
     803           4 :   if (NULL == pos->ogh)
     804           1 :     poc->task = GNUNET_SCHEDULER_add_now (&conclude_task,
     805             :                                           poc);
     806             :   else
     807           3 :     poc->task = GNUNET_SCHEDULER_add_at (pos->deadline,
     808             :                                          &conclude_task,
     809             :                                          poc);
     810             : }
     811             : 
     812             : 
     813             : /**
     814             :  * Free the state of a "GET order" CMD, and possibly
     815             :  * cancel a pending operation thereof.
     816             :  *
     817             :  * @param cls closure.
     818             :  * @param cmd command being run.
     819             :  */
     820             : static void
     821           4 : wallet_poll_order_conclude_cleanup (void *cls,
     822             :                                     const struct TALER_TESTING_Command *cmd)
     823             : {
     824           4 :   struct WalletPollOrderConcludeState *poc = cls;
     825             : 
     826           4 :   if (NULL != poc->task)
     827             :   {
     828           0 :     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
     829             :                 "Command `%s' was not terminated\n",
     830             :                 TALER_TESTING_interpreter_get_current_label (
     831             :                   poc->is));
     832           0 :     GNUNET_SCHEDULER_cancel (poc->task);
     833           0 :     poc->task = NULL;
     834             :   }
     835           4 :   GNUNET_free (poc);
     836           4 : }
     837             : 
     838             : 
     839             : struct TALER_TESTING_Command
     840           4 : TALER_TESTING_cmd_wallet_poll_order_conclude (
     841             :   const char *label,
     842             :   unsigned int expected_http_status,
     843             :   const char *expected_refund_amount,
     844             :   const char *poll_start_reference)
     845             : {
     846             :   struct WalletPollOrderConcludeState *cps;
     847             : 
     848           4 :   cps = GNUNET_new (struct WalletPollOrderConcludeState);
     849           4 :   cps->start_reference = poll_start_reference;
     850           4 :   cps->expected_http_status = expected_http_status;
     851           4 :   if (NULL != expected_refund_amount)
     852             :   {
     853           2 :     cps->expected_refund = true;
     854           2 :     GNUNET_assert (GNUNET_OK ==
     855             :                    TALER_string_to_amount (expected_refund_amount,
     856             :                                            &cps->expected_refund_amount));
     857             :   }
     858             :   {
     859           4 :     struct TALER_TESTING_Command cmd = {
     860             :       .cls = cps,
     861             :       .label = label,
     862             :       .run = &wallet_poll_order_conclude_run,
     863             :       .cleanup = &wallet_poll_order_conclude_cleanup
     864             :     };
     865             : 
     866           4 :     return cmd;
     867             :   }
     868             : }
     869             : 
     870             : 
     871             : struct TALER_TESTING_Command
     872           1 : TALER_TESTING_cmd_wallet_poll_order_conclude2 (
     873             :   const char *label,
     874             :   unsigned int expected_http_status,
     875             :   const char *expected_refund_amount,
     876             :   const char *poll_start_reference,
     877             :   const char *already_paid_order_id)
     878             : {
     879             :   struct WalletPollOrderConcludeState *cps;
     880             :   struct TALER_TESTING_Command cmd;
     881             : 
     882           1 :   cmd = TALER_TESTING_cmd_wallet_poll_order_conclude (
     883             :     label,
     884             :     expected_http_status,
     885             :     expected_refund_amount,
     886             :     poll_start_reference);
     887           1 :   cps = cmd.cls;
     888           1 :   cps->already_paid_order_id = already_paid_order_id;
     889           1 :   return cmd;
     890             : }
     891             : 
     892             : 
     893             : /* end of testing_api_cmd_wallet_get_order.c */

Generated by: LCOV version 1.14