LCOV - code coverage report
Current view: top level - testing - testing_api_cmd_post_orders_paid.c (source / functions) Hit Total Coverage
Test: GNU Taler coverage report Lines: 0 55 0.0 %
Date: 2020-08-15 06:12:35 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 lib/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 GNUNET_HashCode *h_contract_terms;
     117             :   struct TALER_MerchantSignatureP *merchant_sig;
     118             : 
     119           0 :   ops->is = is;
     120             : 
     121           0 :   pay_cmd = TALER_TESTING_interpreter_lookup_command (is,
     122             :                                                       ops->pay_reference);
     123           0 :   if (NULL == pay_cmd)
     124           0 :     TALER_TESTING_FAIL (is);
     125           0 :   if (GNUNET_OK !=
     126           0 :       TALER_TESTING_get_trait_merchant_sig (pay_cmd,
     127             :                                             0,
     128             :                                             &merchant_sig))
     129           0 :     TALER_TESTING_FAIL (is);
     130           0 :   if (GNUNET_OK !=
     131           0 :       TALER_TESTING_get_trait_proposal_reference (pay_cmd,
     132             :                                                   0,
     133             :                                                   &proposal_reference))
     134           0 :     TALER_TESTING_FAIL (is);
     135           0 :   proposal_cmd = TALER_TESTING_interpreter_lookup_command (is,
     136             :                                                            proposal_reference);
     137             : 
     138           0 :   if (NULL == proposal_cmd)
     139           0 :     TALER_TESTING_FAIL (is);
     140             : 
     141             :   {
     142             :     const json_t *contract_terms;
     143             :     const char *error_name;
     144             :     unsigned int error_line;
     145             : 
     146           0 :     if (GNUNET_OK !=
     147           0 :         TALER_TESTING_get_trait_contract_terms (proposal_cmd,
     148             :                                                 0,
     149             :                                                 &contract_terms))
     150           0 :       TALER_TESTING_FAIL (is);
     151             :     {
     152             :       /* Get information that needs to be put verbatim in the
     153             :        * deposit permission */
     154             :       struct GNUNET_JSON_Specification spec[] = {
     155           0 :         GNUNET_JSON_spec_string ("order_id",
     156             :                                  &order_id),
     157           0 :         GNUNET_JSON_spec_end ()
     158             :       };
     159             : 
     160           0 :       if (GNUNET_OK !=
     161           0 :           GNUNET_JSON_parse (contract_terms,
     162             :                              spec,
     163             :                              &error_name,
     164             :                              &error_line))
     165             :       {
     166             :         char *js;
     167             : 
     168           0 :         js = json_dumps (contract_terms,
     169             :                          JSON_INDENT (1));
     170           0 :         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     171             :                     "Parser failed on %s:%u for input `%s'\n",
     172             :                     error_name,
     173             :                     error_line,
     174             :                     js);
     175           0 :         free (js);
     176           0 :         TALER_TESTING_FAIL (is);
     177             :       }
     178             :     }
     179             :   }
     180             : 
     181           0 :   if (GNUNET_OK !=
     182           0 :       TALER_TESTING_get_trait_h_contract_terms (proposal_cmd,
     183             :                                                 0,
     184             :                                                 &h_contract_terms))
     185           0 :     TALER_TESTING_FAIL (is);
     186             : 
     187           0 :   ops->oph = TALER_MERCHANT_order_paid (is->ctx,
     188             :                                         ops->merchant_url,
     189             :                                         order_id,
     190             :                                         ops->session_id,
     191             :                                         h_contract_terms,
     192             :                                         merchant_sig,
     193             :                                         &paid_cb,
     194             :                                         ops);
     195           0 :   if (NULL == ops->oph)
     196           0 :     TALER_TESTING_FAIL (is);
     197             : }
     198             : 
     199             : 
     200             : /**
     201             :  * Free a "paid" CMD, and cancel it if need be.
     202             :  *
     203             :  * @param cls closure.
     204             :  * @param cmd command currently being freed.
     205             :  */
     206             : static void
     207           0 : paid_cleanup (void *cls,
     208             :               const struct TALER_TESTING_Command *cmd)
     209             : {
     210           0 :   struct PostOrdersPaidState *ops = cls;
     211             : 
     212           0 :   if (NULL != ops->oph)
     213             :   {
     214           0 :     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
     215             :                 "Command `%s' did not complete.\n",
     216             :                 TALER_TESTING_interpreter_get_current_label (
     217             :                   ops->is));
     218           0 :     TALER_MERCHANT_order_paid_cancel (ops->oph);
     219             :   }
     220           0 :   GNUNET_free (ops);
     221           0 : }
     222             : 
     223             : 
     224             : /**
     225             :  * Make an "order paid" test command.
     226             :  *
     227             :  * @param label command label
     228             :  * @param merchant_url merchant base URL
     229             :  * @param pay_reference reference to the payment to verify
     230             :  * @param session_id the session to use for the verification.
     231             :  * @param http_status expected HTTP response code
     232             :  * @return the command
     233             :  */
     234             : struct TALER_TESTING_Command
     235           0 : TALER_TESTING_cmd_merchant_post_orders_paid (const char *label,
     236             :                                              const char *merchant_url,
     237             :                                              const char *pay_reference,
     238             :                                              const char *session_id,
     239             :                                              unsigned int http_status)
     240             : {
     241             :   struct PostOrdersPaidState *ops;
     242             : 
     243           0 :   ops = GNUNET_new (struct PostOrdersPaidState);
     244           0 :   ops->http_status = http_status;
     245           0 :   ops->pay_reference = pay_reference;
     246           0 :   ops->merchant_url = merchant_url;
     247           0 :   ops->session_id = session_id;
     248             :   {
     249           0 :     struct TALER_TESTING_Command cmd = {
     250             :       .cls = ops,
     251             :       .label = label,
     252             :       .run = &paid_run,
     253             :       .cleanup = &paid_cleanup
     254             :     };
     255             : 
     256           0 :     return cmd;
     257             :   }
     258             : }

Generated by: LCOV version 1.14