LCOV - code coverage report
Current view: top level - testing - testing_api_cmd_get_product.c (source / functions) Hit Total Coverage
Test: GNU Taler coverage report Lines: 0 102 0.0 %
Date: 2020-08-15 06:12:35 Functions: 0 4 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_get_product.c
      21             :  * @brief command to test GET /product/$ID
      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 product" CMD.
      33             :  */
      34             : struct GetProductState
      35             : {
      36             : 
      37             :   /**
      38             :    * Handle for a "GET product" request.
      39             :    */
      40             :   struct TALER_MERCHANT_ProductGetHandle *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             :    * ID of the product to run GET for.
      54             :    */
      55             :   const char *product_id;
      56             : 
      57             :   /**
      58             :    * Reference for a POST or PATCH /products CMD (optional).
      59             :    */
      60             :   const char *product_reference;
      61             : 
      62             :   /**
      63             :    * Expected HTTP response code.
      64             :    */
      65             :   unsigned int http_status;
      66             : 
      67             : };
      68             : 
      69             : 
      70             : /**
      71             :  * Callback for a /get/product/$ID operation.
      72             :  *
      73             :  * @param cls closure for this function
      74             :  * @param hr HTTP response details
      75             :  * @param description description of the product
      76             :  * @param description_i18n Map from IETF BCP 47 language tags to localized descriptions
      77             :  * @param unit unit in which the product is measured (liters, kilograms, packages, etc.)
      78             :  * @param price the price for one @a unit of the product, zero is used to imply that
      79             :  *              this product is not sold separately or that the price is not fixed and
      80             :  *              must be supplied by the front-end.  If non-zero, price must include
      81             :  *              applicable taxes.
      82             :  * @param image base64-encoded product image
      83             :  * @param taxes list of taxes paid by the merchant
      84             :  * @param total_stock in @a units, -1 to indicate "infinite" (i.e. electronic books),
      85             :  *                does NOT indicate remaining stocks, to get remaining stocks,
      86             :  *                subtract @a total_sold and @a total_lost. Note that this still
      87             :  *                does not then say how many of the remaining inventory are locked.
      88             :  * @param total_sold in @a units, total number of @a unit of product sold
      89             :  * @param total_lost in @a units, total number of @a unit of product lost from inventory
      90             :  * @param location where the product is in stock
      91             :  * @param next_restock when the next restocking is expected to happen, 0 for unknown,
      92             :  *                     #GNUNET_TIME_UNIT_FOREVER_ABS for 'never'.
      93             :  */
      94             : static void
      95           0 : get_product_cb (void *cls,
      96             :                 const struct TALER_MERCHANT_HttpResponse *hr,
      97             :                 const char *description,
      98             :                 const json_t *description_i18n,
      99             :                 const char *unit,
     100             :                 const struct TALER_Amount *price,
     101             :                 const json_t *image,
     102             :                 const json_t *taxes,
     103             :                 int64_t total_stock,
     104             :                 uint64_t total_sold,
     105             :                 uint64_t total_lost,
     106             :                 const json_t *location,
     107             :                 struct GNUNET_TIME_Absolute next_restock)
     108             : {
     109           0 :   struct GetProductState *gis = cls;
     110             :   const struct TALER_TESTING_Command *product_cmd;
     111             : 
     112           0 :   product_cmd = TALER_TESTING_interpreter_lookup_command (
     113             :     gis->is,
     114             :     gis->product_reference);
     115             : 
     116           0 :   gis->igh = NULL;
     117           0 :   if (gis->http_status != hr->http_status)
     118             :   {
     119           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     120             :                 "Unexpected response code %u (%d) to command %s\n",
     121             :                 hr->http_status,
     122             :                 (int) hr->ec,
     123             :                 TALER_TESTING_interpreter_get_current_label (gis->is));
     124           0 :     TALER_TESTING_interpreter_fail (gis->is);
     125           0 :     return;
     126             :   }
     127           0 :   switch (hr->http_status)
     128             :   {
     129           0 :   case MHD_HTTP_OK:
     130             :     {
     131             :       const char *expected_description;
     132           0 :       if (GNUNET_OK !=
     133           0 :           TALER_TESTING_get_trait_string (product_cmd,
     134             :                                           0,
     135             :                                           &expected_description))
     136           0 :         TALER_TESTING_interpreter_fail (gis->is);
     137           0 :       if (0 != strcmp (description,
     138             :                        expected_description))
     139             :       {
     140           0 :         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     141             :                     "Product description does not match\n");
     142           0 :         TALER_TESTING_interpreter_fail (gis->is);
     143           0 :         return;
     144             :       }
     145             :     }
     146             :     {
     147             :       const json_t *expected_description_i18n;
     148           0 :       if (GNUNET_OK !=
     149           0 :           TALER_TESTING_get_trait_json (product_cmd,
     150             :                                         0,
     151             :                                         &expected_description_i18n))
     152           0 :         TALER_TESTING_interpreter_fail (gis->is);
     153           0 :       if (1 != json_equal (description_i18n,
     154             :                            expected_description_i18n))
     155             :       {
     156           0 :         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     157             :                     "Product description i18n does not match\n");
     158           0 :         TALER_TESTING_interpreter_fail (gis->is);
     159           0 :         return;
     160             :       }
     161             :     }
     162             :     {
     163             :       const struct TALER_Amount *expected_price;
     164           0 :       if (GNUNET_OK !=
     165           0 :           TALER_TESTING_get_trait_amount_obj (product_cmd,
     166             :                                               0,
     167             :                                               &expected_price))
     168           0 :         TALER_TESTING_interpreter_fail (gis->is);
     169           0 :       if ((GNUNET_OK != TALER_amount_cmp_currency (price,
     170           0 :                                                    expected_price)) ||
     171           0 :           (0 != TALER_amount_cmp (price,
     172             :                                   expected_price)))
     173             :       {
     174           0 :         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     175             :                     "Product price does not match\n");
     176           0 :         TALER_TESTING_interpreter_fail (gis->is);
     177           0 :         return;
     178             :       }
     179             :     }
     180             :     {
     181             :       const json_t *expected_image;
     182           0 :       if (GNUNET_OK !=
     183           0 :           TALER_TESTING_get_trait_json (product_cmd,
     184             :                                         1,
     185             :                                         &expected_image))
     186           0 :         TALER_TESTING_interpreter_fail (gis->is);
     187           0 :       if (1 != json_equal (image,
     188             :                            expected_image))
     189             :       {
     190           0 :         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     191             :                     "Product image does not match\n");
     192           0 :         TALER_TESTING_interpreter_fail (gis->is);
     193           0 :         return;
     194             :       }
     195             :     }
     196             :     {
     197             :       const json_t *expected_taxes;
     198           0 :       if (GNUNET_OK !=
     199           0 :           TALER_TESTING_get_trait_json (product_cmd,
     200             :                                         2,
     201             :                                         &expected_taxes))
     202           0 :         TALER_TESTING_interpreter_fail (gis->is);
     203           0 :       if (1 != json_equal (taxes,
     204             :                            expected_taxes))
     205             :       {
     206           0 :         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     207             :                     "Product taxes do not match\n");
     208           0 :         TALER_TESTING_interpreter_fail (gis->is);
     209           0 :         return;
     210             :       }
     211             :     }
     212             :     {
     213             :       const char *expected_unit;
     214           0 :       if (GNUNET_OK !=
     215           0 :           TALER_TESTING_get_trait_string (product_cmd,
     216             :                                           1,
     217             :                                           &expected_unit))
     218           0 :         TALER_TESTING_interpreter_fail (gis->is);
     219           0 :       if (0 != strcmp (unit,
     220             :                        expected_unit))
     221             :       {
     222           0 :         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     223             :                     "Product unit does not match\n");
     224           0 :         TALER_TESTING_interpreter_fail (gis->is);
     225           0 :         return;
     226             :       }
     227             :     }
     228             :     {
     229             :       const json_t *expected_location;
     230           0 :       if (GNUNET_OK !=
     231           0 :           TALER_TESTING_get_trait_json (product_cmd,
     232             :                                         3,
     233             :                                         &expected_location))
     234           0 :         TALER_TESTING_interpreter_fail (gis->is);
     235           0 :       if (1 != json_equal (location,
     236             :                            expected_location))
     237             :       {
     238           0 :         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     239             :                     "Product location does not match\n");
     240           0 :         TALER_TESTING_interpreter_fail (gis->is);
     241           0 :         return;
     242             :       }
     243             :     }
     244             :     {
     245             :       const int64_t *expected_total_stock;
     246           0 :       if (GNUNET_OK !=
     247           0 :           TALER_TESTING_get_trait_int64 (product_cmd,
     248             :                                          0,
     249             :                                          &expected_total_stock))
     250           0 :         TALER_TESTING_interpreter_fail (gis->is);
     251           0 :       if (total_stock != *expected_total_stock)
     252             :       {
     253           0 :         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     254             :                     "Product total stock does not match\n");
     255           0 :         TALER_TESTING_interpreter_fail (gis->is);
     256           0 :         return;
     257             :       }
     258             :     }
     259             :     {
     260             :       const struct GNUNET_TIME_Absolute *expected_next_restock;
     261             :       struct GNUNET_TIME_Absolute expected_next_restock_round;
     262           0 :       if (GNUNET_OK !=
     263           0 :           TALER_TESTING_get_trait_absolute_time (product_cmd,
     264             :                                                  0,
     265             :                                                  &expected_next_restock))
     266           0 :         TALER_TESTING_interpreter_fail (gis->is);
     267           0 :       expected_next_restock_round = *expected_next_restock;
     268           0 :       GNUNET_TIME_round_abs (&expected_next_restock_round);
     269           0 :       if (next_restock.abs_value_us != expected_next_restock_round.abs_value_us)
     270             :       {
     271           0 :         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     272             :                     "Product next restock does not match\n");
     273           0 :         TALER_TESTING_interpreter_fail (gis->is);
     274           0 :         return;
     275             :       }
     276             :     }
     277           0 :     break;
     278           0 :   default:
     279           0 :     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
     280             :                 "Unhandled HTTP status.\n");
     281             :   }
     282           0 :   TALER_TESTING_interpreter_next (gis->is);
     283             : }
     284             : 
     285             : 
     286             : /**
     287             :  * Run the "GET product" CMD.
     288             :  *
     289             :  *
     290             :  * @param cls closure.
     291             :  * @param cmd command being run now.
     292             :  * @param is interpreter state.
     293             :  */
     294             : static void
     295           0 : get_product_run (void *cls,
     296             :                  const struct TALER_TESTING_Command *cmd,
     297             :                  struct TALER_TESTING_Interpreter *is)
     298             : {
     299           0 :   struct GetProductState *gis = cls;
     300             : 
     301           0 :   gis->is = is;
     302           0 :   gis->igh = TALER_MERCHANT_product_get (is->ctx,
     303             :                                          gis->merchant_url,
     304             :                                          gis->product_id,
     305             :                                          &get_product_cb,
     306             :                                          gis);
     307           0 :   GNUNET_assert (NULL != gis->igh);
     308           0 : }
     309             : 
     310             : 
     311             : /**
     312             :  * Free the state of a "GET product" CMD, and possibly
     313             :  * cancel a pending operation thereof.
     314             :  *
     315             :  * @param cls closure.
     316             :  * @param cmd command being run.
     317             :  */
     318             : static void
     319           0 : get_product_cleanup (void *cls,
     320             :                      const struct TALER_TESTING_Command *cmd)
     321             : {
     322           0 :   struct GetProductState *gis = cls;
     323             : 
     324           0 :   if (NULL != gis->igh)
     325             :   {
     326           0 :     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
     327             :                 "GET /products/$ID operation did not complete\n");
     328           0 :     TALER_MERCHANT_product_get_cancel (gis->igh);
     329             :   }
     330           0 :   GNUNET_free (gis);
     331           0 : }
     332             : 
     333             : 
     334             : /**
     335             :  * Define a "GET product" CMD.
     336             :  *
     337             :  * @param label command label.
     338             :  * @param merchant_url base URL of the merchant serving the
     339             :  *        GET /products/$ID request.
     340             :  * @param product_id the ID of the product to query
     341             :  * @param http_status expected HTTP response code.
     342             :  * @param product_reference reference to a "POST /products" or "PATCH /products/$ID" CMD
     343             :  *        that will provide what we expect the backend to return to us
     344             :  * @return the command.
     345             :  */
     346             : struct TALER_TESTING_Command
     347           0 : TALER_TESTING_cmd_merchant_get_product (const char *label,
     348             :                                         const char *merchant_url,
     349             :                                         const char *product_id,
     350             :                                         unsigned int http_status,
     351             :                                         const char *product_reference)
     352             : {
     353             :   struct GetProductState *gis;
     354             : 
     355           0 :   gis = GNUNET_new (struct GetProductState);
     356           0 :   gis->merchant_url = merchant_url;
     357           0 :   gis->product_id = product_id;
     358           0 :   gis->http_status = http_status;
     359           0 :   gis->product_reference = product_reference;
     360             :   {
     361           0 :     struct TALER_TESTING_Command cmd = {
     362             :       .cls = gis,
     363             :       .label = label,
     364             :       .run = &get_product_run,
     365             :       .cleanup = &get_product_cleanup
     366             :     };
     367             : 
     368           0 :     return cmd;
     369             :   }
     370             : }
     371             : 
     372             : 
     373             : /* end of testing_api_cmd_get_product.c */

Generated by: LCOV version 1.14