LCOV - code coverage report
Current view: top level - testing - testing_api_cmd_post_products.c (source / functions) Hit Total Coverage
Test: GNU Taler coverage report Lines: 0 70 0.0 %
Date: 2020-08-15 06:12:35 Functions: 0 6 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_post_products.c
      21             :  * @brief command to test POST /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 "POST /products" CMD.
      33             :  */
      34             : struct PostProductsState
      35             : {
      36             : 
      37             :   /**
      38             :    * Handle for a "GET product" request.
      39             :    */
      40             :   struct TALER_MERCHANT_ProductsPostHandle *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 POST 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             :    * where the product is in stock
      94             :    */
      95             :   json_t *address;
      96             : 
      97             :   /**
      98             :    * when the next restocking is expected to happen, 0 for unknown,
      99             :    */
     100             :   struct GNUNET_TIME_Absolute next_restock;
     101             : 
     102             :   /**
     103             :    * Expected HTTP response code.
     104             :    */
     105             :   unsigned int http_status;
     106             : 
     107             : };
     108             : 
     109             : 
     110             : /**
     111             :  * Callback for a POST /products operation.
     112             :  *
     113             :  * @param cls closure for this function
     114             :  */
     115             : static void
     116           0 : post_products_cb (void *cls,
     117             :                   const struct TALER_MERCHANT_HttpResponse *hr)
     118             : {
     119           0 :   struct PostProductsState *pis = cls;
     120             : 
     121           0 :   pis->iph = NULL;
     122           0 :   if (pis->http_status != hr->http_status)
     123             :   {
     124           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     125             :                 "Unexpected response code %u (%d) to command %s\n",
     126             :                 hr->http_status,
     127             :                 (int) hr->ec,
     128             :                 TALER_TESTING_interpreter_get_current_label (pis->is));
     129           0 :     TALER_TESTING_interpreter_fail (pis->is);
     130           0 :     return;
     131             :   }
     132           0 :   switch (hr->http_status)
     133             :   {
     134           0 :   case MHD_HTTP_NO_CONTENT:
     135           0 :     break;
     136           0 :   case MHD_HTTP_CONFLICT:
     137           0 :     break;
     138             :   // FIXME: add other legitimate states here...
     139           0 :   default:
     140           0 :     GNUNET_break (0);
     141           0 :     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
     142             :                 "Unhandled HTTP status.\n");
     143             :   }
     144           0 :   TALER_TESTING_interpreter_next (pis->is);
     145             : }
     146             : 
     147             : 
     148             : /**
     149             :  * Run the "POST /products" CMD.
     150             :  *
     151             :  *
     152             :  * @param cls closure.
     153             :  * @param cmd command being run now.
     154             :  * @param is interpreter state.
     155             :  */
     156             : static void
     157           0 : post_products_run (void *cls,
     158             :                    const struct TALER_TESTING_Command *cmd,
     159             :                    struct TALER_TESTING_Interpreter *is)
     160             : {
     161           0 :   struct PostProductsState *pis = cls;
     162             : 
     163           0 :   pis->is = is;
     164           0 :   pis->iph = TALER_MERCHANT_products_post (is->ctx,
     165             :                                            pis->merchant_url,
     166             :                                            pis->product_id,
     167             :                                            pis->description,
     168           0 :                                            pis->description_i18n,
     169             :                                            pis->unit,
     170           0 :                                            &pis->price,
     171           0 :                                            pis->image,
     172           0 :                                            pis->taxes,
     173             :                                            pis->total_stock,
     174           0 :                                            pis->address,
     175             :                                            pis->next_restock,
     176             :                                            &post_products_cb,
     177             :                                            pis);
     178           0 :   GNUNET_assert (NULL != pis->iph);
     179           0 : }
     180             : 
     181             : 
     182             : /**
     183             :  * Offers information from the POST /products CMD state to other
     184             :  * commands.
     185             :  *
     186             :  * @param cls closure
     187             :  * @param ret[out] result (could be anything)
     188             :  * @param trait name of the trait
     189             :  * @param index index number of the object to extract.
     190             :  * @return #GNUNET_OK on success
     191             :  */
     192             : static int
     193           0 : post_products_traits (void *cls,
     194             :                       const void **ret,
     195             :                       const char *trait,
     196             :                       unsigned int index)
     197             : {
     198           0 :   struct PostProductsState *pps = cls;
     199             :   struct TALER_TESTING_Trait traits[] = {
     200           0 :     TALER_TESTING_make_trait_string (0, pps->description),
     201           0 :     TALER_TESTING_make_trait_json (0, pps->description_i18n),
     202           0 :     TALER_TESTING_make_trait_string (1, pps->unit),
     203           0 :     TALER_TESTING_make_trait_amount_obj (0, &pps->price),
     204           0 :     TALER_TESTING_make_trait_json (1, pps->image),
     205           0 :     TALER_TESTING_make_trait_json (2, pps->taxes),
     206           0 :     TALER_TESTING_make_trait_int64 (0, &pps->total_stock),
     207           0 :     TALER_TESTING_make_trait_json (3, pps->address),
     208           0 :     TALER_TESTING_make_trait_absolute_time (0, &pps->next_restock),
     209           0 :     TALER_TESTING_make_trait_string (2, pps->product_id),
     210           0 :     TALER_TESTING_trait_end (),
     211             :   };
     212             : 
     213           0 :   return TALER_TESTING_get_trait (traits,
     214             :                                   ret,
     215             :                                   trait,
     216             :                                   index);
     217             : }
     218             : 
     219             : 
     220             : /**
     221             :  * Free the state of a "POST product" CMD, and possibly
     222             :  * cancel a pending operation thereof.
     223             :  *
     224             :  * @param cls closure.
     225             :  * @param cmd command being run.
     226             :  */
     227             : static void
     228           0 : post_products_cleanup (void *cls,
     229             :                        const struct TALER_TESTING_Command *cmd)
     230             : {
     231           0 :   struct PostProductsState *pis = cls;
     232             : 
     233           0 :   if (NULL != pis->iph)
     234             :   {
     235           0 :     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
     236             :                 "POST /products operation did not complete\n");
     237           0 :     TALER_MERCHANT_products_post_cancel (pis->iph);
     238             :   }
     239           0 :   json_decref (pis->description_i18n);
     240           0 :   json_decref (pis->image);
     241           0 :   json_decref (pis->taxes);
     242           0 :   json_decref (pis->address);
     243           0 :   GNUNET_free (pis);
     244           0 : }
     245             : 
     246             : 
     247             : /**
     248             :  * Define a "POST /products" CMD.
     249             :  *
     250             :  * @param label command label.
     251             :  * @param merchant_url base URL of the merchant serving the
     252             :  *        POST /products request.
     253             :  * @param product_id the ID of the product to query
     254             :  * @param description description of the product
     255             :  * @param description_i18n Map from IETF BCP 47 language tags to localized descriptions
     256             :  * @param unit unit in which the product is measured (liters, kilograms, packages, etc.)
     257             :  * @param price the price for one @a unit of the product, zero is used to imply that
     258             :  *              this product is not sold separately or that the price is not fixed and
     259             :  *              must be supplied by the front-end.  If non-zero, price must include
     260             :  *              applicable taxes.
     261             :  * @param image base64-encoded product image
     262             :  * @param taxes list of taxes paid by the merchant
     263             :  * @param total_stock in @a units, -1 to indicate "infinite" (i.e. electronic books)
     264             :  * @param address where the product is in stock
     265             :  * @param next_restock when the next restocking is expected to happen, 0 for unknown,
     266             :  *                     #GNUNET_TIME_UNIT_FOREVER_ABS for 'never'.
     267             :  * @param http_status expected HTTP response code.
     268             :  * @return the command.
     269             :  */
     270             : struct TALER_TESTING_Command
     271           0 : TALER_TESTING_cmd_merchant_post_products2 (
     272             :   const char *label,
     273             :   const char *merchant_url,
     274             :   const char *product_id,
     275             :   const char *description,
     276             :   json_t *description_i18n,
     277             :   const char *unit,
     278             :   const char *price,
     279             :   json_t *image,
     280             :   json_t *taxes,
     281             :   int64_t total_stock,
     282             :   json_t *address,
     283             :   struct GNUNET_TIME_Absolute next_restock,
     284             :   unsigned int http_status)
     285             : {
     286             :   struct PostProductsState *pis;
     287             : 
     288           0 :   pis = GNUNET_new (struct PostProductsState);
     289           0 :   pis->merchant_url = merchant_url;
     290           0 :   pis->product_id = product_id;
     291           0 :   pis->http_status = http_status;
     292           0 :   pis->description = description;
     293           0 :   pis->description_i18n = description_i18n; /* ownership taken */
     294           0 :   pis->unit = unit;
     295           0 :   GNUNET_assert (GNUNET_OK ==
     296             :                  TALER_string_to_amount (price,
     297             :                                          &pis->price));
     298           0 :   pis->image = image; /* ownership taken */
     299           0 :   pis->taxes = taxes; /* ownership taken */
     300           0 :   pis->total_stock = total_stock;
     301           0 :   pis->address = address; /* ownership taken */
     302           0 :   pis->next_restock = next_restock;
     303             :   {
     304           0 :     struct TALER_TESTING_Command cmd = {
     305             :       .cls = pis,
     306             :       .label = label,
     307             :       .run = &post_products_run,
     308             :       .cleanup = &post_products_cleanup,
     309             :       .traits = &post_products_traits
     310             :     };
     311             : 
     312           0 :     return cmd;
     313             :   }
     314             : }
     315             : 
     316             : 
     317             : /**
     318             :  * Define a "POST /products" CMD, simple version
     319             :  *
     320             :  * @param label command label.
     321             :  * @param merchant_url base URL of the merchant serving the
     322             :  *        POST /products request.
     323             :  * @param product_id the ID of the product to create
     324             :  * @param description name of the product
     325             :  * @param price price of the product
     326             :  * @param http_status expected HTTP response code.
     327             :  * @return the command.
     328             :  */
     329             : struct TALER_TESTING_Command
     330           0 : TALER_TESTING_cmd_merchant_post_products (const char *label,
     331             :                                           const char *merchant_url,
     332             :                                           const char *product_id,
     333             :                                           const char *description,
     334             :                                           const char *price,
     335             :                                           unsigned int http_status)
     336             : {
     337           0 :   return TALER_TESTING_cmd_merchant_post_products2 (
     338             :     label,
     339             :     merchant_url,
     340             :     product_id,
     341             :     description,
     342             :     json_pack ("{s:s}", "en", description),
     343             :     "test-unit",
     344             :     price,
     345             :     json_object (),
     346             :     json_object (),
     347             :     4,
     348             :     json_pack ("{s:s}", "street", "my street"),
     349             :     GNUNET_TIME_UNIT_ZERO_ABS,
     350             :     http_status);
     351             : }
     352             : 
     353             : 
     354             : /* end of testing_api_cmd_post_products.c */

Generated by: LCOV version 1.14