LCOV - code coverage report
Current view: top level - testing - testing_api_cmd_post_products.c (source / functions) Hit Total Coverage
Test: GNU Taler merchant coverage report Lines: 64 78 82.1 %
Date: 2021-08-30 06:54:17 Functions: 6 6 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_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             :   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             :    * 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             :  * @param hr response being processed
     115             :  */
     116             : static void
     117           5 : post_products_cb (void *cls,
     118             :                   const struct TALER_MERCHANT_HttpResponse *hr)
     119             : {
     120           5 :   struct PostProductsState *pis = cls;
     121             : 
     122           5 :   pis->iph = NULL;
     123           5 :   if (pis->http_status != hr->http_status)
     124             :   {
     125           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     126             :                 "Unexpected response code %u (%d) to command %s\n",
     127             :                 hr->http_status,
     128             :                 (int) hr->ec,
     129             :                 TALER_TESTING_interpreter_get_current_label (pis->is));
     130           0 :     TALER_TESTING_interpreter_fail (pis->is);
     131           0 :     return;
     132             :   }
     133           5 :   switch (hr->http_status)
     134             :   {
     135           4 :   case MHD_HTTP_NO_CONTENT:
     136           4 :     break;
     137           0 :   case MHD_HTTP_UNAUTHORIZED:
     138           0 :     break;
     139           0 :   case MHD_HTTP_FORBIDDEN:
     140           0 :     break;
     141           0 :   case MHD_HTTP_NOT_FOUND:
     142           0 :     break;
     143           1 :   case MHD_HTTP_CONFLICT:
     144           1 :     break;
     145           0 :   default:
     146           0 :     GNUNET_break (0);
     147           0 :     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
     148             :                 "Unhandled HTTP status %u for POST /products.\n",
     149             :                 hr->http_status);
     150             :   }
     151           5 :   TALER_TESTING_interpreter_next (pis->is);
     152             : }
     153             : 
     154             : 
     155             : /**
     156             :  * Run the "POST /products" CMD.
     157             :  *
     158             :  *
     159             :  * @param cls closure.
     160             :  * @param cmd command being run now.
     161             :  * @param is interpreter state.
     162             :  */
     163             : static void
     164           5 : post_products_run (void *cls,
     165             :                    const struct TALER_TESTING_Command *cmd,
     166             :                    struct TALER_TESTING_Interpreter *is)
     167             : {
     168           5 :   struct PostProductsState *pis = cls;
     169             : 
     170           5 :   pis->is = is;
     171          10 :   pis->iph = TALER_MERCHANT_products_post (is->ctx,
     172             :                                            pis->merchant_url,
     173             :                                            pis->product_id,
     174             :                                            pis->description,
     175           5 :                                            pis->description_i18n,
     176             :                                            pis->unit,
     177           5 :                                            &pis->price,
     178           5 :                                            pis->image,
     179           5 :                                            pis->taxes,
     180             :                                            pis->total_stock,
     181           5 :                                            pis->address,
     182             :                                            pis->next_restock,
     183             :                                            &post_products_cb,
     184             :                                            pis);
     185           5 :   GNUNET_assert (NULL != pis->iph);
     186           5 : }
     187             : 
     188             : 
     189             : /**
     190             :  * Offers information from the POST /products CMD state to other
     191             :  * commands.
     192             :  *
     193             :  * @param cls closure
     194             :  * @param[out] ret result (could be anything)
     195             :  * @param trait name of the trait
     196             :  * @param index index number of the object to extract.
     197             :  * @return #GNUNET_OK on success
     198             :  */
     199             : static int
     200          29 : post_products_traits (void *cls,
     201             :                       const void **ret,
     202             :                       const char *trait,
     203             :                       unsigned int index)
     204             : {
     205          29 :   struct PostProductsState *pps = cls;
     206             :   struct TALER_TESTING_Trait traits[] = {
     207          29 :     TALER_TESTING_make_trait_string (0, pps->description),
     208          29 :     TALER_TESTING_make_trait_json (0, pps->description_i18n),
     209          29 :     TALER_TESTING_make_trait_string (1, pps->unit),
     210          29 :     TALER_TESTING_make_trait_amount_obj (0, &pps->price),
     211          29 :     TALER_TESTING_make_trait_string (3, pps->image),
     212          29 :     TALER_TESTING_make_trait_json (2, pps->taxes),
     213          29 :     TALER_TESTING_make_trait_int64 (0, &pps->total_stock),
     214          29 :     TALER_TESTING_make_trait_json (3, pps->address),
     215          29 :     TALER_TESTING_make_trait_absolute_time (0, &pps->next_restock),
     216          29 :     TALER_TESTING_make_trait_string (2, pps->product_id),
     217          29 :     TALER_TESTING_trait_end (),
     218             :   };
     219             : 
     220          29 :   return TALER_TESTING_get_trait (traits,
     221             :                                   ret,
     222             :                                   trait,
     223             :                                   index);
     224             : }
     225             : 
     226             : 
     227             : /**
     228             :  * Free the state of a "POST product" CMD, and possibly
     229             :  * cancel a pending operation thereof.
     230             :  *
     231             :  * @param cls closure.
     232             :  * @param cmd command being run.
     233             :  */
     234             : static void
     235           5 : post_products_cleanup (void *cls,
     236             :                        const struct TALER_TESTING_Command *cmd)
     237             : {
     238           5 :   struct PostProductsState *pis = cls;
     239             : 
     240           5 :   if (NULL != pis->iph)
     241             :   {
     242           0 :     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
     243             :                 "POST /products operation did not complete\n");
     244           0 :     TALER_MERCHANT_products_post_cancel (pis->iph);
     245             :   }
     246           5 :   json_decref (pis->description_i18n);
     247           5 :   GNUNET_free (pis->image);
     248           5 :   json_decref (pis->taxes);
     249           5 :   json_decref (pis->address);
     250           5 :   GNUNET_free (pis);
     251           5 : }
     252             : 
     253             : 
     254             : struct TALER_TESTING_Command
     255           5 : TALER_TESTING_cmd_merchant_post_products2 (
     256             :   const char *label,
     257             :   const char *merchant_url,
     258             :   const char *product_id,
     259             :   const char *description,
     260             :   json_t *description_i18n,
     261             :   const char *unit,
     262             :   const char *price,
     263             :   const char *image,
     264             :   json_t *taxes,
     265             :   int64_t total_stock,
     266             :   json_t *address,
     267             :   struct GNUNET_TIME_Absolute next_restock,
     268             :   unsigned int http_status)
     269             : {
     270             :   struct PostProductsState *pis;
     271             : 
     272           5 :   GNUNET_assert ((NULL == taxes) ||
     273             :                  json_is_array (taxes));
     274           5 :   GNUNET_assert ((NULL == description_i18n) ||
     275             :                  json_is_object (description_i18n));
     276           5 :   pis = GNUNET_new (struct PostProductsState);
     277           5 :   pis->merchant_url = merchant_url;
     278           5 :   pis->product_id = product_id;
     279           5 :   pis->http_status = http_status;
     280           5 :   pis->description = description;
     281           5 :   pis->description_i18n = description_i18n; /* ownership taken */
     282           5 :   pis->unit = unit;
     283           5 :   GNUNET_assert (GNUNET_OK ==
     284             :                  TALER_string_to_amount (price,
     285             :                                          &pis->price));
     286           5 :   pis->image = GNUNET_strdup (image);
     287           5 :   pis->taxes = taxes; /* ownership taken */
     288           5 :   pis->total_stock = total_stock;
     289           5 :   pis->address = address; /* ownership taken */
     290           5 :   pis->next_restock = next_restock;
     291             :   {
     292           5 :     struct TALER_TESTING_Command cmd = {
     293             :       .cls = pis,
     294             :       .label = label,
     295             :       .run = &post_products_run,
     296             :       .cleanup = &post_products_cleanup,
     297             :       .traits = &post_products_traits
     298             :     };
     299             : 
     300           5 :     return cmd;
     301             :   }
     302             : }
     303             : 
     304             : 
     305             : struct TALER_TESTING_Command
     306           5 : TALER_TESTING_cmd_merchant_post_products (const char *label,
     307             :                                           const char *merchant_url,
     308             :                                           const char *product_id,
     309             :                                           const char *description,
     310             :                                           const char *price,
     311             :                                           unsigned int http_status)
     312             : {
     313           5 :   return TALER_TESTING_cmd_merchant_post_products2 (
     314             :     label,
     315             :     merchant_url,
     316             :     product_id,
     317             :     description,
     318             :     json_pack ("{s:s}", "en", description),
     319             :     "test-unit",
     320             :     price,
     321             :     "",
     322             :     json_array (),
     323             :     4,
     324             :     json_pack ("{s:s}", "street", "my street"),
     325             :     GNUNET_TIME_UNIT_ZERO_ABS,
     326             :     http_status);
     327             : }
     328             : 
     329             : 
     330             : /* end of testing_api_cmd_post_products.c */

Generated by: LCOV version 1.14