LCOV - code coverage report
Current view: top level - testing - testing_api_cmd_patch_product.c (source / functions) Hit Total Coverage
Test: GNU Taler merchant coverage report Lines: 63 76 82.9 %
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_patch_product.c
      21             :  * @brief command to test PATCH /product
      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 "PATCH /product" CMD.
      33             :  */
      34             : struct PatchProductState
      35             : {
      36             : 
      37             :   /**
      38             :    * Handle for a "GET product" request.
      39             :    */
      40             :   struct TALER_MERCHANT_ProductPatchHandle *iph;
      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             :    * ID of the product to run GET for.
      54             :    */
      55             :   const char *product_id;
      56             : 
      57             :   /**
      58             :    * description of the product
      59             :    */
      60             :   const char *description;
      61             : 
      62             :   /**
      63             :    * Map from IETF BCP 47 language tags to localized descriptions
      64             :    */
      65             :   json_t *description_i18n;
      66             : 
      67             :   /**
      68             :    * unit in which the product is measured (liters, kilograms, packages, etc.)
      69             :    */
      70             :   const char *unit;
      71             : 
      72             :   /**
      73             :    * the price for one @a unit of the product
      74             :    */
      75             :   struct TALER_Amount price;
      76             : 
      77             :   /**
      78             :    * base64-encoded product image
      79             :    */
      80             :   char *image;
      81             : 
      82             :   /**
      83             :    * list of taxes paid by the merchant
      84             :    */
      85             :   json_t *taxes;
      86             : 
      87             :   /**
      88             :    * in @e units, -1 to indicate "infinite" (i.e. electronic books)
      89             :    */
      90             :   int64_t total_stock;
      91             : 
      92             :   /**
      93             :    * in @e units.
      94             :    */
      95             :   int64_t total_lost;
      96             : 
      97             :   /**
      98             :    * where the product is in stock
      99             :    */
     100             :   json_t *address;
     101             : 
     102             :   /**
     103             :    * when the next restocking is expected to happen, 0 for unknown,
     104             :    */
     105             :   struct GNUNET_TIME_Absolute next_restock;
     106             : 
     107             :   /**
     108             :    * Expected HTTP response code.
     109             :    */
     110             :   unsigned int http_status;
     111             : 
     112             : };
     113             : 
     114             : 
     115             : /**
     116             :  * Callback for a PATCH /products/$ID operation.
     117             :  *
     118             :  * @param cls closure for this function
     119             :  * @param hr response being processed
     120             :  */
     121             : static void
     122           3 : patch_product_cb (void *cls,
     123             :                   const struct TALER_MERCHANT_HttpResponse *hr)
     124             : {
     125           3 :   struct PatchProductState *pis = cls;
     126             : 
     127           3 :   pis->iph = NULL;
     128           3 :   if (pis->http_status != hr->http_status)
     129             :   {
     130           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     131             :                 "Unexpected response code %u (%d) to command %s\n",
     132             :                 hr->http_status,
     133             :                 (int) hr->ec,
     134             :                 TALER_TESTING_interpreter_get_current_label (pis->is));
     135           0 :     TALER_TESTING_interpreter_fail (pis->is);
     136           0 :     return;
     137             :   }
     138           3 :   switch (hr->http_status)
     139             :   {
     140           2 :   case MHD_HTTP_NO_CONTENT:
     141           2 :     break;
     142           0 :   case MHD_HTTP_UNAUTHORIZED:
     143           0 :     break;
     144           0 :   case MHD_HTTP_FORBIDDEN:
     145           0 :     break;
     146           1 :   case MHD_HTTP_NOT_FOUND:
     147           1 :     break;
     148           0 :   case MHD_HTTP_CONFLICT:
     149           0 :     break;
     150           0 :   default:
     151           0 :     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
     152             :                 "Unhandled HTTP status %u for PATCH /products/ID.\n",
     153             :                 hr->http_status);
     154             :   }
     155           3 :   TALER_TESTING_interpreter_next (pis->is);
     156             : }
     157             : 
     158             : 
     159             : /**
     160             :  * Run the "PATCH /products/$ID" CMD.
     161             :  *
     162             :  *
     163             :  * @param cls closure.
     164             :  * @param cmd command being run now.
     165             :  * @param is interpreter state.
     166             :  */
     167             : static void
     168           3 : patch_product_run (void *cls,
     169             :                    const struct TALER_TESTING_Command *cmd,
     170             :                    struct TALER_TESTING_Interpreter *is)
     171             : {
     172           3 :   struct PatchProductState *pis = cls;
     173             : 
     174           3 :   pis->is = is;
     175           6 :   pis->iph = TALER_MERCHANT_product_patch (is->ctx,
     176             :                                            pis->merchant_url,
     177             :                                            pis->product_id,
     178             :                                            pis->description,
     179           3 :                                            pis->description_i18n,
     180             :                                            pis->unit,
     181           3 :                                            &pis->price,
     182           3 :                                            pis->image,
     183           3 :                                            pis->taxes,
     184             :                                            pis->total_stock,
     185           3 :                                            pis->total_lost,
     186           3 :                                            pis->address,
     187             :                                            pis->next_restock,
     188             :                                            &patch_product_cb,
     189             :                                            pis);
     190           3 :   GNUNET_assert (NULL != pis->iph);
     191           3 : }
     192             : 
     193             : 
     194             : /**
     195             :  * Offers information from the PATCH /products CMD state to other
     196             :  * commands.
     197             :  *
     198             :  * @param cls closure
     199             :  * @param[out] ret result (could be anything)
     200             :  * @param trait name of the trait
     201             :  * @param index index number of the object to extract.
     202             :  * @return #GNUNET_OK on success
     203             :  */
     204             : static int
     205          20 : patch_product_traits (void *cls,
     206             :                       const void **ret,
     207             :                       const char *trait,
     208             :                       unsigned int index)
     209             : {
     210          20 :   struct PatchProductState *pps = cls;
     211             :   struct TALER_TESTING_Trait traits[] = {
     212          20 :     TALER_TESTING_make_trait_string (0, pps->description),
     213          20 :     TALER_TESTING_make_trait_json (0, pps->description_i18n),
     214          20 :     TALER_TESTING_make_trait_string (1, pps->unit),
     215          20 :     TALER_TESTING_make_trait_amount_obj (0, &pps->price),
     216          20 :     TALER_TESTING_make_trait_string (3, pps->image),
     217          20 :     TALER_TESTING_make_trait_json (2, pps->taxes),
     218          20 :     TALER_TESTING_make_trait_int64 (0, &pps->total_stock),
     219          20 :     TALER_TESTING_make_trait_json (3, pps->address),
     220          20 :     TALER_TESTING_make_trait_absolute_time (0, &pps->next_restock),
     221          20 :     TALER_TESTING_make_trait_string (2, pps->product_id),
     222          20 :     TALER_TESTING_trait_end (),
     223             :   };
     224             : 
     225          20 :   return TALER_TESTING_get_trait (traits,
     226             :                                   ret,
     227             :                                   trait,
     228             :                                   index);
     229             : }
     230             : 
     231             : 
     232             : /**
     233             :  * Free the state of a "GET product" CMD, and possibly
     234             :  * cancel a pending operation thereof.
     235             :  *
     236             :  * @param cls closure.
     237             :  * @param cmd command being run.
     238             :  */
     239             : static void
     240           3 : patch_product_cleanup (void *cls,
     241             :                        const struct TALER_TESTING_Command *cmd)
     242             : {
     243           3 :   struct PatchProductState *pis = cls;
     244             : 
     245           3 :   if (NULL != pis->iph)
     246             :   {
     247           0 :     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
     248             :                 "PATCH /products/$ID operation did not complete\n");
     249           0 :     TALER_MERCHANT_product_patch_cancel (pis->iph);
     250             :   }
     251           3 :   json_decref (pis->description_i18n);
     252           3 :   GNUNET_free (pis->image);
     253           3 :   json_decref (pis->taxes);
     254           3 :   json_decref (pis->address);
     255           3 :   GNUNET_free (pis);
     256           3 : }
     257             : 
     258             : 
     259             : struct TALER_TESTING_Command
     260           3 : TALER_TESTING_cmd_merchant_patch_product (
     261             :   const char *label,
     262             :   const char *merchant_url,
     263             :   const char *product_id,
     264             :   const char *description,
     265             :   json_t *description_i18n,
     266             :   const char *unit,
     267             :   const char *price,
     268             :   const char *image,
     269             :   json_t *taxes,
     270             :   int64_t total_stock,
     271             :   uint64_t total_lost,
     272             :   json_t *address,
     273             :   struct GNUNET_TIME_Absolute next_restock,
     274             :   unsigned int http_status)
     275             : {
     276             :   struct PatchProductState *pis;
     277             : 
     278           3 :   GNUNET_assert ( (NULL == taxes) ||
     279             :                   json_is_array (taxes));
     280           3 :   pis = GNUNET_new (struct PatchProductState);
     281           3 :   pis->merchant_url = merchant_url;
     282           3 :   pis->product_id = product_id;
     283           3 :   pis->http_status = http_status;
     284           3 :   pis->description = description;
     285           3 :   pis->description_i18n = description_i18n; /* ownership taken */
     286           3 :   pis->unit = unit;
     287           3 :   GNUNET_assert (GNUNET_OK ==
     288             :                  TALER_string_to_amount (price,
     289             :                                          &pis->price));
     290           3 :   pis->image = GNUNET_strdup (image);
     291           3 :   pis->taxes = taxes; /* ownership taken */
     292           3 :   pis->total_stock = total_stock;
     293           3 :   pis->total_lost = total_lost;
     294           3 :   pis->address = address; /* ownership taken */
     295           3 :   pis->next_restock = next_restock;
     296             :   {
     297           3 :     struct TALER_TESTING_Command cmd = {
     298             :       .cls = pis,
     299             :       .label = label,
     300             :       .run = &patch_product_run,
     301             :       .cleanup = &patch_product_cleanup,
     302             :       .traits = &patch_product_traits
     303             :     };
     304             : 
     305           3 :     return cmd;
     306             :   }
     307             : }
     308             : 
     309             : 
     310             : /* end of testing_api_cmd_patch_product.c */

Generated by: LCOV version 1.14