LCOV - code coverage report
Current view: top level - testing - testing_api_cmd_post_account.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 71.4 % 63 45
Test Date: 2025-11-13 17:46:01 Functions: 100.0 % 5 5

            Line data    Source code
       1              : /*
       2              :   This file is part of TALER
       3              :   Copyright (C) 2023 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_account.c
      21              :  * @brief command to test POST /account
      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 "POST /account" CMD.
      33              :  */
      34              : struct PostAccountState
      35              : {
      36              : 
      37              :   /**
      38              :    * Handle for a "GET product" request.
      39              :    */
      40              :   struct TALER_MERCHANT_AccountsPostHandle *aph;
      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              :    * Wire hash of the created account, set on success.
      54              :    */
      55              :   struct TALER_MerchantWireHashP h_wire;
      56              : 
      57              :   /**
      58              :    * RFC 8905 URI for the account to create.
      59              :    */
      60              :   struct TALER_FullPayto payto_uri;
      61              : 
      62              :   /**
      63              :    * Credit facade URL for the account to create.
      64              :    */
      65              :   char *credit_facade_url;
      66              : 
      67              :   /**
      68              :    * Credit facade credentials for the account to create.
      69              :    */
      70              :   json_t *credit_facade_credentials;
      71              : 
      72              :   /**
      73              :    * Expected HTTP response code.
      74              :    */
      75              :   unsigned int http_status;
      76              : 
      77              : };
      78              : 
      79              : 
      80              : /**
      81              :  * Callback for a POST /account operation.
      82              :  *
      83              :  * @param cls closure for this function
      84              :  * @param apr response being processed
      85              :  */
      86              : static void
      87           10 : post_account_cb (void *cls,
      88              :                  const struct TALER_MERCHANT_AccountsPostResponse *apr)
      89              : {
      90           10 :   struct PostAccountState *pas = cls;
      91              : 
      92           10 :   pas->aph = NULL;
      93           10 :   if (pas->http_status != apr->hr.http_status)
      94              :   {
      95            0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
      96              :                 "Unexpected response code %u (%d) to command %s\n",
      97              :                 apr->hr.http_status,
      98              :                 (int) apr->hr.ec,
      99              :                 TALER_TESTING_interpreter_get_current_label (pas->is));
     100            0 :     TALER_TESTING_interpreter_fail (pas->is);
     101            0 :     return;
     102              :   }
     103           10 :   switch (apr->hr.http_status)
     104              :   {
     105           10 :   case MHD_HTTP_OK:
     106           10 :     pas->h_wire = apr->details.ok.h_wire;
     107           10 :     break;
     108            0 :   case MHD_HTTP_UNAUTHORIZED:
     109            0 :     break;
     110            0 :   case MHD_HTTP_FORBIDDEN:
     111            0 :     break;
     112            0 :   case MHD_HTTP_NOT_FOUND:
     113            0 :     break;
     114            0 :   case MHD_HTTP_CONFLICT:
     115            0 :     break;
     116            0 :   default:
     117            0 :     GNUNET_break (0);
     118            0 :     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
     119              :                 "Unhandled HTTP status %u for POST /account.\n",
     120              :                 apr->hr.http_status);
     121              :   }
     122           10 :   TALER_TESTING_interpreter_next (pas->is);
     123              : }
     124              : 
     125              : 
     126              : /**
     127              :  * Run the "POST /account" CMD.
     128              :  *
     129              :  *
     130              :  * @param cls closure.
     131              :  * @param cmd command being run now.
     132              :  * @param is interpreter state.
     133              :  */
     134              : static void
     135           10 : post_account_run (void *cls,
     136              :                   const struct TALER_TESTING_Command *cmd,
     137              :                   struct TALER_TESTING_Interpreter *is)
     138              : {
     139           10 :   struct PostAccountState *pas = cls;
     140              : 
     141           10 :   pas->is = is;
     142           10 :   pas->aph = TALER_MERCHANT_accounts_post (
     143              :     TALER_TESTING_interpreter_get_context (is),
     144              :     pas->merchant_url,
     145              :     pas->payto_uri,
     146           10 :     pas->credit_facade_url,
     147           10 :     pas->credit_facade_credentials,
     148              :     &post_account_cb,
     149              :     pas);
     150           10 :   GNUNET_assert (NULL != pas->aph);
     151           10 : }
     152              : 
     153              : 
     154              : /**
     155              :  * Offers information from the POST /account CMD state to other
     156              :  * commands.
     157              :  *
     158              :  * @param cls closure
     159              :  * @param[out] ret result (could be anything)
     160              :  * @param trait name of the trait
     161              :  * @param index index number of the object to extract.
     162              :  * @return #GNUNET_OK on success
     163              :  */
     164              : static enum GNUNET_GenericReturnValue
     165            2 : post_account_traits (void *cls,
     166              :                      const void **ret,
     167              :                      const char *trait,
     168              :                      unsigned int index)
     169              : {
     170            2 :   struct PostAccountState *pps = cls;
     171              :   struct TALER_TESTING_Trait traits[] = {
     172            2 :     TALER_TESTING_make_trait_h_wires (
     173              :       0,
     174            2 :       &pps->h_wire),
     175            2 :     TALER_TESTING_make_trait_h_wire (
     176            2 :       &pps->h_wire),
     177            2 :     TALER_TESTING_make_trait_payto_uris (
     178              :       0,
     179            2 :       &pps->payto_uri),
     180            2 :     TALER_TESTING_make_trait_merchant_base_url (
     181              :       pps->merchant_url),
     182            2 :     TALER_TESTING_trait_end (),
     183              :   };
     184              : 
     185            2 :   return TALER_TESTING_get_trait (traits,
     186              :                                   ret,
     187              :                                   trait,
     188              :                                   index);
     189              : }
     190              : 
     191              : 
     192              : /**
     193              :  * Free the state of a "POST product" 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           10 : post_account_cleanup (void *cls,
     201              :                       const struct TALER_TESTING_Command *cmd)
     202              : {
     203           10 :   struct PostAccountState *pas = cls;
     204              : 
     205           10 :   if (NULL != pas->aph)
     206              :   {
     207            0 :     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
     208              :                 "POST /account operation did not complete\n");
     209            0 :     TALER_MERCHANT_accounts_post_cancel (pas->aph);
     210              :   }
     211           10 :   GNUNET_free (pas->payto_uri.full_payto);
     212           10 :   GNUNET_free (pas->credit_facade_url);
     213           10 :   json_decref (pas->credit_facade_credentials);
     214           10 :   GNUNET_free (pas);
     215           10 : }
     216              : 
     217              : 
     218              : struct TALER_TESTING_Command
     219           10 : TALER_TESTING_cmd_merchant_post_account (
     220              :   const char *label,
     221              :   const char *merchant_url,
     222              :   struct TALER_FullPayto payto_uri,
     223              :   const char *credit_facade_url,
     224              :   const json_t *credit_facade_credentials,
     225              :   unsigned int http_status)
     226              : {
     227              :   struct PostAccountState *pas;
     228              : 
     229           10 :   pas = GNUNET_new (struct PostAccountState);
     230           10 :   pas->merchant_url = merchant_url;
     231              :   pas->payto_uri.full_payto
     232           10 :     = GNUNET_strdup (payto_uri.full_payto);
     233           10 :   if (NULL != credit_facade_url)
     234            0 :     pas->credit_facade_url = GNUNET_strdup (credit_facade_url);
     235           10 :   if (NULL != credit_facade_credentials)
     236              :     pas->credit_facade_credentials
     237            0 :       = json_incref ((json_t *) credit_facade_credentials);
     238           10 :   pas->http_status = http_status;
     239              :   {
     240           10 :     struct TALER_TESTING_Command cmd = {
     241              :       .cls = pas,
     242              :       .label = label,
     243              :       .run = &post_account_run,
     244              :       .cleanup = &post_account_cleanup,
     245              :       .traits = &post_account_traits
     246              :     };
     247              : 
     248           10 :     return cmd;
     249              :   }
     250              : }
     251              : 
     252              : 
     253              : /* end of testing_api_cmd_post_account.c */
        

Generated by: LCOV version 2.0-1