LCOV - code coverage report
Current view: top level - testing - testing_api_cmd_tip_authorize.c (source / functions) Hit Total Coverage
Test: GNU Taler coverage report Lines: 0 90 0.0 %
Date: 2020-08-15 06:12:35 Functions: 0 10 0.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :   This file is part of TALER
       3             :   Copyright (C) 2014-2020 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             : /**
      21             :  * @file lib/testing_api_cmd_tip_authorize.c
      22             :  * @brief command to test the tipping.
      23             :  * @author Marcello Stanisci
      24             :  */
      25             : 
      26             : #include "platform.h"
      27             : #include <taler/taler_exchange_service.h>
      28             : #include <taler/taler_testing_lib.h>
      29             : #include "taler_merchant_service.h"
      30             : #include "taler_merchant_testing_lib.h"
      31             : 
      32             : 
      33             : /**
      34             :  * State for a /tip-authorize CMD.
      35             :  */
      36             : struct TipAuthorizeState
      37             : {
      38             : 
      39             :   /**
      40             :    * Merchant base URL.
      41             :    */
      42             :   const char *merchant_url;
      43             : 
      44             :   /**
      45             :    * Expected HTTP response code.
      46             :    */
      47             :   unsigned int http_status;
      48             : 
      49             :   /**
      50             :    * Reference to the reserv to authorize the tip
      51             :    * from (if NULL, the merchant decides).
      52             :    */
      53             :   const char *reserve_reference;
      54             : 
      55             :   /**
      56             :    * Human-readable justification for the
      57             :    * tip authorization carried on by this CMD.
      58             :    */
      59             :   const char *justification;
      60             : 
      61             :   /**
      62             :    * Amount that should be authorized for tipping.
      63             :    */
      64             :   struct TALER_Amount amount;
      65             : 
      66             :   /**
      67             :    * Expected Taler error code for this CMD.
      68             :    */
      69             :   enum TALER_ErrorCode expected_ec;
      70             : 
      71             :   /**
      72             :    * Tip taler:// URI.
      73             :    */
      74             :   const char *tip_uri;
      75             : 
      76             :   /**
      77             :    * The tip id; set when the CMD succeeds.
      78             :    */
      79             :   struct GNUNET_HashCode tip_id;
      80             : 
      81             :   /**
      82             :    * Expiration date for this tip.
      83             :    */
      84             :   struct GNUNET_TIME_Absolute tip_expiration;
      85             : 
      86             :   /**
      87             :    * Handle to the on-going /tip-authorize request.
      88             :    */
      89             :   struct TALER_MERCHANT_TipAuthorizeHandle *tao;
      90             : 
      91             :   /**
      92             :    * The interpreter state.
      93             :    */
      94             :   struct TALER_TESTING_Interpreter *is;
      95             : };
      96             : 
      97             : 
      98             : /**
      99             :  * Callback for a /tip-authorize request.  Set into the state
     100             :  * what was returned from the backend (@a tip_id and @a
     101             :  * tip_expiration).
     102             :  *
     103             :  * @param cls closure
     104             :  * @param hr HTTP response we got
     105             :  * @param taler_tip_uri URI to let the wallet know about the tip
     106             :  * @param tip_id unique identifier for the tip
     107             :  */
     108             : static void
     109           0 : tip_authorize_cb (void *cls,
     110             :                   const struct TALER_MERCHANT_HttpResponse *hr,
     111             :                   struct GNUNET_HashCode *tip_id,
     112             :                   const char *taler_tip_uri,
     113             :                   struct GNUNET_TIME_Absolute expiration)
     114             : {
     115           0 :   struct TipAuthorizeState *tas = cls;
     116             : 
     117           0 :   tas->tao = NULL;
     118           0 :   if (tas->http_status != hr->http_status)
     119             :   {
     120           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     121             :                 "Unexpected response code %u (%d) to command %s\n",
     122             :                 hr->http_status,
     123             :                 hr->ec,
     124             :                 TALER_TESTING_interpreter_get_current_label (tas->is));
     125           0 :     TALER_TESTING_interpreter_fail (tas->is);
     126           0 :     return;
     127             :   }
     128             : 
     129           0 :   if (tas->expected_ec != hr->ec)
     130             :   {
     131           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     132             :                 "Unexpected error code %d (%u) to command %s\n",
     133             :                 (int) hr->ec,
     134             :                 hr->http_status,
     135             :                 TALER_TESTING_interpreter_get_current_label (tas->is));
     136           0 :     TALER_TESTING_interpreter_fail (tas->is);
     137           0 :     return;
     138             :   }
     139           0 :   if ( (MHD_HTTP_OK == hr->http_status) &&
     140           0 :        (TALER_EC_NONE == hr->ec) )
     141             :   {
     142           0 :     tas->tip_uri = strdup (taler_tip_uri);
     143           0 :     tas->tip_id = *tip_id;
     144           0 :     tas->tip_expiration = expiration;
     145             :   }
     146           0 :   TALER_TESTING_interpreter_next (tas->is);
     147             : }
     148             : 
     149             : 
     150             : /**
     151             :  * Offers information from the /tip-authorize CMD state to other
     152             :  * commands.
     153             :  *
     154             :  * @param cls closure
     155             :  * @param ret[out] result (could be anything)
     156             :  * @param trait name of the trait
     157             :  * @param index index number of the object to extract.
     158             :  * @return #GNUNET_OK on success
     159             :  */
     160             : static int
     161           0 : tip_authorize_traits (void *cls,
     162             :                       const void **ret,
     163             :                       const char *trait,
     164             :                       unsigned int index)
     165             : {
     166           0 :   struct TipAuthorizeState *tas = cls;
     167             : 
     168             :   {
     169             :     struct TALER_TESTING_Trait traits[] = {
     170           0 :       TALER_TESTING_make_trait_tip_id (0, &tas->tip_id),
     171           0 :       TALER_TESTING_make_trait_amount_obj (0, &tas->amount),
     172           0 :       TALER_TESTING_make_trait_string (0, tas->justification),
     173           0 :       TALER_TESTING_make_trait_absolute_time (0, &tas->tip_expiration),
     174           0 :       TALER_TESTING_trait_end (),
     175             :     };
     176             : 
     177           0 :     return TALER_TESTING_get_trait (traits,
     178             :                                     ret,
     179             :                                     trait,
     180             :                                     index);
     181             :   }
     182             : }
     183             : 
     184             : 
     185             : /**
     186             :  * Runs the /tip-authorize CMD
     187             :  *
     188             :  * @param cls closure
     189             :  * @param cmd the CMD representing _this_ command
     190             :  * @param is interpreter state
     191             :  */
     192             : static void
     193           0 : tip_authorize_run (void *cls,
     194             :                    const struct TALER_TESTING_Command *cmd,
     195             :                    struct TALER_TESTING_Interpreter *is)
     196             : {
     197           0 :   struct TipAuthorizeState *tas = cls;
     198             : 
     199           0 :   tas->is = is;
     200           0 :   if (NULL == tas->reserve_reference)
     201             :   {
     202           0 :     tas->tao = TALER_MERCHANT_tip_authorize (is->ctx,
     203             :                                              tas->merchant_url,
     204             :                                              "http://merchant.com/pickup",
     205           0 :                                              &tas->amount,
     206             :                                              tas->justification,
     207             :                                              &tip_authorize_cb,
     208             :                                              tas);
     209             :   }
     210             :   else
     211             :   {
     212             :     const struct TALER_TESTING_Command *reserve_cmd;
     213             :     const struct TALER_ReservePublicKeyP *reserve_pub;
     214             : 
     215           0 :     reserve_cmd = TALER_TESTING_interpreter_lookup_command (
     216             :       tas->is,
     217             :       tas->reserve_reference);
     218           0 :     GNUNET_assert (GNUNET_OK ==
     219             :                    TALER_TESTING_get_trait_reserve_pub (reserve_cmd,
     220             :                                                         0,
     221             :                                                         &reserve_pub));
     222           0 :     tas->tao = TALER_MERCHANT_tip_authorize2 (is->ctx,
     223             :                                               tas->merchant_url,
     224             :                                               reserve_pub,
     225             :                                               "http://merchant.com/pickup",
     226           0 :                                               &tas->amount,
     227             :                                               tas->justification,
     228             :                                               &tip_authorize_cb,
     229             :                                               tas);
     230             :   }
     231             : 
     232           0 :   GNUNET_assert (NULL != tas->tao);
     233           0 : }
     234             : 
     235             : 
     236             : /**
     237             :  * Run the /tip-authorize CMD, the "fake" version of it.
     238             :  *
     239             :  * @param cls closure
     240             :  * @param cmd the CMD representing _this_ command
     241             :  * @param is interpreter state *
     242             :  */
     243             : static void
     244           0 : tip_authorize_fake_run (void *cls,
     245             :                         const struct TALER_TESTING_Command *cmd,
     246             :                         struct TALER_TESTING_Interpreter *is)
     247             : {
     248           0 :   struct TipAuthorizeState *tas = cls;
     249             : 
     250             :   /* Make up a tip id.  */
     251           0 :   GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_WEAK,
     252           0 :                               &tas->tip_id,
     253             :                               sizeof (struct GNUNET_HashCode));
     254             : 
     255           0 :   TALER_TESTING_interpreter_next (is);
     256           0 : }
     257             : 
     258             : 
     259             : /**
     260             :  * Free the state from a /tip-authorize CMD, and possibly
     261             :  * cancel any pending operation.
     262             :  *
     263             :  * @param cls closure
     264             :  * @param cmd the /tip-authorize CMD that is about to be freed.
     265             :  */
     266             : static void
     267           0 : tip_authorize_cleanup (void *cls,
     268             :                        const struct TALER_TESTING_Command *cmd)
     269             : {
     270           0 :   struct TipAuthorizeState *tas = cls;
     271             : 
     272           0 :   if (NULL != tas->tao)
     273             :   {
     274           0 :     TALER_LOG_WARNING ("Tip-autorize operation"
     275             :                        " did not complete\n");
     276           0 :     TALER_MERCHANT_tip_authorize_cancel (tas->tao);
     277             :   }
     278           0 :   GNUNET_free (tas);
     279           0 : }
     280             : 
     281             : 
     282             : /**
     283             :  * Create a /tip-authorize CMD, specifying the Taler error code
     284             :  * that is expected to be returned by the backend.
     285             :  *
     286             :  * @param label this command label
     287             :  * @param merchant_url the base URL of the merchant that will
     288             :  *        serve the /tip-authorize request.
     289             :  * @param exchange_url the base URL of the exchange that owns
     290             :  *        the reserve from which the tip is going to be gotten.
     291             :  * @param http_status the HTTP response code which is expected
     292             :  *        for this operation.
     293             :  * @param justification human-readable justification for this
     294             :  *        tip authorization.
     295             :  * @param amount the amount to authorize for tipping.
     296             :  * @param ec expected Taler-defined error code.
     297             :  */
     298             : struct TALER_TESTING_Command
     299           0 : TALER_TESTING_cmd_tip_authorize_with_ec (const char *label,
     300             :                                          const char *merchant_url,
     301             :                                          const char *exchange_url,
     302             :                                          unsigned int http_status,
     303             :                                          const char *justification,
     304             :                                          const char *amount,
     305             :                                          enum TALER_ErrorCode ec)
     306             : {
     307             :   struct TipAuthorizeState *tas;
     308             : 
     309           0 :   tas = GNUNET_new (struct TipAuthorizeState);
     310           0 :   tas->merchant_url = merchant_url;
     311           0 :   tas->justification = justification;
     312           0 :   tas->http_status = http_status;
     313           0 :   tas->expected_ec = ec;
     314           0 :   GNUNET_assert (GNUNET_OK ==
     315             :                  TALER_string_to_amount (amount,
     316             :                                          &tas->amount));
     317             :   {
     318           0 :     struct TALER_TESTING_Command cmd = {
     319             :       .label = label,
     320             :       .cls = tas,
     321             :       .run = &tip_authorize_run,
     322             :       .cleanup = &tip_authorize_cleanup,
     323             :       .traits = &tip_authorize_traits
     324             :     };
     325             : 
     326           0 :     return cmd;
     327             :   }
     328             : }
     329             : 
     330             : 
     331             : /**
     332             :  * Create a /tip-authorize CMD, specifying the Taler error code
     333             :  * that is expected to be returned by the backend.
     334             :  *
     335             :  * @param label this command label
     336             :  * @param merchant_url the base URL of the merchant that will
     337             :  *        serve the /tip-authorize request.
     338             :  * @param exchange_url the base URL of the exchange that owns
     339             :  *        the reserve from which the tip is going to be gotten.
     340             :  * @param reserve_reference reference to a command that created
     341             :  *        a reserve.
     342             :  * @param http_status the HTTP response code which is expected
     343             :  *        for this operation.
     344             :  * @param justification human-readable justification for this
     345             :  *        tip authorization.
     346             :  * @param amount the amount to authorize for tipping.
     347             :  * @param ec expected Taler-defined error code.
     348             :  */
     349             : struct TALER_TESTING_Command
     350           0 : TALER_TESTING_cmd_tip_authorize_from_reserve_with_ec (const char *label,
     351             :                                                       const char *merchant_url,
     352             :                                                       const char *exchange_url,
     353             :                                                       const char *
     354             :                                                       reserve_reference,
     355             :                                                       unsigned int http_status,
     356             :                                                       const char *justification,
     357             :                                                       const char *amount,
     358             :                                                       enum TALER_ErrorCode ec)
     359             : {
     360             :   struct TipAuthorizeState *tas;
     361             : 
     362           0 :   tas = GNUNET_new (struct TipAuthorizeState);
     363           0 :   tas->merchant_url = merchant_url;
     364           0 :   tas->justification = justification;
     365           0 :   tas->http_status = http_status;
     366           0 :   tas->expected_ec = ec;
     367           0 :   tas->reserve_reference = reserve_reference;
     368           0 :   GNUNET_assert (GNUNET_OK ==
     369             :                  TALER_string_to_amount (amount,
     370             :                                          &tas->amount));
     371             :   {
     372           0 :     struct TALER_TESTING_Command cmd = {
     373             :       .label = label,
     374             :       .cls = tas,
     375             :       .run = &tip_authorize_run,
     376             :       .cleanup = &tip_authorize_cleanup,
     377             :       .traits = &tip_authorize_traits
     378             :     };
     379             : 
     380           0 :     return cmd;
     381             :   }
     382             : }
     383             : 
     384             : 
     385             : /**
     386             :  * Create a /tip-authorize CMD.
     387             :  *
     388             :  * @param label this command label
     389             :  * @param merchant_url the base URL of the merchant that will
     390             :  *        serve the /tip-authorize request.
     391             :  * @param exchange_url the base URL of the exchange that owns
     392             :  *        the reserve from which the tip is going to be gotten.
     393             :  * @param http_status the HTTP response code which is expected
     394             :  *        for this operation.
     395             :  * @param justification human-readable justification for this
     396             :  *        tip authorization.
     397             :  * @param amount the amount to authorize for tipping.
     398             :  */
     399             : struct TALER_TESTING_Command
     400           0 : TALER_TESTING_cmd_tip_authorize (const char *label,
     401             :                                  const char *merchant_url,
     402             :                                  const char *exchange_url,
     403             :                                  unsigned int http_status,
     404             :                                  const char *justification,
     405             :                                  const char *amount)
     406             : {
     407             :   struct TipAuthorizeState *tas;
     408             : 
     409           0 :   tas = GNUNET_new (struct TipAuthorizeState);
     410           0 :   tas->merchant_url = merchant_url;
     411           0 :   tas->justification = justification;
     412           0 :   tas->http_status = http_status;
     413           0 :   GNUNET_assert (GNUNET_OK ==
     414             :                  TALER_string_to_amount (amount,
     415             :                                          &tas->amount));
     416             :   {
     417           0 :     struct TALER_TESTING_Command cmd = {
     418             :       .label = label,
     419             :       .cls = tas,
     420             :       .run = &tip_authorize_run,
     421             :       .cleanup = &tip_authorize_cleanup,
     422             :       .traits = &tip_authorize_traits
     423             :     };
     424             : 
     425           0 :     return cmd;
     426             :   }
     427             : }
     428             : 
     429             : 
     430             : /**
     431             :  * Create a /tip-authorize CMD.
     432             :  *
     433             :  * @param label this command label
     434             :  * @param merchant_url the base URL of the merchant that will
     435             :  *        serve the /tip-authorize request.
     436             :  * @param exchange_url the base URL of the exchange that owns
     437             :  *        the reserve from which the tip is going to be gotten.
     438             :  * @param reserve_reference reference to a command that created
     439             :  *        a reserve.
     440             :  * @param http_status the HTTP response code which is expected
     441             :  *        for this operation.
     442             :  * @param justification human-readable justification for this
     443             :  *        tip authorization.
     444             :  * @param amount the amount to authorize for tipping.
     445             :  */
     446             : struct TALER_TESTING_Command
     447           0 : TALER_TESTING_cmd_tip_authorize_from_reserve (const char *label,
     448             :                                               const char *merchant_url,
     449             :                                               const char *exchange_url,
     450             :                                               const char *reserve_refernce,
     451             :                                               unsigned int http_status,
     452             :                                               const char *justification,
     453             :                                               const char *amount)
     454             : {
     455             :   struct TipAuthorizeState *tas;
     456             : 
     457           0 :   tas = GNUNET_new (struct TipAuthorizeState);
     458           0 :   tas->merchant_url = merchant_url;
     459           0 :   tas->reserve_reference = reserve_refernce;
     460           0 :   tas->justification = justification;
     461           0 :   tas->http_status = http_status;
     462           0 :   GNUNET_assert (GNUNET_OK ==
     463             :                  TALER_string_to_amount (amount,
     464             :                                          &tas->amount));
     465             :   {
     466           0 :     struct TALER_TESTING_Command cmd = {
     467             :       .label = label,
     468             :       .cls = tas,
     469             :       .run = &tip_authorize_run,
     470             :       .cleanup = &tip_authorize_cleanup,
     471             :       .traits = &tip_authorize_traits
     472             :     };
     473             : 
     474           0 :     return cmd;
     475             :   }
     476             : }
     477             : 
     478             : 
     479             : /**
     480             :  * This commands does not query the backend at all,
     481             :  * but just makes up a fake authorization id that will
     482             :  * be subsequently used by the "pick up" CMD in order
     483             :  * to test against such a case.
     484             :  *
     485             :  * @param label command label.
     486             :  * @return the command.
     487             :  */
     488             : struct TALER_TESTING_Command
     489           0 : TALER_TESTING_cmd_tip_authorize_fake (const char *label)
     490             : {
     491             :   struct TipAuthorizeState *tas;
     492             : 
     493           0 :   tas = GNUNET_new (struct TipAuthorizeState);
     494             :   {
     495           0 :     struct TALER_TESTING_Command cmd = {
     496             :       .label = label,
     497             :       .cls = tas,
     498             :       .run = &tip_authorize_fake_run,
     499             :       .cleanup = &tip_authorize_cleanup,
     500             :       .traits = &tip_authorize_traits
     501             :     };
     502             : 
     503           0 :     return cmd;
     504             :   }
     505             : }
     506             : 
     507             : 
     508             : /* end of testing_api_cmd_tip_authorize.c */

Generated by: LCOV version 1.14