LCOV - code coverage report
Current view: top level - testing - testing_api_cmd_merchant_get_tip.c (source / functions) Hit Total Coverage
Test: GNU Taler coverage report Lines: 0 107 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_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_Absolute 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 == TALER_amount_get_zero (total_picked_up->currency,
     112             :                                                      &expected_total_picked_up));
     113           0 :   if (gts->http_status != hr->http_status)
     114             :   {
     115           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     116             :                 "Unexpected response code %u (%d) to command %s\n",
     117             :                 hr->http_status,
     118             :                 (int) hr->ec,
     119             :                 TALER_TESTING_interpreter_get_current_label (gts->is));
     120           0 :     TALER_TESTING_interpreter_fail (gts->is);
     121           0 :     return;
     122             :   }
     123           0 :   switch (hr->http_status)
     124             :   {
     125           0 :   case MHD_HTTP_OK:
     126             :     // FIXME: use gts->tip_reference here to
     127             :     // check if the data returned matches that from the POST / PATCH
     128             :     {
     129             :       const struct TALER_Amount *initial_amount;
     130           0 :       if (GNUNET_OK !=
     131           0 :           TALER_TESTING_get_trait_amount_obj (authorize_cmd,
     132             :                                               0,
     133             :                                               &initial_amount))
     134           0 :         TALER_TESTING_FAIL (gts->is);
     135           0 :       if ((GNUNET_OK != TALER_amount_cmp_currency (total_authorized,
     136           0 :                                                    initial_amount)) ||
     137           0 :           (0 != TALER_amount_cmp (total_authorized,
     138             :                                   initial_amount)))
     139             :       {
     140           0 :         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     141             :                     "Tip authorized amount does not match\n");
     142           0 :         TALER_TESTING_interpreter_fail (gts->is);
     143           0 :         return;
     144             :       }
     145             :     }
     146             :     {
     147             :       const char *justification;
     148           0 :       if (GNUNET_OK !=
     149           0 :           TALER_TESTING_get_trait_string (authorize_cmd,
     150             :                                           0,
     151             :                                           &justification))
     152           0 :         TALER_TESTING_FAIL (gts->is);
     153           0 :       if (0 != strcmp (reason,
     154             :                        justification))
     155             :       {
     156           0 :         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     157             :                     "Tip authorized reason does not match\n");
     158           0 :         TALER_TESTING_interpreter_fail (gts->is);
     159           0 :         return;
     160             :       }
     161             :     }
     162             :     {
     163             :       const struct GNUNET_TIME_Absolute *tip_expiration;
     164           0 :       if (GNUNET_OK !=
     165           0 :           TALER_TESTING_get_trait_absolute_time (authorize_cmd,
     166             :                                                  0,
     167             :                                                  &tip_expiration))
     168           0 :         TALER_TESTING_FAIL (gts->is);
     169           0 :       if (tip_expiration->abs_value_us != expiration.abs_value_us)
     170             :       {
     171           0 :         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     172             :                     "Tip authorized expiration does not match\n");
     173           0 :         TALER_TESTING_interpreter_fail (gts->is);
     174           0 :         return;
     175             :       }
     176             :     }
     177           0 :     if (pickups_length != gts->pickups_length)
     178             :     {
     179           0 :       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     180             :                   "Length of pickups array does not match\n");
     181           0 :       TALER_TESTING_interpreter_fail (gts->is);
     182           0 :       return;
     183             :     }
     184             :     {
     185           0 :       for (unsigned int i = 0; i < pickups_length; ++i)
     186             :       {
     187             :         const struct TALER_TESTING_Command *pickup_cmd;
     188             : 
     189           0 :         pickup_cmd = TALER_TESTING_interpreter_lookup_command (gts->is,
     190           0 :                                                                gts->pickups[i]);
     191             :         {
     192             :           const uint64_t *num_planchets;
     193             : 
     194           0 :           if (GNUNET_OK !=
     195           0 :               TALER_TESTING_get_trait_uint64 (pickup_cmd,
     196             :                                               0,
     197             :                                               &num_planchets))
     198           0 :             TALER_TESTING_FAIL (gts->is);
     199             : 
     200           0 :           if (*num_planchets != pickups[i].num_planchets)
     201             :           {
     202           0 :             GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     203             :                         "Pickup planchet count does not match\n");
     204           0 :             TALER_TESTING_interpreter_fail (gts->is);
     205           0 :             return;
     206             :           }
     207             :         }
     208             :         {
     209             :           const struct TALER_Amount *total;
     210             : 
     211           0 :           if (GNUNET_OK !=
     212           0 :               TALER_TESTING_get_trait_amount_obj (pickup_cmd,
     213           0 :                                                   pickups[i].num_planchets,
     214             :                                                   &total))
     215           0 :             TALER_TESTING_FAIL (gts->is);
     216             : 
     217           0 :           if ((GNUNET_OK != TALER_amount_cmp_currency (total,
     218           0 :                                                        &pickups[i].
     219           0 :                                                        requested_amount)) ||
     220           0 :               (0 != TALER_amount_cmp (total,
     221           0 :                                       &pickups[i].requested_amount)))
     222             :           {
     223           0 :             GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     224             :                         "Pickup planchet sum does not match\n");
     225           0 :             TALER_TESTING_interpreter_fail (gts->is);
     226           0 :             return;
     227             :           }
     228           0 :           GNUNET_assert (0 < TALER_amount_add (&expected_total_picked_up,
     229             :                                                &expected_total_picked_up,
     230             :                                                total));
     231             :         }
     232             :       }
     233           0 :       if ((GNUNET_OK != TALER_amount_cmp_currency (&expected_total_picked_up,
     234           0 :                                                    total_picked_up)) ||
     235           0 :           (0 != TALER_amount_cmp (&expected_total_picked_up,
     236             :                                   total_picked_up)))
     237             :       {
     238           0 :         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     239             :                     "Tip picked up amount does not match\n");
     240           0 :         TALER_TESTING_interpreter_fail (gts->is);
     241           0 :         return;
     242             :       }
     243             :     }
     244           0 :     break;
     245           0 :   default:
     246           0 :     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
     247             :                 "Unhandled HTTP status.\n");
     248             :   }
     249           0 :   TALER_TESTING_interpreter_next (gts->is);
     250             : }
     251             : 
     252             : 
     253             : /**
     254             :  * Run the "GET tip" CMD.
     255             :  *
     256             :  * @param cls closure.
     257             :  * @param cmd command being run now.
     258             :  * @param is interpreter state.
     259             :  */
     260             : static void
     261           0 : merchant_get_tip_run (void *cls,
     262             :                       const struct TALER_TESTING_Command *cmd,
     263             :                       struct TALER_TESTING_Interpreter *is)
     264             : {
     265           0 :   struct MerchantTipGetState *tgs = cls;
     266             :   const struct TALER_TESTING_Command *tip_cmd;
     267             :   const struct GNUNET_HashCode *tip_id;
     268             : 
     269           0 :   tip_cmd = TALER_TESTING_interpreter_lookup_command (is,
     270             :                                                       tgs->tip_reference);
     271             : 
     272           0 :   if (GNUNET_OK !=
     273           0 :       TALER_TESTING_get_trait_tip_id (tip_cmd,
     274             :                                       0,
     275             :                                       &tip_id))
     276           0 :     TALER_TESTING_FAIL (is);
     277             : 
     278           0 :   tgs->is = is;
     279           0 :   tgs->tgh = TALER_MERCHANT_merchant_tip_get (is->ctx,
     280             :                                               tgs->merchant_url,
     281             :                                               tip_id,
     282           0 :                                               tgs->fetch_pickups,
     283             :                                               &merchant_get_tip_cb,
     284             :                                               tgs);
     285           0 :   GNUNET_assert (NULL != tgs->tgh);
     286             : }
     287             : 
     288             : 
     289             : /**
     290             : * Free the state of a "GET tip" CMD, and possibly
     291             : * cancel a pending operation thereof.
     292             : *
     293             : * @param cls closure.
     294             : * @param cmd command being run.
     295             : */
     296             : static void
     297           0 : merchant_get_tip_cleanup (void *cls,
     298             :                           const struct TALER_TESTING_Command *cmd)
     299             : {
     300           0 :   struct MerchantTipGetState *tgs = cls;
     301             : 
     302           0 :   if (NULL != tgs->tgh)
     303             :   {
     304           0 :     TALER_LOG_WARNING ("Get tip operation did not complete\n");
     305           0 :     TALER_MERCHANT_merchant_tip_get_cancel (tgs->tgh);
     306             :   }
     307           0 :   GNUNET_free (tgs);
     308           0 : }
     309             : 
     310             : 
     311             : /**
     312             :  * Define a GET /private/tips/$TIP_IDE CMD.
     313             :  *
     314             :  * @param label the command label
     315             :  * @param merchant_url base URL of the merchant which will
     316             :  *        serve the request.
     317             :  * @param tip_reference reference to a command that created a tip.
     318             :  * @param http_status expected HTTP response code for the request.
     319             :  */
     320             : struct TALER_TESTING_Command
     321           0 : TALER_TESTING_cmd_merchant_get_tip (const char *label,
     322             :                                     const char *merchant_url,
     323             :                                     const char *tip_reference,
     324             :                                     unsigned int http_status)
     325             : {
     326             :   struct MerchantTipGetState *tgs;
     327             : 
     328           0 :   tgs = GNUNET_new (struct MerchantTipGetState);
     329           0 :   tgs->merchant_url = merchant_url;
     330           0 :   tgs->tip_reference = tip_reference;
     331           0 :   tgs->http_status = http_status;
     332             :   {
     333           0 :     struct TALER_TESTING_Command cmd = {
     334             :       .cls = tgs,
     335             :       .label = label,
     336             :       .run = &merchant_get_tip_run,
     337             :       .cleanup = &merchant_get_tip_cleanup
     338             :     };
     339             : 
     340           0 :     return cmd;
     341             :   }
     342             : }
     343             : 
     344             : 
     345             : /**
     346             :  * Define a GET /private/tips/$TIP_ID CMD.
     347             :  *
     348             :  * @param label the command label
     349             :  * @param merchant_url base URL of the merchant which will
     350             :  *        serve the request.
     351             :  * @param tip_reference reference to a command that created a tip.
     352             :  * @param pickup_refs a NULL-terminated list of pickup commands
     353             :  *        associated with the tip.
     354             :  * @param http_status expected HTTP response code for the request.
     355             :  * @param ... NULL-terminated list of labels (const char *) of
     356             :  *        pickup (commands) we expect to be returned in the list
     357             :  *        (assuming @a http_code is #MHD_HTTP_OK)
     358             :  */
     359             : struct TALER_TESTING_Command
     360           0 : TALER_TESTING_cmd_merchant_get_tip_with_pickups (const char *label,
     361             :                                                  const char *merchant_url,
     362             :                                                  const char *tip_reference,
     363             :                                                  unsigned int http_status,
     364             :                                                  ...)
     365             : {
     366             :   struct MerchantTipGetState *tgs;
     367             : 
     368           0 :   tgs = GNUNET_new (struct MerchantTipGetState);
     369           0 :   tgs->merchant_url = merchant_url;
     370           0 :   tgs->tip_reference = tip_reference;
     371           0 :   tgs->fetch_pickups = true;
     372           0 :   tgs->http_status = http_status;
     373             :   {
     374             :     const char *clabel;
     375             :     va_list ap;
     376             : 
     377           0 :     va_start (ap, http_status);
     378           0 :     while (NULL != (clabel = va_arg (ap, const char *)))
     379             :     {
     380           0 :       GNUNET_array_append (tgs->pickups,
     381             :                            tgs->pickups_length,
     382             :                            clabel);
     383             :     }
     384           0 :     va_end (ap);
     385             :   }
     386             :   {
     387           0 :     struct TALER_TESTING_Command cmd = {
     388             :       .cls = tgs,
     389             :       .label = label,
     390             :       .run = &merchant_get_tip_run,
     391             :       .cleanup = &merchant_get_tip_cleanup
     392             :     };
     393             : 
     394           0 :     return cmd;
     395             :   }
     396             : }
     397             : 
     398             : 
     399             : /* end of testing_api_cmd_merchant_get_tip.c */

Generated by: LCOV version 1.14