merchant

Merchant backend to process payments, run by merchants
Log | Files | Refs | Submodules | README | LICENSE

testing_api_cmd_instance_auth.c (6025B)


      1 /*
      2   This file is part of TALER
      3   Copyright (C) 2021 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_instance_auth.c
     21  * @brief command to test /private/auth POSTing
     22  * @author Christian Grothoff
     23  */
     24 #include "taler/platform.h"
     25 #include <taler/taler_exchange_service.h>
     26 #include <taler/taler_testing_lib.h>
     27 #include "taler/taler_merchant_service.h"
     28 #include "taler/taler_merchant_testing_lib.h"
     29 #include <taler/taler-merchant/post-management-instances-INSTANCE-auth.h>
     30 
     31 
     32 /**
     33  * State of a "POST /instances/$ID/private/auth" CMD.
     34  */
     35 struct AuthInstanceState
     36 {
     37 
     38   /**
     39    * Handle for a "POST auth" request.
     40    */
     41   struct TALER_MERCHANT_PostManagementInstancesAuthHandle *iaph;
     42 
     43   /**
     44    * The interpreter state.
     45    */
     46   struct TALER_TESTING_Interpreter *is;
     47 
     48   /**
     49    * Base URL of the merchant serving the request.
     50    */
     51   const char *merchant_url;
     52 
     53   /**
     54    * ID of the instance to run GET for.
     55    */
     56   const char *instance_id;
     57 
     58   /**
     59    * Desired token. Can be NULL
     60    */
     61   const char *auth_token;
     62 
     63   /**
     64    * Expected HTTP response code.
     65    */
     66   unsigned int http_status;
     67 
     68 };
     69 
     70 
     71 /**
     72  * Callback for a POST /instances/$ID/private/auth operation.
     73  *
     74  * @param cls closure for this function
     75  * @param hr response being processed
     76  */
     77 static void
     78 auth_instance_cb (void *cls,
     79                   const struct
     80                   TALER_MERCHANT_PostManagementInstancesAuthResponse *iar)
     81 {
     82   struct AuthInstanceState *ais = cls;
     83 
     84   ais->iaph = NULL;
     85   if (ais->http_status != iar->hr.http_status)
     86   {
     87     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     88                 "Unexpected response code %u (%d) to command %s\n",
     89                 iar->hr.http_status,
     90                 (int) iar->hr.ec,
     91                 TALER_TESTING_interpreter_get_current_label (ais->is));
     92     TALER_TESTING_interpreter_fail (ais->is);
     93     return;
     94   }
     95   switch (iar->hr.http_status)
     96   {
     97   case MHD_HTTP_NO_CONTENT:
     98     break;
     99   case MHD_HTTP_BAD_REQUEST:
    100     /* likely invalid auth_token value, we do not check client-side */
    101     break;
    102   case MHD_HTTP_FORBIDDEN:
    103     break;
    104   default:
    105     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
    106                 "Unhandled HTTP status %u (%d) returned from /private/auth operation.\n",
    107                 iar->hr.http_status,
    108                 iar->hr.ec);
    109   }
    110   TALER_TESTING_interpreter_next (ais->is);
    111 }
    112 
    113 
    114 /**
    115  * Run the "AUTH /instances/$ID" CMD.
    116  *
    117  *
    118  * @param cls closure.
    119  * @param cmd command being run now.
    120  * @param is interpreter state.
    121  */
    122 static void
    123 auth_instance_run (void *cls,
    124                    const struct TALER_TESTING_Command *cmd,
    125                    struct TALER_TESTING_Interpreter *is)
    126 {
    127   struct AuthInstanceState *ais = cls;
    128 
    129   ais->is = is;
    130   ais->iaph = TALER_MERCHANT_post_management_instances_auth_create (
    131     TALER_TESTING_interpreter_get_context (is),
    132     ais->merchant_url,
    133     ais->instance_id);
    134   TALER_MERCHANT_post_management_instances_auth_set_options (
    135     ais->iaph,
    136     TALER_MERCHANT_post_management_instances_auth_option_password (ais->auth_token));
    137   {
    138     enum TALER_ErrorCode ec;
    139 
    140     ec = TALER_MERCHANT_post_management_instances_auth_start (
    141       ais->iaph,
    142       &auth_instance_cb,
    143       ais);
    144     GNUNET_assert (TALER_EC_NONE == ec);
    145   }
    146 }
    147 
    148 
    149 /**
    150  * Free the state of a "POST instance auth" CMD, and possibly
    151  * cancel a pending operation thereof.
    152  *
    153  * @param cls closure.
    154  * @param cmd command being run.
    155  */
    156 static void
    157 auth_instance_cleanup (void *cls,
    158                        const struct TALER_TESTING_Command *cmd)
    159 {
    160   struct AuthInstanceState *ais = cls;
    161 
    162   if (NULL != ais->iaph)
    163   {
    164     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
    165                 "POST /instance/$ID/auth operation did not complete\n");
    166     TALER_MERCHANT_post_management_instances_auth_cancel (ais->iaph);
    167   }
    168   GNUNET_free (ais);
    169 }
    170 
    171 
    172 /**
    173  * Offer internal data to other commands.
    174  *
    175  * @param cls closure
    176  * @param[out] ret result (could be anything)
    177  * @param trait name of the trait
    178  * @param index index number of the object to extract.
    179  * @return #GNUNET_OK on success
    180  */
    181 static enum GNUNET_GenericReturnValue
    182 auth_instance_traits (void *cls,
    183                       const void **ret,
    184                       const char *trait,
    185                       unsigned int index)
    186 {
    187   struct AuthInstanceState *ais = cls;
    188   struct TALER_TESTING_Trait traits[] = {
    189     TALER_TESTING_make_trait_auth_token (ais->auth_token),
    190     TALER_TESTING_trait_end ()
    191   };
    192 
    193   return TALER_TESTING_get_trait (traits,
    194                                   ret,
    195                                   trait,
    196                                   index);
    197 }
    198 
    199 
    200 struct TALER_TESTING_Command
    201 TALER_TESTING_cmd_merchant_post_instance_auth (const char *label,
    202                                                const char *merchant_url,
    203                                                const char *instance_id,
    204                                                const char *auth_token,
    205                                                unsigned int http_status)
    206 {
    207   struct AuthInstanceState *ais;
    208 
    209   ais = GNUNET_new (struct AuthInstanceState);
    210   ais->merchant_url = merchant_url;
    211   ais->instance_id = instance_id;
    212   ais->auth_token = auth_token;
    213   ais->http_status = http_status;
    214 
    215   {
    216     struct TALER_TESTING_Command cmd = {
    217       .cls = ais,
    218       .label = label,
    219       .run = &auth_instance_run,
    220       .cleanup = &auth_instance_cleanup,
    221       .traits = &auth_instance_traits
    222     };
    223 
    224     return cmd;
    225   }
    226 }
    227 
    228 
    229 /* end of testing_api_cmd_auth_instance.c */