LCOV - code coverage report
Current view: top level - testing - testing_api_cmd_get_products.c (source / functions) Hit Total Coverage
Test: GNU Taler merchant coverage report Lines: 39 57 68.4 %
Date: 2021-08-30 06:54:17 Functions: 4 4 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_products.c
      21             :  * @brief command to test GET /products
      22             :  * @author Christian Grothoff
      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 products" CMD.
      33             :  */
      34             : struct GetProductsState
      35             : {
      36             : 
      37             :   /**
      38             :    * Handle for a "GET product" request.
      39             :    */
      40             :   struct TALER_MERCHANT_ProductsGetHandle *igh;
      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             :    * Expected HTTP response code.
      54             :    */
      55             :   unsigned int http_status;
      56             : 
      57             :   /**
      58             :    * The list of product references.
      59             :    */
      60             :   const char **products;
      61             : 
      62             :   /**
      63             :    * Length of @e products.
      64             :    */
      65             :   unsigned int products_length;
      66             : 
      67             : };
      68             : 
      69             : 
      70             : /**
      71             :  * Callback for a GET /products operation.
      72             :  *
      73             :  * @param cls closure for this function
      74             :  * @param hr HTTP response details
      75             :  * @param products_length length of the @a products array
      76             :  * @param products array of products the requested instance offers
      77             :  */
      78             : static void
      79           7 : get_products_cb (void *cls,
      80             :                  const struct TALER_MERCHANT_HttpResponse *hr,
      81             :                  unsigned int products_length,
      82             :                  const struct TALER_MERCHANT_InventoryEntry products[])
      83             : {
      84           7 :   struct GetProductsState *gis = cls;
      85             : 
      86           7 :   gis->igh = NULL;
      87           7 :   if (gis->http_status != hr->http_status)
      88             :   {
      89           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
      90             :                 "Unexpected response code %u (%d) to command %s\n",
      91             :                 hr->http_status,
      92             :                 (int) hr->ec,
      93             :                 TALER_TESTING_interpreter_get_current_label (gis->is));
      94           0 :     TALER_TESTING_interpreter_fail (gis->is);
      95           0 :     return;
      96             :   }
      97           7 :   switch (hr->http_status)
      98             :   {
      99           5 :   case MHD_HTTP_OK:
     100           5 :     if (products_length != gis->products_length)
     101             :     {
     102           0 :       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     103             :                   "Length of products found does not match\n");
     104           0 :       TALER_TESTING_interpreter_fail (gis->is);
     105           0 :       return;
     106             :     }
     107           6 :     for (unsigned int i = 0; i < gis->products_length; ++i)
     108             :     {
     109             :       const struct TALER_TESTING_Command *product_cmd;
     110             : 
     111           1 :       product_cmd = TALER_TESTING_interpreter_lookup_command (
     112             :         gis->is,
     113           1 :         gis->products[i]);
     114             : 
     115             :       {
     116             :         const char *product_id;
     117           1 :         if (GNUNET_OK !=
     118           1 :             TALER_TESTING_get_trait_string (product_cmd,
     119             :                                             2,
     120             :                                             &product_id))
     121             :         {
     122           0 :           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     123             :                       "Could not fetch product id\n");
     124           0 :           TALER_TESTING_interpreter_fail (gis->is);
     125           0 :           return;
     126             :         }
     127           1 :         if (0 != strcmp (products[i].product_id,
     128             :                          product_id))
     129             :         {
     130           0 :           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     131             :                       "Product id does not match\n");
     132           0 :           TALER_TESTING_interpreter_fail (gis->is);
     133           0 :           return;
     134             :         }
     135             :       }
     136             :     }
     137           5 :     break;
     138           2 :   case MHD_HTTP_UNAUTHORIZED:
     139           2 :     break;
     140           0 :   case MHD_HTTP_NOT_FOUND:
     141             :     /* instance does not exist */
     142           0 :     break;
     143           0 :   default:
     144           0 :     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
     145             :                 "Unhandled HTTP status %u (%d).\n",
     146             :                 hr->http_status,
     147             :                 hr->ec);
     148             :   }
     149           7 :   TALER_TESTING_interpreter_next (gis->is);
     150             : }
     151             : 
     152             : 
     153             : /**
     154             :  * Run the "GET /products" CMD.
     155             :  *
     156             :  *
     157             :  * @param cls closure.
     158             :  * @param cmd command being run now.
     159             :  * @param is interpreter state.
     160             :  */
     161             : static void
     162           7 : get_products_run (void *cls,
     163             :                   const struct TALER_TESTING_Command *cmd,
     164             :                   struct TALER_TESTING_Interpreter *is)
     165             : {
     166           7 :   struct GetProductsState *gis = cls;
     167             : 
     168           7 :   gis->is = is;
     169           7 :   gis->igh = TALER_MERCHANT_products_get (is->ctx,
     170             :                                           gis->merchant_url,
     171             :                                           &get_products_cb,
     172             :                                           gis);
     173           7 :   GNUNET_assert (NULL != gis->igh);
     174           7 : }
     175             : 
     176             : 
     177             : /**
     178             :  * Free the state of a "GET product" CMD, and possibly
     179             :  * cancel a pending operation thereof.
     180             :  *
     181             :  * @param cls closure.
     182             :  * @param cmd command being run.
     183             :  */
     184             : static void
     185           7 : get_products_cleanup (void *cls,
     186             :                       const struct TALER_TESTING_Command *cmd)
     187             : {
     188           7 :   struct GetProductsState *gis = cls;
     189             : 
     190           7 :   if (NULL != gis->igh)
     191             :   {
     192           0 :     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
     193             :                 "GET /products operation did not complete\n");
     194           0 :     TALER_MERCHANT_products_get_cancel (gis->igh);
     195             :   }
     196           7 :   GNUNET_array_grow (gis->products,
     197             :                      gis->products_length,
     198             :                      0);
     199           7 :   GNUNET_free (gis);
     200           7 : }
     201             : 
     202             : 
     203             : struct TALER_TESTING_Command
     204           7 : TALER_TESTING_cmd_merchant_get_products (const char *label,
     205             :                                          const char *merchant_url,
     206             :                                          unsigned int http_status,
     207             :                                          ...)
     208             : {
     209             :   struct GetProductsState *gis;
     210             : 
     211           7 :   gis = GNUNET_new (struct GetProductsState);
     212           7 :   gis->merchant_url = merchant_url;
     213           7 :   gis->http_status = http_status;
     214             :   {
     215             :     const char *clabel;
     216             :     va_list ap;
     217             : 
     218           7 :     va_start (ap, http_status);
     219           8 :     while (NULL != (clabel = va_arg (ap, const char *)))
     220             :     {
     221           1 :       GNUNET_array_append (gis->products,
     222             :                            gis->products_length,
     223             :                            clabel);
     224             :     }
     225           7 :     va_end (ap);
     226             :   }
     227             :   {
     228           7 :     struct TALER_TESTING_Command cmd = {
     229             :       .cls = gis,
     230             :       .label = label,
     231             :       .run = &get_products_run,
     232             :       .cleanup = &get_products_cleanup
     233             :     };
     234             : 
     235           7 :     return cmd;
     236             :   }
     237             : }
     238             : 
     239             : 
     240             : /* end of testing_api_cmd_get_products.c */

Generated by: LCOV version 1.14