LCOV - code coverage report
Current view: top level - testing - testing_api_cmd_get_tips.c (source / functions) Hit Total Coverage
Test: GNU Taler merchant coverage report Lines: 57 79 72.2 %
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) 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_get_tips.c
      21             :  * @brief command to test GET /private/tips
      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             : /**
      32             :  * State of a "GET tips" CMD.
      33             :  */
      34             : struct GetTipsState
      35             : {
      36             : 
      37             :   /**
      38             :    * Handle for a "GET tips" request.
      39             :    */
      40             :   struct TALER_MERCHANT_TipsGetHandle *tgh;
      41             : 
      42             :   /**
      43             :    * The interpreter state.
      44             :    */
      45             :   struct TALER_TESTING_Interpreter *is;
      46             : 
      47             :   /**
      48             :    * Base URL of the merchant serving the request.
      49             :    */
      50             :   const char *merchant_url;
      51             : 
      52             :   /**
      53             :    * Row to start querying the database from.
      54             :    */
      55             :   uint64_t offset;
      56             : 
      57             :   /**
      58             :    * How many rows to return (with direction).
      59             :    */
      60             :   int64_t limit;
      61             : 
      62             :   /**
      63             :    * Expected HTTP response code.
      64             :    */
      65             :   unsigned int http_status;
      66             : 
      67             :   /**
      68             :    * Length of @e tips.
      69             :    */
      70             :   unsigned int tips_length;
      71             : 
      72             :   /**
      73             :    * References to tips that we expect to be found.
      74             :    */
      75             :   const char **tips;
      76             : 
      77             : };
      78             : 
      79             : /**
      80             :  * Callback for a GET /private/tips operation.
      81             :  *
      82             :  * @param cls closure for this function
      83             :  * @param hr HTTP response details
      84             :  * @param tips_length length of the @a tips array
      85             :  * @param tips array of tips
      86             :  */
      87             : static void
      88           3 : get_tips_cb (void *cls,
      89             :              const struct TALER_MERCHANT_HttpResponse *hr,
      90             :              unsigned int tips_length,
      91             :              const struct TALER_MERCHANT_TipEntry tips[])
      92             : {
      93           3 :   struct GetTipsState *gts = cls;
      94             : 
      95           3 :   gts->tgh = NULL;
      96           3 :   if (gts->http_status != hr->http_status)
      97             :   {
      98           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
      99             :                 "Unexpected response code %u (%d) to command %s\n",
     100             :                 hr->http_status,
     101             :                 (int) hr->ec,
     102             :                 TALER_TESTING_interpreter_get_current_label (gts->is));
     103           0 :     TALER_TESTING_interpreter_fail (gts->is);
     104           0 :     return;
     105             :   }
     106           3 :   switch (hr->http_status)
     107             :   {
     108           3 :   case MHD_HTTP_OK:
     109           3 :     if (tips_length != gts->tips_length)
     110             :     {
     111           0 :       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     112             :                   "Tips length does not match\n");
     113           0 :       TALER_TESTING_interpreter_fail (gts->is);
     114           0 :       return;
     115             :     }
     116           8 :     for (unsigned int i = 0; i < tips_length; ++i)
     117             :     {
     118             :       const struct TALER_TESTING_Command *tip_cmd;
     119             : 
     120           5 :       tip_cmd = TALER_TESTING_interpreter_lookup_command (
     121             :         gts->is,
     122           5 :         gts->tips[i]);
     123             :       {
     124             :         const struct GNUNET_HashCode *tip_id;
     125             : 
     126           5 :         if (GNUNET_OK !=
     127           5 :             TALER_TESTING_get_trait_tip_id (tip_cmd,
     128             :                                             0,
     129             :                                             &tip_id))
     130             :         {
     131           0 :           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     132             :                       "Could not fetch tip id\n");
     133           0 :           TALER_TESTING_interpreter_fail (gts->is);
     134           0 :           return;
     135             :         }
     136           5 :         if (0 != GNUNET_memcmp (tip_id,
     137             :                                 &tips[i].tip_id))
     138             :         {
     139           0 :           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     140             :                       "Tip id does not match\n");
     141           0 :           TALER_TESTING_interpreter_fail (gts->is);
     142           0 :           return;
     143             :         }
     144             :       }
     145             :       {
     146             :         const struct TALER_Amount *tip_amount;
     147             : 
     148           5 :         if (GNUNET_OK !=
     149           5 :             TALER_TESTING_get_trait_amount_obj (tip_cmd,
     150             :                                                 0,
     151             :                                                 &tip_amount))
     152             :         {
     153           0 :           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     154             :                       "Could not fetch tip amount\n");
     155           0 :           TALER_TESTING_interpreter_fail (gts->is);
     156           0 :           return;
     157             :         }
     158           5 :         if ((GNUNET_OK != TALER_amount_cmp_currency (tip_amount,
     159          10 :                                                      &tips[i].tip_amount)) ||
     160           5 :             (0 != TALER_amount_cmp (tip_amount,
     161           5 :                                     &tips[i].tip_amount)))
     162             :         {
     163           0 :           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     164             :                       "Tip amount does not match\n");
     165           0 :           TALER_TESTING_interpreter_fail (gts->is);
     166           0 :           return;
     167             :         }
     168             :       }
     169             :     }
     170           3 :     break;
     171           0 :   default:
     172           0 :     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
     173             :                 "Unhandled HTTP status.\n");
     174             :   }
     175           3 :   TALER_TESTING_interpreter_next (gts->is);
     176             : }
     177             : 
     178             : 
     179             : /**
     180             :  * Run the "GET /private/tips" CMD.
     181             :  *
     182             :  * @param cls closure.
     183             :  * @param cmd command being run now.
     184             :  * @param is interpreter state.
     185             :  */
     186             : static void
     187           3 : get_tips_run (void *cls,
     188             :               const struct TALER_TESTING_Command *cmd,
     189             :               struct TALER_TESTING_Interpreter *is)
     190             : {
     191           3 :   struct GetTipsState *gts = cls;
     192             : 
     193           3 :   gts->is = is;
     194           3 :   gts->tgh = TALER_MERCHANT_tips_get2 (is->ctx,
     195             :                                        gts->merchant_url,
     196             :                                        TALER_EXCHANGE_YNA_NO,
     197             :                                        gts->limit,
     198             :                                        gts->offset,
     199             :                                        &get_tips_cb,
     200             :                                        gts);
     201             : 
     202           3 :   GNUNET_assert (NULL != gts->tgh);
     203           3 : }
     204             : 
     205             : 
     206             : /**
     207             :  * Free the state of a "GET tips" CMD, and possibly
     208             :  * cancel a pending operation thereof.
     209             :  *
     210             :  * @param cls closure.
     211             :  * @param cmd command being run.
     212             :  */
     213             : static void
     214           3 : get_tips_cleanup (void *cls,
     215             :                   const struct TALER_TESTING_Command *cmd)
     216             : {
     217           3 :   struct GetTipsState *gts = cls;
     218             : 
     219           3 :   if (NULL != gts->tgh)
     220             :   {
     221           0 :     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
     222             :                 "GET /private/tips operation did not complete\n");
     223           0 :     TALER_MERCHANT_tips_get_cancel (gts->tgh);
     224             :   }
     225           3 :   GNUNET_array_grow (gts->tips,
     226             :                      gts->tips_length,
     227             :                      0);
     228           3 :   GNUNET_free (gts);
     229           3 : }
     230             : 
     231             : 
     232             : struct TALER_TESTING_Command
     233           1 : TALER_TESTING_cmd_get_tips (const char *label,
     234             :                             const char *merchant_url,
     235             :                             unsigned int http_status,
     236             :                             ...)
     237             : {
     238             :   struct GetTipsState *gts;
     239             : 
     240           1 :   gts = GNUNET_new (struct GetTipsState);
     241           1 :   gts->merchant_url = merchant_url;
     242           1 :   gts->offset = INT64_MAX;
     243           1 :   gts->limit = -20;
     244           1 :   gts->http_status = http_status;
     245             :   {
     246             :     const char *clabel;
     247             :     va_list ap;
     248             : 
     249           1 :     va_start (ap, http_status);
     250           3 :     while (NULL != (clabel = va_arg (ap, const char *)))
     251             :     {
     252           2 :       GNUNET_array_append (gts->tips,
     253             :                            gts->tips_length,
     254             :                            clabel);
     255             :     }
     256           1 :     va_end (ap);
     257             :   }
     258             :   {
     259           1 :     struct TALER_TESTING_Command cmd = {
     260             :       .cls = gts,
     261             :       .label = label,
     262             :       .run = &get_tips_run,
     263             :       .cleanup = &get_tips_cleanup
     264             :     };
     265             : 
     266           1 :     return cmd;
     267             :   }
     268             : }
     269             : 
     270             : 
     271             : struct TALER_TESTING_Command
     272           2 : TALER_TESTING_cmd_get_tips2 (const char *label,
     273             :                              const char *merchant_url,
     274             :                              uint64_t offset,
     275             :                              int64_t limit,
     276             :                              unsigned int http_status,
     277             :                              ...)
     278             : {
     279             :   struct GetTipsState *gts;
     280             : 
     281           2 :   gts = GNUNET_new (struct GetTipsState);
     282           2 :   gts->merchant_url = merchant_url;
     283           2 :   gts->offset = offset;
     284           2 :   gts->limit = limit;
     285           2 :   gts->http_status = http_status;
     286             :   {
     287             :     const char *clabel;
     288             :     va_list ap;
     289             : 
     290           2 :     va_start (ap, http_status);
     291           5 :     while (NULL != (clabel = va_arg (ap, const char *)))
     292             :     {
     293           3 :       GNUNET_array_append (gts->tips,
     294             :                            gts->tips_length,
     295             :                            clabel);
     296             :     }
     297           2 :     va_end (ap);
     298             :   }
     299             :   {
     300           2 :     struct TALER_TESTING_Command cmd = {
     301             :       .cls = gts,
     302             :       .label = label,
     303             :       .run = &get_tips_run,
     304             :       .cleanup = &get_tips_cleanup
     305             :     };
     306             : 
     307           2 :     return cmd;
     308             :   }
     309             : }
     310             : 
     311             : 
     312             : /* end of testing_api_cmd_get_tips.c */

Generated by: LCOV version 1.14