LCOV - code coverage report
Current view: top level - testing - testing_api_cmd_collect_order.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 77.8 % 45 35
Test Date: 2026-09-11 17:45:24 Functions: 100.0 % 4 4

            Line data    Source code
       1              : /*
       2              :   This file is part of TALER
       3              :   Copyright (C) 2026 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 src/testing/testing_api_cmd_collect_order.c
      21              :  * @brief command to test POST /private/orders/$ORDER_ID/collect
      22              :  * @author Bohdan Potuzhnyi
      23              :  * @author Volodymyr Potuzhnyi
      24              :  */
      25              : #include "platform.h"
      26              : struct CollectOrderState;
      27              : #define TALER_MERCHANT_POST_PRIVATE_ORDERS_COLLECT_RESULT_CLOSURE \
      28              :   struct CollectOrderState
      29              : #include <taler/taler_exchange_service.h>
      30              : #include <taler/taler_testing_lib.h>
      31              : #include "taler/taler_merchant_service.h"
      32              : #include "taler/taler_merchant_testing_lib.h"
      33              : #include <taler/merchant/post-private-orders-ORDER_ID-collect.h>
      34              : 
      35              : 
      36              : /**
      37              :  * State of a "POST /private/orders/$ORDER_ID/collect" CMD.
      38              :  */
      39              : struct CollectOrderState
      40              : {
      41              : 
      42              :   /**
      43              :    * Handle for a "collect order" request.
      44              :    */
      45              :   struct TALER_MERCHANT_PostPrivateOrdersCollectHandle *och;
      46              : 
      47              :   /**
      48              :    * The interpreter state.
      49              :    */
      50              :   struct TALER_TESTING_Interpreter *is;
      51              : 
      52              :   /**
      53              :    * Base URL of the merchant serving the request.
      54              :    */
      55              :   const char *merchant_url;
      56              : 
      57              :   /**
      58              :    * ID of the order to collect.
      59              :    */
      60              :   const char *order_id;
      61              : 
      62              :   /**
      63              :    * Session ID to store with the payment, NULL for none.
      64              :    */
      65              :   const char *session_id;
      66              : 
      67              :   /**
      68              :    * Choice to complete, negative for v0 contracts.
      69              :    */
      70              :   int16_t choice_index;
      71              : 
      72              :   /**
      73              :    * Expected HTTP response code.
      74              :    */
      75              :   unsigned int http_status;
      76              : 
      77              : };
      78              : 
      79              : 
      80              : /**
      81              :  * Callback for a POST /private/orders/$ID/collect operation.
      82              :  *
      83              :  * @param cos closure for this function
      84              :  * @param cr response being processed
      85              :  */
      86              : static void
      87           20 : collect_order_cb (
      88              :   struct CollectOrderState *cos,
      89              :   const struct TALER_MERCHANT_PostPrivateOrdersCollectResponse *cr)
      90              : {
      91           20 :   cos->och = NULL;
      92           20 :   if (cos->http_status != cr->hr.http_status)
      93              :   {
      94            0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
      95              :                 "Unexpected response code %u (%d) to command %s\n",
      96              :                 cr->hr.http_status,
      97              :                 (int) cr->hr.ec,
      98              :                 TALER_TESTING_interpreter_get_current_label (cos->is));
      99            0 :     TALER_TESTING_interpreter_fail (cos->is);
     100            0 :     return;
     101              :   }
     102           20 :   switch (cr->hr.http_status)
     103              :   {
     104            6 :   case MHD_HTTP_OK:
     105            6 :     break;
     106            6 :   case MHD_HTTP_BAD_REQUEST:
     107            6 :     break;
     108            0 :   case MHD_HTTP_UNAUTHORIZED:
     109            0 :     break;
     110            2 :   case MHD_HTTP_NOT_FOUND:
     111            2 :     break;
     112            6 :   case MHD_HTTP_CONFLICT:
     113            6 :     break;
     114            0 :   default:
     115            0 :     GNUNET_break (0);
     116            0 :     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
     117              :                 "Unhandled HTTP status %u for collect order.\n",
     118              :                 cr->hr.http_status);
     119              :   }
     120           20 :   TALER_TESTING_interpreter_next (cos->is);
     121              : }
     122              : 
     123              : 
     124              : /**
     125              :  * Run the "collect order" CMD.
     126              :  *
     127              :  * @param cls closure.
     128              :  * @param cmd command being run now.
     129              :  * @param is interpreter state.
     130              :  */
     131              : static void
     132           20 : collect_order_run (void *cls,
     133              :                    const struct TALER_TESTING_Command *cmd,
     134              :                    struct TALER_TESTING_Interpreter *is)
     135              : {
     136           20 :   struct CollectOrderState *cos = cls;
     137              : 
     138           20 :   cos->is = is;
     139           20 :   cos->och = TALER_MERCHANT_post_private_orders_collect_create (
     140              :     TALER_TESTING_interpreter_get_context (is),
     141              :     cos->merchant_url,
     142              :     cos->order_id,
     143              :     cos->session_id,
     144           20 :     cos->choice_index);
     145              :   {
     146              :     enum TALER_ErrorCode ec;
     147              : 
     148           20 :     ec = TALER_MERCHANT_post_private_orders_collect_start (cos->och,
     149              :                                                            &collect_order_cb,
     150              :                                                            cos);
     151           20 :     GNUNET_assert (TALER_EC_NONE == ec);
     152              :   }
     153           20 : }
     154              : 
     155              : 
     156              : /**
     157              :  * Free the state of a "collect order" CMD, and possibly
     158              :  * cancel a pending operation thereof.
     159              :  *
     160              :  * @param cls closure.
     161              :  * @param cmd command being run.
     162              :  */
     163              : static void
     164           20 : collect_order_cleanup (void *cls,
     165              :                        const struct TALER_TESTING_Command *cmd)
     166              : {
     167           20 :   struct CollectOrderState *cos = cls;
     168              : 
     169           20 :   if (NULL != cos->och)
     170              :   {
     171            0 :     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
     172              :                 "POST /private/orders/$ORDER_ID/collect operation did not complete\n");
     173            0 :     TALER_MERCHANT_post_private_orders_collect_cancel (cos->och);
     174              :   }
     175           20 :   GNUNET_free (cos);
     176           20 : }
     177              : 
     178              : 
     179              : struct TALER_TESTING_Command
     180           20 : TALER_TESTING_cmd_merchant_collect_order (const char *label,
     181              :                                           const char *merchant_url,
     182              :                                           const char *order_id,
     183              :                                           const char *session_id,
     184              :                                           int16_t choice_index,
     185              :                                           unsigned int http_status)
     186              : {
     187              :   struct CollectOrderState *cos;
     188              : 
     189           20 :   cos = GNUNET_new (struct CollectOrderState);
     190           20 :   cos->merchant_url = merchant_url;
     191           20 :   cos->order_id = order_id;
     192           20 :   cos->session_id = session_id;
     193           20 :   cos->choice_index = choice_index;
     194           20 :   cos->http_status = http_status;
     195              :   {
     196           20 :     struct TALER_TESTING_Command cmd = {
     197              :       .cls = cos,
     198              :       .label = label,
     199              :       .run = &collect_order_run,
     200              :       .cleanup = &collect_order_cleanup
     201              :     };
     202              : 
     203           20 :     return cmd;
     204              :   }
     205              : }
        

Generated by: LCOV version 2.0-1