LCOV - code coverage report
Current view: top level - testing - testing_api_cmd_delete_instance.c (source / functions) Hit Total Coverage
Test: GNU Taler merchant coverage report Lines: 40 47 85.1 %
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_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           9 : delete_instance_cb (void *cls,
      78             :                     const struct TALER_MERCHANT_HttpResponse *hr)
      79             : {
      80           9 :   struct DeleteInstanceState *dis = cls;
      81             : 
      82           9 :   dis->igh = NULL;
      83           9 :   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           9 :   switch (hr->http_status)
      94             :   {
      95           7 :   case MHD_HTTP_NO_CONTENT:
      96           7 :     break;
      97           1 :   case MHD_HTTP_UNAUTHORIZED:
      98           1 :     break;
      99           1 :   case MHD_HTTP_NOT_FOUND:
     100           1 :     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           9 :   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           9 : delete_instance_run (void *cls,
     119             :                      const struct TALER_TESTING_Command *cmd,
     120             :                      struct TALER_TESTING_Interpreter *is)
     121             : {
     122           9 :   struct DeleteInstanceState *dis = cls;
     123             : 
     124           9 :   dis->is = is;
     125           9 :   if (dis->purge)
     126           7 :     dis->igh = TALER_MERCHANT_instance_purge (is->ctx,
     127             :                                               dis->merchant_url,
     128             :                                               dis->instance_id,
     129             :                                               &delete_instance_cb,
     130             :                                               dis);
     131             :   else
     132           2 :     dis->igh = TALER_MERCHANT_instance_delete (is->ctx,
     133             :                                                dis->merchant_url,
     134             :                                                dis->instance_id,
     135             :                                                &delete_instance_cb,
     136             :                                                dis);
     137           9 :   GNUNET_assert (NULL != dis->igh);
     138           9 : }
     139             : 
     140             : 
     141             : /**
     142             :  * Free the state of a "DELETE instance" CMD, and possibly
     143             :  * cancel a pending operation thereof.
     144             :  *
     145             :  * @param cls closure.
     146             :  * @param cmd command being run.
     147             :  */
     148             : static void
     149           9 : delete_instance_cleanup (void *cls,
     150             :                          const struct TALER_TESTING_Command *cmd)
     151             : {
     152           9 :   struct DeleteInstanceState *dis = cls;
     153             : 
     154           9 :   if (NULL != dis->igh)
     155             :   {
     156           0 :     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
     157             :                 "DELETE /instances/$ID operation did not complete\n");
     158           0 :     TALER_MERCHANT_instance_delete_cancel (dis->igh);
     159             :   }
     160           9 :   GNUNET_free (dis);
     161           9 : }
     162             : 
     163             : 
     164             : struct TALER_TESTING_Command
     165           2 : TALER_TESTING_cmd_merchant_delete_instance (const char *label,
     166             :                                             const char *merchant_url,
     167             :                                             const char *instance_id,
     168             :                                             unsigned int http_status)
     169             : {
     170             :   struct DeleteInstanceState *dis;
     171             : 
     172           2 :   dis = GNUNET_new (struct DeleteInstanceState);
     173           2 :   dis->merchant_url = merchant_url;
     174           2 :   dis->instance_id = instance_id;
     175           2 :   dis->http_status = http_status;
     176             :   {
     177           2 :     struct TALER_TESTING_Command cmd = {
     178             :       .cls = dis,
     179             :       .label = label,
     180             :       .run = &delete_instance_run,
     181             :       .cleanup = &delete_instance_cleanup
     182             :     };
     183             : 
     184           2 :     return cmd;
     185             :   }
     186             : }
     187             : 
     188             : 
     189             : struct TALER_TESTING_Command
     190           7 : TALER_TESTING_cmd_merchant_purge_instance (const char *label,
     191             :                                            const char *merchant_url,
     192             :                                            const char *instance_id,
     193             :                                            unsigned int http_status)
     194             : {
     195             :   struct DeleteInstanceState *dis;
     196             : 
     197           7 :   dis = GNUNET_new (struct DeleteInstanceState);
     198           7 :   dis->merchant_url = merchant_url;
     199           7 :   dis->instance_id = instance_id;
     200           7 :   dis->http_status = http_status;
     201           7 :   dis->purge = true;
     202             :   {
     203           7 :     struct TALER_TESTING_Command cmd = {
     204             :       .cls = dis,
     205             :       .label = label,
     206             :       .run = &delete_instance_run,
     207             :       .cleanup = &delete_instance_cleanup
     208             :     };
     209             : 
     210           7 :     return cmd;
     211             :   }
     212             : }
     213             : 
     214             : 
     215             : /* end of testing_api_cmd_delete_instance.c */

Generated by: LCOV version 1.14