merchant

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

testing_api_cmd_post_donau_instances.c (5647B)


      1 /*
      2   This file is part of TALER
      3   Copyright (C) 2020-2024 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,
     11   but 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_donau_instances.c
     21  * @brief command to test POST /donau request
     22  * @author Bohdan Potuzhnyi
     23  * @author Vlada Svirsh
     24  */
     25 
     26 #include "taler/platform.h"
     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/taler_merchant_donau.h"
     32 #include <donau/donau_testing_lib.h>
     33 #include <taler/taler-merchant/post-private-donau.h>
     34 
     35 
     36 /**
     37  * State of a "POST /donau" CMD.
     38  */
     39 struct PostDonauState
     40 {
     41   /**
     42    * Handle for a "POST donau" request.
     43    */
     44   struct TALER_MERCHANT_PostPrivateDonauHandle *dph;
     45 
     46   /**
     47    * The interpreter state.
     48    */
     49   struct TALER_TESTING_Interpreter *is;
     50 
     51   /**
     52    * Base URL of the merchant serving the request.
     53    */
     54   const char *merchant_url;
     55 
     56   /**
     57    * Charity details.
     58    */
     59   struct TALER_MERCHANT_Charity charity;
     60 
     61   /**
     62    * Merchant reference to fetch public key.
     63    */
     64   const char *merchant_reference;
     65 
     66   /**
     67    * Expected HTTP response code.
     68    */
     69   unsigned int http_status;
     70 };
     71 
     72 /**
     73  * Callback for a POST /donau operation.
     74  *
     75  * @param cls closure for this function
     76  * @param hr response being processed
     77  */
     78 static void
     79 post_donau_cb (void *cls,
     80                const struct TALER_MERCHANT_PostPrivateDonauResponse *pdr)
     81 {
     82   struct PostDonauState *pds = cls;
     83 
     84   pds->dph = NULL;
     85   if (pds->http_status != pdr->hr.http_status)
     86   {
     87     TALER_TESTING_unexpected_status_with_body (
     88       pds->is,
     89       pdr->hr.http_status,
     90       pds->http_status,
     91       pdr->hr.reply);
     92     TALER_TESTING_interpreter_fail (pds->is);
     93     return;
     94   }
     95 
     96   switch (pdr->hr.http_status)
     97   {
     98   case MHD_HTTP_NO_CONTENT:
     99     break;
    100   case MHD_HTTP_BAD_REQUEST:
    101     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
    102                 "POST /donau returned BAD REQUEST: %s\n",
    103                 json_dumps (pdr->hr.reply, JSON_INDENT (2)));
    104     break;
    105   case MHD_HTTP_UNAUTHORIZED:
    106     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
    107                 "POST /donau returned UNAUTHORIZED\n");
    108     break;
    109   case MHD_HTTP_NOT_FOUND:
    110     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
    111                 "POST /donau returned NOT FOUND\n");
    112     break;
    113   default:
    114     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
    115                 "Unhandled HTTP status %u for POST /donau\n",
    116                 pdr->hr.http_status);
    117   }
    118   TALER_TESTING_interpreter_next (pds->is);
    119 }
    120 
    121 
    122 /**
    123  * Run the "POST /donau" CMD.
    124  *
    125  * @param cls closure.
    126  * @param cmd command being run now.
    127  * @param is interpreter state.
    128  */
    129 static void
    130 post_donau_run (void *cls,
    131                 const struct TALER_TESTING_Command *cmd,
    132                 struct TALER_TESTING_Interpreter *is)
    133 {
    134   struct PostDonauState *pds = cls;
    135 
    136   pds->is = is;
    137   pds->charity.charity_url = TALER_TESTING_get_donau_url (is);
    138   pds->dph = TALER_MERCHANT_post_private_donau_create (
    139     TALER_TESTING_interpreter_get_context (is),
    140     pds->merchant_url,
    141     &pds->charity);
    142   {
    143     enum TALER_ErrorCode ec;
    144 
    145     ec = TALER_MERCHANT_post_private_donau_start (pds->dph,
    146                                                   &post_donau_cb,
    147                                                   pds);
    148     GNUNET_assert (TALER_EC_NONE == ec);
    149   }
    150 }
    151 
    152 
    153 /**
    154  * Free the state of a "POST /donau" CMD, and possibly
    155  * cancel a pending operation thereof.
    156  *
    157  * @param cls closure.
    158  * @param cmd command being run.
    159  */
    160 static void
    161 post_donau_cleanup (void *cls,
    162                     const struct TALER_TESTING_Command *cmd)
    163 {
    164   struct PostDonauState *pds = cls;
    165 
    166   if (NULL != pds->dph)
    167   {
    168     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
    169                 "POST /donau operation did not complete\n");
    170     TALER_MERCHANT_post_private_donau_cancel (pds->dph);
    171   }
    172   GNUNET_free (pds);
    173 }
    174 
    175 
    176 /**
    177  * Create a new testing command for POST /donau.
    178  */
    179 struct TALER_TESTING_Command
    180 TALER_TESTING_cmd_merchant_post_donau_instance (
    181   const char *label,
    182   const char *url,
    183   const char *merchant_reference,
    184   unsigned int
    185   expected_http_status,
    186   ...)
    187 {
    188   struct PostDonauState *pds = GNUNET_new (struct PostDonauState);
    189   struct TALER_Amount max_amount;
    190   struct TALER_Amount date_amount;
    191 
    192   const char*mamount = "EUR:100";
    193   const char*damount = "EUR:20";
    194 
    195   GNUNET_assert (GNUNET_OK ==
    196                  TALER_string_to_amount (mamount,
    197                                          &max_amount));
    198 
    199   GNUNET_assert (GNUNET_OK ==
    200                  TALER_string_to_amount (damount,
    201                                          &date_amount));
    202 
    203   {
    204     struct TALER_MERCHANT_Charity charity = {
    205       .charity_url = "http://replaced.in.post_donau_run/",
    206       .charity_id = 1,
    207     };
    208 
    209     pds->merchant_reference = merchant_reference;
    210     pds->merchant_url = url;
    211     pds->charity = charity;
    212     pds->http_status = expected_http_status;
    213 
    214     {
    215       struct TALER_TESTING_Command cmd = {
    216         .cls = pds,
    217         .label = label,
    218         .run = &post_donau_run,
    219         .cleanup = &post_donau_cleanup
    220       };
    221 
    222       return cmd;
    223     }
    224 
    225   }
    226 }