merchant

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

testing_api_cmd_instance_auth.c (7237B)


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