merchant

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

testing_api_cmd_instance_auth.c (5883B)


      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     ais->auth_token);
    135   {
    136     enum TALER_ErrorCode ec;
    137 
    138     ec = TALER_MERCHANT_post_management_instances_auth_start (
    139       ais->iaph,
    140       &auth_instance_cb,
    141       ais);
    142     GNUNET_assert (TALER_EC_NONE == ec);
    143   }
    144 }
    145 
    146 
    147 /**
    148  * Free the state of a "POST instance auth" CMD, and possibly
    149  * cancel a pending operation thereof.
    150  *
    151  * @param cls closure.
    152  * @param cmd command being run.
    153  */
    154 static void
    155 auth_instance_cleanup (void *cls,
    156                        const struct TALER_TESTING_Command *cmd)
    157 {
    158   struct AuthInstanceState *ais = cls;
    159 
    160   if (NULL != ais->iaph)
    161   {
    162     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
    163                 "POST /instance/$ID/auth operation did not complete\n");
    164     TALER_MERCHANT_post_management_instances_auth_cancel (ais->iaph);
    165   }
    166   GNUNET_free (ais);
    167 }
    168 
    169 
    170 /**
    171  * Offer internal data to other commands.
    172  *
    173  * @param cls closure
    174  * @param[out] ret result (could be anything)
    175  * @param trait name of the trait
    176  * @param index index number of the object to extract.
    177  * @return #GNUNET_OK on success
    178  */
    179 static enum GNUNET_GenericReturnValue
    180 auth_instance_traits (void *cls,
    181                       const void **ret,
    182                       const char *trait,
    183                       unsigned int index)
    184 {
    185   struct AuthInstanceState *ais = cls;
    186   struct TALER_TESTING_Trait traits[] = {
    187     TALER_TESTING_make_trait_auth_token (ais->auth_token),
    188     TALER_TESTING_trait_end ()
    189   };
    190 
    191   return TALER_TESTING_get_trait (traits,
    192                                   ret,
    193                                   trait,
    194                                   index);
    195 }
    196 
    197 
    198 struct TALER_TESTING_Command
    199 TALER_TESTING_cmd_merchant_post_instance_auth (const char *label,
    200                                                const char *merchant_url,
    201                                                const char *instance_id,
    202                                                const char *auth_token,
    203                                                unsigned int http_status)
    204 {
    205   struct AuthInstanceState *ais;
    206 
    207   ais = GNUNET_new (struct AuthInstanceState);
    208   ais->merchant_url = merchant_url;
    209   ais->instance_id = instance_id;
    210   ais->auth_token = auth_token;
    211   ais->http_status = http_status;
    212 
    213   {
    214     struct TALER_TESTING_Command cmd = {
    215       .cls = ais,
    216       .label = label,
    217       .run = &auth_instance_run,
    218       .cleanup = &auth_instance_cleanup,
    219       .traits = &auth_instance_traits
    220     };
    221 
    222     return cmd;
    223   }
    224 }
    225 
    226 
    227 /* end of testing_api_cmd_auth_instance.c */