LCOV - code coverage report
Current view: top level - testing - testing_api_cmd_get_template.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 41 63 65.1 %
Date: 2025-06-23 16:22:09 Functions: 4 4 100.0 %

          Line data    Source code
       1             : /*
       2             :   This file is part of TALER
       3             :   Copyright (C) 2022 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_get_template.c
      21             :  * @brief command to test GET /templates/$ID
      22             :  * @author Priscilla HUANG
      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 "GET template" CMD.
      33             :  */
      34             : struct GetTemplateState
      35             : {
      36             : 
      37             :   /**
      38             :    * Handle for a "GET template" request.
      39             :    */
      40             :   struct TALER_MERCHANT_TemplateGetHandle *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 template to run GET for.
      54             :    */
      55             :   const char *template_id;
      56             : 
      57             :   /**
      58             :    * Reference for a POST or PATCH /templates CMD (optional).
      59             :    */
      60             :   const char *template_reference;
      61             : 
      62             :   /**
      63             :    * Expected HTTP response code.
      64             :    */
      65             :   unsigned int http_status;
      66             : 
      67             : };
      68             : 
      69             : 
      70             : /**
      71             :  * Callback for a /get/templates/$ID operation.
      72             :  *
      73             :  * @param cls closure for this function
      74             :  * @param tgr HTTP response details
      75             :  */
      76             : static void
      77           6 : get_template_cb (void *cls,
      78             :                  const struct TALER_MERCHANT_TemplateGetResponse *tgr)
      79             : {
      80           6 :   struct GetTemplateState *gis = cls;
      81             :   const struct TALER_TESTING_Command *template_cmd;
      82             : 
      83           6 :   gis->igh = NULL;
      84           6 :   if (gis->http_status != tgr->hr.http_status)
      85             :   {
      86           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
      87             :                 "Unexpected response code %u (%d) to command %s\n",
      88             :                 tgr->hr.http_status,
      89             :                 (int) tgr->hr.ec,
      90             :                 TALER_TESTING_interpreter_get_current_label (gis->is));
      91           0 :     TALER_TESTING_interpreter_fail (gis->is);
      92           0 :     return;
      93             :   }
      94           6 :   switch (tgr->hr.http_status)
      95             :   {
      96           4 :   case MHD_HTTP_OK:
      97             :     {
      98             :       const char *expected_description;
      99             : 
     100           4 :       template_cmd = TALER_TESTING_interpreter_lookup_command (
     101             :         gis->is,
     102             :         gis->template_reference);
     103           4 :       if (GNUNET_OK !=
     104           4 :           TALER_TESTING_get_trait_template_description (template_cmd,
     105             :                                                         &expected_description))
     106           0 :         TALER_TESTING_interpreter_fail (gis->is);
     107           4 :       if (0 != strcmp (tgr->details.ok.template_description,
     108             :                        expected_description))
     109             :       {
     110           0 :         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     111             :                     "Template description does not match\n");
     112           0 :         TALER_TESTING_interpreter_fail (gis->is);
     113           0 :         return;
     114             :       }
     115             :     }
     116             :     {
     117             :       const char *expected_otp_id;
     118             : 
     119           4 :       if (GNUNET_OK !=
     120           4 :           TALER_TESTING_get_trait_otp_id (template_cmd,
     121             :                                           &expected_otp_id))
     122           0 :         TALER_TESTING_interpreter_fail (gis->is);
     123           4 :       if ( ( (NULL == tgr->details.ok.otp_id) && (NULL != expected_otp_id)) ||
     124           4 :            ( (NULL != tgr->details.ok.otp_id) && (NULL == expected_otp_id)) ||
     125           4 :            ( (NULL != tgr->details.ok.otp_id) &&
     126           0 :              (0 != strcmp (tgr->details.ok.otp_id,
     127             :                            expected_otp_id)) ) )
     128             :       {
     129           0 :         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     130             :                     "Template pos_key `%s' does not match `%s'\n",
     131             :                     tgr->details.ok.otp_id,
     132             :                     expected_otp_id);
     133           0 :         TALER_TESTING_interpreter_fail (gis->is);
     134           0 :         return;
     135             :       }
     136             :     }
     137             :     {
     138             :       const json_t *expected_template_contract;
     139             : 
     140           4 :       if (GNUNET_OK !=
     141           4 :           TALER_TESTING_get_trait_template_contract (template_cmd,
     142             :                                                      &expected_template_contract))
     143           0 :         TALER_TESTING_interpreter_fail (gis->is);
     144           4 :       if (1 != json_equal (tgr->details.ok.template_contract,
     145             :                            expected_template_contract))
     146             :       {
     147           0 :         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     148             :                     "Template contract does not match\n");
     149           0 :         TALER_TESTING_interpreter_fail (gis->is);
     150           0 :         return;
     151             :       }
     152             :     }
     153           4 :     break;
     154           0 :   case MHD_HTTP_UNAUTHORIZED:
     155           0 :     break;
     156           2 :   case MHD_HTTP_NOT_FOUND:
     157           2 :     break;
     158           0 :   default:
     159           0 :     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
     160             :                 "Unhandled HTTP status.\n");
     161             :   }
     162           6 :   TALER_TESTING_interpreter_next (gis->is);
     163             : }
     164             : 
     165             : 
     166             : /**
     167             :  * Run the "GET template" CMD.
     168             :  *
     169             :  *
     170             :  * @param cls closure.
     171             :  * @param cmd command being run now.
     172             :  * @param is interpreter state.
     173             :  */
     174             : static void
     175           6 : get_template_run (void *cls,
     176             :                   const struct TALER_TESTING_Command *cmd,
     177             :                   struct TALER_TESTING_Interpreter *is)
     178             : {
     179           6 :   struct GetTemplateState *gis = cls;
     180             : 
     181           6 :   gis->is = is;
     182           6 :   gis->igh = TALER_MERCHANT_template_get (
     183             :     TALER_TESTING_interpreter_get_context (is),
     184             :     gis->merchant_url,
     185             :     gis->template_id,
     186             :     &get_template_cb,
     187             :     gis);
     188           6 :   GNUNET_assert (NULL != gis->igh);
     189           6 : }
     190             : 
     191             : 
     192             : /**
     193             :  * Free the state of a "GET template" CMD, and possibly
     194             :  * cancel a pending operation thereof.
     195             :  *
     196             :  * @param cls closure.
     197             :  * @param cmd command being run.
     198             :  */
     199             : static void
     200           6 : get_template_cleanup (void *cls,
     201             :                       const struct TALER_TESTING_Command *cmd)
     202             : {
     203           6 :   struct GetTemplateState *gis = cls;
     204             : 
     205           6 :   if (NULL != gis->igh)
     206             :   {
     207           0 :     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
     208             :                 "GET /templates/$ID operation did not complete\n");
     209           0 :     TALER_MERCHANT_template_get_cancel (gis->igh);
     210             :   }
     211           6 :   GNUNET_free (gis);
     212           6 : }
     213             : 
     214             : 
     215             : struct TALER_TESTING_Command
     216           6 : TALER_TESTING_cmd_merchant_get_template (const char *label,
     217             :                                          const char *merchant_url,
     218             :                                          const char *template_id,
     219             :                                          unsigned int http_status,
     220             :                                          const char *template_reference)
     221             : {
     222             :   struct GetTemplateState *gis;
     223             : 
     224           6 :   gis = GNUNET_new (struct GetTemplateState);
     225           6 :   gis->merchant_url = merchant_url;
     226           6 :   gis->template_id = template_id;
     227           6 :   gis->http_status = http_status;
     228           6 :   gis->template_reference = template_reference;
     229             :   {
     230           6 :     struct TALER_TESTING_Command cmd = {
     231             :       .cls = gis,
     232             :       .label = label,
     233             :       .run = &get_template_run,
     234             :       .cleanup = &get_template_cleanup
     235             :     };
     236             : 
     237           6 :     return cmd;
     238             :   }
     239             : }
     240             : 
     241             : 
     242             : /* end of testing_api_cmd_get_template.c */

Generated by: LCOV version 1.16