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

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

Generated by: LCOV version 1.14