LCOV - code coverage report
Current view: top level - testing - testing_api_cmd_merchant_get_order.c (source / functions) Hit Total Coverage
Test: GNU Taler merchant coverage report Lines: 226 338 66.9 %
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_merchant_get_order.c
      21             :  * @brief command to test GET /private/orders/$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 /private/orders/$ORDER_ID CMD.
      33             :  */
      34             : struct MerchantGetOrderState
      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 /private/orders/$ORDER_ID request.
      48             :    */
      49             :   struct TALER_MERCHANT_OrderMerchantGetHandle *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             :    * Expected order status.
      63             :    */
      64             :   enum TALER_MERCHANT_OrderStatusCode osc;
      65             : 
      66             :   /**
      67             :    * A NULL-terminated list of refunds associated with this order.
      68             :    */
      69             :   const char **refunds;
      70             : 
      71             :   /**
      72             :    * The length of @e refunds.
      73             :    */
      74             :   unsigned int refunds_length;
      75             : 
      76             :   /**
      77             :    * A NULL-terminated list of transfers associated with this order.
      78             :    */
      79             :   const char **transfers;
      80             : 
      81             :   /**
      82             :    * The length of @e transfers.
      83             :    */
      84             :   unsigned int transfers_length;
      85             : 
      86             :   /**
      87             :    * A list of forget commands that apply to this order's contract terms.
      88             :    */
      89             :   const char **forgets;
      90             : 
      91             :   /**
      92             :    * The length of @e forgets.
      93             :    */
      94             :   unsigned int forgets_length;
      95             : 
      96             :   /**
      97             :    * Whether the order was refunded or not.
      98             :    */
      99             :   bool refunded;
     100             : 
     101             :   /**
     102             :    * Whether the order was wired or not.
     103             :    */
     104             :   bool wired;
     105             : };
     106             : 
     107             : 
     108             : /**
     109             :  * Forget part of the contract terms.
     110             :  *
     111             :  * @param cls pointer to the result of the forget operation.
     112             :  * @param object_id name of the object to forget.
     113             :  * @param parent parent of the object at @e object_id.
     114             :  */
     115             : static void
     116           7 : apply_forget (void *cls,
     117             :               const char *object_id,
     118             :               json_t *parent)
     119             : {
     120           7 :   int *res = cls;
     121             : 
     122           7 :   if (GNUNET_SYSERR ==
     123           7 :       TALER_JSON_contract_part_forget (parent,
     124             :                                        object_id))
     125           0 :     *res = GNUNET_SYSERR;
     126           7 : }
     127             : 
     128             : 
     129             : /**
     130             :  * Callback to process a GET /orders/$ID request
     131             :  *
     132             :  * @param cls closure
     133             :  * @param hr HTTP response details
     134             :  * @param osr order status response details (on success)
     135             :  */
     136             : static void
     137           9 : merchant_get_order_cb (
     138             :   void *cls,
     139             :   const struct TALER_MERCHANT_HttpResponse *hr,
     140             :   const struct TALER_MERCHANT_OrderStatusResponse *osr)
     141             : {
     142             :   /* FIXME, deeper checks should be implemented here. */
     143           9 :   struct MerchantGetOrderState *gos = cls;
     144             : 
     145           9 :   gos->ogh = NULL;
     146           9 :   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
     147             :               "GET /private/orders/$ID completed with status %u\n",
     148             :               hr->http_status);
     149           9 :   if (gos->http_status != hr->http_status)
     150             :   {
     151           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     152             :                 "Unexpected response code %u (%d) to command %s\n",
     153             :                 hr->http_status,
     154             :                 (int) hr->ec,
     155             :                 TALER_TESTING_interpreter_get_current_label (gos->is));
     156           0 :     TALER_TESTING_interpreter_fail (gos->is);
     157           0 :     return;
     158             :   }
     159           9 :   switch (hr->http_status)
     160             :   {
     161           9 :   case MHD_HTTP_OK:
     162             :     // FIXME: use gts->tip_reference here to
     163             :     // check if the data returned matches that from the POST / PATCH
     164           9 :     if (gos->osc != osr->status)
     165             :     {
     166           0 :       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     167             :                   "Order paid does not match\n");
     168           0 :       TALER_TESTING_interpreter_fail (gos->is);
     169           0 :       return;
     170             :     }
     171           9 :     switch (osr->status)
     172             :     {
     173           7 :     case TALER_MERCHANT_OSC_PAID:
     174             :       {
     175             :         const struct TALER_TESTING_Command *order_cmd;
     176             :         struct TALER_Amount refunded_total;
     177             : 
     178           7 :         order_cmd = TALER_TESTING_interpreter_lookup_command (
     179             :           gos->is,
     180             :           gos->order_reference);
     181             : 
     182             :         {
     183             :           const json_t *expected_contract_terms;
     184             :           json_t *ct;
     185             : 
     186           7 :           if (GNUNET_OK !=
     187           7 :               TALER_TESTING_get_trait_contract_terms (order_cmd,
     188             :                                                       0,
     189             :                                                       &expected_contract_terms))
     190             :           {
     191           0 :             GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     192             :                         "Could not fetch order contract terms\n");
     193           0 :             TALER_TESTING_interpreter_fail (gos->is);
     194           0 :             return;
     195             :           }
     196             : 
     197           7 :           ct = json_deep_copy (expected_contract_terms);
     198             : 
     199             :           /* Apply all forgets, then compare */
     200          13 :           for (unsigned int i = 0; i < gos->forgets_length; ++i)
     201             :           {
     202             :             const struct TALER_TESTING_Command *forget_cmd;
     203             :             const unsigned int *paths_length;
     204             : 
     205           6 :             forget_cmd = TALER_TESTING_interpreter_lookup_command (
     206             :               gos->is,
     207           6 :               gos->forgets[i]);
     208             : 
     209           6 :             if (GNUNET_OK !=
     210           6 :                 TALER_TESTING_get_trait_uint32 (forget_cmd,
     211             :                                                 0,
     212             :                                                 &paths_length))
     213             :             {
     214           0 :               GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     215             :                           "Couldn't fetch forget paths length\n");
     216           0 :               TALER_TESTING_interpreter_fail (gos->is);
     217           0 :               return;
     218             :             }
     219             : 
     220          12 :             for (unsigned int j = 0; j < *paths_length; ++j)
     221             :             {
     222             :               const char *path;
     223           6 :               int res = GNUNET_OK;
     224             : 
     225           6 :               if (GNUNET_OK !=
     226           6 :                   TALER_TESTING_get_trait_string (forget_cmd,
     227             :                                                   j,
     228             :                                                   &path))
     229             :               {
     230           0 :                 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     231             :                             "Couldn't fetch forget path\n");
     232           0 :                 TALER_TESTING_interpreter_fail (gos->is);
     233           0 :                 return;
     234             :               }
     235             : 
     236           6 :               GNUNET_assert (GNUNET_OK ==
     237             :                              TALER_JSON_expand_path (ct,
     238             :                                                      path,
     239             :                                                      &apply_forget,
     240             :                                                      &res));
     241           6 :               GNUNET_assert (GNUNET_OK == res);
     242             :             }
     243             :           }
     244             : 
     245           7 :           if (1 != json_equal (ct,
     246             :                                osr->details.paid.contract_terms))
     247             :           {
     248           0 :             GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     249             :                         "Order contract terms do not match\n");
     250           0 :             TALER_TESTING_interpreter_fail (gos->is);
     251           0 :             return;
     252             :           }
     253             : 
     254           7 :           json_decref (ct);
     255             :         }
     256           7 :         if (gos->wired != osr->details.paid.wired)
     257             :         {
     258           0 :           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     259             :                       "Order wired does not match\n");
     260           0 :           TALER_TESTING_interpreter_fail (gos->is);
     261           0 :           return;
     262             :         }
     263           7 :         if (gos->transfers_length != osr->details.paid.wts_len)
     264             :         {
     265           0 :           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     266             :                       "Number of transfers found does not match\n");
     267           0 :           TALER_TESTING_interpreter_fail (gos->is);
     268           0 :           return;
     269             :         }
     270          11 :         for (unsigned int i = 0; i < gos->transfers_length; ++i)
     271             :         {
     272             :           const struct TALER_TESTING_Command *transfer_cmd;
     273             : 
     274           4 :           transfer_cmd = TALER_TESTING_interpreter_lookup_command (
     275             :             gos->is,
     276           4 :             gos->transfers[i]);
     277             :           {
     278             :             const struct TALER_WireTransferIdentifierRawP *wtid;
     279             : 
     280           4 :             if (GNUNET_OK !=
     281           4 :                 TALER_TESTING_get_trait_wtid (transfer_cmd,
     282             :                                               0,
     283             :                                               &wtid))
     284             :             {
     285           0 :               GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     286             :                           "Could not fetch wire transfer id\n");
     287           0 :               TALER_TESTING_interpreter_fail (gos->is);
     288           0 :               return;
     289             :             }
     290           4 :             if (0 != GNUNET_memcmp (wtid,
     291             :                                     &osr->details.paid.wts[i].wtid))
     292             :             {
     293           0 :               GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     294             :                           "Wire transfer id does not match\n");
     295           0 :               TALER_TESTING_interpreter_fail (gos->is);
     296           0 :               return;
     297             :             }
     298             :           }
     299             :           {
     300             :             const char *exchange_url;
     301             : 
     302           4 :             if (GNUNET_OK !=
     303           4 :                 TALER_TESTING_get_trait_string (transfer_cmd,
     304             :                                                 1,
     305             :                                                 &exchange_url))
     306             :             {
     307           0 :               GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     308             :                           "Could not fetch wire transfer exchange url\n");
     309           0 :               TALER_TESTING_interpreter_fail (gos->is);
     310           0 :               return;
     311             :             }
     312           4 :             if (0 != strcmp (exchange_url,
     313           4 :                              osr->details.paid.wts[i].exchange_url))
     314             :             {
     315           0 :               GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     316             :                           "Wire transfer exchange url does not match\n");
     317           0 :               TALER_TESTING_interpreter_fail (gos->is);
     318           0 :               return;
     319             :             }
     320             :           }
     321             :           {
     322             :             struct TALER_Amount transfer_total;
     323             :             const struct TALER_Amount *transfer_amount;
     324             :             const struct TALER_Amount *transfer_fee;
     325             : 
     326           4 :             if ((GNUNET_OK !=
     327           4 :                  TALER_TESTING_get_trait_amount_obj (transfer_cmd,
     328             :                                                      0,
     329           4 :                                                      &transfer_amount)) ||
     330             :                 (GNUNET_OK !=
     331           4 :                  TALER_TESTING_get_trait_amount_obj (transfer_cmd,
     332             :                                                      1,
     333             :                                                      &transfer_fee)))
     334             :             {
     335           0 :               GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     336             :                           "Could not fetch wire transfer amount/fee\n");
     337           0 :               TALER_TESTING_interpreter_fail (gos->is);
     338           0 :               return;
     339             :             }
     340           4 :             if (0 > TALER_amount_add (&transfer_total,
     341             :                                       transfer_amount,
     342             :                                       transfer_fee))
     343             :             {
     344           0 :               GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     345             :                           "Could not total wire transfer\n");
     346           0 :               TALER_TESTING_interpreter_fail (gos->is);
     347           0 :               return;
     348             :             }
     349           4 :             if ((GNUNET_OK != TALER_amount_cmp_currency (&transfer_total,
     350           4 :                                                          &osr->details.paid.wts[
     351             :                                                            i]
     352           4 :                                                          .total_amount)) ||
     353           4 :                 (0 != TALER_amount_cmp (&transfer_total,
     354           4 :                                         &osr->details.paid.wts[i].total_amount)))
     355             :             {
     356           0 :               GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     357             :                           "Wire transfer total does not match\n");
     358           0 :               TALER_TESTING_interpreter_fail (gos->is);
     359           0 :               return;
     360             :             }
     361             :           }
     362             :         }
     363           7 :         if (gos->refunded != osr->details.paid.refunded)
     364             :         {
     365           0 :           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     366             :                       "Order refunded does not match\n");
     367           0 :           TALER_TESTING_interpreter_fail (gos->is);
     368           0 :           return;
     369             :         }
     370           7 :         if (gos->refunds_length != osr->details.paid.refunds_len)
     371             :         {
     372           0 :           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     373             :                       "Number of refunds found does not match\n");
     374           0 :           TALER_TESTING_interpreter_fail (gos->is);
     375           0 :           return;
     376             :         }
     377           7 :         if (0 < gos->refunds_length)
     378           2 :           GNUNET_assert (GNUNET_OK ==
     379             :                          TALER_amount_set_zero (
     380             :                            osr->details.paid.refund_amount.currency,
     381             :                            &refunded_total));
     382          11 :         for (unsigned int i = 0; i < gos->refunds_length; ++i)
     383             :         {
     384             :           const struct TALER_TESTING_Command *refund_cmd;
     385             : 
     386           4 :           refund_cmd = TALER_TESTING_interpreter_lookup_command (
     387             :             gos->is,
     388           4 :             gos->refunds[i]);
     389             :           {
     390             :             const char *expected_amount_str;
     391             :             struct TALER_Amount expected_amount;
     392           4 :             struct TALER_Amount *amount_found =
     393           4 :               &osr->details.paid.refunds[i].refund_amount;
     394             : 
     395           4 :             if (GNUNET_OK !=
     396           4 :                 TALER_TESTING_get_trait_string (refund_cmd,
     397             :                                                 0,
     398             :                                                 &expected_amount_str))
     399             :             {
     400           0 :               GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     401             :                           "Could not fetch refund amount\n");
     402           0 :               TALER_TESTING_interpreter_fail (gos->is);
     403           0 :               return;
     404             :             }
     405           4 :             GNUNET_assert (GNUNET_OK ==
     406             :                            TALER_string_to_amount (expected_amount_str,
     407             :                                                    &expected_amount));
     408           4 :             GNUNET_assert (0 <= TALER_amount_add (&refunded_total,
     409             :                                                   &refunded_total,
     410             :                                                   amount_found));
     411           4 :             if ((GNUNET_OK !=
     412           4 :                  TALER_amount_cmp_currency (&expected_amount,
     413           4 :                                             &refunded_total)) ||
     414           4 :                 (0 != TALER_amount_cmp (&expected_amount,
     415             :                                         &refunded_total)))
     416             :             {
     417           0 :               GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     418             :                           "Refund amounts do not match\n");
     419           0 :               TALER_TESTING_interpreter_fail (gos->is);
     420           0 :               return;
     421             :             }
     422             :           }
     423             :           {
     424             :             const char *expected_reason;
     425             : 
     426           4 :             if (GNUNET_OK !=
     427           4 :                 TALER_TESTING_get_trait_string (refund_cmd,
     428             :                                                 1,
     429             :                                                 &expected_reason))
     430             :             {
     431           0 :               GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     432             :                           "Could not fetch reason\n");
     433           0 :               TALER_TESTING_interpreter_fail (gos->is);
     434           0 :               return;
     435             :             }
     436           4 :             if (0 != strcmp (expected_reason,
     437           4 :                              osr->details.paid.refunds[i].reason))
     438             :             {
     439           0 :               GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     440             :                           "Refund reason does not match\n");
     441           0 :               TALER_TESTING_interpreter_fail (gos->is);
     442           0 :               return;
     443             :             }
     444             :           }
     445             :         }
     446             : 
     447           7 :         if (gos->wired != osr->details.paid.wired)
     448             :         {
     449           0 :           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     450             :                       "Order wired does not match\n");
     451           0 :           TALER_TESTING_interpreter_fail (gos->is);
     452           0 :           return;
     453             :         }
     454             :       }
     455           7 :       break;
     456           1 :     case TALER_MERCHANT_OSC_CLAIMED:
     457             :       /* FIXME: Check contract terms... */
     458           1 :       break;
     459           1 :     case TALER_MERCHANT_OSC_UNPAID:
     460             :       {
     461             :         /* FIXME: Check all of the members of `pud` */
     462             :         struct TALER_MERCHANT_PayUriData pud;
     463             :         const struct TALER_TESTING_Command *order_cmd;
     464             :         const char *order_id;
     465             :         const struct TALER_ClaimTokenP *claim_token;
     466             : 
     467           1 :         if (GNUNET_OK !=
     468           1 :             TALER_MERCHANT_parse_pay_uri (osr->details.unpaid.taler_pay_uri,
     469             :                                           &pud))
     470             :         {
     471           0 :           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     472             :                       "Taler pay uri `%s' is malformed\n",
     473             :                       osr->details.unpaid.taler_pay_uri);
     474           0 :           TALER_TESTING_interpreter_fail (gos->is);
     475           0 :           return;
     476             :         }
     477             : 
     478           1 :         order_cmd = TALER_TESTING_interpreter_lookup_command (
     479             :           gos->is,
     480             :           gos->order_reference);
     481             : 
     482           1 :         if (GNUNET_OK !=
     483           1 :             TALER_TESTING_get_trait_order_id (order_cmd,
     484             :                                               0,
     485             :                                               &order_id))
     486             :         {
     487           0 :           TALER_MERCHANT_parse_pay_uri_free (&pud);
     488           0 :           TALER_TESTING_FAIL (gos->is);
     489             :         }
     490             : 
     491           1 :         if (GNUNET_OK !=
     492           1 :             TALER_TESTING_get_trait_claim_token (order_cmd,
     493             :                                                  0,
     494             :                                                  &claim_token))
     495             :         {
     496           0 :           TALER_MERCHANT_parse_pay_uri_free (&pud);
     497           0 :           TALER_TESTING_FAIL (gos->is);
     498             :         }
     499             :         {
     500             :           char *port;
     501             :           char *host;
     502             : 
     503           1 :           if (GNUNET_OK !=
     504           1 :               GNUNET_CONFIGURATION_get_value_string (gos->is->cfg,
     505             :                                                      "merchant",
     506             :                                                      "PORT",
     507             :                                                      &port))
     508             :           {
     509             :             /* How did we get here without a configured port? */
     510           0 :             GNUNET_break (0);
     511           0 :             TALER_TESTING_interpreter_fail (gos->is);
     512           0 :             TALER_MERCHANT_parse_pay_uri_free (&pud);
     513           0 :             return;
     514             :           }
     515           1 :           GNUNET_asprintf (&host,
     516             :                            "localhost:%s",
     517             :                            port);
     518           1 :           GNUNET_free (port);
     519           1 :           if ((0 != strcmp (host,
     520           1 :                             pud.merchant_host)) ||
     521           1 :               (NULL != pud.merchant_prefix_path) ||
     522           1 :               (0 != strcmp (order_id,
     523           1 :                             pud.order_id)) ||
     524           1 :               (NULL != pud.ssid))
     525             :           {
     526           0 :             GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     527             :                         "Order pay uri `%s' does not match, wanted %s/%s\n",
     528             :                         osr->details.unpaid.taler_pay_uri,
     529             :                         host,
     530             :                         order_id);
     531           0 :             TALER_TESTING_interpreter_fail (gos->is);
     532           0 :             TALER_MERCHANT_parse_pay_uri_free (&pud);
     533           0 :             GNUNET_free (host);
     534           0 :             return;
     535             :           }
     536           1 :           GNUNET_free (host);
     537             :         }
     538             :         /* The claim token is not given in the pay uri if the order
     539             :            has been claimed already. */
     540           1 :         if ( (NULL != pud.claim_token) &&
     541           1 :              ( (NULL == claim_token) ||
     542           1 :                (0 != GNUNET_memcmp (claim_token,
     543             :                                     pud.claim_token)) ) )
     544             :         {
     545           0 :           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     546             :                       "Order pay uri claim token does not match (%d/%d/%d/%d)\n",
     547             :                       NULL == pud.claim_token,
     548             :                       NULL == claim_token,
     549             :                       (NULL != pud.claim_token) &&
     550             :                       GNUNET_is_zero (pud.claim_token),
     551             :                       (NULL != claim_token) &&
     552             :                       GNUNET_is_zero (claim_token));
     553           0 :           TALER_TESTING_interpreter_fail (gos->is);
     554           0 :           TALER_MERCHANT_parse_pay_uri_free (&pud);
     555           0 :           return;
     556             :         }
     557           1 :         TALER_MERCHANT_parse_pay_uri_free (&pud);
     558             :       }
     559           1 :       break;
     560             :     }
     561           9 :     break;
     562           0 :   default:
     563           0 :     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
     564             :                 "Unhandled HTTP status.\n");
     565             :   }
     566           9 :   TALER_TESTING_interpreter_next (gos->is);
     567             : }
     568             : 
     569             : 
     570             : /**
     571             :  * Run the "GET order" CMD.
     572             :  *
     573             :  * @param cls closure.
     574             :  * @param cmd command being run now.
     575             :  * @param is interpreter state.
     576             :  */
     577             : static void
     578           9 : merchant_get_order_run (void *cls,
     579             :                         const struct TALER_TESTING_Command *cmd,
     580             :                         struct TALER_TESTING_Interpreter *is)
     581             : {
     582           9 :   struct MerchantGetOrderState *gos = cls;
     583             :   const struct TALER_TESTING_Command *order_cmd;
     584             :   const char *order_id;
     585             :   const struct GNUNET_HashCode *h_contract;
     586             : 
     587           9 :   order_cmd = TALER_TESTING_interpreter_lookup_command (
     588             :     is,
     589             :     gos->order_reference);
     590             : 
     591           9 :   if (GNUNET_OK !=
     592           9 :       TALER_TESTING_get_trait_order_id (order_cmd,
     593             :                                         0,
     594             :                                         &order_id))
     595           0 :     TALER_TESTING_FAIL (is);
     596             : 
     597           9 :   if (GNUNET_OK !=
     598           9 :       TALER_TESTING_get_trait_h_contract_terms (order_cmd,
     599             :                                                 0,
     600             :                                                 &h_contract))
     601           0 :     TALER_TESTING_FAIL (is);
     602             : 
     603           9 :   gos->is = is;
     604           9 :   gos->ogh = TALER_MERCHANT_merchant_order_get (is->ctx,
     605             :                                                 gos->merchant_url,
     606             :                                                 order_id,
     607             :                                                 NULL,
     608             :                                                 true,
     609             :                                                 GNUNET_TIME_UNIT_ZERO,
     610             :                                                 &merchant_get_order_cb,
     611             :                                                 gos);
     612             : }
     613             : 
     614             : 
     615             : /**
     616             :  * Free the state of a "GET order" CMD, and possibly
     617             :  * cancel a pending operation thereof.
     618             :  *
     619             :  * @param cls closure.
     620             :  * @param cmd command being run.
     621             :  */
     622             : static void
     623           9 : merchant_get_order_cleanup (void *cls,
     624             :                             const struct TALER_TESTING_Command *cmd)
     625             : {
     626           9 :   struct MerchantGetOrderState *gos = cls;
     627             : 
     628           9 :   if (NULL != gos->ogh)
     629             :   {
     630           0 :     TALER_LOG_WARNING ("Get tip operation did not complete\n");
     631           0 :     TALER_MERCHANT_merchant_order_get_cancel (gos->ogh);
     632             :   }
     633           9 :   GNUNET_array_grow (gos->transfers,
     634             :                      gos->transfers_length,
     635             :                      0);
     636           9 :   GNUNET_array_grow (gos->refunds,
     637             :                      gos->refunds_length,
     638             :                      0);
     639           9 :   GNUNET_array_grow (gos->forgets,
     640             :                      gos->forgets_length,
     641             :                      0);
     642           9 :   GNUNET_free (gos);
     643           9 : }
     644             : 
     645             : 
     646             : struct TALER_TESTING_Command
     647           4 : TALER_TESTING_cmd_merchant_get_order (const char *label,
     648             :                                       const char *merchant_url,
     649             :                                       const char *order_reference,
     650             :                                       enum TALER_MERCHANT_OrderStatusCode osc,
     651             :                                       bool refunded,
     652             :                                       unsigned int http_status,
     653             :                                       ...)
     654             : {
     655             :   struct MerchantGetOrderState *gos;
     656             : 
     657           4 :   gos = GNUNET_new (struct MerchantGetOrderState);
     658           4 :   gos->merchant_url = merchant_url;
     659           4 :   gos->order_reference = order_reference;
     660           4 :   gos->osc = osc;
     661           4 :   gos->refunded = refunded;
     662           4 :   gos->http_status = http_status;
     663           4 :   if (refunded)
     664             :   {
     665             :     const char *clabel;
     666             :     va_list ap;
     667             : 
     668           1 :     va_start (ap, http_status);
     669           3 :     while (NULL != (clabel = va_arg (ap, const char *)))
     670             :     {
     671           2 :       GNUNET_array_append (gos->refunds,
     672             :                            gos->refunds_length,
     673             :                            clabel);
     674             :     }
     675           1 :     va_end (ap);
     676             :   }
     677             :   {
     678           4 :     struct TALER_TESTING_Command cmd = {
     679             :       .cls = gos,
     680             :       .label = label,
     681             :       .run = &merchant_get_order_run,
     682             :       .cleanup = &merchant_get_order_cleanup
     683             :     };
     684             : 
     685           4 :     return cmd;
     686             :   }
     687             : }
     688             : 
     689             : 
     690             : struct TALER_TESTING_Command
     691           5 : TALER_TESTING_cmd_merchant_get_order2 (const char *label,
     692             :                                        const char *merchant_url,
     693             :                                        const char *order_reference,
     694             :                                        enum TALER_MERCHANT_OrderStatusCode osc,
     695             :                                        bool wired,
     696             :                                        const char **transfers,
     697             :                                        bool refunded,
     698             :                                        const char **refunds,
     699             :                                        const char **forgets,
     700             :                                        unsigned int http_status)
     701             : {
     702             :   struct MerchantGetOrderState *gos;
     703             : 
     704           5 :   gos = GNUNET_new (struct MerchantGetOrderState);
     705           5 :   gos->merchant_url = merchant_url;
     706           5 :   gos->order_reference = order_reference;
     707           5 :   gos->osc = osc;
     708           5 :   gos->wired = wired;
     709           5 :   gos->refunded = refunded;
     710           5 :   gos->http_status = http_status;
     711           5 :   gos->transfers = NULL;
     712           5 :   gos->transfers_length = 0;
     713           5 :   gos->refunds = NULL;
     714           5 :   gos->refunds_length = 0;
     715           5 :   gos->forgets = NULL;
     716           5 :   gos->forgets_length = 0;
     717           5 :   if (wired)
     718             :   {
     719           8 :     for (const char **clabel = transfers; *clabel != NULL; ++clabel)
     720             :     {
     721           4 :       GNUNET_array_append (gos->transfers,
     722             :                            gos->transfers_length,
     723             :                            *clabel);
     724             :     }
     725             :   }
     726           5 :   if (refunded)
     727             :   {
     728           3 :     for (const char **clabel = refunds; *clabel != NULL; ++clabel)
     729             :     {
     730           2 :       GNUNET_array_append (gos->refunds,
     731             :                            gos->refunds_length,
     732             :                            *clabel);
     733             :     }
     734             :   }
     735           5 :   if (NULL != forgets)
     736             :   {
     737           9 :     for (const char **clabel = forgets; *clabel != NULL; ++clabel)
     738             :     {
     739           6 :       GNUNET_array_append (gos->forgets,
     740             :                            gos->forgets_length,
     741             :                            *clabel);
     742             :     }
     743             :   }
     744             :   {
     745           5 :     struct TALER_TESTING_Command cmd = {
     746             :       .cls = gos,
     747             :       .label = label,
     748             :       .run = &merchant_get_order_run,
     749             :       .cleanup = &merchant_get_order_cleanup
     750             :     };
     751             : 
     752           5 :     return cmd;
     753             :   }
     754             : }
     755             : 
     756             : 
     757             : struct MerchantPollOrderConcludeState
     758             : {
     759             :   /**
     760             :    * The interpreter state.
     761             :    */
     762             :   struct TALER_TESTING_Interpreter *is;
     763             : 
     764             :   /**
     765             :    * Reference to a command that can provide a poll order start command.
     766             :    */
     767             :   const char *start_reference;
     768             : 
     769             :   /**
     770             :    * Task to wait for the deadline.
     771             :    */
     772             :   struct GNUNET_SCHEDULER_Task *task;
     773             : 
     774             :   /**
     775             :    * Expected HTTP response status code.
     776             :    */
     777             :   unsigned int expected_http_status;
     778             : };
     779             : 
     780             : 
     781             : struct MerchantPollOrderStartState
     782             : {
     783             :   /**
     784             :    * The merchant base URL.
     785             :    */
     786             :   const char *merchant_url;
     787             : 
     788             :   /**
     789             :    * The handle to the current GET /private/orders/$ORDER_ID request.
     790             :    */
     791             :   struct TALER_MERCHANT_OrderMerchantGetHandle *ogh;
     792             : 
     793             :   /**
     794             :    * The interpreter state.
     795             :    */
     796             :   struct TALER_TESTING_Interpreter *is;
     797             : 
     798             :   /**
     799             :    * Reference to a command that created an order.
     800             :    */
     801             :   const char *order_id;
     802             : 
     803             :   /**
     804             :    * How long to wait for server to return a response.
     805             :    */
     806             :   struct GNUNET_TIME_Relative timeout;
     807             : 
     808             :   /**
     809             :    * Conclude state waiting for completion (if any).
     810             :    */
     811             :   struct MerchantPollOrderConcludeState *cs;
     812             : 
     813             :   /**
     814             :    * The HTTP status code returned by the backend.
     815             :    */
     816             :   unsigned int http_status;
     817             : 
     818             :   /**
     819             :    * When the request should be completed by.
     820             :    */
     821             :   struct GNUNET_TIME_Absolute deadline;
     822             : };
     823             : 
     824             : 
     825             : /**
     826             :  * Task called when either the timeout for the GET /private/order/$ID command
     827             :  * expired or we got a response.  Checks if the result is what we expected.
     828             :  *
     829             :  * @param cls a `struct MerchantPollOrderConcludeState`
     830             :  */
     831             : static void
     832           2 : conclude_task (void *cls)
     833             : {
     834           2 :   struct MerchantPollOrderConcludeState *ppc = cls;
     835             :   const struct TALER_TESTING_Command *poll_cmd;
     836             :   struct MerchantPollOrderStartState *cps;
     837             :   struct GNUNET_TIME_Absolute now;
     838             : 
     839           2 :   ppc->task = NULL;
     840             :   poll_cmd =
     841           2 :     TALER_TESTING_interpreter_lookup_command (ppc->is,
     842             :                                               ppc->start_reference);
     843           2 :   if (NULL == poll_cmd)
     844           0 :     TALER_TESTING_FAIL (ppc->is);
     845           2 :   cps = poll_cmd->cls;
     846           2 :   if (NULL != cps->ogh)
     847             :   {
     848           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     849             :                 "Expected poll GET /private/orders/$ORDER_ID to have completed, but it did not!\n");
     850           0 :     TALER_TESTING_FAIL (ppc->is);
     851             :   }
     852           2 :   if (cps->http_status != ppc->expected_http_status)
     853             :   {
     854           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     855             :                 "Expected HTTP status %u, got %u\n",
     856             :                 ppc->expected_http_status,
     857             :                 cps->http_status);
     858           0 :     TALER_TESTING_FAIL (ppc->is);
     859             :   }
     860           2 :   now = GNUNET_TIME_absolute_get ();
     861           2 :   if ((GNUNET_TIME_absolute_add (cps->deadline,
     862           2 :                                  GNUNET_TIME_UNIT_SECONDS).abs_value_us <
     863           2 :        now.abs_value_us) )
     864             :   {
     865           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     866             :                 "Expected answer to be delayed until %llu, but got response at %llu\n",
     867             :                 (unsigned long long) cps->deadline.abs_value_us,
     868             :                 (unsigned long long) now.abs_value_us);
     869           0 :     TALER_TESTING_FAIL (ppc->is);
     870             :   }
     871           2 :   TALER_TESTING_interpreter_next (ppc->is);
     872             : }
     873             : 
     874             : 
     875             : /**
     876             :  * Callback to process a GET /private/orders/$ID request
     877             :  *
     878             :  * @param cls closure
     879             :  * @param hr HTTP response details
     880             :  * @param osr order status response details (on success)
     881             :  */
     882             : static void
     883           2 : merchant_poll_order_cb (
     884             :   void *cls,
     885             :   const struct TALER_MERCHANT_HttpResponse *hr,
     886             :   const struct TALER_MERCHANT_OrderStatusResponse *osr)
     887             : {
     888             :   /* FIXME, deeper checks should be implemented here. */
     889           2 :   struct MerchantPollOrderStartState *pos = cls;
     890             : 
     891           2 :   pos->ogh = NULL;
     892           2 :   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
     893             :               "GET /private/orders/$ID finished with status %u.\n",
     894             :               hr->http_status);
     895           2 :   pos->http_status = hr->http_status;
     896           2 :   switch (hr->http_status)
     897             :   {
     898           2 :   case MHD_HTTP_OK:
     899             :     // FIXME: keep data from 'osr' here for checking?
     900           2 :     break;
     901           0 :   default:
     902           0 :     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
     903             :                 "Unhandled HTTP status.\n");
     904             :   }
     905           2 :   if (NULL != pos->cs)
     906             :   {
     907           0 :     GNUNET_SCHEDULER_cancel (pos->cs->task);
     908           0 :     pos->cs->task = GNUNET_SCHEDULER_add_now (&conclude_task,
     909           0 :                                               pos->cs);
     910             :   }
     911           2 : }
     912             : 
     913             : 
     914             : /**
     915             :  * Run the "GET order" CMD.
     916             :  *
     917             :  * @param cls closure.
     918             :  * @param cmd command being run now.
     919             :  * @param is interpreter state.
     920             :  */
     921             : static void
     922           2 : merchant_poll_order_start_run (void *cls,
     923             :                                const struct TALER_TESTING_Command *cmd,
     924             :                                struct TALER_TESTING_Interpreter *is)
     925             : {
     926           2 :   struct MerchantPollOrderStartState *pos = cls;
     927             : 
     928             :   /* add 1s grace time to timeout */
     929             :   pos->deadline
     930           2 :     = GNUNET_TIME_absolute_add (GNUNET_TIME_relative_to_absolute (pos->timeout),
     931             :                                 GNUNET_TIME_UNIT_SECONDS);
     932           2 :   pos->is = is;
     933           2 :   pos->ogh = TALER_MERCHANT_merchant_order_get (is->ctx,
     934             :                                                 pos->merchant_url,
     935             :                                                 pos->order_id,
     936             :                                                 NULL,
     937             :                                                 false,
     938             :                                                 pos->timeout,
     939             :                                                 &merchant_poll_order_cb,
     940             :                                                 pos);
     941           2 :   GNUNET_assert (NULL != pos->ogh);
     942             :   /* We CONTINUE to run the interpreter while the long-polled command
     943             :      completes asynchronously! */
     944           2 :   TALER_TESTING_interpreter_next (pos->is);
     945           2 : }
     946             : 
     947             : 
     948             : /**
     949             :  * Free the state of a "GET order" CMD, and possibly
     950             :  * cancel a pending operation thereof.
     951             :  *
     952             :  * @param cls closure.
     953             :  * @param cmd command being run.
     954             :  */
     955             : static void
     956           2 : merchant_poll_order_start_cleanup (void *cls,
     957             :                                    const struct TALER_TESTING_Command *cmd)
     958             : {
     959           2 :   struct MerchantPollOrderStartState *pos = cls;
     960             : 
     961           2 :   if (NULL != pos->ogh)
     962             :   {
     963           0 :     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
     964             :                 "Command `%s' was not terminated\n",
     965             :                 TALER_TESTING_interpreter_get_current_label (
     966             :                   pos->is));
     967           0 :     TALER_MERCHANT_merchant_order_get_cancel (pos->ogh);
     968             :   }
     969           2 :   GNUNET_free (pos);
     970           2 : }
     971             : 
     972             : 
     973             : /**
     974             :  * Start a long poll for GET /private/orders/$ORDER_ID.
     975             :  */
     976             : struct TALER_TESTING_Command
     977           2 : TALER_TESTING_cmd_poll_order_start (const char *label,
     978             :                                     const char *merchant_url,
     979             :                                     const char *order_id,
     980             :                                     struct GNUNET_TIME_Relative timeout)
     981             : {
     982             :   struct MerchantPollOrderStartState *pos;
     983             : 
     984           2 :   pos = GNUNET_new (struct MerchantPollOrderStartState);
     985           2 :   pos->order_id = order_id;
     986           2 :   pos->merchant_url = merchant_url;
     987           2 :   pos->timeout = timeout;
     988             :   {
     989           2 :     struct TALER_TESTING_Command cmd = {
     990             :       .cls = pos,
     991             :       .label = label,
     992             :       .run = &merchant_poll_order_start_run,
     993             :       .cleanup = &merchant_poll_order_start_cleanup
     994             :     };
     995             : 
     996           2 :     return cmd;
     997             :   }
     998             : }
     999             : 
    1000             : 
    1001             : /**
    1002             :  * Run the "GET order conclude" CMD.
    1003             :  *
    1004             :  * @param cls closure.
    1005             :  * @param cmd command being run now.
    1006             :  * @param is interpreter state.
    1007             :  */
    1008             : static void
    1009           2 : merchant_poll_order_conclude_run (void *cls,
    1010             :                                   const struct TALER_TESTING_Command *cmd,
    1011             :                                   struct TALER_TESTING_Interpreter *is)
    1012             : {
    1013           2 :   struct MerchantPollOrderConcludeState *poc = cls;
    1014             :   const struct TALER_TESTING_Command *poll_cmd;
    1015             :   struct MerchantPollOrderStartState *pos;
    1016             : 
    1017           2 :   poc->is = is;
    1018             :   poll_cmd =
    1019           2 :     TALER_TESTING_interpreter_lookup_command (is,
    1020             :                                               poc->start_reference);
    1021           2 :   if (NULL == poll_cmd)
    1022           0 :     TALER_TESTING_FAIL (poc->is);
    1023           2 :   GNUNET_assert (poll_cmd->run == &merchant_poll_order_start_run);
    1024           2 :   pos = poll_cmd->cls;
    1025           2 :   pos->cs = poc;
    1026           2 :   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
    1027             :               "Waiting on GET /private/orders/$ID of %s (%s)\n",
    1028             :               poc->start_reference,
    1029             :               (NULL == pos->ogh)
    1030             :               ? "finished"
    1031             :               : "active");
    1032           2 :   if (NULL == pos->ogh)
    1033           2 :     poc->task = GNUNET_SCHEDULER_add_now (&conclude_task,
    1034             :                                           poc);
    1035             :   else
    1036           0 :     poc->task = GNUNET_SCHEDULER_add_at (pos->deadline,
    1037             :                                          &conclude_task,
    1038             :                                          poc);
    1039             : }
    1040             : 
    1041             : 
    1042             : /**
    1043             :  * Free the state of a "GET order" CMD, and possibly
    1044             :  * cancel a pending operation thereof.
    1045             :  *
    1046             :  * @param cls closure.
    1047             :  * @param cmd command being run.
    1048             :  */
    1049             : static void
    1050           2 : merchant_poll_order_conclude_cleanup (void *cls,
    1051             :                                       const struct TALER_TESTING_Command *cmd)
    1052             : {
    1053           2 :   struct MerchantPollOrderConcludeState *poc = cls;
    1054             : 
    1055           2 :   if (NULL != poc->task)
    1056             :   {
    1057           0 :     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
    1058             :                 "Command `%s' was not terminated\n",
    1059             :                 TALER_TESTING_interpreter_get_current_label (
    1060             :                   poc->is));
    1061           0 :     GNUNET_SCHEDULER_cancel (poc->task);
    1062           0 :     poc->task = NULL;
    1063             :   }
    1064           2 :   GNUNET_free (poc);
    1065           2 : }
    1066             : 
    1067             : 
    1068             : /**
    1069             :  * Complete a long poll for GET /private/orders/$ORDER_ID.
    1070             :  */
    1071             : struct TALER_TESTING_Command
    1072           2 : TALER_TESTING_cmd_poll_order_conclude (const char *label,
    1073             :                                        unsigned int http_status,
    1074             :                                        const char *poll_start_reference)
    1075             : {
    1076             :   struct MerchantPollOrderConcludeState *cps;
    1077             : 
    1078           2 :   cps = GNUNET_new (struct MerchantPollOrderConcludeState);
    1079           2 :   cps->start_reference = poll_start_reference;
    1080           2 :   cps->expected_http_status = http_status;
    1081             :   {
    1082           2 :     struct TALER_TESTING_Command cmd = {
    1083             :       .cls = cps,
    1084             :       .label = label,
    1085             :       .run = &merchant_poll_order_conclude_run,
    1086             :       .cleanup = &merchant_poll_order_conclude_cleanup
    1087             :     };
    1088             : 
    1089           2 :     return cmd;
    1090             :   }
    1091             : }
    1092             : 
    1093             : 
    1094             : /* end of testing_api_cmd_merchant_get_order.c */

Generated by: LCOV version 1.14