gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[taler-merchant] 28/31: implement orders with choices test case


From: gnunet
Subject: [taler-merchant] 28/31: implement orders with choices test case
Date: Thu, 18 Apr 2024 08:39:21 +0200

This is an automated email from the git hooks/post-receive script.

christian-blaettler pushed a commit to branch master
in repository merchant.

commit b47605662e03d1ad86d0a86d28f3fcd4cc060f36
Author: Christian Blättler <blatc2@bfh.ch>
AuthorDate: Tue Apr 16 23:30:50 2024 +0200

    implement orders with choices test case
---
 src/include/taler_merchant_testing_lib.h         |  63 +++++++++
 src/testing/test_merchant_api.c                  |  60 ++++++++
 src/testing/testing_api_cmd_post_orders.c        | 169 ++++++++++++++++++++++-
 src/testing/testing_api_cmd_post_products.c      |   2 +-
 src/testing/testing_api_cmd_post_tokenfamilies.c |   1 -
 5 files changed, 291 insertions(+), 4 deletions(-)

diff --git a/src/include/taler_merchant_testing_lib.h 
b/src/include/taler_merchant_testing_lib.h
index ebc80bd5..47d081fc 100644
--- a/src/include/taler_merchant_testing_lib.h
+++ b/src/include/taler_merchant_testing_lib.h
@@ -605,6 +605,35 @@ TALER_TESTING_cmd_merchant_post_orders3 (
   const char *amount);
 
 
+/**
+ * Create an order with a choices array with input and output tokens.
+ *
+ * @param label command label
+ * @param cfg configuration to use
+ * @param merchant_url base URL of the merchant serving
+ *        the proposal request.
+ * @param http_status expected HTTP status.
+ * @param token_family_reference label of the POST /tokenfamilies cmd.
+ * @param order_id the name of the order to add.
+ * @param refund_deadline the deadline for refunds on this order.
+ * @param pay_deadline the deadline for payment on this order.
+ * @param amount the amount this order is for.
+ * @return the command
+ */
+
+struct TALER_TESTING_Command
+TALER_TESTING_cmd_merchant_post_orders_choices (
+  const char *label,
+  const struct GNUNET_CONFIGURATION_Handle *cfg,
+  const char *merchant_url,
+  unsigned int http_status,
+  const char *token_family_reference,
+  const char *order_id,
+  struct GNUNET_TIME_Timestamp refund_deadline,
+  struct GNUNET_TIME_Timestamp pay_deadline,
+  const char *amount);
+
+
 /**
  * Define a "GET /orders" CMD.
  *
@@ -1474,6 +1503,40 @@ TALER_TESTING_cmd_merchant_post_using_templates (
   unsigned int http_status);
 
 
+/* ****** Token Families ******* */
+
+
+/**
+ * Define a "POST /tokenfamilies" CMD.
+ *
+ * @param label command label.
+ * @param merchant_url base URL of the merchant serving the
+ *       POST /tokenfamilies request.
+ * @param http_status expected HTTP response code.
+ * @param slug slug of the token family.
+ * @param name name of the token family.
+ * @param description description of the token family.
+ * @param description_i18n internationalized description of the token family.
+ * @param valid_after start of the validity time of the token family.
+ * @param valid_before end of the validity time of the token family.
+ * @param duration validity duration of an issued token of the token family.
+ * @param kind kind of the token family. either "subscription" or "discount".
+ * @return the command.
+ */
+struct TALER_TESTING_Command
+TALER_TESTING_cmd_merchant_post_tokenfamilies (
+  const char *label,
+  const char *merchant_url,
+  unsigned int http_status,
+  const char *slug,
+  const char *name,
+  const char *description,
+  json_t *description_i18n,
+  struct GNUNET_TIME_Timestamp valid_after,
+  struct GNUNET_TIME_Timestamp valid_before,
+  struct GNUNET_TIME_Relative duration,
+  const char *kind);
+
 /* ****** Webhooks ******* */
 
 
diff --git a/src/testing/test_merchant_api.c b/src/testing/test_merchant_api.c
index ed07bce6..78fd0da6 100644
--- a/src/testing/test_merchant_api.c
+++ b/src/testing/test_merchant_api.c
@@ -24,6 +24,7 @@
  * @author Marcello Stanisci
  */
 #include "platform.h"
+#include <gnunet/gnunet_time_lib.h>
 #include <taler/taler_util.h>
 #include <taler/taler_signatures.h>
 #include <taler/taler_exchange_service.h>
@@ -1656,6 +1657,65 @@ run (void *cls,
     TALER_TESTING_cmd_end ()
   };
 
+  struct TALER_TESTING_Command tokens[] = {
+    /**
+     * Move money to the exchange's bank account.
+     */
+    cmd_transfer_to_exchange ("create-reserve-tokens",
+                              "EUR:10.02"),
+    /**
+     * Make a reserve exist, according to the previous transfer.
+     */
+    cmd_exec_wirewatch ("wirewatch-1"),
+    TALER_TESTING_cmd_check_bank_admin_transfer ("check_bank_transfer-tokens",
+                                                 "EUR:10.02",
+                                                 payer_payto,
+                                                 exchange_payto,
+                                                 "create-reserve-tokens"),
+    TALER_TESTING_cmd_withdraw_amount ("withdraw-coin-1",
+                                       "create-reserve-tokens",
+                                       "EUR:5",
+                                       0,
+                                       MHD_HTTP_OK),
+    TALER_TESTING_cmd_withdraw_amount ("withdraw-coin-2",
+                                       "create-reserve-tokens",
+                                       "EUR:5",
+                                       0,
+                                       MHD_HTTP_OK),
+    TALER_TESTING_cmd_merchant_get_orders ("get-orders-empty",
+                                           merchant_url,
+                                           MHD_HTTP_OK,
+                                           NULL),
+    TALER_TESTING_cmd_merchant_post_tokenfamilies ("create-tokenfamily",
+                                                   merchant_url,
+                                                   MHD_HTTP_NO_CONTENT,
+                                                   "subscription-1",
+                                                   "Subscription",
+                                                   "A subscription.",
+                                                   NULL,
+                                                   GNUNET_TIME_timestamp_get 
(),
+                                                   
GNUNET_TIME_relative_to_timestamp (GNUNET_TIME_UNIT_YEARS),
+                                                   GNUNET_TIME_UNIT_MONTHS,
+                                                   "subscription"),
+    TALER_TESTING_cmd_merchant_post_orders_choices 
("create-order-with-choices",
+                                                    cred.cfg,
+                                                    merchant_url,
+                                                    MHD_HTTP_OK,
+                                                    "create-tokenfamily",
+                                                    "5-choices",
+                                                    GNUNET_TIME_UNIT_ZERO_TS,
+                                                    
GNUNET_TIME_UNIT_FOREVER_TS,
+                                                    "EUR:5.0"),
+    /**
+     * Check the reserve is depleted.
+     */
+    TALER_TESTING_cmd_status ("withdraw-status-1",
+                              "create-reserve-tokens",
+                              "EUR:0",
+                              MHD_HTTP_OK),
+
+  };
+
   struct TALER_TESTING_Command commands[] = {
     /* general setup */
     TALER_TESTING_cmd_run_fakebank (
diff --git a/src/testing/testing_api_cmd_post_orders.c 
b/src/testing/testing_api_cmd_post_orders.c
index d5cfdddc..8f7bd46d 100644
--- a/src/testing/testing_api_cmd_post_orders.c
+++ b/src/testing/testing_api_cmd_post_orders.c
@@ -24,6 +24,10 @@
  */
 
 #include "platform.h"
+#include <gnunet/gnunet_common.h>
+#include <gnunet/gnunet_time_lib.h>
+#include <jansson.h>
+#include <stdint.h>
 #include <taler/taler_exchange_service.h>
 #include <taler/taler_testing_lib.h>
 #include "taler_merchant_service.h"
@@ -55,6 +59,11 @@ struct OrdersState
    */
   const char *expected_order_id;
 
+  /**
+   * Reference to a POST /tokenfamilies command. Can be NULL.
+   */
+  const char *token_family_reference;
+
   /**
    * Contract terms obtained from the backend.
    */
@@ -65,6 +74,11 @@ struct OrdersState
    */
   json_t *order_terms;
 
+  /**
+   * Choices array with inputs and outputs for v1 order.
+   */
+  json_t *choices;
+
   /**
    * Contract terms hash code.
    */
@@ -562,6 +576,118 @@ orders_run2 (void *cls,
 }
 
 
+/**
+ * Constructs the json for a the choices of an order request.
+ *
+ * @param slug the name of the order to add, can be NULL.
+ * @param valid_after valid_after date for the input and output token.
+ * @param[out] choices where to write the json string.
+ */
+static void
+make_choices_json (
+  const char *input_slug,
+  const char *output_slug,
+  uint16_t input_count,
+  uint16_t output_count,
+  struct GNUNET_TIME_Timestamp input_valid_after,
+  struct GNUNET_TIME_Timestamp output_valid_after,
+  json_t **choices)
+{
+  json_t *c;
+
+  c = json_pack("[{s:o, s:o}]",
+    "inputs", json_pack("[{s:s, s:i, s:s, s:o}]",
+                        "kind", "token",
+                        "count", input_count,
+                        "token_family_slug", input_slug,
+                        "valid_after", 
GNUNET_JSON_from_timestamp(input_valid_after)),
+    "outputs", json_pack("[{s:s, s:i, s:s, s:o}]",
+                         "kind", "token",
+                         "count", output_count,
+                         "token_family_slug", output_slug,
+                         "valid_after", 
GNUNET_JSON_from_timestamp(output_valid_after)));
+
+  *choices = c;
+}
+
+
+/**
+ * Run a "orders" CMD.
+ *
+ * @param cls closure.
+ * @param cmd command currently being run.
+ * @param is interpreter state.
+ */
+static void
+orders_run3 (void *cls,
+            const struct TALER_TESTING_Command *cmd,
+            struct TALER_TESTING_Interpreter *is)
+{
+  struct OrdersState *ps = cls;
+  struct GNUNET_TIME_Absolute now;
+  const char *slug;
+
+  ps->is = is;
+    now = GNUNET_TIME_absolute_get_monotonic (ps->cfg);
+  if (NULL == json_object_get (ps->order_terms,
+                               "order_id"))
+  {
+    char *order_id;
+
+    order_id = GNUNET_STRINGS_data_to_string_alloc (
+      &now,
+      sizeof (now));
+    GNUNET_assert (0 ==
+                   json_object_set_new (ps->order_terms,
+                                        "order_id",
+                                        json_string (order_id)));
+    GNUNET_free (order_id);
+  }
+
+  {
+    const struct TALER_TESTING_Command *token_family_cmd;
+    token_family_cmd =
+      TALER_TESTING_interpreter_lookup_command (is,
+                                                ps->token_family_reference);
+    if (NULL == token_family_cmd)
+      TALER_TESTING_FAIL (is);
+    if (GNUNET_OK !=
+      TALER_TESTING_get_trait_token_family_slug (token_family_cmd,
+                                                 &slug))
+      TALER_TESTING_FAIL (is);
+  }
+  make_choices_json (slug, slug,
+                     1, 1,
+                     GNUNET_TIME_absolute_to_timestamp(now),
+                     GNUNET_TIME_absolute_to_timestamp(now),
+                     &ps->choices);
+
+  GNUNET_assert (0 ==
+                 json_object_set_new (ps->order_terms,
+                                      "choices",
+                                      ps->choices)
+  );
+  GNUNET_assert (0 ==
+                 json_object_set_new (ps->order_terms,
+                                      "version",
+                                      json_string ("1"))
+  );
+
+
+  GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_WEAK,
+                              &ps->nonce,
+                              sizeof (struct GNUNET_CRYPTO_EddsaPublicKey));
+  ps->po = TALER_MERCHANT_orders_post (TALER_TESTING_interpreter_get_context (
+                                         is),
+                                       ps->merchant_url,
+                                       ps->order_terms,
+                                       GNUNET_TIME_UNIT_ZERO,
+                                       &order_cb,
+                                       ps);
+  GNUNET_assert (NULL != ps->po);
+}
+
+
 /**
  * Free the state of a "orders" CMD, and possibly
  * cancel it if it did not complete.
@@ -651,8 +777,7 @@ make_order_json (const char *order_id,
     "dummy_array", /* For testing forgetting parts of arrays */
     "item", "speakers",
     "item", "headphones",
-    "item", "earbuds"
-    );
+    "item", "earbuds");
   GNUNET_assert (GNUNET_OK ==
                  TALER_JSON_expand_path (contract_terms,
                                          "$.dummy_obj",
@@ -828,3 +953,43 @@ TALER_TESTING_cmd_merchant_post_orders3 (
     return cmd;
   }
 }
+
+
+struct TALER_TESTING_Command
+TALER_TESTING_cmd_merchant_post_orders_choices (
+  const char *label,
+  const struct GNUNET_CONFIGURATION_Handle *cfg,
+  const char *merchant_url,
+  unsigned int http_status,
+  const char *token_family_reference,
+  const char *order_id,
+  struct GNUNET_TIME_Timestamp refund_deadline,
+  struct GNUNET_TIME_Timestamp pay_deadline,
+  const char *amount)
+{
+  struct OrdersState *ps;
+
+  ps = GNUNET_new (struct OrdersState);
+  ps->cfg = cfg;
+  make_order_json (order_id,
+                   refund_deadline,
+                   pay_deadline,
+                   amount,
+                   &ps->order_terms);
+  ps->http_status = http_status;
+  ps->token_family_reference = token_family_reference;
+  ps->expected_order_id = order_id;
+  ps->merchant_url = merchant_url;
+  ps->with_claim = true;
+  {
+    struct TALER_TESTING_Command cmd = {
+      .cls = ps,
+      .label = label,
+      .run = &orders_run3,
+      .cleanup = &orders_cleanup,
+      .traits = &orders_traits
+    };
+
+    return cmd;
+  }
+}
\ No newline at end of file
diff --git a/src/testing/testing_api_cmd_post_products.c 
b/src/testing/testing_api_cmd_post_products.c
index 4ffafddc..c841f1b1 100644
--- a/src/testing/testing_api_cmd_post_products.c
+++ b/src/testing/testing_api_cmd_post_products.c
@@ -35,7 +35,7 @@ struct PostProductsState
 {
 
   /**
-   * Handle for a "GET product" request.
+   * Handle for a "POST /products" request.
    */
   struct TALER_MERCHANT_ProductsPostHandle *iph;
 
diff --git a/src/testing/testing_api_cmd_post_tokenfamilies.c 
b/src/testing/testing_api_cmd_post_tokenfamilies.c
index fa2d4407..ce76a543 100644
--- a/src/testing/testing_api_cmd_post_tokenfamilies.c
+++ b/src/testing/testing_api_cmd_post_tokenfamilies.c
@@ -232,7 +232,6 @@ post_tokenfamilies_cleanup (void *cls,
 struct TALER_TESTING_Command
 TALER_TESTING_cmd_merchant_post_tokenfamilies (
   const char *label,
-  const struct GNUNET_CONFIGURATION_Handle *cfg,
   const char *merchant_url,
   unsigned int http_status,
   const char *slug,

-- 
To stop receiving notification emails like this one, please contact
gnunet@gnunet.org.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]