LCOV - code coverage report
Current view: top level - testing - testing_api_cmd_post_orders_paid.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 72.7 % 55 40
Test Date: 2025-10-21 13:10:50 Functions: 100.0 % 4 4

            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 testing_api_cmd_post_orders_paid.c
      21              :  * @brief command to test POST /orders/$ID/paid.
      22              :  * @author Jonathan Buchanan
      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 "POST /orders/$ID/paid" CMD.
      33              :  */
      34              : struct PostOrdersPaidState
      35              : {
      36              : 
      37              :   /**
      38              :    * Handle for a "POST /paid" request.
      39              :    */
      40              :   struct TALER_MERCHANT_OrderPaidHandle *oph;
      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              :    * Reference to the "pay" command to verify.
      54              :    */
      55              :   const char *pay_reference;
      56              : 
      57              :   /**
      58              :    * The session to use for the request.
      59              :    */
      60              :   const char *session_id;
      61              : 
      62              :   /**
      63              :    * Expected HTTP response code.
      64              :    */
      65              :   unsigned int http_status;
      66              : 
      67              : };
      68              : 
      69              : 
      70              : /**
      71              :  * Response from the merchant after POST /paid.
      72              :  *
      73              :  * @param cls pointer to `struct PostOrdersPaidState`.
      74              :  * @param opr the response.
      75              :  */
      76              : static void
      77            4 : paid_cb (void *cls,
      78              :          const struct TALER_MERCHANT_OrderPaidResponse *opr)
      79              : {
      80            4 :   struct PostOrdersPaidState *ops = cls;
      81              : 
      82            4 :   ops->oph = NULL;
      83            4 :   if (ops->http_status != opr->hr.http_status)
      84              :   {
      85            0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
      86              :                 "Unexpected response code %u (%d) to command %s\n",
      87              :                 opr->hr.http_status,
      88              :                 (int) opr->hr.ec,
      89              :                 TALER_TESTING_interpreter_get_current_label (ops->is));
      90            0 :     TALER_TESTING_FAIL (ops->is);
      91              :   }
      92            4 :   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
      93              :               "Successful order-paid (HTTP status: %u)\n",
      94              :               ops->http_status);
      95            4 :   TALER_TESTING_interpreter_next (ops->is);
      96              : }
      97              : 
      98              : 
      99              : /**
     100              :  * Run a "paid" CMD.
     101              :  *
     102              :  * @param cls closure
     103              :  * @param cmd command being run.
     104              :  * @param is interpreter state
     105              :  */
     106              : static void
     107            4 : paid_run (void *cls,
     108              :           const struct TALER_TESTING_Command *cmd,
     109              :           struct TALER_TESTING_Interpreter *is)
     110              : {
     111            4 :   struct PostOrdersPaidState *ops = cls;
     112              :   const struct TALER_TESTING_Command *pay_cmd;
     113              :   const char *proposal_reference;
     114              :   const struct TALER_TESTING_Command *proposal_cmd;
     115              :   const char *order_id;
     116              :   const struct TALER_PrivateContractHashP *h_contract_terms;
     117              :   const struct TALER_MerchantSignatureP *merchant_sig;
     118              : 
     119            4 :   ops->is = is;
     120            4 :   pay_cmd = TALER_TESTING_interpreter_lookup_command (is,
     121              :                                                       ops->pay_reference);
     122            4 :   if (NULL == pay_cmd)
     123            0 :     TALER_TESTING_FAIL (is);
     124            4 :   if (GNUNET_OK !=
     125            4 :       TALER_TESTING_get_trait_merchant_sig (pay_cmd,
     126              :                                             &merchant_sig))
     127            0 :     TALER_TESTING_FAIL (is);
     128            4 :   if (GNUNET_OK !=
     129            4 :       TALER_TESTING_get_trait_proposal_reference (pay_cmd,
     130              :                                                   &proposal_reference))
     131            0 :     TALER_TESTING_FAIL (is);
     132            4 :   proposal_cmd = TALER_TESTING_interpreter_lookup_command (is,
     133              :                                                            proposal_reference);
     134              : 
     135            4 :   if (NULL == proposal_cmd)
     136            0 :     TALER_TESTING_FAIL (is);
     137              : 
     138              :   {
     139              :     const json_t *contract_terms;
     140              :     const char *error_name;
     141              :     unsigned int error_line;
     142              : 
     143            4 :     if (GNUNET_OK !=
     144            4 :         TALER_TESTING_get_trait_contract_terms (proposal_cmd,
     145              :                                                 &contract_terms))
     146            0 :       TALER_TESTING_FAIL (is);
     147              :     {
     148              :       /* Get information that needs to be put verbatim in the
     149              :        * deposit permission */
     150              :       struct GNUNET_JSON_Specification spec[] = {
     151            4 :         GNUNET_JSON_spec_string ("order_id",
     152              :                                  &order_id),
     153            4 :         GNUNET_JSON_spec_end ()
     154              :       };
     155              : 
     156            4 :       if (GNUNET_OK !=
     157            4 :           GNUNET_JSON_parse (contract_terms,
     158              :                              spec,
     159              :                              &error_name,
     160              :                              &error_line))
     161              :       {
     162              :         char *js;
     163              : 
     164            0 :         js = json_dumps (contract_terms,
     165              :                          JSON_INDENT (1));
     166            0 :         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     167              :                     "Parser failed on %s:%u for input `%s'\n",
     168              :                     error_name,
     169              :                     error_line,
     170              :                     js);
     171            0 :         free (js);
     172            0 :         TALER_TESTING_FAIL (is);
     173              :       }
     174              :     }
     175              :   }
     176              : 
     177            4 :   if (GNUNET_OK !=
     178            4 :       TALER_TESTING_get_trait_h_contract_terms (proposal_cmd,
     179              :                                                 &h_contract_terms))
     180            0 :     TALER_TESTING_FAIL (is);
     181              : 
     182            4 :   ops->oph = TALER_MERCHANT_order_paid (TALER_TESTING_interpreter_get_context (
     183              :                                           is),
     184              :                                         ops->merchant_url,
     185              :                                         order_id,
     186              :                                         ops->session_id,
     187              :                                         h_contract_terms,
     188              :                                         NULL,
     189              :                                         merchant_sig,
     190              :                                         &paid_cb,
     191              :                                         ops);
     192            4 :   if (NULL == ops->oph)
     193            0 :     TALER_TESTING_FAIL (is);
     194              : }
     195              : 
     196              : 
     197              : /**
     198              :  * Free a "paid" CMD, and cancel it if need be.
     199              :  *
     200              :  * @param cls closure.
     201              :  * @param cmd command currently being freed.
     202              :  */
     203              : static void
     204            4 : paid_cleanup (void *cls,
     205              :               const struct TALER_TESTING_Command *cmd)
     206              : {
     207            4 :   struct PostOrdersPaidState *ops = cls;
     208              : 
     209            4 :   if (NULL != ops->oph)
     210              :   {
     211            0 :     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
     212              :                 "Command `%s' did not complete.\n",
     213              :                 TALER_TESTING_interpreter_get_current_label (
     214              :                   ops->is));
     215            0 :     TALER_MERCHANT_order_paid_cancel (ops->oph);
     216              :   }
     217            4 :   GNUNET_free (ops);
     218            4 : }
     219              : 
     220              : 
     221              : struct TALER_TESTING_Command
     222            4 : TALER_TESTING_cmd_merchant_post_orders_paid (const char *label,
     223              :                                              const char *merchant_url,
     224              :                                              const char *pay_reference,
     225              :                                              const char *session_id,
     226              :                                              unsigned int http_status)
     227              : {
     228              :   struct PostOrdersPaidState *ops;
     229              : 
     230            4 :   ops = GNUNET_new (struct PostOrdersPaidState);
     231            4 :   ops->http_status = http_status;
     232            4 :   ops->pay_reference = pay_reference;
     233            4 :   ops->merchant_url = merchant_url;
     234            4 :   ops->session_id = session_id;
     235              :   {
     236            4 :     struct TALER_TESTING_Command cmd = {
     237              :       .cls = ops,
     238              :       .label = label,
     239              :       .run = &paid_run,
     240              :       .cleanup = &paid_cleanup
     241              :     };
     242              : 
     243            4 :     return cmd;
     244              :   }
     245              : }
        

Generated by: LCOV version 2.0-1