LCOV - code coverage report
Current view: top level - testing - testing_api_cmd_get_orders.c (source / functions) Hit Total Coverage
Test: GNU Taler merchant coverage report Lines: 115 159 72.3 %
Date: 2021-08-30 06:54:17 Functions: 12 12 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_get_orders.c
      21             :  * @brief command to test GET /orders
      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 of a "GET orders" CMD.
      33             :  */
      34             : struct GetOrdersState
      35             : {
      36             : 
      37             :   /**
      38             :    * Handle for a "GET orders" request.
      39             :    */
      40             :   struct TALER_MERCHANT_OrdersGetHandle *ogh;
      41             : 
      42             :   /**
      43             :    * The interpreter state.
      44             :    */
      45             :   struct TALER_TESTING_Interpreter *is;
      46             : 
      47             :   /**
      48             :    * Base URL of the merchant serving the request.
      49             :    */
      50             :   const char *merchant_url;
      51             : 
      52             :   /**
      53             :    * Expected HTTP response code.
      54             :    */
      55             :   unsigned int http_status;
      56             : 
      57             :   /**
      58             :    * A NULL-terminated array of CMD labels that created orders.
      59             :    */
      60             :   const char **orders;
      61             : 
      62             :   /**
      63             :    * The length of @e orders.
      64             :    */
      65             :   unsigned int orders_length;
      66             : 
      67             : };
      68             : 
      69             : 
      70             : /**
      71             :  * Callback for a GET /orders operation.
      72             :  *
      73             :  * @param cls closure for this function
      74             :  * @param hr HTTP response
      75             :  * @param orders_length how many orders are returned
      76             :  * @param orders all the orders' details
      77             :  */
      78             : static void
      79           3 : get_orders_cb (void *cls,
      80             :                const struct TALER_MERCHANT_HttpResponse *hr,
      81             :                unsigned int orders_length,
      82             :                const struct TALER_MERCHANT_OrderEntry orders[])
      83             : {
      84           3 :   struct GetOrdersState *gos = cls;
      85             : 
      86           3 :   gos->ogh = NULL;
      87           3 :   if (gos->http_status != hr->http_status)
      88             :   {
      89           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
      90             :                 "Unexpected response code %u (%d) to command %s\n",
      91             :                 hr->http_status,
      92             :                 (int) hr->ec,
      93             :                 TALER_TESTING_interpreter_get_current_label (gos->is));
      94           0 :     TALER_TESTING_interpreter_fail (gos->is);
      95           0 :     return;
      96             :   }
      97           3 :   switch (hr->http_status)
      98             :   {
      99           3 :   case MHD_HTTP_OK:
     100           3 :     if (orders_length != gos->orders_length)
     101             :     {
     102           0 :       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     103             :                   "Number of orders found does not match\n");
     104           0 :       TALER_TESTING_interpreter_fail (gos->is);
     105           0 :       return;
     106             :     }
     107           7 :     for (unsigned int i = 0; i < orders_length; ++i)
     108             :     {
     109             :       const struct TALER_TESTING_Command *order_cmd;
     110             : 
     111           4 :       order_cmd = TALER_TESTING_interpreter_lookup_command (
     112             :         gos->is,
     113           4 :         gos->orders[i]);
     114             : 
     115             :       {
     116             :         const char *order_id;
     117           4 :         if (GNUNET_OK !=
     118           4 :             TALER_TESTING_get_trait_order_id (order_cmd,
     119             :                                               0,
     120             :                                               &order_id))
     121             :         {
     122           0 :           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     123             :                       "Could not fetch order id\n");
     124           0 :           TALER_TESTING_interpreter_fail (gos->is);
     125           0 :           return;
     126             :         }
     127           4 :         if (0 != strcmp (orders[i].order_id,
     128             :                          order_id))
     129             :         {
     130           0 :           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     131             :                       "Order id does not match\n");
     132           0 :           TALER_TESTING_interpreter_fail (gos->is);
     133           0 :           return;
     134             :         }
     135             :       }
     136             :       {
     137             :         const json_t *contract_terms;
     138             :         struct TALER_Amount amount;
     139             :         const char *summary;
     140             :         struct GNUNET_JSON_Specification spec[] = {
     141           4 :           GNUNET_JSON_spec_string ("summary",
     142             :                                    &summary),
     143           4 :           TALER_JSON_spec_amount_any ("amount",
     144             :                                       &amount),
     145           4 :           GNUNET_JSON_spec_end ()
     146             :         };
     147             : 
     148           4 :         if (GNUNET_OK !=
     149           4 :             TALER_TESTING_get_trait_contract_terms (order_cmd,
     150             :                                                     0,
     151             :                                                     &contract_terms))
     152             :         {
     153           0 :           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     154             :                       "Could not fetch order contract terms\n");
     155           0 :           TALER_TESTING_interpreter_fail (gos->is);
     156           0 :           return;
     157             :         }
     158           4 :         if (GNUNET_OK !=
     159           4 :             GNUNET_JSON_parse (contract_terms,
     160             :                                spec,
     161             :                                NULL, NULL))
     162             :         {
     163           0 :           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     164             :                       "Could not parse order contract terms\n");
     165           0 :           TALER_TESTING_interpreter_fail (gos->is);
     166           0 :           return;
     167             :         }
     168           4 :         if ((0 != strcmp (summary,
     169           8 :                           orders[i].summary)) ||
     170           4 :             (GNUNET_OK != TALER_amount_cmp_currency (&amount,
     171           8 :                                                      &orders[i].amount)) ||
     172           4 :             (0 != TALER_amount_cmp (&amount,
     173           4 :                                     &orders[i].amount)))
     174             :         {
     175           0 :           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     176             :                       "Order summary and/or amount does not match\n");
     177           0 :           TALER_TESTING_interpreter_fail (gos->is);
     178           0 :           return;
     179             :         }
     180             :       }
     181             :     }
     182           3 :     break;
     183           0 :   default:
     184           0 :     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
     185             :                 "Unhandled HTTP status.\n");
     186             :   }
     187           3 :   TALER_TESTING_interpreter_next (gos->is);
     188             : }
     189             : 
     190             : 
     191             : /**
     192             :  * Run the "GET /orders" CMD.
     193             :  *
     194             :  *
     195             :  * @param cls closure.
     196             :  * @param cmd command being run now.
     197             :  * @param is interpreter state.
     198             :  */
     199             : static void
     200           3 : get_orders_run (void *cls,
     201             :                 const struct TALER_TESTING_Command *cmd,
     202             :                 struct TALER_TESTING_Interpreter *is)
     203             : {
     204           3 :   struct GetOrdersState *gos = cls;
     205             : 
     206           3 :   gos->is = is;
     207           3 :   gos->ogh = TALER_MERCHANT_orders_get (is->ctx,
     208             :                                         gos->merchant_url,
     209             :                                         &get_orders_cb,
     210             :                                         gos);
     211           3 :   GNUNET_assert (NULL != gos->ogh);
     212           3 : }
     213             : 
     214             : 
     215             : /**
     216             :  * Free the state of a "GET orders" CMD, and possibly
     217             :  * cancel a pending operation thereof.
     218             :  *
     219             :  * @param cls closure.
     220             :  * @param cmd command being run.
     221             :  */
     222             : static void
     223           3 : get_orders_cleanup (void *cls,
     224             :                     const struct TALER_TESTING_Command *cmd)
     225             : {
     226           3 :   struct GetOrdersState *gos = cls;
     227             : 
     228           3 :   if (NULL != gos->ogh)
     229             :   {
     230           0 :     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
     231             :                 "GET /orders operation did not complete\n");
     232           0 :     TALER_MERCHANT_orders_get_cancel (gos->ogh);
     233             :   }
     234           3 :   GNUNET_array_grow (gos->orders,
     235             :                      gos->orders_length,
     236             :                      0);
     237           3 :   GNUNET_free (gos);
     238           3 : }
     239             : 
     240             : 
     241             : struct TALER_TESTING_Command
     242           3 : TALER_TESTING_cmd_merchant_get_orders (const char *label,
     243             :                                        const char *merchant_url,
     244             :                                        unsigned int http_status,
     245             :                                        ...)
     246             : {
     247             :   struct GetOrdersState *gos;
     248             : 
     249           3 :   gos = GNUNET_new (struct GetOrdersState);
     250           3 :   gos->merchant_url = merchant_url;
     251           3 :   gos->http_status = http_status;
     252             :   {
     253             :     const char *clabel;
     254             :     va_list ap;
     255             : 
     256           3 :     va_start (ap, http_status);
     257           7 :     while (NULL != (clabel = va_arg (ap, const char *)))
     258             :     {
     259           4 :       GNUNET_array_append (gos->orders,
     260             :                            gos->orders_length,
     261             :                            clabel);
     262             :     }
     263           3 :     va_end (ap);
     264             :   }
     265             :   {
     266           3 :     struct TALER_TESTING_Command cmd = {
     267             :       .cls = gos,
     268             :       .label = label,
     269             :       .run = &get_orders_run,
     270             :       .cleanup = &get_orders_cleanup
     271             :     };
     272             : 
     273           3 :     return cmd;
     274             :   }
     275             : }
     276             : 
     277             : 
     278             : struct MerchantPollOrdersConcludeState
     279             : {
     280             :   /**
     281             :    * The interpreter state.
     282             :    */
     283             :   struct TALER_TESTING_Interpreter *is;
     284             : 
     285             :   /**
     286             :    * Reference to a command that can provide a poll orders start command.
     287             :    */
     288             :   const char *start_reference;
     289             : 
     290             :   /**
     291             :    * Task to wait for the deadline.
     292             :    */
     293             :   struct GNUNET_SCHEDULER_Task *task;
     294             : 
     295             :   /**
     296             :    * Expected HTTP response status code.
     297             :    */
     298             :   unsigned int expected_http_status;
     299             : };
     300             : 
     301             : 
     302             : struct MerchantPollOrdersStartState
     303             : {
     304             :   /**
     305             :    * The merchant base URL.
     306             :    */
     307             :   const char *merchant_url;
     308             : 
     309             :   /**
     310             :    * The handle to the current GET /private/orders request.
     311             :    */
     312             :   struct TALER_MERCHANT_OrdersGetHandle *ogh;
     313             : 
     314             :   /**
     315             :    * The interpreter state.
     316             :    */
     317             :   struct TALER_TESTING_Interpreter *is;
     318             : 
     319             :   /**
     320             :    * How long to wait for server to return a response.
     321             :    */
     322             :   struct GNUNET_TIME_Relative timeout;
     323             : 
     324             :   /**
     325             :    * Conclude state waiting for completion (if any).
     326             :    */
     327             :   struct MerchantPollOrdersConcludeState *cs;
     328             : 
     329             :   /**
     330             :    * The HTTP status code returned by the backend.
     331             :    */
     332             :   unsigned int http_status;
     333             : 
     334             :   /**
     335             :    * When the request should be completed by.
     336             :    */
     337             :   struct GNUNET_TIME_Absolute deadline;
     338             : };
     339             : 
     340             : 
     341             : /**
     342             :  * Task called when either the timeout for the get orders
     343             :  * command expired or we got a response.  Checks if the
     344             :  * result is what we expected.
     345             :  *
     346             :  * @param cls a `struct MerchantPollOrdersConcludeState`
     347             :  */
     348             : static void
     349           1 : conclude_task (void *cls)
     350             : {
     351           1 :   struct MerchantPollOrdersConcludeState *poc = cls;
     352             :   const struct TALER_TESTING_Command *poll_cmd;
     353             :   struct MerchantPollOrdersStartState *pos;
     354             :   struct GNUNET_TIME_Absolute now;
     355             : 
     356           1 :   poc->task = NULL;
     357             :   poll_cmd =
     358           1 :     TALER_TESTING_interpreter_lookup_command (poc->is,
     359             :                                               poc->start_reference);
     360           1 :   if (NULL == poll_cmd)
     361           0 :     TALER_TESTING_FAIL (poc->is);
     362           1 :   pos = poll_cmd->cls;
     363           1 :   if (NULL != pos->ogh)
     364             :   {
     365           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     366             :                 "Expected poll GET /private/orders to have completed, but it did not!\n");
     367           0 :     TALER_TESTING_FAIL (poc->is);
     368             :   }
     369           1 :   if (pos->http_status != poc->expected_http_status)
     370             :   {
     371           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     372             :                 "Expected HTTP status %u, got %u\n",
     373             :                 poc->expected_http_status,
     374             :                 pos->http_status);
     375           0 :     TALER_TESTING_FAIL (poc->is);
     376             :   }
     377           1 :   now = GNUNET_TIME_absolute_get ();
     378           1 :   if ((GNUNET_TIME_absolute_add (pos->deadline,
     379           1 :                                  GNUNET_TIME_UNIT_SECONDS).abs_value_us <
     380           1 :        now.abs_value_us) )
     381             :   {
     382           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     383             :                 "Expected answer to be delayed until %llu, but got response at %llu\n",
     384             :                 (unsigned long long) pos->deadline.abs_value_us,
     385             :                 (unsigned long long) now.abs_value_us);
     386           0 :     TALER_TESTING_FAIL (poc->is);
     387             :   }
     388           1 :   TALER_TESTING_interpreter_next (poc->is);
     389             : }
     390             : 
     391             : 
     392             : /**
     393             :  * Callback to process a GET /orders request
     394             :  *
     395             :  * @param cls closure
     396             :  * @param hr HTTP response details
     397             :  * @param orders_length how many orders are returned
     398             :  * @param orders the returned orders
     399             :  */
     400             : static void
     401           1 : merchant_poll_orders_cb (
     402             :   void *cls,
     403             :   const struct TALER_MERCHANT_HttpResponse *hr,
     404             :   unsigned int orders_length,
     405             :   const struct TALER_MERCHANT_OrderEntry orders[])
     406             : {
     407             :   /* FIXME, deeper checks should be implemented here. */
     408           1 :   struct MerchantPollOrdersStartState *pos = cls;
     409             : 
     410           1 :   pos->ogh = NULL;
     411           1 :   if (MHD_HTTP_OK != hr->http_status)
     412             :   {
     413           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     414             :                 "Unexpected response code %u (%d) to command %s\n",
     415             :                 hr->http_status,
     416             :                 (int) hr->ec,
     417             :                 TALER_TESTING_interpreter_get_current_label (pos->is));
     418           0 :     TALER_TESTING_interpreter_fail (pos->is);
     419           0 :     return;
     420             :   }
     421           1 :   switch (hr->http_status)
     422             :   {
     423           1 :   case MHD_HTTP_OK:
     424             :     // FIXME: use order references
     425             :     // check if the data returned matches that from the POST / PATCH
     426           1 :     break;
     427           0 :   default:
     428           0 :     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
     429             :                 "Unhandled HTTP status.\n");
     430             :   }
     431           1 :   pos->http_status = hr->http_status;
     432           1 :   if (NULL != pos->cs)
     433             :   {
     434           1 :     GNUNET_SCHEDULER_cancel (pos->cs->task);
     435           1 :     pos->cs->task = GNUNET_SCHEDULER_add_now (&conclude_task,
     436           1 :                                               pos->cs);
     437             :   }
     438             : }
     439             : 
     440             : 
     441             : /**
     442             :  * Run the "GET orders" CMD.
     443             :  *
     444             :  * @param cls closure.
     445             :  * @param cmd command being run now.
     446             :  * @param is interpreter state.
     447             :  */
     448             : static void
     449           1 : merchant_poll_orders_start_run (void *cls,
     450             :                                 const struct TALER_TESTING_Command *cmd,
     451             :                                 struct TALER_TESTING_Interpreter *is)
     452             : {
     453           1 :   struct MerchantPollOrdersStartState *pos = cls;
     454             : 
     455             :   /* add 1s grace time to timeout */
     456             :   pos->deadline
     457           1 :     = GNUNET_TIME_absolute_add (GNUNET_TIME_relative_to_absolute (pos->timeout),
     458             :                                 GNUNET_TIME_UNIT_SECONDS);
     459           1 :   pos->is = is;
     460           1 :   pos->ogh = TALER_MERCHANT_orders_get2 (is->ctx,
     461             :                                          pos->merchant_url,
     462             :                                          TALER_EXCHANGE_YNA_ALL,
     463             :                                          TALER_EXCHANGE_YNA_ALL,
     464             :                                          TALER_EXCHANGE_YNA_ALL,
     465             :                                          GNUNET_TIME_UNIT_ZERO_ABS,
     466             :                                          1,
     467             :                                          2,
     468             :                                          pos->timeout,
     469             :                                          &merchant_poll_orders_cb,
     470             :                                          pos);
     471           1 :   GNUNET_assert (NULL != pos->ogh);
     472             :   /* We CONTINUE to run the interpreter while the long-polled command
     473             :      completes asynchronously! */
     474           1 :   TALER_TESTING_interpreter_next (pos->is);
     475           1 : }
     476             : 
     477             : 
     478             : /**
     479             :  * Free the state of a "GET orders" CMD, and possibly
     480             :  * cancel a pending operation thereof.
     481             :  *
     482             :  * @param cls closure.
     483             :  * @param cmd command being run.
     484             :  */
     485             : static void
     486           1 : merchant_poll_orders_start_cleanup (void *cls,
     487             :                                     const struct TALER_TESTING_Command *cmd)
     488             : {
     489           1 :   struct MerchantPollOrdersStartState *pos = cls;
     490             : 
     491           1 :   if (NULL != pos->ogh)
     492             :   {
     493           0 :     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
     494             :                 "Command `%s' was not terminated\n",
     495             :                 TALER_TESTING_interpreter_get_current_label (
     496             :                   pos->is));
     497           0 :     TALER_MERCHANT_orders_get_cancel (pos->ogh);
     498             :   }
     499           1 :   GNUNET_free (pos);
     500           1 : }
     501             : 
     502             : 
     503             : /**
     504             :  * Start a long poll for GET /private/orders.
     505             :  */
     506             : struct TALER_TESTING_Command
     507           1 : TALER_TESTING_cmd_poll_orders_start (const char *label,
     508             :                                      const char *merchant_url,
     509             :                                      struct GNUNET_TIME_Relative timeout)
     510             : {
     511             :   struct MerchantPollOrdersStartState *pos;
     512             : 
     513           1 :   pos = GNUNET_new (struct MerchantPollOrdersStartState);
     514           1 :   pos->merchant_url = merchant_url;
     515           1 :   pos->timeout = timeout;
     516             :   {
     517           1 :     struct TALER_TESTING_Command cmd = {
     518             :       .cls = pos,
     519             :       .label = label,
     520             :       .run = &merchant_poll_orders_start_run,
     521             :       .cleanup = &merchant_poll_orders_start_cleanup
     522             :     };
     523             : 
     524           1 :     return cmd;
     525             :   }
     526             : }
     527             : 
     528             : 
     529             : /**
     530             :  * Run the "GET orders" CMD.
     531             :  *
     532             :  * @param cls closure.
     533             :  * @param cmd command being run now.
     534             :  * @param is interpreter state.
     535             :  */
     536             : static void
     537           1 : merchant_poll_orders_conclude_run (void *cls,
     538             :                                    const struct TALER_TESTING_Command *cmd,
     539             :                                    struct TALER_TESTING_Interpreter *is)
     540             : {
     541           1 :   struct MerchantPollOrdersConcludeState *poc = cls;
     542             :   const struct TALER_TESTING_Command *poll_cmd;
     543             :   struct MerchantPollOrdersStartState *pos;
     544             : 
     545           1 :   poc->is = is;
     546             :   poll_cmd =
     547           1 :     TALER_TESTING_interpreter_lookup_command (is,
     548             :                                               poc->start_reference);
     549           1 :   if (NULL == poll_cmd)
     550           0 :     TALER_TESTING_FAIL (poc->is);
     551           1 :   GNUNET_assert (poll_cmd->run == &merchant_poll_orders_start_run);
     552           1 :   pos = poll_cmd->cls;
     553           1 :   pos->cs = poc;
     554           1 :   if (NULL == pos->ogh)
     555           0 :     poc->task = GNUNET_SCHEDULER_add_now (&conclude_task,
     556             :                                           poc);
     557             :   else
     558           1 :     poc->task = GNUNET_SCHEDULER_add_at (pos->deadline,
     559             :                                          &conclude_task,
     560             :                                          poc);
     561             : }
     562             : 
     563             : 
     564             : /**
     565             :  * Free the state of a "GET orders" CMD, and possibly
     566             :  * cancel a pending operation thereof.
     567             :  *
     568             :  * @param cls closure.
     569             :  * @param cmd command being run.
     570             :  */
     571             : static void
     572           1 : merchant_poll_orders_conclude_cleanup (void *cls,
     573             :                                        const struct TALER_TESTING_Command *cmd)
     574             : {
     575           1 :   struct MerchantPollOrdersConcludeState *poc = cls;
     576             : 
     577           1 :   if (NULL != poc->task)
     578             :   {
     579           0 :     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
     580             :                 "Command `%s' was not terminated\n",
     581             :                 TALER_TESTING_interpreter_get_current_label (
     582             :                   poc->is));
     583           0 :     GNUNET_SCHEDULER_cancel (poc->task);
     584           0 :     poc->task = NULL;
     585             :   }
     586           1 :   GNUNET_free (poc);
     587           1 : }
     588             : 
     589             : 
     590             : /**
     591             :  * Complete a long poll for GET /private/orders.
     592             :  */
     593             : struct TALER_TESTING_Command
     594           1 : TALER_TESTING_cmd_poll_orders_conclude (const char *label,
     595             :                                         unsigned int http_status,
     596             :                                         const char *poll_start_reference)
     597             : {
     598             :   struct MerchantPollOrdersConcludeState *poc;
     599             : 
     600           1 :   poc = GNUNET_new (struct MerchantPollOrdersConcludeState);
     601           1 :   poc->start_reference = poll_start_reference;
     602           1 :   poc->expected_http_status = http_status;
     603             :   {
     604           1 :     struct TALER_TESTING_Command cmd = {
     605             :       .cls = poc,
     606             :       .label = label,
     607             :       .run = &merchant_poll_orders_conclude_run,
     608             :       .cleanup = &merchant_poll_orders_conclude_cleanup
     609             :     };
     610             : 
     611           1 :     return cmd;
     612             :   }
     613             : }
     614             : 
     615             : 
     616             : /* end of testing_api_cmd_get_orders.c */

Generated by: LCOV version 1.14