LCOV - code coverage report
Current view: top level - testing - testing_api_cmd_post_orders_paid.c (source / functions) Hit Total Coverage
Test: GNU Taler merchant coverage report Lines: 0 55 0.0 %
Date: 2022-08-25 06:17:04 Functions: 0 4 0.0 %
Legend: Lines: hit not hit

          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 requet.
      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 hr the http response.
      75             :  */
      76             : static void
      77           0 : paid_cb (void *cls,
      78             :          const struct TALER_MERCHANT_HttpResponse *hr)
      79             : {
      80           0 :   struct PostOrdersPaidState *ops = cls;
      81             : 
      82           0 :   ops->oph = NULL;
      83           0 :   if (ops->http_status != hr->http_status)
      84             :   {
      85           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
      86             :                 "Unexpected response code %u (%d) to command %s\n",
      87             :                 hr->http_status,
      88             :                 (int) hr->ec,
      89             :                 TALER_TESTING_interpreter_get_current_label (ops->is));
      90           0 :     TALER_TESTING_FAIL (ops->is);
      91             :   }
      92           0 :   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
      93             :               "Successful order-paid (HTTP status: %u)\n",
      94             :               ops->http_status);
      95           0 :   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           0 : paid_run (void *cls,
     108             :           const struct TALER_TESTING_Command *cmd,
     109             :           struct TALER_TESTING_Interpreter *is)
     110             : {
     111           0 :   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           0 :   ops->is = is;
     120           0 :   pay_cmd = TALER_TESTING_interpreter_lookup_command (is,
     121             :                                                       ops->pay_reference);
     122           0 :   if (NULL == pay_cmd)
     123           0 :     TALER_TESTING_FAIL (is);
     124           0 :   if (GNUNET_OK !=
     125           0 :       TALER_TESTING_get_trait_merchant_sig (pay_cmd,
     126             :                                             &merchant_sig))
     127           0 :     TALER_TESTING_FAIL (is);
     128           0 :   if (GNUNET_OK !=
     129           0 :       TALER_TESTING_get_trait_proposal_reference (pay_cmd,
     130             :                                                   &proposal_reference))
     131           0 :     TALER_TESTING_FAIL (is);
     132           0 :   proposal_cmd = TALER_TESTING_interpreter_lookup_command (is,
     133             :                                                            *proposal_reference);
     134             : 
     135           0 :   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           0 :     if (GNUNET_OK !=
     144           0 :         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           0 :         GNUNET_JSON_spec_string ("order_id",
     152             :                                  &order_id),
     153           0 :         GNUNET_JSON_spec_end ()
     154             :       };
     155             : 
     156           0 :       if (GNUNET_OK !=
     157           0 :           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           0 :   if (GNUNET_OK !=
     178           0 :       TALER_TESTING_get_trait_h_contract_terms (proposal_cmd,
     179             :                                                 &h_contract_terms))
     180           0 :     TALER_TESTING_FAIL (is);
     181             : 
     182           0 :   ops->oph = TALER_MERCHANT_order_paid (is->ctx,
     183             :                                         ops->merchant_url,
     184             :                                         order_id,
     185             :                                         ops->session_id,
     186             :                                         h_contract_terms,
     187             :                                         merchant_sig,
     188             :                                         &paid_cb,
     189             :                                         ops);
     190           0 :   if (NULL == ops->oph)
     191           0 :     TALER_TESTING_FAIL (is);
     192             : }
     193             : 
     194             : 
     195             : /**
     196             :  * Free a "paid" CMD, and cancel it if need be.
     197             :  *
     198             :  * @param cls closure.
     199             :  * @param cmd command currently being freed.
     200             :  */
     201             : static void
     202           0 : paid_cleanup (void *cls,
     203             :               const struct TALER_TESTING_Command *cmd)
     204             : {
     205           0 :   struct PostOrdersPaidState *ops = cls;
     206             : 
     207           0 :   if (NULL != ops->oph)
     208             :   {
     209           0 :     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
     210             :                 "Command `%s' did not complete.\n",
     211             :                 TALER_TESTING_interpreter_get_current_label (
     212             :                   ops->is));
     213           0 :     TALER_MERCHANT_order_paid_cancel (ops->oph);
     214             :   }
     215           0 :   GNUNET_free (ops);
     216           0 : }
     217             : 
     218             : 
     219             : struct TALER_TESTING_Command
     220           0 : TALER_TESTING_cmd_merchant_post_orders_paid (const char *label,
     221             :                                              const char *merchant_url,
     222             :                                              const char *pay_reference,
     223             :                                              const char *session_id,
     224             :                                              unsigned int http_status)
     225             : {
     226             :   struct PostOrdersPaidState *ops;
     227             : 
     228           0 :   ops = GNUNET_new (struct PostOrdersPaidState);
     229           0 :   ops->http_status = http_status;
     230           0 :   ops->pay_reference = pay_reference;
     231           0 :   ops->merchant_url = merchant_url;
     232           0 :   ops->session_id = session_id;
     233             :   {
     234           0 :     struct TALER_TESTING_Command cmd = {
     235             :       .cls = ops,
     236             :       .label = label,
     237             :       .run = &paid_run,
     238             :       .cleanup = &paid_cleanup
     239             :     };
     240             : 
     241           0 :     return cmd;
     242             :   }
     243             : }

Generated by: LCOV version 1.14