merchant

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

testing_api_cmd_get_product_image.c (5606B)


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