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: 0 59 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) 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           0 : wallet_tip_get_cb (void *cls,
      87             :                    const struct TALER_MERCHANT_HttpResponse *hr,
      88             :                    struct GNUNET_TIME_Timestamp 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_Timestamp *expiration;
     131             : 
     132           0 :       if (GNUNET_OK !=
     133           0 :           TALER_TESTING_get_trait_timestamp (tip_cmd,
     134             :                                              0,
     135             :                                              &expiration))
     136           0 :         TALER_TESTING_interpreter_fail (gts->is);
     137           0 :       if (GNUNET_TIME_timestamp_cmp (*expiration,
     138             :                                      !=,
     139             :                                      reserve_expiration))
     140             :       {
     141           0 :         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     142             :                     "Tip expiration does not match\n");
     143           0 :         TALER_TESTING_interpreter_fail (gts->is);
     144           0 :         return;
     145             :       }
     146             :     }
     147           0 :     break;
     148           0 :   default:
     149           0 :     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
     150             :                 "Unhandled HTTP status.\n");
     151             :   }
     152           0 :   TALER_TESTING_interpreter_next (gts->is);
     153             : }
     154             : 
     155             : 
     156             : /**
     157             :  * Run the "GET tip" CMD.
     158             :  *
     159             :  * @param cls closure.
     160             :  * @param cmd command being run now.
     161             :  * @param is interpreter state.
     162             :  */
     163             : static void
     164           0 : wallet_get_tip_run (void *cls,
     165             :                     const struct TALER_TESTING_Command *cmd,
     166             :                     struct TALER_TESTING_Interpreter *is)
     167             : {
     168           0 :   struct WalletTipGetState *tgs = cls;
     169             :   const struct TALER_TESTING_Command *tip_cmd;
     170             :   const struct TALER_TipIdentifierP *tip_id;
     171             : 
     172           0 :   tip_cmd = TALER_TESTING_interpreter_lookup_command (is,
     173             :                                                       tgs->tip_reference);
     174             : 
     175           0 :   if (GNUNET_OK !=
     176           0 :       TALER_TESTING_get_trait_tip_id (tip_cmd,
     177             :                                       &tip_id))
     178           0 :     TALER_TESTING_FAIL (is);
     179             : 
     180           0 :   tgs->is = is;
     181           0 :   tgs->tgh = TALER_MERCHANT_wallet_tip_get (is->ctx,
     182             :                                             tgs->merchant_url,
     183             :                                             tip_id,
     184             :                                             &wallet_tip_get_cb,
     185             :                                             tgs);
     186             : }
     187             : 
     188             : 
     189             : /**
     190             :  * Free the state of a "GET tip" CMD, and possibly
     191             :  * cancel a pending operation thereof.
     192             :  *
     193             :  * @param cls closure.
     194             :  * @param cmd command being run.
     195             :  */
     196             : static void
     197           0 : wallet_get_tip_cleanup (void *cls,
     198             :                         const struct TALER_TESTING_Command *cmd)
     199             : {
     200           0 :   struct WalletTipGetState *tgs = cls;
     201             : 
     202           0 :   if (NULL != tgs->tgh)
     203             :   {
     204           0 :     TALER_LOG_WARNING ("Get tip operation did not complete\n");
     205           0 :     TALER_MERCHANT_wallet_tip_get_cancel (tgs->tgh);
     206             :   }
     207           0 :   GNUNET_free (tgs);
     208           0 : }
     209             : 
     210             : 
     211             : struct TALER_TESTING_Command
     212           0 : TALER_TESTING_cmd_wallet_get_tip (const char *label,
     213             :                                   const char *merchant_url,
     214             :                                   const char *tip_reference,
     215             :                                   unsigned int http_status)
     216             : {
     217             :   struct WalletTipGetState *tgs;
     218             : 
     219           0 :   tgs = GNUNET_new (struct WalletTipGetState);
     220           0 :   tgs->merchant_url = merchant_url;
     221           0 :   tgs->tip_reference = tip_reference;
     222           0 :   tgs->http_status = http_status;
     223             :   {
     224           0 :     struct TALER_TESTING_Command cmd = {
     225             :       .cls = tgs,
     226             :       .label = label,
     227             :       .run = &wallet_get_tip_run,
     228             :       .cleanup = &wallet_get_tip_cleanup
     229             :     };
     230             : 
     231           0 :     return cmd;
     232             :   }
     233             : }
     234             : 
     235             : 
     236             : struct TALER_TESTING_Command
     237           0 : TALER_TESTING_cmd_wallet_get_tip2 (const char *label,
     238             :                                    const char *merchant_url,
     239             :                                    const char *tip_reference,
     240             :                                    const char *amount_remaining,
     241             :                                    unsigned int http_status)
     242             : {
     243             :   struct WalletTipGetState *tgs;
     244             : 
     245           0 :   tgs = GNUNET_new (struct WalletTipGetState);
     246           0 :   tgs->merchant_url = merchant_url;
     247           0 :   tgs->tip_reference = tip_reference;
     248           0 :   tgs->cmp_amounts = true;
     249           0 :   GNUNET_assert (GNUNET_OK == TALER_string_to_amount (amount_remaining,
     250             :                                                       &tgs->amount_remaining));
     251           0 :   tgs->http_status = http_status;
     252             :   {
     253           0 :     struct TALER_TESTING_Command cmd = {
     254             :       .cls = tgs,
     255             :       .label = label,
     256             :       .run = &wallet_get_tip_run,
     257             :       .cleanup = &wallet_get_tip_cleanup
     258             :     };
     259             : 
     260           0 :     return cmd;
     261             :   }
     262             : }
     263             : 
     264             : 
     265             : /* end of testing_api_cmd_wallet_get_tip.c */

Generated by: LCOV version 1.14