LCOV - code coverage report
Current view: top level - testing - testing_api_cmd_merchant_get_tip.c (source / functions) Hit Total Coverage
Test: GNU Taler merchant coverage report Lines: 0 109 0.0 %
Date: 2022-06-30 06:15:34 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 testing_api_cmd_merchant_get_tip.c
      21             :  * @brief command to test GET /private/tips/$TIP_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             :  * State for a GET /private/tips/$TIP_ID CMD.
      32             :  */
      33             : struct MerchantTipGetState
      34             : {
      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             :    * Whether to fetch and compare pickups.
      48             :    */
      49             :   bool fetch_pickups;
      50             : 
      51             :   /**
      52             :    * The length of @e pickups.
      53             :    */
      54             :   unsigned int pickups_length;
      55             : 
      56             :   /**
      57             :    * The NULL-terminated list of pickup commands associated with the tip.
      58             :    */
      59             :   const char **pickups;
      60             : 
      61             :   /**
      62             :    * The handle to the current GET /tips/$TIP_ID request.
      63             :    */
      64             :   struct TALER_MERCHANT_TipMerchantGetHandle *tgh;
      65             : 
      66             :   /**
      67             :    * The interpreter state.
      68             :    */
      69             :   struct TALER_TESTING_Interpreter *is;
      70             : 
      71             :   /**
      72             :    * Reference to a command that created a tip.
      73             :    */
      74             :   const char *tip_reference;
      75             : };
      76             : 
      77             : 
      78             : /**
      79             :  * Callback for a GET /private/tips/$TIP_ID operation.
      80             :  *
      81             :  * @param cls closure for this function
      82             :  * @param hr http response
      83             :  * @param total_authorized the total amount authorized for the tip
      84             :  * @param total_picked_up the total amount of the tip that has been picked up
      85             :  * @param reason why the tip was authorized
      86             :  * @param expiration when the tip will expire
      87             :  * @param reserve_pub public key of the reserve the tip is drawing from
      88             :  * @param pickups_length number of pickups associated with the tip
      89             :  * @param pickups the array of pickups associated with the tip
      90             :  */
      91             : static void
      92           0 : merchant_get_tip_cb (void *cls,
      93             :                      const struct TALER_MERCHANT_HttpResponse *hr,
      94             :                      const struct TALER_Amount *total_authorized,
      95             :                      const struct TALER_Amount *total_picked_up,
      96             :                      const char *reason,
      97             :                      struct GNUNET_TIME_Timestamp expiration,
      98             :                      const struct TALER_ReservePublicKeyP *reserve_pub,
      99             :                      unsigned int pickups_length,
     100             :                      const struct TALER_MERCHANT_PickupDetail pickups[])
     101             : {
     102             :   /* FIXME, deeper checks should be implemented here. */
     103           0 :   struct MerchantTipGetState *gts = cls;
     104             :   const struct TALER_TESTING_Command *authorize_cmd;
     105             :   struct TALER_Amount expected_total_picked_up;
     106             : 
     107           0 :   authorize_cmd = TALER_TESTING_interpreter_lookup_command (gts->is,
     108             :                                                             gts->tip_reference);
     109             : 
     110           0 :   gts->tgh = NULL;
     111           0 :   GNUNET_assert (GNUNET_OK ==
     112             :                  TALER_amount_set_zero (total_picked_up->currency,
     113             :                                         &expected_total_picked_up));
     114           0 :   if (gts->http_status != hr->http_status)
     115             :   {
     116           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     117             :                 "Unexpected response code %u (%d) to command %s\n",
     118             :                 hr->http_status,
     119             :                 (int) hr->ec,
     120             :                 TALER_TESTING_interpreter_get_current_label (gts->is));
     121           0 :     TALER_TESTING_interpreter_fail (gts->is);
     122           0 :     return;
     123             :   }
     124           0 :   switch (hr->http_status)
     125             :   {
     126           0 :   case MHD_HTTP_OK:
     127             :     // FIXME: use gts->tip_reference here to
     128             :     // check if the data returned matches that from the POST / PATCH
     129             :     {
     130             :       const struct TALER_Amount *initial_amount;
     131             : 
     132           0 :       if (GNUNET_OK !=
     133           0 :           TALER_TESTING_get_trait_amount (authorize_cmd,
     134             :                                           &initial_amount))
     135           0 :         TALER_TESTING_FAIL (gts->is);
     136           0 :       if ((GNUNET_OK !=
     137           0 :            TALER_amount_cmp_currency (total_authorized,
     138           0 :                                       initial_amount)) ||
     139           0 :           (0 != TALER_amount_cmp (total_authorized,
     140             :                                   initial_amount)))
     141             :       {
     142           0 :         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     143             :                     "Tip authorized amount does not match\n");
     144           0 :         TALER_TESTING_interpreter_fail (gts->is);
     145           0 :         return;
     146             :       }
     147             :     }
     148             :     {
     149             :       const char **justification;
     150             : 
     151           0 :       if (GNUNET_OK !=
     152           0 :           TALER_TESTING_get_trait_reason (authorize_cmd,
     153             :                                           &justification))
     154           0 :         TALER_TESTING_FAIL (gts->is);
     155           0 :       if (0 != strcmp (reason,
     156             :                        *justification))
     157             :       {
     158           0 :         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     159             :                     "Tip authorized reason does not match\n");
     160           0 :         TALER_TESTING_interpreter_fail (gts->is);
     161           0 :         return;
     162             :       }
     163             :     }
     164             :     {
     165             :       const struct GNUNET_TIME_Timestamp *tip_expiration;
     166             : 
     167           0 :       if (GNUNET_OK !=
     168           0 :           TALER_TESTING_get_trait_timestamp (authorize_cmd,
     169             :                                              0,
     170             :                                              &tip_expiration))
     171           0 :         TALER_TESTING_FAIL (gts->is);
     172           0 :       if (GNUNET_TIME_timestamp_cmp (*tip_expiration,
     173             :                                      !=,
     174             :                                      expiration))
     175             :       {
     176           0 :         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     177             :                     "Tip authorized expiration does not match\n");
     178           0 :         TALER_TESTING_interpreter_fail (gts->is);
     179           0 :         return;
     180             :       }
     181             :     }
     182           0 :     if (pickups_length != gts->pickups_length)
     183             :     {
     184           0 :       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     185             :                   "Length of pickups array does not match\n");
     186           0 :       TALER_TESTING_interpreter_fail (gts->is);
     187           0 :       return;
     188             :     }
     189             :     {
     190           0 :       for (unsigned int i = 0; i < pickups_length; ++i)
     191             :       {
     192             :         const struct TALER_TESTING_Command *pickup_cmd;
     193             : 
     194           0 :         pickup_cmd = TALER_TESTING_interpreter_lookup_command (gts->is,
     195           0 :                                                                gts->pickups[i]);
     196             :         {
     197             :           const uint32_t *num_planchets;
     198             : 
     199           0 :           if (GNUNET_OK !=
     200           0 :               TALER_TESTING_get_trait_num_planchets (pickup_cmd,
     201             :                                                      &num_planchets))
     202           0 :             TALER_TESTING_FAIL (gts->is);
     203             : 
     204           0 :           if (*num_planchets != pickups[i].num_planchets)
     205             :           {
     206           0 :             GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     207             :                         "Pickup planchet count does not match\n");
     208           0 :             TALER_TESTING_interpreter_fail (gts->is);
     209           0 :             return;
     210             :           }
     211             :         }
     212             :         {
     213             :           const struct TALER_Amount *total;
     214             : 
     215           0 :           if (GNUNET_OK !=
     216           0 :               TALER_TESTING_get_trait_amount (pickup_cmd,
     217             :                                               &total))
     218           0 :             TALER_TESTING_FAIL (gts->is);
     219             : 
     220           0 :           if ( (GNUNET_OK !=
     221           0 :                 TALER_amount_cmp_currency (total,
     222           0 :                                            &pickups[i].requested_amount)) ||
     223           0 :                (0 != TALER_amount_cmp (total,
     224           0 :                                        &pickups[i].requested_amount)))
     225             :           {
     226           0 :             GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     227             :                         "Pickup planchet sum does not match\n");
     228           0 :             TALER_TESTING_interpreter_fail (gts->is);
     229           0 :             return;
     230             :           }
     231           0 :           GNUNET_assert (0 < TALER_amount_add (&expected_total_picked_up,
     232             :                                                &expected_total_picked_up,
     233             :                                                total));
     234             :         }
     235             :       }
     236           0 :       if ( (GNUNET_OK !=
     237           0 :             TALER_amount_cmp_currency (&expected_total_picked_up,
     238           0 :                                        total_picked_up)) ||
     239             :            (0 !=
     240           0 :             TALER_amount_cmp (&expected_total_picked_up,
     241             :                               total_picked_up)) )
     242             :       {
     243           0 :         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     244             :                     "Tip picked up amount does not match\n");
     245           0 :         TALER_TESTING_interpreter_fail (gts->is);
     246           0 :         return;
     247             :       }
     248             :     }
     249           0 :     break;
     250           0 :   default:
     251           0 :     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
     252             :                 "Unhandled HTTP status.\n");
     253             :   }
     254           0 :   TALER_TESTING_interpreter_next (gts->is);
     255             : }
     256             : 
     257             : 
     258             : /**
     259             :  * Run the "GET tip" CMD.
     260             :  *
     261             :  * @param cls closure.
     262             :  * @param cmd command being run now.
     263             :  * @param is interpreter state.
     264             :  */
     265             : static void
     266           0 : merchant_get_tip_run (void *cls,
     267             :                       const struct TALER_TESTING_Command *cmd,
     268             :                       struct TALER_TESTING_Interpreter *is)
     269             : {
     270           0 :   struct MerchantTipGetState *tgs = cls;
     271             :   const struct TALER_TESTING_Command *tip_cmd;
     272             :   const struct TALER_TipIdentifierP *tip_id;
     273             : 
     274           0 :   tip_cmd = TALER_TESTING_interpreter_lookup_command (is,
     275             :                                                       tgs->tip_reference);
     276             : 
     277           0 :   if (GNUNET_OK !=
     278           0 :       TALER_TESTING_get_trait_tip_id (tip_cmd,
     279             :                                       &tip_id))
     280           0 :     TALER_TESTING_FAIL (is);
     281             : 
     282           0 :   tgs->is = is;
     283           0 :   tgs->tgh = TALER_MERCHANT_merchant_tip_get (is->ctx,
     284             :                                               tgs->merchant_url,
     285             :                                               tip_id,
     286           0 :                                               tgs->fetch_pickups,
     287             :                                               &merchant_get_tip_cb,
     288             :                                               tgs);
     289           0 :   GNUNET_assert (NULL != tgs->tgh);
     290             : }
     291             : 
     292             : 
     293             : /**
     294             : * Free the state of a "GET tip" CMD, and possibly
     295             : * cancel a pending operation thereof.
     296             : *
     297             : * @param cls closure.
     298             : * @param cmd command being run.
     299             : */
     300             : static void
     301           0 : merchant_get_tip_cleanup (void *cls,
     302             :                           const struct TALER_TESTING_Command *cmd)
     303             : {
     304           0 :   struct MerchantTipGetState *tgs = cls;
     305             : 
     306           0 :   if (NULL != tgs->tgh)
     307             :   {
     308           0 :     TALER_LOG_WARNING ("Get tip operation did not complete\n");
     309           0 :     TALER_MERCHANT_merchant_tip_get_cancel (tgs->tgh);
     310             :   }
     311           0 :   GNUNET_array_grow (tgs->pickups,
     312             :                      tgs->pickups_length,
     313             :                      0);
     314           0 :   GNUNET_free (tgs);
     315           0 : }
     316             : 
     317             : 
     318             : struct TALER_TESTING_Command
     319           0 : TALER_TESTING_cmd_merchant_get_tip (const char *label,
     320             :                                     const char *merchant_url,
     321             :                                     const char *tip_reference,
     322             :                                     unsigned int http_status)
     323             : {
     324             :   struct MerchantTipGetState *tgs;
     325             : 
     326           0 :   tgs = GNUNET_new (struct MerchantTipGetState);
     327           0 :   tgs->merchant_url = merchant_url;
     328           0 :   tgs->tip_reference = tip_reference;
     329           0 :   tgs->http_status = http_status;
     330             :   {
     331           0 :     struct TALER_TESTING_Command cmd = {
     332             :       .cls = tgs,
     333             :       .label = label,
     334             :       .run = &merchant_get_tip_run,
     335             :       .cleanup = &merchant_get_tip_cleanup
     336             :     };
     337             : 
     338           0 :     return cmd;
     339             :   }
     340             : }
     341             : 
     342             : 
     343             : struct TALER_TESTING_Command
     344           0 : TALER_TESTING_cmd_merchant_get_tip_with_pickups (const char *label,
     345             :                                                  const char *merchant_url,
     346             :                                                  const char *tip_reference,
     347             :                                                  unsigned int http_status,
     348             :                                                  ...)
     349             : {
     350             :   struct MerchantTipGetState *tgs;
     351             : 
     352           0 :   tgs = GNUNET_new (struct MerchantTipGetState);
     353           0 :   tgs->merchant_url = merchant_url;
     354           0 :   tgs->tip_reference = tip_reference;
     355           0 :   tgs->fetch_pickups = true;
     356           0 :   tgs->http_status = http_status;
     357             :   {
     358             :     const char *clabel;
     359             :     va_list ap;
     360             : 
     361           0 :     va_start (ap, http_status);
     362           0 :     while (NULL != (clabel = va_arg (ap, const char *)))
     363             :     {
     364           0 :       GNUNET_array_append (tgs->pickups,
     365             :                            tgs->pickups_length,
     366             :                            clabel);
     367             :     }
     368           0 :     va_end (ap);
     369             :   }
     370             :   {
     371           0 :     struct TALER_TESTING_Command cmd = {
     372             :       .cls = tgs,
     373             :       .label = label,
     374             :       .run = &merchant_get_tip_run,
     375             :       .cleanup = &merchant_get_tip_cleanup
     376             :     };
     377             : 
     378           0 :     return cmd;
     379             :   }
     380             : }
     381             : 
     382             : 
     383             : /* end of testing_api_cmd_merchant_get_tip.c */

Generated by: LCOV version 1.14