LCOV - code coverage report
Current view: top level - testing - testing_api_cmd_delete_reserve.c (source / functions) Hit Total Coverage
Test: GNU Taler merchant coverage report Lines: 40 50 80.0 %
Date: 2021-08-30 06:54:17 Functions: 5 5 100.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_delete_reserve.c
      21             :  * @brief command to test DELETE /reserves/$RESERVE_PUB
      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 "DELETE /reserves/$RESERVE_PUB" CMD.
      33             :  */
      34             : struct DeleteReserveState
      35             : {
      36             : 
      37             :   /**
      38             :    * Handle for a "DELETE reserve" request.
      39             :    */
      40             :   struct TALER_MERCHANT_ReserveDeleteHandle *rdh;
      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 a command that provides a reserve.
      54             :    */
      55             :   const char *reserve_reference;
      56             : 
      57             :   /**
      58             :    * Expected HTTP response code.
      59             :    */
      60             :   unsigned int http_status;
      61             : 
      62             :   /**
      63             :    * Use purge, not delete.
      64             :    */
      65             :   bool purge;
      66             : 
      67             : };
      68             : 
      69             : 
      70             : /**
      71             :  * Callback for a DELETE /reserves/$RESERVE_PUB operation.
      72             :  *
      73             :  * @param cls closure for this function
      74             :  * @param hr response being processed
      75             :  */
      76             : static void
      77           3 : delete_reserve_cb (void *cls,
      78             :                    const struct TALER_MERCHANT_HttpResponse *hr)
      79             : {
      80           3 :   struct DeleteReserveState *drs = cls;
      81             : 
      82           3 :   drs->rdh = NULL;
      83           3 :   if (drs->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 (drs->is));
      90           0 :     TALER_TESTING_interpreter_fail (drs->is);
      91           0 :     return;
      92             :   }
      93           3 :   switch (hr->http_status)
      94             :   {
      95           2 :   case MHD_HTTP_NO_CONTENT:
      96           2 :     break;
      97           1 :   case MHD_HTTP_NOT_FOUND:
      98           1 :     break;
      99           0 :   case MHD_HTTP_CONFLICT:
     100           0 :     break;
     101           0 :   default:
     102           0 :     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
     103             :                 "Unhandled HTTP status %u for DELETE reserve.\n",
     104             :                 hr->http_status);
     105             :   }
     106           3 :   TALER_TESTING_interpreter_next (drs->is);
     107             : }
     108             : 
     109             : 
     110             : /**
     111             :  * Run the "DELETE reserve" CMD.
     112             :  *
     113             :  * @param cls closure.
     114             :  * @param cmd command being run now.
     115             :  * @param is interpreter state.
     116             :  */
     117             : static void
     118           3 : delete_reserve_run (void *cls,
     119             :                     const struct TALER_TESTING_Command *cmd,
     120             :                     struct TALER_TESTING_Interpreter *is)
     121             : {
     122           3 :   struct DeleteReserveState *drs = cls;
     123             :   const struct TALER_TESTING_Command *reserve_cmd;
     124             :   const struct TALER_ReservePublicKeyP *reserve_pub;
     125             : 
     126           3 :   reserve_cmd = TALER_TESTING_interpreter_lookup_command (
     127             :     is,
     128             :     drs->reserve_reference);
     129           3 :   if (GNUNET_OK !=
     130           3 :       TALER_TESTING_get_trait_reserve_pub (reserve_cmd,
     131             :                                            0,
     132             :                                            &reserve_pub))
     133           0 :     TALER_TESTING_FAIL (is);
     134             : 
     135           3 :   drs->is = is;
     136           3 :   if (drs->purge)
     137           2 :     drs->rdh = TALER_MERCHANT_reserve_purge (is->ctx,
     138             :                                              drs->merchant_url,
     139             :                                              reserve_pub,
     140             :                                              &delete_reserve_cb,
     141             :                                              drs);
     142             :   else
     143           1 :     drs->rdh = TALER_MERCHANT_reserve_delete (is->ctx,
     144             :                                               drs->merchant_url,
     145             :                                               reserve_pub,
     146             :                                               &delete_reserve_cb,
     147             :                                               drs);
     148             : 
     149           3 :   GNUNET_assert (NULL != drs->rdh);
     150             : }
     151             : 
     152             : 
     153             : /**
     154             :  * Free the state of a "DELETE reserve" CMD, and possibly
     155             :  * cancel a pending operation thereof.
     156             :  *
     157             :  * @param cls closure.
     158             :  * @param cmd command being run.
     159             :  */
     160             : static void
     161           3 : delete_reserve_cleanup (void *cls,
     162             :                         const struct TALER_TESTING_Command *cmd)
     163             : {
     164           3 :   struct DeleteReserveState *drs = cls;
     165             : 
     166           3 :   if (NULL != drs->rdh)
     167             :   {
     168           0 :     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
     169             :                 "DELETE /reserves/$RESERVE_PUB operation did not complete\n");
     170           0 :     TALER_MERCHANT_reserve_delete_cancel (drs->rdh);
     171             :   }
     172           3 :   GNUNET_free (drs);
     173           3 : }
     174             : 
     175             : 
     176             : struct TALER_TESTING_Command
     177           1 : TALER_TESTING_cmd_merchant_delete_reserve (const char *label,
     178             :                                            const char *merchant_url,
     179             :                                            const char *reserve_reference,
     180             :                                            unsigned int http_status)
     181             : {
     182             :   struct DeleteReserveState *drs;
     183             : 
     184           1 :   drs = GNUNET_new (struct DeleteReserveState);
     185           1 :   drs->merchant_url = merchant_url;
     186           1 :   drs->reserve_reference = reserve_reference;
     187           1 :   drs->http_status = http_status;
     188             :   {
     189           1 :     struct TALER_TESTING_Command cmd = {
     190             :       .cls = drs,
     191             :       .label = label,
     192             :       .run = &delete_reserve_run,
     193             :       .cleanup = &delete_reserve_cleanup
     194             :     };
     195             : 
     196           1 :     return cmd;
     197             :   }
     198             : }
     199             : 
     200             : 
     201             : struct TALER_TESTING_Command
     202           2 : TALER_TESTING_cmd_merchant_purge_reserve (const char *label,
     203             :                                           const char *merchant_url,
     204             :                                           const char *reserve_reference,
     205             :                                           unsigned int http_status)
     206             : {
     207             :   struct DeleteReserveState *drs;
     208             : 
     209           2 :   drs = GNUNET_new (struct DeleteReserveState);
     210           2 :   drs->merchant_url = merchant_url;
     211           2 :   drs->reserve_reference = reserve_reference;
     212           2 :   drs->http_status = http_status;
     213           2 :   drs->purge = true;
     214             :   {
     215           2 :     struct TALER_TESTING_Command cmd = {
     216             :       .cls = drs,
     217             :       .label = label,
     218             :       .run = &delete_reserve_run,
     219             :       .cleanup = &delete_reserve_cleanup
     220             :     };
     221             : 
     222           2 :     return cmd;
     223             :   }
     224             : }
     225             : 
     226             : 
     227             : /* end of testing_api_cmd_delete_reserve.c */

Generated by: LCOV version 1.14