merchant

Merchant backend to process payments, run by merchants
Log | Files | Refs | Submodules | README | LICENSE

testing_api_cmd_get_product_image.c (5901B)


      1 /*
      2   This file is part of TALER
      3   Copyright (C) 2025 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 src/testing/testing_api_cmd_get_product_image.c
     21  * @brief command to test GET /products/$HASH/image
     22  * @author Bohdan Potuzhnyi
     23  */
     24 #include "platform.h"
     25 struct GetProductImageState;
     26 #define TALER_MERCHANT_GET_PRODUCTS_IMAGE_RESULT_CLOSURE struct GetProductImageState
     27 #include <taler/taler_exchange_service.h>
     28 #include <taler/taler_testing_lib.h>
     29 #include "taler/taler_merchant_service.h"
     30 #include "taler/taler_merchant_testing_lib.h"
     31 #include <taler/merchant/get-products-IMAGE_HASH-image.h>
     32 
     33 
     34 /**
     35  * State of a "GET product image" CMD.
     36  */
     37 struct GetProductImageState
     38 {
     39   /**
     40    * Handle for a "GET product image" request.
     41    */
     42   struct TALER_MERCHANT_GetProductsImageHandle *pigh;
     43 
     44   /**
     45    * The interpreter state.
     46    */
     47   struct TALER_TESTING_Interpreter *is;
     48 
     49   /**
     50    * Base URL of the merchant serving the request.
     51    */
     52   const char *merchant_url;
     53 
     54   /**
     55    * Reference for a POST or PATCH /products CMD (optional).
     56    */
     57   const char *product_reference;
     58 
     59   /**
     60    * Expected HTTP response code.
     61    */
     62   unsigned int http_status;
     63 
     64   /**
     65    * Expected image as a data URL.
     66    */
     67   char *expected_image;
     68 
     69   /**
     70    * Hash over the expected image, lowercase hex encoding.
     71    */
     72   const char *image_hash;
     73 };
     74 
     75 
     76 /**
     77  * Callback for a /products/$HASH/image operation.
     78  *
     79  * @param cls closure for this function
     80  * @param pir response details
     81  */
     82 static void
     83 get_product_image_cb (struct GetProductImageState *gis,
     84                       const struct TALER_MERCHANT_GetProductsImageResponse *pir)
     85 {
     86 
     87   gis->pigh = NULL;
     88   if (gis->http_status != pir->hr.http_status)
     89   {
     90     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     91                 "Unexpected response code %u (%d) to command %s\n",
     92                 pir->hr.http_status,
     93                 (int) pir->hr.ec,
     94                 TALER_TESTING_interpreter_get_current_label (gis->is));
     95     TALER_TESTING_interpreter_fail (gis->is);
     96     return;
     97   }
     98   switch (pir->hr.http_status)
     99   {
    100   case MHD_HTTP_OK:
    101     if (NULL != gis->expected_image)
    102     {
    103       if (0 != strcmp (pir->details.ok.image,
    104                        gis->expected_image))
    105       {
    106         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    107                     "Product image did not match expected value\n");
    108         TALER_TESTING_interpreter_fail (gis->is);
    109         return;
    110       }
    111     }
    112     break;
    113   default:
    114     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
    115                 "Unhandled HTTP status.\n");
    116   }
    117   TALER_TESTING_interpreter_next (gis->is);
    118 }
    119 
    120 
    121 /**
    122  * Run the "GET product image" CMD.
    123  *
    124  * @param cls closure.
    125  * @param cmd command being run now.
    126  * @param is interpreter state.
    127  */
    128 static void
    129 get_product_image_run (void *cls,
    130                        const struct TALER_TESTING_Command *cmd,
    131                        struct TALER_TESTING_Interpreter *is)
    132 {
    133   struct GetProductImageState *gis = cls;
    134   const struct TALER_TESTING_Command *product_cmd;
    135 
    136   gis->is = is;
    137   gis->expected_image = NULL;
    138   if (NULL != gis->product_reference)
    139   {
    140     const char *product_image;
    141 
    142     product_cmd = TALER_TESTING_interpreter_lookup_command (
    143       is,
    144       gis->product_reference);
    145     if (NULL == product_cmd)
    146     {
    147       GNUNET_break (0);
    148       TALER_TESTING_interpreter_fail (is);
    149       return;
    150     }
    151     if (GNUNET_OK !=
    152         TALER_TESTING_get_trait_product_image (product_cmd,
    153                                                &product_image))
    154     {
    155       TALER_TESTING_interpreter_fail (is);
    156       return;
    157     }
    158     gis->expected_image = GNUNET_strdup (product_image);
    159   }
    160   gis->pigh
    161     = TALER_MERCHANT_get_products_image_create (
    162         TALER_TESTING_interpreter_get_context (is),
    163         gis->merchant_url,
    164         gis->image_hash);
    165   {
    166     enum TALER_ErrorCode ec;
    167 
    168     ec = TALER_MERCHANT_get_products_image_start (
    169       gis->pigh,
    170       &get_product_image_cb,
    171       gis);
    172     GNUNET_assert (TALER_EC_NONE == ec);
    173   }
    174 }
    175 
    176 
    177 /**
    178  * Free the state of a "GET product image" CMD, and possibly
    179  * cancel a pending operation thereof.
    180  *
    181  * @param cls closure.
    182  * @param cmd command being run.
    183  */
    184 static void
    185 get_product_image_cleanup (void *cls,
    186                            const struct TALER_TESTING_Command *cmd)
    187 {
    188   struct GetProductImageState *gis = cls;
    189 
    190   if (NULL != gis->pigh)
    191   {
    192     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
    193                 "GET /products/$HASH/image operation did not complete\n");
    194     TALER_MERCHANT_get_products_image_cancel (gis->pigh);
    195   }
    196   GNUNET_free (gis->expected_image);
    197   GNUNET_free (gis);
    198 }
    199 
    200 
    201 struct TALER_TESTING_Command
    202 TALER_TESTING_cmd_get_product_image (const char *label,
    203                                      const char *merchant_url,
    204                                      const char *product_reference,
    205                                      const char *image_hash,
    206                                      unsigned int http_status)
    207 {
    208   struct GetProductImageState *gis;
    209 
    210   gis = GNUNET_new (struct GetProductImageState);
    211   gis->merchant_url = merchant_url;
    212   gis->product_reference = product_reference;
    213   gis->http_status = http_status;
    214   gis->image_hash = image_hash;
    215   {
    216     struct TALER_TESTING_Command cmd = {
    217       .cls = gis,
    218       .label = label,
    219       .run = &get_product_image_run,
    220       .cleanup = &get_product_image_cleanup
    221     };
    222 
    223     return cmd;
    224   }
    225 }