LCOV - code coverage report
Current view: top level - testing - testing_api_cmd_delete_instance.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 40 47 85.1 %
Date: 2025-06-23 16:22:09 Functions: 5 5 100.0 %

          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_instance.c
      21             :  * @brief command to test DELETE /instance/$ID
      22             :  * @author Christian Grothoff
      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 /instances/$ID" CMD.
      33             :  */
      34             : struct DeleteInstanceState
      35             : {
      36             : 
      37             :   /**
      38             :    * Handle for a "DELETE instance" request.
      39             :    */
      40             :   struct TALER_MERCHANT_InstanceDeleteHandle *igh;
      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             :    * ID of the instance to run DELETE for.
      54             :    */
      55             :   const char *instance_id;
      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/instances/$ID operation.
      72             :  *
      73             :  * @param cls closure for this function
      74             :  * @param hr response being processed
      75             :  */
      76             : static void
      77          18 : delete_instance_cb (void *cls,
      78             :                     const struct TALER_MERCHANT_HttpResponse *hr)
      79             : {
      80          18 :   struct DeleteInstanceState *dis = cls;
      81             : 
      82          18 :   dis->igh = NULL;
      83          18 :   if (dis->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 (dis->is));
      90           0 :     TALER_TESTING_interpreter_fail (dis->is);
      91           0 :     return;
      92             :   }
      93          18 :   switch (hr->http_status)
      94             :   {
      95          14 :   case MHD_HTTP_NO_CONTENT:
      96          14 :     break;
      97           2 :   case MHD_HTTP_UNAUTHORIZED:
      98           2 :     break;
      99           2 :   case MHD_HTTP_NOT_FOUND:
     100           2 :     break;
     101           0 :   default:
     102           0 :     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
     103             :                 "Unhandled HTTP status %d for DELETE instance.\n",
     104             :                 hr->http_status);
     105             :   }
     106          18 :   TALER_TESTING_interpreter_next (dis->is);
     107             : }
     108             : 
     109             : 
     110             : /**
     111             :  * Run the "DELETE instance" CMD.
     112             :  *
     113             :  * @param cls closure.
     114             :  * @param cmd command being run now.
     115             :  * @param is interpreter state.
     116             :  */
     117             : static void
     118          18 : delete_instance_run (void *cls,
     119             :                      const struct TALER_TESTING_Command *cmd,
     120             :                      struct TALER_TESTING_Interpreter *is)
     121             : {
     122          18 :   struct DeleteInstanceState *dis = cls;
     123             : 
     124          18 :   dis->is = is;
     125          18 :   if (dis->purge)
     126          14 :     dis->igh = TALER_MERCHANT_instance_purge (
     127             :       TALER_TESTING_interpreter_get_context (is),
     128             :       dis->merchant_url,
     129             :       dis->instance_id,
     130             :       &delete_instance_cb,
     131             :       dis);
     132             :   else
     133           4 :     dis->igh = TALER_MERCHANT_instance_delete (
     134             :       TALER_TESTING_interpreter_get_context (is),
     135             :       dis->merchant_url,
     136             :       dis->instance_id,
     137             :       &delete_instance_cb,
     138             :       dis);
     139          18 :   GNUNET_assert (NULL != dis->igh);
     140          18 : }
     141             : 
     142             : 
     143             : /**
     144             :  * Free the state of a "DELETE instance" CMD, and possibly
     145             :  * cancel a pending operation thereof.
     146             :  *
     147             :  * @param cls closure.
     148             :  * @param cmd command being run.
     149             :  */
     150             : static void
     151          18 : delete_instance_cleanup (void *cls,
     152             :                          const struct TALER_TESTING_Command *cmd)
     153             : {
     154          18 :   struct DeleteInstanceState *dis = cls;
     155             : 
     156          18 :   if (NULL != dis->igh)
     157             :   {
     158           0 :     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
     159             :                 "DELETE /instances/$ID operation did not complete\n");
     160           0 :     TALER_MERCHANT_instance_delete_cancel (dis->igh);
     161             :   }
     162          18 :   GNUNET_free (dis);
     163          18 : }
     164             : 
     165             : 
     166             : struct TALER_TESTING_Command
     167           4 : TALER_TESTING_cmd_merchant_delete_instance (const char *label,
     168             :                                             const char *merchant_url,
     169             :                                             const char *instance_id,
     170             :                                             unsigned int http_status)
     171             : {
     172             :   struct DeleteInstanceState *dis;
     173             : 
     174           4 :   dis = GNUNET_new (struct DeleteInstanceState);
     175           4 :   dis->merchant_url = merchant_url;
     176           4 :   dis->instance_id = instance_id;
     177           4 :   dis->http_status = http_status;
     178             :   {
     179           4 :     struct TALER_TESTING_Command cmd = {
     180             :       .cls = dis,
     181             :       .label = label,
     182             :       .run = &delete_instance_run,
     183             :       .cleanup = &delete_instance_cleanup
     184             :     };
     185             : 
     186           4 :     return cmd;
     187             :   }
     188             : }
     189             : 
     190             : 
     191             : struct TALER_TESTING_Command
     192          14 : TALER_TESTING_cmd_merchant_purge_instance (const char *label,
     193             :                                            const char *merchant_url,
     194             :                                            const char *instance_id,
     195             :                                            unsigned int http_status)
     196             : {
     197             :   struct DeleteInstanceState *dis;
     198             : 
     199          14 :   dis = GNUNET_new (struct DeleteInstanceState);
     200          14 :   dis->merchant_url = merchant_url;
     201          14 :   dis->instance_id = instance_id;
     202          14 :   dis->http_status = http_status;
     203          14 :   dis->purge = true;
     204             :   {
     205          14 :     struct TALER_TESTING_Command cmd = {
     206             :       .cls = dis,
     207             :       .label = label,
     208             :       .run = &delete_instance_run,
     209             :       .cleanup = &delete_instance_cleanup
     210             :     };
     211             : 
     212          14 :     return cmd;
     213             :   }
     214             : }
     215             : 
     216             : 
     217             : /* end of testing_api_cmd_delete_instance.c */

Generated by: LCOV version 1.16