LCOV - code coverage report
Current view: top level - testing - testing_api_cmd_wallet_get_tip.c (source / functions) Hit Total Coverage
Test: GNU Taler coverage report Lines: 0 59 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) 2014-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_wallet_get_tip.c
      21             :  * @brief command to test the tipping.
      22             :  * @author Marcello Stanisci
      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 for a GET /tips/$TIP_ID CMD.
      33             :  */
      34             : struct WalletTipGetState
      35             : {
      36             : 
      37             :   /**
      38             :    * The merchant base URL.
      39             :    */
      40             :   const char *merchant_url;
      41             : 
      42             :   /**
      43             :    * Expected HTTP response code for this CMD.
      44             :    */
      45             :   unsigned int http_status;
      46             : 
      47             :   /**
      48             :    * Whether to compare amounts or not.
      49             :    */
      50             :   bool cmp_amounts;
      51             : 
      52             :   /**
      53             :    * The expected amount remaining.
      54             :    */
      55             :   struct TALER_Amount amount_remaining;
      56             : 
      57             :   /**
      58             :    * The handle to the current GET /tips/$TIP_ID request.
      59             :    */
      60             :   struct TALER_MERCHANT_TipWalletGetHandle *tgh;
      61             : 
      62             :   /**
      63             :    * The interpreter state.
      64             :    */
      65             :   struct TALER_TESTING_Interpreter *is;
      66             : 
      67             :   /**
      68             :    * Reference to a command that created a tip.
      69             :    */
      70             :   const char *tip_reference;
      71             : };
      72             : 
      73             : 
      74             : /**
      75             :  * Callback to process a GET /tips/$TIP_ID request, it mainly
      76             :  * checks that what the backend returned matches the command's
      77             :  * expectations.
      78             :  *
      79             :  * @param cls closure
      80             :  * @param hr HTTP response
      81             :  * @param reserve_expiration when the tip reserve will expire
      82             :  * @param exchange_url from where to pick up the tip
      83             :  * @param amount_remaining how much is remaining
      84             :  */
      85             : static void
      86           0 : wallet_tip_get_cb (void *cls,
      87             :                    const struct TALER_MERCHANT_HttpResponse *hr,
      88             :                    struct GNUNET_TIME_Absolute reserve_expiration,
      89             :                    const char *exchange_url,
      90             :                    const struct TALER_Amount *amount_remaining)
      91             : {
      92             :   /* FIXME, deeper checks should be implemented here. */
      93           0 :   struct WalletTipGetState *gts = cls;
      94             :   const struct TALER_TESTING_Command *tip_cmd;
      95             : 
      96           0 :   tip_cmd = TALER_TESTING_interpreter_lookup_command (
      97             :     gts->is,
      98             :     gts->tip_reference);
      99             : 
     100           0 :   gts->tgh = NULL;
     101           0 :   if (gts->http_status != hr->http_status)
     102             :   {
     103           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     104             :                 "Unexpected response code %u (%d) to command %s\n",
     105             :                 hr->http_status,
     106             :                 (int) hr->ec,
     107             :                 TALER_TESTING_interpreter_get_current_label (gts->is));
     108           0 :     TALER_TESTING_interpreter_fail (gts->is);
     109           0 :     return;
     110             :   }
     111           0 :   switch (hr->http_status)
     112             :   {
     113           0 :   case MHD_HTTP_OK:
     114             :     // FIXME: use gts->tip_reference here to
     115             :     // check if the data returned matches that from the POST / PATCH
     116           0 :     if (gts->cmp_amounts)
     117             :     {
     118           0 :       if ((GNUNET_OK != TALER_amount_cmp_currency (&gts->amount_remaining,
     119           0 :                                                    amount_remaining)) ||
     120           0 :           (0 != TALER_amount_cmp (&gts->amount_remaining,
     121             :                                   amount_remaining)))
     122             :       {
     123           0 :         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     124             :                     "Amount remaining on tip does not match\n");
     125           0 :         TALER_TESTING_interpreter_fail (gts->is);
     126           0 :         return;
     127             :       }
     128             :     }
     129             :     {
     130             :       const struct GNUNET_TIME_Absolute *expiration;
     131             : 
     132           0 :       if (GNUNET_OK !=
     133           0 :           TALER_TESTING_get_trait_absolute_time (tip_cmd,
     134             :                                                  0,
     135             :                                                  &expiration))
     136           0 :         TALER_TESTING_interpreter_fail (gts->is);
     137           0 :       if (expiration->abs_value_us != reserve_expiration.abs_value_us)
     138             :       {
     139           0 :         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     140             :                     "Tip expiration does not match\n");
     141           0 :         TALER_TESTING_interpreter_fail (gts->is);
     142           0 :         return;
     143             :       }
     144             :     }
     145           0 :     break;
     146           0 :   default:
     147           0 :     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
     148             :                 "Unhandled HTTP status.\n");
     149             :   }
     150           0 :   TALER_TESTING_interpreter_next (gts->is);
     151             : }
     152             : 
     153             : 
     154             : /**
     155             :  * Run the "GET tip" CMD.
     156             :  *
     157             :  * @param cls closure.
     158             :  * @param cmd command being run now.
     159             :  * @param is interpreter state.
     160             :  */
     161             : static void
     162           0 : wallet_get_tip_run (void *cls,
     163             :                     const struct TALER_TESTING_Command *cmd,
     164             :                     struct TALER_TESTING_Interpreter *is)
     165             : {
     166           0 :   struct WalletTipGetState *tgs = cls;
     167             :   const struct TALER_TESTING_Command *tip_cmd;
     168             :   const struct GNUNET_HashCode *tip_id;
     169             : 
     170           0 :   tip_cmd = TALER_TESTING_interpreter_lookup_command (is,
     171             :                                                       tgs->tip_reference);
     172             : 
     173           0 :   if (GNUNET_OK !=
     174           0 :       TALER_TESTING_get_trait_tip_id (tip_cmd,
     175             :                                       0,
     176             :                                       &tip_id))
     177           0 :     TALER_TESTING_FAIL (is);
     178             : 
     179           0 :   tgs->is = is;
     180           0 :   tgs->tgh = TALER_MERCHANT_wallet_tip_get (is->ctx,
     181             :                                             tgs->merchant_url,
     182             :                                             tip_id,
     183             :                                             &wallet_tip_get_cb,
     184             :                                             tgs);
     185             : }
     186             : 
     187             : 
     188             : /**
     189             :  * Free the state of a "GET tip" CMD, and possibly
     190             :  * cancel a pending operation thereof.
     191             :  *
     192             :  * @param cls closure.
     193             :  * @param cmd command being run.
     194             :  */
     195             : static void
     196           0 : wallet_get_tip_cleanup (void *cls,
     197             :                         const struct TALER_TESTING_Command *cmd)
     198             : {
     199           0 :   struct WalletTipGetState *tgs = cls;
     200             : 
     201           0 :   if (NULL != tgs->tgh)
     202             :   {
     203           0 :     TALER_LOG_WARNING ("Get tip operation did not complete\n");
     204           0 :     TALER_MERCHANT_wallet_tip_get_cancel (tgs->tgh);
     205             :   }
     206           0 :   GNUNET_free (tgs);
     207           0 : }
     208             : 
     209             : 
     210             : /**
     211             :  * Define a GET /tips/$TIP_ID CMD.
     212             :  *
     213             :  * @param label the command label
     214             :  * @param merchant_url base URL of the merchant which will
     215             :  *        serve the request.
     216             :  * @param tip_reference reference to a command that created a tip.
     217             :  * @param http_status expected HTTP response code for the request.
     218             :  */
     219             : struct TALER_TESTING_Command
     220           0 : TALER_TESTING_cmd_wallet_get_tip (const char *label,
     221             :                                   const char *merchant_url,
     222             :                                   const char *tip_reference,
     223             :                                   unsigned int http_status)
     224             : {
     225             :   struct WalletTipGetState *tgs;
     226             : 
     227           0 :   tgs = GNUNET_new (struct WalletTipGetState);
     228           0 :   tgs->merchant_url = merchant_url;
     229           0 :   tgs->tip_reference = tip_reference;
     230           0 :   tgs->http_status = http_status;
     231             :   {
     232           0 :     struct TALER_TESTING_Command cmd = {
     233             :       .cls = tgs,
     234             :       .label = label,
     235             :       .run = &wallet_get_tip_run,
     236             :       .cleanup = &wallet_get_tip_cleanup
     237             :     };
     238             : 
     239           0 :     return cmd;
     240             :   }
     241             : }
     242             : 
     243             : 
     244             : /**
     245             :  * Define a GET /tips/$TIP_ID CMD.
     246             :  *
     247             :  * @param label the command label
     248             :  * @param merchant_url base URL of the merchant which will
     249             :  *        serve the request.
     250             :  * @param tip_reference reference to a command that created a tip.
     251             :  * @param amount_remaining the balance remaining after pickups.
     252             :  * @param http_status expected HTTP response code for the request.
     253             :  */
     254             : struct TALER_TESTING_Command
     255           0 : TALER_TESTING_cmd_wallet_get_tip2 (const char *label,
     256             :                                    const char *merchant_url,
     257             :                                    const char *tip_reference,
     258             :                                    const char *amount_remaining,
     259             :                                    unsigned int http_status)
     260             : {
     261             :   struct WalletTipGetState *tgs;
     262             : 
     263           0 :   tgs = GNUNET_new (struct WalletTipGetState);
     264           0 :   tgs->merchant_url = merchant_url;
     265           0 :   tgs->tip_reference = tip_reference;
     266           0 :   tgs->cmp_amounts = true;
     267           0 :   GNUNET_assert (GNUNET_OK == TALER_string_to_amount (amount_remaining,
     268             :                                                       &tgs->amount_remaining));
     269           0 :   tgs->http_status = http_status;
     270             :   {
     271           0 :     struct TALER_TESTING_Command cmd = {
     272             :       .cls = tgs,
     273             :       .label = label,
     274             :       .run = &wallet_get_tip_run,
     275             :       .cleanup = &wallet_get_tip_cleanup
     276             :     };
     277             : 
     278           0 :     return cmd;
     279             :   }
     280             : }
     281             : 
     282             : 
     283             : /* end of testing_api_cmd_wallet_get_tip.c */

Generated by: LCOV version 1.14