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

Generated by: LCOV version 1.14