LCOV - code coverage report
Current view: top level - testing - testing_api_cmd_post_templates.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 74.2 % 62 46
Test Date: 2025-11-13 17:46:01 Functions: 100.0 % 6 6

            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_post_templates.c
      21              :  * @brief command to test POST /templates
      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 "POST /templates" CMD.
      33              :  */
      34              : struct PostTemplatesState
      35              : {
      36              : 
      37              :   /**
      38              :    * Handle for a "GET template" request.
      39              :    */
      40              :   struct TALER_MERCHANT_TemplatesPostHandle *iph;
      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 POST for.
      54              :    */
      55              :   const char *template_id;
      56              : 
      57              :   /**
      58              :    * description of the template
      59              :    */
      60              :   const char *template_description;
      61              : 
      62              :   /**
      63              :    * OTP device ID.
      64              :    */
      65              :   char *otp_id;
      66              : 
      67              :   /**
      68              :    * Contract of the company
      69              :    */
      70              :   json_t *template_contract;
      71              : 
      72              :   /**
      73              :    * Expected HTTP response code.
      74              :    */
      75              :   unsigned int http_status;
      76              : 
      77              : };
      78              : 
      79              : 
      80              : /**
      81              :  * Callback for a POST /templates operation.
      82              :  *
      83              :  * @param cls closure for this function
      84              :  * @param hr response being processed
      85              :  */
      86              : static void
      87           12 : post_templates_cb (void *cls,
      88              :                    const struct TALER_MERCHANT_HttpResponse *hr)
      89              : {
      90           12 :   struct PostTemplatesState *tis = cls;
      91              : 
      92           12 :   tis->iph = NULL;
      93           12 :   if (tis->http_status != hr->http_status)
      94              :   {
      95            0 :     TALER_TESTING_unexpected_status_with_body (tis->is,
      96              :                                                hr->http_status,
      97              :                                                tis->http_status,
      98              :                                                hr->reply);
      99            0 :     return;
     100              :   }
     101           12 :   switch (hr->http_status)
     102              :   {
     103           10 :   case MHD_HTTP_NO_CONTENT:
     104           10 :     break;
     105            0 :   case MHD_HTTP_UNAUTHORIZED:
     106            0 :     break;
     107            0 :   case MHD_HTTP_FORBIDDEN:
     108            0 :     break;
     109            0 :   case MHD_HTTP_NOT_FOUND:
     110            0 :     break;
     111            2 :   case MHD_HTTP_CONFLICT:
     112            2 :     break;
     113            0 :   default:
     114            0 :     GNUNET_break (0);
     115            0 :     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
     116              :                 "Unhandled HTTP status %u for POST /templates.\n",
     117              :                 hr->http_status);
     118              :   }
     119           12 :   TALER_TESTING_interpreter_next (tis->is);
     120              : }
     121              : 
     122              : 
     123              : /**
     124              :  * Run the "POST /templates" CMD.
     125              :  *
     126              :  *
     127              :  * @param cls closure.
     128              :  * @param cmd command being run now.
     129              :  * @param is interpreter state.
     130              :  */
     131              : static void
     132           12 : post_templates_run (void *cls,
     133              :                     const struct TALER_TESTING_Command *cmd,
     134              :                     struct TALER_TESTING_Interpreter *is)
     135              : {
     136           12 :   struct PostTemplatesState *tis = cls;
     137              : 
     138           12 :   tis->is = is;
     139           12 :   tis->iph = TALER_MERCHANT_templates_post (
     140              :     TALER_TESTING_interpreter_get_context (is),
     141              :     tis->merchant_url,
     142              :     tis->template_id,
     143              :     tis->template_description,
     144           12 :     tis->otp_id,
     145           12 :     tis->template_contract,
     146              :     &post_templates_cb,
     147              :     tis);
     148           12 :   if (NULL == tis->iph)
     149              :   {
     150            0 :     GNUNET_break (0);
     151            0 :     TALER_TESTING_interpreter_fail (tis->is);
     152            0 :     return;
     153              :   }
     154              : }
     155              : 
     156              : 
     157              : /**
     158              :  * Offers information from the POST /templates CMD state to other
     159              :  * commands.
     160              :  *
     161              :  * @param cls closure
     162              :  * @param[out] ret result (could be anything)
     163              :  * @param trait name of the trait
     164              :  * @param index index number of the object to extract.
     165              :  * @return #GNUNET_OK on success
     166              :  */
     167              : static enum GNUNET_GenericReturnValue
     168           22 : post_templates_traits (void *cls,
     169              :                        const void **ret,
     170              :                        const char *trait,
     171              :                        unsigned int index)
     172              : {
     173           22 :   struct PostTemplatesState *pts = cls;
     174              :   struct TALER_TESTING_Trait traits[] = {
     175           22 :     TALER_TESTING_make_trait_template_description (pts->template_description),
     176           22 :     TALER_TESTING_make_trait_otp_id (pts->otp_id),
     177           22 :     TALER_TESTING_make_trait_template_contract (pts->template_contract),
     178           22 :     TALER_TESTING_make_trait_template_id (pts->template_id),
     179           22 :     TALER_TESTING_trait_end (),
     180              :   };
     181              : 
     182           22 :   return TALER_TESTING_get_trait (traits,
     183              :                                   ret,
     184              :                                   trait,
     185              :                                   index);
     186              : }
     187              : 
     188              : 
     189              : /**
     190              :  * Free the state of a "POST template" CMD, and possibly
     191              :  * cancel a pending operation thereof.
     192              :  *
     193              :  * @param cls closure.
     194              :  * @param cmd command being run.
     195              :  */
     196              : static void
     197           12 : post_templates_cleanup (void *cls,
     198              :                         const struct TALER_TESTING_Command *cmd)
     199              : {
     200           12 :   struct PostTemplatesState *tis = cls;
     201              : 
     202           12 :   if (NULL != tis->iph)
     203              :   {
     204            0 :     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
     205              :                 "POST /templates operation did not complete\n");
     206            0 :     TALER_MERCHANT_templates_post_cancel (tis->iph);
     207              :   }
     208           12 :   GNUNET_free (tis->otp_id);
     209           12 :   json_decref (tis->template_contract);
     210           12 :   GNUNET_free (tis);
     211           12 : }
     212              : 
     213              : 
     214              : struct TALER_TESTING_Command
     215           12 : TALER_TESTING_cmd_merchant_post_templates2 (
     216              :   const char *label,
     217              :   const char *merchant_url,
     218              :   const char *template_id,
     219              :   const char *template_description,
     220              :   const char *otp_id,
     221              :   json_t *template_contract,
     222              :   unsigned int http_status)
     223              : {
     224              :   struct PostTemplatesState *tis;
     225              : 
     226           12 :   GNUNET_assert ((NULL == template_contract) ||
     227              :                  json_is_object (template_contract));
     228              : 
     229           12 :   tis = GNUNET_new (struct PostTemplatesState);
     230           12 :   tis->merchant_url = merchant_url;
     231           12 :   tis->template_id = template_id;
     232           12 :   tis->http_status = http_status;
     233           12 :   tis->template_description = template_description;
     234           12 :   tis->otp_id = (NULL == otp_id) ? NULL : GNUNET_strdup (otp_id);
     235           12 :   tis->template_contract = template_contract;
     236              :   {
     237           12 :     struct TALER_TESTING_Command cmd = {
     238              :       .cls = tis,
     239              :       .label = label,
     240              :       .run = &post_templates_run,
     241              :       .cleanup = &post_templates_cleanup,
     242              :       .traits = &post_templates_traits
     243              :     };
     244              : 
     245           12 :     return cmd;
     246              :   }
     247              : }
     248              : 
     249              : 
     250              : struct TALER_TESTING_Command
     251            8 : TALER_TESTING_cmd_merchant_post_templates (const char *label,
     252              :                                            const char *merchant_url,
     253              :                                            const char *template_id,
     254              :                                            const char *template_description,
     255              :                                            unsigned int http_status)
     256              : {
     257            8 :   return TALER_TESTING_cmd_merchant_post_templates2 (
     258              :     label,
     259              :     merchant_url,
     260              :     template_id,
     261              :     template_description,
     262              :     NULL,
     263            8 :     GNUNET_JSON_PACK (
     264              :       GNUNET_JSON_pack_uint64 ("minimum_age", 0),
     265              :       GNUNET_JSON_pack_time_rel ("pay_duration",
     266              :                                  GNUNET_TIME_UNIT_MINUTES)),
     267              :     http_status);
     268              : }
     269              : 
     270              : 
     271              : /* end of testing_api_cmd_post_templates.c */
        

Generated by: LCOV version 2.0-1