LCOV - code coverage report
Current view: top level - testing - testing_api_cmd_wallet_get_tip.c (source / functions) Hit Total Coverage
Test: GNU Taler merchant coverage report Lines: 44 59 74.6 %
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) 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 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           2 : 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           2 :   struct WalletTipGetState *gts = cls;
      94             :   const struct TALER_TESTING_Command *tip_cmd;
      95             : 
      96           2 :   tip_cmd = TALER_TESTING_interpreter_lookup_command (
      97             :     gts->is,
      98             :     gts->tip_reference);
      99             : 
     100           2 :   gts->tgh = NULL;
     101           2 :   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           2 :   switch (hr->http_status)
     112             :   {
     113           2 :   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           2 :     if (gts->cmp_amounts)
     117             :     {
     118           1 :       if ((GNUNET_OK != TALER_amount_cmp_currency (&gts->amount_remaining,
     119           1 :                                                    amount_remaining)) ||
     120           1 :           (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           2 :       if (GNUNET_OK !=
     133           2 :           TALER_TESTING_get_trait_absolute_time (tip_cmd,
     134             :                                                  0,
     135             :                                                  &expiration))
     136           0 :         TALER_TESTING_interpreter_fail (gts->is);
     137           2 :       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           2 :     break;
     146           0 :   default:
     147           0 :     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
     148             :                 "Unhandled HTTP status.\n");
     149             :   }
     150           2 :   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           2 : wallet_get_tip_run (void *cls,
     163             :                     const struct TALER_TESTING_Command *cmd,
     164             :                     struct TALER_TESTING_Interpreter *is)
     165             : {
     166           2 :   struct WalletTipGetState *tgs = cls;
     167             :   const struct TALER_TESTING_Command *tip_cmd;
     168             :   const struct GNUNET_HashCode *tip_id;
     169             : 
     170           2 :   tip_cmd = TALER_TESTING_interpreter_lookup_command (is,
     171             :                                                       tgs->tip_reference);
     172             : 
     173           2 :   if (GNUNET_OK !=
     174           2 :       TALER_TESTING_get_trait_tip_id (tip_cmd,
     175             :                                       0,
     176             :                                       &tip_id))
     177           0 :     TALER_TESTING_FAIL (is);
     178             : 
     179           2 :   tgs->is = is;
     180           2 :   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           2 : wallet_get_tip_cleanup (void *cls,
     197             :                         const struct TALER_TESTING_Command *cmd)
     198             : {
     199           2 :   struct WalletTipGetState *tgs = cls;
     200             : 
     201           2 :   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           2 :   GNUNET_free (tgs);
     207           2 : }
     208             : 
     209             : 
     210             : struct TALER_TESTING_Command
     211           1 : TALER_TESTING_cmd_wallet_get_tip (const char *label,
     212             :                                   const char *merchant_url,
     213             :                                   const char *tip_reference,
     214             :                                   unsigned int http_status)
     215             : {
     216             :   struct WalletTipGetState *tgs;
     217             : 
     218           1 :   tgs = GNUNET_new (struct WalletTipGetState);
     219           1 :   tgs->merchant_url = merchant_url;
     220           1 :   tgs->tip_reference = tip_reference;
     221           1 :   tgs->http_status = http_status;
     222             :   {
     223           1 :     struct TALER_TESTING_Command cmd = {
     224             :       .cls = tgs,
     225             :       .label = label,
     226             :       .run = &wallet_get_tip_run,
     227             :       .cleanup = &wallet_get_tip_cleanup
     228             :     };
     229             : 
     230           1 :     return cmd;
     231             :   }
     232             : }
     233             : 
     234             : 
     235             : struct TALER_TESTING_Command
     236           1 : TALER_TESTING_cmd_wallet_get_tip2 (const char *label,
     237             :                                    const char *merchant_url,
     238             :                                    const char *tip_reference,
     239             :                                    const char *amount_remaining,
     240             :                                    unsigned int http_status)
     241             : {
     242             :   struct WalletTipGetState *tgs;
     243             : 
     244           1 :   tgs = GNUNET_new (struct WalletTipGetState);
     245           1 :   tgs->merchant_url = merchant_url;
     246           1 :   tgs->tip_reference = tip_reference;
     247           1 :   tgs->cmp_amounts = true;
     248           1 :   GNUNET_assert (GNUNET_OK == TALER_string_to_amount (amount_remaining,
     249             :                                                       &tgs->amount_remaining));
     250           1 :   tgs->http_status = http_status;
     251             :   {
     252           1 :     struct TALER_TESTING_Command cmd = {
     253             :       .cls = tgs,
     254             :       .label = label,
     255             :       .run = &wallet_get_tip_run,
     256             :       .cleanup = &wallet_get_tip_cleanup
     257             :     };
     258             : 
     259           1 :     return cmd;
     260             :   }
     261             : }
     262             : 
     263             : 
     264             : /* end of testing_api_cmd_wallet_get_tip.c */

Generated by: LCOV version 1.14