LCOV - code coverage report
Current view: top level - testing - testing_api_cmd_patch_product.c (source / functions) Hit Total Coverage
Test: GNU Taler coverage report Lines: 0 67 0.0 %
Date: 2020-08-15 06:12:35 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) 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 lib/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             :   json_t *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             :  */
     120             : static void
     121           0 : patch_product_cb (void *cls,
     122             :                   const struct TALER_MERCHANT_HttpResponse *hr)
     123             : {
     124           0 :   struct PatchProductState *pis = cls;
     125             : 
     126           0 :   pis->iph = NULL;
     127           0 :   if (pis->http_status != hr->http_status)
     128             :   {
     129           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     130             :                 "Unexpected response code %u (%d) to command %s\n",
     131             :                 hr->http_status,
     132             :                 (int) hr->ec,
     133             :                 TALER_TESTING_interpreter_get_current_label (pis->is));
     134           0 :     TALER_TESTING_interpreter_fail (pis->is);
     135           0 :     return;
     136             :   }
     137           0 :   switch (hr->http_status)
     138             :   {
     139           0 :   case MHD_HTTP_OK:
     140           0 :     break;
     141             :   // FIXME: add other legitimate states here...
     142           0 :   default:
     143           0 :     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
     144             :                 "Unhandled HTTP status.\n");
     145             :   }
     146           0 :   TALER_TESTING_interpreter_next (pis->is);
     147             : }
     148             : 
     149             : 
     150             : /**
     151             :  * Run the "PATCH /products/$ID" CMD.
     152             :  *
     153             :  *
     154             :  * @param cls closure.
     155             :  * @param cmd command being run now.
     156             :  * @param is interpreter state.
     157             :  */
     158             : static void
     159           0 : patch_product_run (void *cls,
     160             :                    const struct TALER_TESTING_Command *cmd,
     161             :                    struct TALER_TESTING_Interpreter *is)
     162             : {
     163           0 :   struct PatchProductState *pis = cls;
     164             : 
     165           0 :   pis->is = is;
     166           0 :   pis->iph = TALER_MERCHANT_product_patch (is->ctx,
     167             :                                            pis->merchant_url,
     168             :                                            pis->product_id,
     169             :                                            pis->description,
     170           0 :                                            pis->description_i18n,
     171             :                                            pis->unit,
     172           0 :                                            &pis->price,
     173           0 :                                            pis->image,
     174           0 :                                            pis->taxes,
     175             :                                            pis->total_stock,
     176           0 :                                            pis->total_lost,
     177           0 :                                            pis->address,
     178             :                                            pis->next_restock,
     179             :                                            &patch_product_cb,
     180             :                                            pis);
     181           0 :   GNUNET_assert (NULL != pis->iph);
     182           0 : }
     183             : 
     184             : 
     185             : /**
     186             :  * Offers information from the PATCH /products CMD state to other
     187             :  * commands.
     188             :  *
     189             :  * @param cls closure
     190             :  * @param ret[out] result (could be anything)
     191             :  * @param trait name of the trait
     192             :  * @param index index number of the object to extract.
     193             :  * @return #GNUNET_OK on success
     194             :  */
     195             : static int
     196           0 : patch_product_traits (void *cls,
     197             :                       const void **ret,
     198             :                       const char *trait,
     199             :                       unsigned int index)
     200             : {
     201           0 :   struct PatchProductState *pps = cls;
     202             :   struct TALER_TESTING_Trait traits[] = {
     203           0 :     TALER_TESTING_make_trait_string (0, pps->description),
     204           0 :     TALER_TESTING_make_trait_json (0, pps->description_i18n),
     205           0 :     TALER_TESTING_make_trait_string (1, pps->unit),
     206           0 :     TALER_TESTING_make_trait_amount_obj (0, &pps->price),
     207           0 :     TALER_TESTING_make_trait_json (1, pps->image),
     208           0 :     TALER_TESTING_make_trait_json (2, pps->taxes),
     209           0 :     TALER_TESTING_make_trait_int64 (0, &pps->total_stock),
     210           0 :     TALER_TESTING_make_trait_json (3, pps->address),
     211           0 :     TALER_TESTING_make_trait_absolute_time (0, &pps->next_restock),
     212           0 :     TALER_TESTING_make_trait_string (2, pps->product_id),
     213           0 :     TALER_TESTING_trait_end (),
     214             :   };
     215             : 
     216           0 :   return TALER_TESTING_get_trait (traits,
     217             :                                   ret,
     218             :                                   trait,
     219             :                                   index);
     220             : }
     221             : 
     222             : 
     223             : /**
     224             :  * Free the state of a "GET product" CMD, and possibly
     225             :  * cancel a pending operation thereof.
     226             :  *
     227             :  * @param cls closure.
     228             :  * @param cmd command being run.
     229             :  */
     230             : static void
     231           0 : patch_product_cleanup (void *cls,
     232             :                        const struct TALER_TESTING_Command *cmd)
     233             : {
     234           0 :   struct PatchProductState *pis = cls;
     235             : 
     236           0 :   if (NULL != pis->iph)
     237             :   {
     238           0 :     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
     239             :                 "PATCH /products/$ID operation did not complete\n");
     240           0 :     TALER_MERCHANT_product_patch_cancel (pis->iph);
     241             :   }
     242           0 :   json_decref (pis->description_i18n);
     243           0 :   json_decref (pis->image);
     244           0 :   json_decref (pis->taxes);
     245           0 :   json_decref (pis->address);
     246           0 :   GNUNET_free (pis);
     247           0 : }
     248             : 
     249             : 
     250             : /**
     251             :  * Define a "PATCH /products/$ID" CMD.
     252             :  *
     253             :  * @param label command label.
     254             :  * @param merchant_url base URL of the merchant serving the
     255             :  *        PATCH /product request.
     256             :  * @param product_id the ID of the product to query
     257             :  * @param description description of the product
     258             :  * @param description_i18n Map from IETF BCP 47 language tags to localized descriptions
     259             :  * @param unit unit in which the product is measured (liters, kilograms, packages, etc.)
     260             :  * @param price the price for one @a unit of the product, zero is used to imply that
     261             :  *              this product is not sold separately or that the price is not fixed and
     262             :  *              must be supplied by the front-end.  If non-zero, price must include
     263             :  *              applicable taxes.
     264             :  * @param image base64-encoded product image
     265             :  * @param taxes list of taxes paid by the merchant
     266             :  * @param total_stock in @a units, -1 to indicate "infinite" (i.e. electronic books)
     267             :  * @param total_lost in @a units, must be larger than previous values, and may
     268             :  *               not exceed total_stock minus total_sold; if it does, the transaction
     269             :  *               will fail with a #MHD_HTTP_CONFLICT HTTP status code
     270             :  * @param address where the product is in stock
     271             :  * @param next_restock when the next restocking is expected to happen, 0 for unknown,
     272             :  *                     #GNUNET_TIME_UNIT_FOREVER_ABS for 'never'.
     273             :  * @param http_status expected HTTP response code.
     274             :  * @return the command.
     275             :  */
     276             : struct TALER_TESTING_Command
     277           0 : TALER_TESTING_cmd_merchant_patch_product (
     278             :   const char *label,
     279             :   const char *merchant_url,
     280             :   const char *product_id,
     281             :   const char *description,
     282             :   json_t *description_i18n,
     283             :   const char *unit,
     284             :   const char *price,
     285             :   json_t *image,
     286             :   json_t *taxes,
     287             :   int64_t total_stock,
     288             :   uint64_t total_lost,
     289             :   json_t *address,
     290             :   struct GNUNET_TIME_Absolute next_restock,
     291             :   unsigned int http_status)
     292             : {
     293             :   struct PatchProductState *pis;
     294             : 
     295           0 :   pis = GNUNET_new (struct PatchProductState);
     296           0 :   pis->merchant_url = merchant_url;
     297           0 :   pis->product_id = product_id;
     298           0 :   pis->http_status = http_status;
     299           0 :   pis->description = description;
     300           0 :   pis->description_i18n = description_i18n; /* ownership taken */
     301           0 :   pis->unit = unit;
     302           0 :   GNUNET_assert (GNUNET_OK ==
     303             :                  TALER_string_to_amount (price,
     304             :                                          &pis->price));
     305           0 :   pis->image = image; /* ownership taken */
     306           0 :   pis->taxes = taxes; /* ownership taken */
     307           0 :   pis->total_stock = total_stock;
     308           0 :   pis->total_lost = total_lost;
     309           0 :   pis->address = address; /* ownership taken */
     310           0 :   pis->next_restock = next_restock; {
     311           0 :     struct TALER_TESTING_Command cmd = {
     312             :       .cls = pis,
     313             :       .label = label,
     314             :       .run = &patch_product_run,
     315             :       .cleanup = &patch_product_cleanup,
     316             :       .traits = &patch_product_traits
     317             :     };
     318             : 
     319           0 :     return cmd;
     320             :   }
     321             : }
     322             : 
     323             : 
     324             : /* end of testing_api_cmd_patch_product.c */

Generated by: LCOV version 1.14