LCOV - code coverage report
Current view: top level - testing - testing_api_cmd_get_reserve.c (source / functions) Hit Total Coverage
Test: GNU Taler coverage report Lines: 0 90 0.0 %
Date: 2020-08-15 06:12:35 Functions: 0 5 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_get_reserve.c
      21             :  * @brief command to test GET /private/reserves/$RESERVE_PUB
      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             : struct GetReserveState
      32             : {
      33             : 
      34             :   /**
      35             :    * Handle for a "GET reserve" request.
      36             :    */
      37             :   struct TALER_MERCHANT_ReserveGetHandle *rgh;
      38             : 
      39             :   /**
      40             :    * The interpreter state.
      41             :    */
      42             :   struct TALER_TESTING_Interpreter *is;
      43             : 
      44             :   /**
      45             :    * Base URL of the merchant serving the request.
      46             :    */
      47             :   const char *merchant_url;
      48             : 
      49             :   /**
      50             :    * Label for a command that created a reserve.
      51             :    */
      52             :   const char *reserve_reference;
      53             : 
      54             :   /**
      55             :    * Expected HTTP response code.
      56             :    */
      57             :   unsigned int http_status;
      58             : 
      59             :   /**
      60             :    * Fetch tips
      61             :    */
      62             :   bool fetch_tips;
      63             : 
      64             :   /**
      65             :    * Length of @e tips.
      66             :    */
      67             :   unsigned int tips_length;
      68             : 
      69             :   /**
      70             :    * The list of references to tips.
      71             :    */
      72             :   const char **tips;
      73             : };
      74             : 
      75             : 
      76             : static void
      77           0 : get_reserve_cb (void *cls,
      78             :                 const struct TALER_MERCHANT_HttpResponse *hr,
      79             :                 const struct TALER_MERCHANT_ReserveSummary *rs,
      80             :                 bool active,
      81             :                 unsigned int tips_length,
      82             :                 const struct TALER_MERCHANT_TipDetails tips[])
      83             : {
      84             :   /* FIXME, deeper checks should be implemented here. */
      85           0 :   struct GetReserveState *grs = cls;
      86             :   const struct TALER_TESTING_Command *reserve_cmd;
      87             : 
      88           0 :   reserve_cmd = TALER_TESTING_interpreter_lookup_command (
      89             :     grs->is,
      90             :     grs->reserve_reference);
      91             : 
      92           0 :   grs->rgh = NULL;
      93           0 :   if (grs->http_status != hr->http_status)
      94             :   {
      95           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
      96             :                 "Unexpected response code %u (%d) to command %s\n",
      97             :                 hr->http_status,
      98             :                 (int) hr->ec,
      99             :                 TALER_TESTING_interpreter_get_current_label (grs->is));
     100           0 :     TALER_TESTING_interpreter_fail (grs->is);
     101           0 :     return;
     102             :   }
     103           0 :   switch (hr->http_status)
     104             :   {
     105           0 :   case MHD_HTTP_OK:
     106             :     // FIXME: use grs->reserve_reference here to
     107             :     // check if the data returned matches that from the POST / PATCH
     108             :     {
     109             :       const struct TALER_Amount *initial_amount;
     110           0 :       if (GNUNET_OK !=
     111           0 :           TALER_TESTING_get_trait_amount_obj (reserve_cmd,
     112             :                                               0,
     113             :                                               &initial_amount))
     114           0 :         TALER_TESTING_interpreter_fail (grs->is);
     115           0 :       if ((GNUNET_OK != TALER_amount_cmp_currency (&rs->merchant_initial_amount,
     116           0 :                                                    initial_amount)) ||
     117           0 :           (0 != TALER_amount_cmp (&rs->merchant_initial_amount,
     118             :                                   initial_amount)))
     119             :       {
     120           0 :         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     121             :                     "Reserve initial amount does not match\n");
     122           0 :         TALER_TESTING_interpreter_fail (grs->is);
     123           0 :         return;
     124             :       }
     125             :     }
     126           0 :     if (tips_length != grs->tips_length)
     127             :     {
     128           0 :       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     129             :                   "Number of tips authorized does not match\n");
     130           0 :       TALER_TESTING_interpreter_fail (grs->is);
     131           0 :       return;
     132             :     }
     133           0 :     for (unsigned int i = 0; i < tips_length; ++i)
     134             :     {
     135             :       const struct TALER_TESTING_Command *tip_cmd;
     136             : 
     137           0 :       tip_cmd = TALER_TESTING_interpreter_lookup_command (grs->is,
     138           0 :                                                           grs->tips[i]);
     139             :       {
     140             :         const struct GNUNET_HashCode *tip_id;
     141             : 
     142           0 :         if (GNUNET_OK !=
     143           0 :             TALER_TESTING_get_trait_tip_id (tip_cmd,
     144             :                                             0,
     145             :                                             &tip_id))
     146           0 :           TALER_TESTING_interpreter_fail (grs->is);
     147             : 
     148           0 :         if (0 != GNUNET_memcmp (&tips[i].tip_id,
     149             :                                 tip_id))
     150             :         {
     151           0 :           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     152             :                       "Reserve tip id does not match\n");
     153           0 :           TALER_TESTING_interpreter_fail (grs->is);
     154           0 :           return;
     155             :         }
     156             :       }
     157             :       {
     158             :         const struct TALER_Amount *total_amount;
     159             : 
     160           0 :         if (GNUNET_OK !=
     161           0 :             TALER_TESTING_get_trait_amount_obj (tip_cmd,
     162             :                                                 0,
     163             :                                                 &total_amount))
     164           0 :           TALER_TESTING_interpreter_fail (grs->is);
     165             : 
     166           0 :         if ((GNUNET_OK != TALER_amount_cmp_currency (&tips[i].amount,
     167           0 :                                                      total_amount)) ||
     168           0 :             (0 != TALER_amount_cmp (&tips[i].amount,
     169             :                                     total_amount)))
     170             :         {
     171           0 :           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     172             :                       "Reserve tip amount does not match\n");
     173           0 :           TALER_TESTING_interpreter_fail (grs->is);
     174           0 :           return;
     175             :         }
     176             :       }
     177             :       {
     178             :         const char *reason;
     179             : 
     180           0 :         if (GNUNET_OK !=
     181           0 :             TALER_TESTING_get_trait_string (tip_cmd,
     182             :                                             0,
     183             :                                             &reason))
     184           0 :           TALER_TESTING_interpreter_fail (grs->is);
     185             : 
     186           0 :         if (0 != strcmp (tips[i].reason,
     187             :                          reason))
     188             :         {
     189           0 :           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     190             :                       "Reserve tip reason does not match\n");
     191           0 :           TALER_TESTING_interpreter_fail (grs->is);
     192           0 :           return;
     193             :         }
     194             :       }
     195             :     }
     196           0 :     break;
     197           0 :   default:
     198           0 :     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
     199             :                 "Unhandled HTTP status.\n");
     200             :   }
     201           0 :   TALER_TESTING_interpreter_next (grs->is);
     202             : }
     203             : 
     204             : 
     205             : /**
     206             :  * Run the "GET /private/reserves/$RESERVE_PUB" CMD.
     207             :  *
     208             :  * @param cls closure.
     209             :  * @param cmd command being run now.
     210             :  * @param is interpreter state.
     211             :  */
     212             : static void
     213           0 : get_reserve_run (void *cls,
     214             :                  const struct TALER_TESTING_Command *cmd,
     215             :                  struct TALER_TESTING_Interpreter *is)
     216             : {
     217           0 :   struct GetReserveState *grs = cls;
     218             :   const struct TALER_TESTING_Command *reserve_cmd;
     219             :   const struct TALER_ReservePublicKeyP *reserve_pub;
     220             : 
     221           0 :   reserve_cmd = TALER_TESTING_interpreter_lookup_command (
     222             :     is,
     223             :     grs->reserve_reference);
     224           0 :   if (GNUNET_OK !=
     225           0 :       TALER_TESTING_get_trait_reserve_pub (reserve_cmd,
     226             :                                            0,
     227             :                                            &reserve_pub))
     228           0 :     TALER_TESTING_FAIL (is);
     229             : 
     230           0 :   grs->is = is;
     231           0 :   grs->rgh = TALER_MERCHANT_reserve_get (is->ctx,
     232             :                                          grs->merchant_url,
     233             :                                          reserve_pub,
     234           0 :                                          grs->fetch_tips,
     235             :                                          &get_reserve_cb,
     236             :                                          grs);
     237             : 
     238           0 :   GNUNET_assert (NULL != grs->rgh);
     239             : }
     240             : 
     241             : 
     242             : /**
     243             :  * Free the state of a "GET reserve" CMD, and possibly
     244             :  * cancel a pending operation thereof.
     245             :  *
     246             :  * @param cls closure.
     247             :  * @param cmd command being run.
     248             :  */
     249             : static void
     250           0 : get_reserve_cleanup (void *cls,
     251             :                      const struct TALER_TESTING_Command *cmd)
     252             : {
     253           0 :   struct GetReserveState *grs = cls;
     254             : 
     255           0 :   if (NULL != grs->rgh)
     256             :   {
     257           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     258             :                 "GET /private/reserve/$RESERVE_PUB operation did not complete\n");
     259           0 :     TALER_MERCHANT_reserve_get_cancel (grs->rgh);
     260             :   }
     261           0 :   GNUNET_free (grs);
     262           0 : }
     263             : 
     264             : 
     265             : /**
     266             :  * Define a "GET reserve" CMD.
     267             :  *
     268             :  * @param label command label.
     269             :  * @param merchant_url base URL of the merchant serving the request.
     270             :  * @param http_status expected HTTP response code.
     271             :  * @param reserve_reference reference to a "POST /reserves" that provides the
     272             :  *        information we are expecting.
     273             :  * @return the command.
     274             :  */
     275             : struct TALER_TESTING_Command
     276           0 : TALER_TESTING_cmd_merchant_get_reserve (const char *label,
     277             :                                         const char *merchant_url,
     278             :                                         unsigned int http_status,
     279             :                                         const char *reserve_reference)
     280             : {
     281             :   struct GetReserveState *grs;
     282             : 
     283           0 :   grs = GNUNET_new (struct GetReserveState);
     284           0 :   grs->merchant_url = merchant_url;
     285           0 :   grs->http_status = http_status;
     286           0 :   grs->reserve_reference = reserve_reference;
     287           0 :   grs->fetch_tips = false;
     288             :   {
     289           0 :     struct TALER_TESTING_Command cmd = {
     290             :       .cls = grs,
     291             :       .label = label,
     292             :       .run = &get_reserve_run,
     293             :       .cleanup = &get_reserve_cleanup
     294             :     };
     295             : 
     296           0 :     return cmd;
     297             :   }
     298             : }
     299             : 
     300             : 
     301             : /**
     302             :  * Define a "GET reserve" CMD.
     303             :  *
     304             :  * @param label command label.
     305             :  * @param merchant_url base URL of the merchant serving the request.
     306             :  * @param http_status expected HTTP response code.
     307             :  * @param reserve_reference reference to a "POST /reserves" that provides the
     308             :  *        information we are expecting.
     309             :  * @param ... NULL-terminated list of labels (const char *) of
     310             :  *        tip (commands) we expect to be returned in the list
     311             :  *        (assuming @a http_code is #MHD_HTTP_OK)
     312             :  * @return the command.
     313             :  */
     314             : struct TALER_TESTING_Command
     315           0 : TALER_TESTING_cmd_merchant_get_reserve_with_tips (const char *label,
     316             :                                                   const char *merchant_url,
     317             :                                                   unsigned int http_status,
     318             :                                                   const char *reserve_reference,
     319             :                                                   ...)
     320             : {
     321             :   struct GetReserveState *grs;
     322             : 
     323           0 :   grs = GNUNET_new (struct GetReserveState);
     324           0 :   grs->merchant_url = merchant_url;
     325           0 :   grs->http_status = http_status;
     326           0 :   grs->reserve_reference = reserve_reference;
     327           0 :   grs->fetch_tips = true;
     328             :   {
     329             :     const char *clabel;
     330             :     va_list ap;
     331             : 
     332           0 :     va_start (ap, reserve_reference);
     333           0 :     while (NULL != (clabel = va_arg (ap, const char *)))
     334             :     {
     335           0 :       GNUNET_array_append (grs->tips,
     336             :                            grs->tips_length,
     337             :                            clabel);
     338             :     }
     339           0 :     va_end (ap);
     340             :   }
     341             :   {
     342           0 :     struct TALER_TESTING_Command cmd = {
     343             :       .cls = grs,
     344             :       .label = label,
     345             :       .run = &get_reserve_run,
     346             :       .cleanup = &get_reserve_cleanup
     347             :     };
     348             : 
     349           0 :     return cmd;
     350             :   }
     351             : }
     352             : 
     353             : 
     354             : /* end of testing_api_cmd_get_reserve.c */

Generated by: LCOV version 1.14