merchant

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

testing_api_cmd_post_donau_instances.c (5607B)


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